blob: 9e4c7a0b0407993683ee3d2f4095f016f89f7587 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +020034#include <drm/drm_atomic_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030035
Ben Widawskydc39fff2013-10-18 12:32:07 -070036/**
Jani Nikula18afd442016-01-18 09:19:48 +020037 * DOC: RC6
38 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070039 * RC6 is a special power stage which allows the GPU to enter an very
40 * low-voltage mode when idle, using down to 0V while at this stage. This
41 * stage is entered automatically when the GPU is idle when RC6 support is
42 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43 *
44 * There are different RC6 modes available in Intel GPU, which differentiate
45 * among each other with the latency required to enter and leave RC6 and
46 * voltage consumed by the GPU in different states.
47 *
48 * The combination of the following flags define which states GPU is allowed
49 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50 * RC6pp is deepest RC6. Their support by hardware varies according to the
51 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52 * which brings the most power savings; deeper states save more power, but
53 * require higher latency to switch to and wake up.
54 */
55#define INTEL_RC6_ENABLE (1<<0)
56#define INTEL_RC6p_ENABLE (1<<1)
57#define INTEL_RC6pp_ENABLE (1<<2)
58
Ville Syrjälä46f16e62016-10-31 22:37:22 +020059static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030060{
Mika Kuoppalab033bb62016-06-07 17:19:04 +030061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl */
62 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
65 I915_WRITE(GEN8_CONFIG0,
66 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030067
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
72 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020073 /* WaFbcWakeMemOn:skl,bxt,kbl,glk */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030074 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
78 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl */
79 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030081}
82
Ville Syrjälä46f16e62016-10-31 22:37:22 +020083static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020084{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020085 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020086
Nick Hoatha7546152015-06-29 14:07:32 +010087 /* WaDisableSDEUnitClockGating:bxt */
88 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
89 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
90
Imre Deak32608ca2015-03-11 11:10:27 +020091 /*
92 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020093 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020094 */
Imre Deak32608ca2015-03-11 11:10:27 +020095 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020096 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7a2015-12-01 10:23:52 +020097
98 /*
99 * Wa: Backlight PWM may stop in the asserted state, causing backlight
100 * to stay fully on.
101 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200102 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
103 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200104}
105
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200106static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
107{
108 gen9_init_clock_gating(dev_priv);
109
110 /*
111 * WaDisablePWMClockGating:glk
112 * Backlight PWM may stop in the asserted state, causing backlight
113 * to stay fully on.
114 */
115 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
116 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200117
118 /* WaDDIIOTimeout:glk */
119 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
120 u32 val = I915_READ(CHICKEN_MISC_2);
121 val &= ~(GLK_CL0_PWR_DOWN |
122 GLK_CL1_PWR_DOWN |
123 GLK_CL2_PWR_DOWN);
124 I915_WRITE(CHICKEN_MISC_2, val);
125 }
126
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200127}
128
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200129static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200130{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200131 u32 tmp;
132
133 tmp = I915_READ(CLKCFG);
134
135 switch (tmp & CLKCFG_FSB_MASK) {
136 case CLKCFG_FSB_533:
137 dev_priv->fsb_freq = 533; /* 133*4 */
138 break;
139 case CLKCFG_FSB_800:
140 dev_priv->fsb_freq = 800; /* 200*4 */
141 break;
142 case CLKCFG_FSB_667:
143 dev_priv->fsb_freq = 667; /* 167*4 */
144 break;
145 case CLKCFG_FSB_400:
146 dev_priv->fsb_freq = 400; /* 100*4 */
147 break;
148 }
149
150 switch (tmp & CLKCFG_MEM_MASK) {
151 case CLKCFG_MEM_533:
152 dev_priv->mem_freq = 533;
153 break;
154 case CLKCFG_MEM_667:
155 dev_priv->mem_freq = 667;
156 break;
157 case CLKCFG_MEM_800:
158 dev_priv->mem_freq = 800;
159 break;
160 }
161
162 /* detect pineview DDR3 setting */
163 tmp = I915_READ(CSHRDDR3CTL);
164 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
165}
166
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200167static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200168{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200169 u16 ddrpll, csipll;
170
171 ddrpll = I915_READ16(DDRMPLL1);
172 csipll = I915_READ16(CSIPLL0);
173
174 switch (ddrpll & 0xff) {
175 case 0xc:
176 dev_priv->mem_freq = 800;
177 break;
178 case 0x10:
179 dev_priv->mem_freq = 1066;
180 break;
181 case 0x14:
182 dev_priv->mem_freq = 1333;
183 break;
184 case 0x18:
185 dev_priv->mem_freq = 1600;
186 break;
187 default:
188 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
189 ddrpll & 0xff);
190 dev_priv->mem_freq = 0;
191 break;
192 }
193
Daniel Vetter20e4d402012-08-08 23:35:39 +0200194 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200195
196 switch (csipll & 0x3ff) {
197 case 0x00c:
198 dev_priv->fsb_freq = 3200;
199 break;
200 case 0x00e:
201 dev_priv->fsb_freq = 3733;
202 break;
203 case 0x010:
204 dev_priv->fsb_freq = 4266;
205 break;
206 case 0x012:
207 dev_priv->fsb_freq = 4800;
208 break;
209 case 0x014:
210 dev_priv->fsb_freq = 5333;
211 break;
212 case 0x016:
213 dev_priv->fsb_freq = 5866;
214 break;
215 case 0x018:
216 dev_priv->fsb_freq = 6400;
217 break;
218 default:
219 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
220 csipll & 0x3ff);
221 dev_priv->fsb_freq = 0;
222 break;
223 }
224
225 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200226 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200227 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200228 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200229 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200230 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200231 }
232}
233
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300234static const struct cxsr_latency cxsr_latency_table[] = {
235 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
236 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
237 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
238 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
239 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
240
241 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
242 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
243 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
244 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
245 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
246
247 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
248 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
249 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
250 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
251 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
252
253 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
254 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
255 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
256 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
257 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
258
259 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
260 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
261 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
262 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
263 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
264
265 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
266 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
267 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
268 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
269 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
270};
271
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100272static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
273 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300274 int fsb,
275 int mem)
276{
277 const struct cxsr_latency *latency;
278 int i;
279
280 if (fsb == 0 || mem == 0)
281 return NULL;
282
283 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
284 latency = &cxsr_latency_table[i];
285 if (is_desktop == latency->is_desktop &&
286 is_ddr3 == latency->is_ddr3 &&
287 fsb == latency->fsb_freq && mem == latency->mem_freq)
288 return latency;
289 }
290
291 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
292
293 return NULL;
294}
295
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200296static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
297{
298 u32 val;
299
300 mutex_lock(&dev_priv->rps.hw_lock);
301
302 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
303 if (enable)
304 val &= ~FORCE_DDR_HIGH_FREQ;
305 else
306 val |= FORCE_DDR_HIGH_FREQ;
307 val &= ~FORCE_DDR_LOW_FREQ;
308 val |= FORCE_DDR_FREQ_REQ_ACK;
309 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
310
311 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
312 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
313 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
314
315 mutex_unlock(&dev_priv->rps.hw_lock);
316}
317
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200318static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
319{
320 u32 val;
321
322 mutex_lock(&dev_priv->rps.hw_lock);
323
324 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
325 if (enable)
326 val |= DSP_MAXFIFO_PM5_ENABLE;
327 else
328 val &= ~DSP_MAXFIFO_PM5_ENABLE;
329 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
330
331 mutex_unlock(&dev_priv->rps.hw_lock);
332}
333
Ville Syrjäläf4998962015-03-10 17:02:21 +0200334#define FW_WM(value, plane) \
335 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
336
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200337static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300338{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200339 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300340 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300341
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100342 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200343 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300344 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300345 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200346 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200347 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300348 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300349 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200350 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200351 val = I915_READ(DSPFW3);
352 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
353 if (enable)
354 val |= PINEVIEW_SELF_REFRESH_EN;
355 else
356 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300357 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300358 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100359 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200360 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300361 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
362 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
363 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300364 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100365 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300366 /*
367 * FIXME can't find a bit like this for 915G, and
368 * and yet it does have the related watermark in
369 * FW_BLC_SELF. What's going on?
370 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200371 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300372 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
373 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
374 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300375 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300376 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300378 }
379
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200380 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
381 enableddisabled(enable),
382 enableddisabled(was_enabled));
383
384 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300385}
386
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200387bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200388{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200389 bool ret;
390
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200391 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200392 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200393 dev_priv->wm.vlv.cxsr = enable;
394 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200395
396 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200397}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200398
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300399/*
400 * Latency for FIFO fetches is dependent on several factors:
401 * - memory configuration (speed, channels)
402 * - chipset
403 * - current MCH state
404 * It can be fairly high in some situations, so here we assume a fairly
405 * pessimal value. It's a tradeoff between extra memory fetches (if we
406 * set this value too high, the FIFO will fetch frequently to stay full)
407 * and power consumption (set it too low to save power and we might see
408 * FIFO underruns and display "flicker").
409 *
410 * A value of 5us seems to be a good balance; safe for very low end
411 * platforms but not overly aggressive on lower latency configs.
412 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100413static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300414
Ville Syrjäläb5004722015-03-05 21:19:47 +0200415#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
416 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
417
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200418static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200419{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200420 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200421 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200422 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200423 enum pipe pipe = crtc->pipe;
424 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200425
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200426 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200427 uint32_t dsparb, dsparb2, dsparb3;
428 case PIPE_A:
429 dsparb = I915_READ(DSPARB);
430 dsparb2 = I915_READ(DSPARB2);
431 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
432 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
433 break;
434 case PIPE_B:
435 dsparb = I915_READ(DSPARB);
436 dsparb2 = I915_READ(DSPARB2);
437 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
438 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
439 break;
440 case PIPE_C:
441 dsparb2 = I915_READ(DSPARB2);
442 dsparb3 = I915_READ(DSPARB3);
443 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
444 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
445 break;
446 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200447 MISSING_CASE(pipe);
448 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200449 }
450
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200451 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
452 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
453 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
454 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200455
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200456 DRM_DEBUG_KMS("Pipe %c FIFO size: %d/%d/%d/%d\n",
457 pipe_name(pipe),
458 fifo_state->plane[PLANE_PRIMARY],
459 fifo_state->plane[PLANE_SPRITE0],
460 fifo_state->plane[PLANE_SPRITE1],
461 fifo_state->plane[PLANE_CURSOR]);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200462}
463
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200464static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300465{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300466 uint32_t dsparb = I915_READ(DSPARB);
467 int size;
468
469 size = dsparb & 0x7f;
470 if (plane)
471 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
472
473 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
474 plane ? "B" : "A", size);
475
476 return size;
477}
478
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200479static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300480{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300481 uint32_t dsparb = I915_READ(DSPARB);
482 int size;
483
484 size = dsparb & 0x1ff;
485 if (plane)
486 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
487 size >>= 1; /* Convert to cachelines */
488
489 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
490 plane ? "B" : "A", size);
491
492 return size;
493}
494
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200495static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300496{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300497 uint32_t dsparb = I915_READ(DSPARB);
498 int size;
499
500 size = dsparb & 0x7f;
501 size >>= 2; /* Convert to cachelines */
502
503 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
504 plane ? "B" : "A",
505 size);
506
507 return size;
508}
509
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300510/* Pineview has different values for various configs */
511static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300512 .fifo_size = PINEVIEW_DISPLAY_FIFO,
513 .max_wm = PINEVIEW_MAX_WM,
514 .default_wm = PINEVIEW_DFT_WM,
515 .guard_size = PINEVIEW_GUARD_WM,
516 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517};
518static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300519 .fifo_size = PINEVIEW_DISPLAY_FIFO,
520 .max_wm = PINEVIEW_MAX_WM,
521 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
522 .guard_size = PINEVIEW_GUARD_WM,
523 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300524};
525static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300526 .fifo_size = PINEVIEW_CURSOR_FIFO,
527 .max_wm = PINEVIEW_CURSOR_MAX_WM,
528 .default_wm = PINEVIEW_CURSOR_DFT_WM,
529 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
530 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531};
532static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300533 .fifo_size = PINEVIEW_CURSOR_FIFO,
534 .max_wm = PINEVIEW_CURSOR_MAX_WM,
535 .default_wm = PINEVIEW_CURSOR_DFT_WM,
536 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
537 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300538};
539static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300540 .fifo_size = G4X_FIFO_SIZE,
541 .max_wm = G4X_MAX_WM,
542 .default_wm = G4X_MAX_WM,
543 .guard_size = 2,
544 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545};
546static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300547 .fifo_size = I965_CURSOR_FIFO,
548 .max_wm = I965_CURSOR_MAX_WM,
549 .default_wm = I965_CURSOR_DFT_WM,
550 .guard_size = 2,
551 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300553static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300554 .fifo_size = I965_CURSOR_FIFO,
555 .max_wm = I965_CURSOR_MAX_WM,
556 .default_wm = I965_CURSOR_DFT_WM,
557 .guard_size = 2,
558 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559};
560static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300561 .fifo_size = I945_FIFO_SIZE,
562 .max_wm = I915_MAX_WM,
563 .default_wm = 1,
564 .guard_size = 2,
565 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300566};
567static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300568 .fifo_size = I915_FIFO_SIZE,
569 .max_wm = I915_MAX_WM,
570 .default_wm = 1,
571 .guard_size = 2,
572 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300574static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300575 .fifo_size = I855GM_FIFO_SIZE,
576 .max_wm = I915_MAX_WM,
577 .default_wm = 1,
578 .guard_size = 2,
579 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300580};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300581static const struct intel_watermark_params i830_bc_wm_info = {
582 .fifo_size = I855GM_FIFO_SIZE,
583 .max_wm = I915_MAX_WM/2,
584 .default_wm = 1,
585 .guard_size = 2,
586 .cacheline_size = I830_FIFO_LINE_SIZE,
587};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200588static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300589 .fifo_size = I830_FIFO_SIZE,
590 .max_wm = I915_MAX_WM,
591 .default_wm = 1,
592 .guard_size = 2,
593 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300594};
595
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300596/**
597 * intel_calculate_wm - calculate watermark level
598 * @clock_in_khz: pixel clock
599 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200600 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601 * @latency_ns: memory latency for the platform
602 *
603 * Calculate the watermark level (the level at which the display plane will
604 * start fetching from memory again). Each chip has a different display
605 * FIFO size and allocation, so the caller needs to figure that out and pass
606 * in the correct intel_watermark_params structure.
607 *
608 * As the pixel clock runs, the FIFO will be drained at a rate that depends
609 * on the pixel size. When it reaches the watermark level, it'll start
610 * fetching FIFO line sized based chunks from memory until the FIFO fills
611 * past the watermark point. If the FIFO drains completely, a FIFO underrun
612 * will occur, and a display engine hang could result.
613 */
614static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
615 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200616 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300617 unsigned long latency_ns)
618{
619 long entries_required, wm_size;
620
621 /*
622 * Note: we need to make sure we don't overflow for various clock &
623 * latency values.
624 * clocks go from a few thousand to several hundred thousand.
625 * latency is usually a few thousand
626 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200627 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300628 1000;
629 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
630
631 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
632
633 wm_size = fifo_size - (entries_required + wm->guard_size);
634
635 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
636
637 /* Don't promote wm_size to unsigned... */
638 if (wm_size > (long)wm->max_wm)
639 wm_size = wm->max_wm;
640 if (wm_size <= 0)
641 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300642
643 /*
644 * Bspec seems to indicate that the value shouldn't be lower than
645 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
646 * Lets go for 8 which is the burst size since certain platforms
647 * already use a hardcoded 8 (which is what the spec says should be
648 * done).
649 */
650 if (wm_size <= 8)
651 wm_size = 8;
652
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300653 return wm_size;
654}
655
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200656static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300657{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200658 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300659
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200660 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200661 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300662 if (enabled)
663 return NULL;
664 enabled = crtc;
665 }
666 }
667
668 return enabled;
669}
670
Ville Syrjälä432081b2016-10-31 22:37:03 +0200671static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300672{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200673 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200674 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300675 const struct cxsr_latency *latency;
676 u32 reg;
677 unsigned long wm;
678
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100679 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
680 dev_priv->is_ddr3,
681 dev_priv->fsb_freq,
682 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300683 if (!latency) {
684 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300685 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300686 return;
687 }
688
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200689 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300690 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200691 const struct drm_display_mode *adjusted_mode =
692 &crtc->config->base.adjusted_mode;
693 const struct drm_framebuffer *fb =
694 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200695 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300696 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300697
698 /* Display SR */
699 wm = intel_calculate_wm(clock, &pineview_display_wm,
700 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200701 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300702 reg = I915_READ(DSPFW1);
703 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200704 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300705 I915_WRITE(DSPFW1, reg);
706 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
707
708 /* cursor SR */
709 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
710 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200711 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300712 reg = I915_READ(DSPFW3);
713 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200714 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300715 I915_WRITE(DSPFW3, reg);
716
717 /* Display HPLL off SR */
718 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
719 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200720 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300721 reg = I915_READ(DSPFW3);
722 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200723 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300724 I915_WRITE(DSPFW3, reg);
725
726 /* cursor HPLL off SR */
727 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
728 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200729 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300730 reg = I915_READ(DSPFW3);
731 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200732 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300733 I915_WRITE(DSPFW3, reg);
734 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
735
Imre Deak5209b1f2014-07-01 12:36:17 +0300736 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300737 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300738 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300739 }
740}
741
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200742static bool g4x_compute_wm0(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300743 int plane,
744 const struct intel_watermark_params *display,
745 int display_latency_ns,
746 const struct intel_watermark_params *cursor,
747 int cursor_latency_ns,
748 int *plane_wm,
749 int *cursor_wm)
750{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200751 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300752 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200753 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200754 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755 int line_time_us, line_count;
756 int entries, tlb_miss;
757
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200758 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200759 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300760 *cursor_wm = cursor->guard_size;
761 *plane_wm = display->guard_size;
762 return false;
763 }
764
Ville Syrjäläefc26112016-10-31 22:37:04 +0200765 adjusted_mode = &crtc->config->base.adjusted_mode;
766 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100767 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800768 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200769 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200770 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771
772 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200773 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300774 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
775 if (tlb_miss > 0)
776 entries += tlb_miss;
777 entries = DIV_ROUND_UP(entries, display->cacheline_size);
778 *plane_wm = entries + display->guard_size;
779 if (*plane_wm > (int)display->max_wm)
780 *plane_wm = display->max_wm;
781
782 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200783 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300784 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200785 entries = line_count * crtc->base.cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300786 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
787 if (tlb_miss > 0)
788 entries += tlb_miss;
789 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
790 *cursor_wm = entries + cursor->guard_size;
791 if (*cursor_wm > (int)cursor->max_wm)
792 *cursor_wm = (int)cursor->max_wm;
793
794 return true;
795}
796
797/*
798 * Check the wm result.
799 *
800 * If any calculated watermark values is larger than the maximum value that
801 * can be programmed into the associated watermark register, that watermark
802 * must be disabled.
803 */
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200804static bool g4x_check_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300805 int display_wm, int cursor_wm,
806 const struct intel_watermark_params *display,
807 const struct intel_watermark_params *cursor)
808{
809 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
810 display_wm, cursor_wm);
811
812 if (display_wm > display->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100813 DRM_DEBUG_KMS("display watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300814 display_wm, display->max_wm);
815 return false;
816 }
817
818 if (cursor_wm > cursor->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100819 DRM_DEBUG_KMS("cursor watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300820 cursor_wm, cursor->max_wm);
821 return false;
822 }
823
824 if (!(display_wm || cursor_wm)) {
825 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
826 return false;
827 }
828
829 return true;
830}
831
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200832static bool g4x_compute_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300833 int plane,
834 int latency_ns,
835 const struct intel_watermark_params *display,
836 const struct intel_watermark_params *cursor,
837 int *display_wm, int *cursor_wm)
838{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200839 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300840 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200841 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200842 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300843 unsigned long line_time_us;
844 int line_count, line_size;
845 int small, large;
846 int entries;
847
848 if (!latency_ns) {
849 *display_wm = *cursor_wm = 0;
850 return false;
851 }
852
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200853 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200854 adjusted_mode = &crtc->config->base.adjusted_mode;
855 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100856 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800857 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200858 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200859 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300860
Ville Syrjälä922044c2014-02-14 14:18:57 +0200861 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300862 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200863 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300864
865 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200866 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300867 large = line_count * line_size;
868
869 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
870 *display_wm = entries + display->guard_size;
871
872 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläefc26112016-10-31 22:37:04 +0200873 entries = line_count * cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300874 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
875 *cursor_wm = entries + cursor->guard_size;
876
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200877 return g4x_check_srwm(dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300878 *display_wm, *cursor_wm,
879 display, cursor);
880}
881
Ville Syrjälä15665972015-03-10 16:16:28 +0200882#define FW_WM_VLV(value, plane) \
883 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
884
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200885static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200886 const struct vlv_wm_values *wm)
887{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200888 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200889
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200890 for_each_pipe(dev_priv, pipe) {
891 I915_WRITE(VLV_DDL(pipe),
892 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
893 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
894 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
895 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
896 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200897
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200898 /*
899 * Zero the (unused) WM1 watermarks, and also clear all the
900 * high order bits so that there are no out of bounds values
901 * present in the registers during the reprogramming.
902 */
903 I915_WRITE(DSPHOWM, 0);
904 I915_WRITE(DSPHOWM1, 0);
905 I915_WRITE(DSPFW4, 0);
906 I915_WRITE(DSPFW5, 0);
907 I915_WRITE(DSPFW6, 0);
908
Ville Syrjäläae801522015-03-05 21:19:49 +0200909 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200910 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200911 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
912 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
913 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200914 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200915 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
916 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
917 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200918 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200919 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200920
921 if (IS_CHERRYVIEW(dev_priv)) {
922 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200923 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
924 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200925 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200926 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
927 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200928 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200929 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
930 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200931 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200932 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200933 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
934 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
935 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
936 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
937 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
938 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
939 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
940 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
941 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200942 } else {
943 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200944 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
945 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200946 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200947 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200948 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
949 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
950 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
951 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
952 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
953 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200954 }
955
956 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200957}
958
Ville Syrjälä15665972015-03-10 16:16:28 +0200959#undef FW_WM_VLV
960
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300961/* latency must be in 0.1us units. */
962static unsigned int vlv_wm_method2(unsigned int pixel_rate,
963 unsigned int pipe_htotal,
964 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200965 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300966 unsigned int latency)
967{
968 unsigned int ret;
969
970 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200971 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300972 ret = DIV_ROUND_UP(ret, 64);
973
974 return ret;
975}
976
Ville Syrjäläbb726512016-10-31 22:37:24 +0200977static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300978{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300979 /* all latencies in usec */
980 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
981
Ville Syrjälä58590c12015-09-08 21:05:12 +0300982 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
983
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300984 if (IS_CHERRYVIEW(dev_priv)) {
985 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
986 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300987
988 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300989 }
990}
991
Ville Syrjäläe339d672016-11-28 19:37:17 +0200992static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
993 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300994 int level)
995{
Ville Syrjäläe339d672016-11-28 19:37:17 +0200996 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300997 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +0200998 const struct drm_display_mode *adjusted_mode =
999 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001000 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001001
1002 if (dev_priv->wm.pri_latency[level] == 0)
1003 return USHRT_MAX;
1004
Ville Syrjäläe339d672016-11-28 19:37:17 +02001005 if (!plane_state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001006 return 0;
1007
Daniel Vetteref426c12017-01-04 11:41:10 +01001008 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001009 clock = adjusted_mode->crtc_clock;
1010 htotal = adjusted_mode->crtc_htotal;
1011 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001012 if (WARN_ON(htotal == 0))
1013 htotal = 1;
1014
1015 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1016 /*
1017 * FIXME the formula gives values that are
1018 * too big for the cursor FIFO, and hence we
1019 * would never be able to use cursors. For
1020 * now just hardcode the watermark.
1021 */
1022 wm = 63;
1023 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001024 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001025 dev_priv->wm.pri_latency[level] * 10);
1026 }
1027
1028 return min_t(int, wm, USHRT_MAX);
1029}
1030
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001031static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1032{
1033 return (active_planes & (BIT(PLANE_SPRITE0) |
1034 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1035}
1036
Ville Syrjälä5012e602017-03-02 19:14:56 +02001037static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001038{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001039 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001040 const struct vlv_pipe_wm *raw =
1041 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001042 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001043 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1044 int num_active_planes = hweight32(active_planes);
1045 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001046 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001047 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001048 unsigned int total_rate;
1049 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001050
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001051 /*
1052 * When enabling sprite0 after sprite1 has already been enabled
1053 * we tend to get an underrun unless sprite0 already has some
1054 * FIFO space allcoated. Hence we always allocate at least one
1055 * cacheline for sprite0 whenever sprite1 is enabled.
1056 *
1057 * All other plane enable sequences appear immune to this problem.
1058 */
1059 if (vlv_need_sprite0_fifo_workaround(active_planes))
1060 sprite0_fifo_extra = 1;
1061
Ville Syrjälä5012e602017-03-02 19:14:56 +02001062 total_rate = raw->plane[PLANE_PRIMARY] +
1063 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001064 raw->plane[PLANE_SPRITE1] +
1065 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001066
Ville Syrjälä5012e602017-03-02 19:14:56 +02001067 if (total_rate > fifo_size)
1068 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001069
Ville Syrjälä5012e602017-03-02 19:14:56 +02001070 if (total_rate == 0)
1071 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001072
Ville Syrjälä5012e602017-03-02 19:14:56 +02001073 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001074 unsigned int rate;
1075
Ville Syrjälä5012e602017-03-02 19:14:56 +02001076 if ((active_planes & BIT(plane_id)) == 0) {
1077 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001078 continue;
1079 }
1080
Ville Syrjälä5012e602017-03-02 19:14:56 +02001081 rate = raw->plane[plane_id];
1082 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1083 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001084 }
1085
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001086 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1087 fifo_left -= sprite0_fifo_extra;
1088
Ville Syrjälä5012e602017-03-02 19:14:56 +02001089 fifo_state->plane[PLANE_CURSOR] = 63;
1090
1091 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001092
1093 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001094 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001095 int plane_extra;
1096
1097 if (fifo_left == 0)
1098 break;
1099
Ville Syrjälä5012e602017-03-02 19:14:56 +02001100 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001101 continue;
1102
1103 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001104 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001105 fifo_left -= plane_extra;
1106 }
1107
Ville Syrjälä5012e602017-03-02 19:14:56 +02001108 WARN_ON(active_planes != 0 && fifo_left != 0);
1109
1110 /* give it all to the first plane if none are active */
1111 if (active_planes == 0) {
1112 WARN_ON(fifo_left != fifo_size);
1113 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1114 }
1115
1116 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001117}
1118
Ville Syrjäläff32c542017-03-02 19:14:57 +02001119static int vlv_num_wm_levels(struct drm_i915_private *dev_priv)
1120{
1121 return dev_priv->wm.max_level + 1;
1122}
1123
1124/* mark all levels starting from 'level' as invalid */
1125static void vlv_invalidate_wms(struct intel_crtc *crtc,
1126 struct vlv_wm_state *wm_state, int level)
1127{
1128 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1129
1130 for (; level < vlv_num_wm_levels(dev_priv); level++) {
1131 enum plane_id plane_id;
1132
1133 for_each_plane_id_on_crtc(crtc, plane_id)
1134 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1135
1136 wm_state->sr[level].cursor = USHRT_MAX;
1137 wm_state->sr[level].plane = USHRT_MAX;
1138 }
1139}
1140
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001141static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1142{
1143 if (wm > fifo_size)
1144 return USHRT_MAX;
1145 else
1146 return fifo_size - wm;
1147}
1148
Ville Syrjäläff32c542017-03-02 19:14:57 +02001149/*
1150 * Starting from 'level' set all higher
1151 * levels to 'value' in the "raw" watermarks.
1152 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001153static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001154 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001155{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001156 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1157 int num_levels = vlv_num_wm_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001158 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001159
Ville Syrjäläff32c542017-03-02 19:14:57 +02001160 for (; level < num_levels; level++) {
1161 struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001162
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001163 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001164 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001165 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001166
1167 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001168}
1169
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001170static bool vlv_plane_wm_compute(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001171 const struct intel_plane_state *plane_state)
1172{
1173 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1174 enum plane_id plane_id = plane->id;
1175 int num_levels = vlv_num_wm_levels(to_i915(plane->base.dev));
1176 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001177 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001178
1179 if (!plane_state->base.visible) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001180 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1181 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001182 }
1183
1184 for (level = 0; level < num_levels; level++) {
1185 struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1186 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1187 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1188
Ville Syrjäläff32c542017-03-02 19:14:57 +02001189 if (wm > max_wm)
1190 break;
1191
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001192 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001193 raw->plane[plane_id] = wm;
1194 }
1195
1196 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001197 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001198
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001199out:
1200 if (dirty)
1201 DRM_DEBUG_KMS("%s wms: [0]=%d,[1]=%d,[2]=%d\n",
1202 plane->base.name,
1203 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1204 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1205 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1206
1207 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001208}
1209
1210static bool vlv_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1211 enum plane_id plane_id, int level)
1212{
1213 const struct vlv_pipe_wm *raw =
1214 &crtc_state->wm.vlv.raw[level];
1215 const struct vlv_fifo_state *fifo_state =
1216 &crtc_state->wm.vlv.fifo_state;
1217
1218 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1219}
1220
1221static bool vlv_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
1222{
1223 return vlv_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1224 vlv_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1225 vlv_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1226 vlv_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1227}
1228
1229static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001230{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001231 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001232 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001233 struct intel_atomic_state *state =
1234 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001235 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001236 const struct vlv_fifo_state *fifo_state =
1237 &crtc_state->wm.vlv.fifo_state;
1238 int num_active_planes = hweight32(crtc_state->active_planes &
1239 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001240 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001241 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001242 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001243 enum plane_id plane_id;
1244 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001245 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001246
Ville Syrjäläff32c542017-03-02 19:14:57 +02001247 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1248 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001249 to_intel_plane_state(plane->base.state);
1250
Ville Syrjäläff32c542017-03-02 19:14:57 +02001251 if (plane_state->base.crtc != &crtc->base &&
1252 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001253 continue;
1254
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001255 if (vlv_plane_wm_compute(crtc_state, plane_state))
1256 dirty |= BIT(plane->id);
1257 }
1258
1259 /*
1260 * DSPARB registers may have been reset due to the
1261 * power well being turned off. Make sure we restore
1262 * them to a consistent state even if no primary/sprite
1263 * planes are initially active.
1264 */
1265 if (needs_modeset)
1266 crtc_state->fifo_changed = true;
1267
1268 if (!dirty)
1269 return 0;
1270
1271 /* cursor changes don't warrant a FIFO recompute */
1272 if (dirty & ~BIT(PLANE_CURSOR)) {
1273 const struct intel_crtc_state *old_crtc_state =
1274 to_intel_crtc_state(crtc->base.state);
1275 const struct vlv_fifo_state *old_fifo_state =
1276 &old_crtc_state->wm.vlv.fifo_state;
1277
1278 ret = vlv_compute_fifo(crtc_state);
1279 if (ret)
1280 return ret;
1281
1282 if (needs_modeset ||
1283 memcmp(old_fifo_state, fifo_state,
1284 sizeof(*fifo_state)) != 0)
1285 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001286 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001287
Ville Syrjäläff32c542017-03-02 19:14:57 +02001288 /* initially allow all levels */
1289 wm_state->num_levels = vlv_num_wm_levels(dev_priv);
1290 /*
1291 * Note that enabling cxsr with no primary/sprite planes
1292 * enabled can wedge the pipe. Hence we only allow cxsr
1293 * with exactly one enabled primary/sprite plane.
1294 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001295 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001296
Ville Syrjälä5012e602017-03-02 19:14:56 +02001297 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001298 const struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1299 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001300
Ville Syrjäläff32c542017-03-02 19:14:57 +02001301 if (!vlv_crtc_wm_is_valid(crtc_state, level))
1302 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001303
Ville Syrjäläff32c542017-03-02 19:14:57 +02001304 for_each_plane_id_on_crtc(crtc, plane_id) {
1305 wm_state->wm[level].plane[plane_id] =
1306 vlv_invert_wm_value(raw->plane[plane_id],
1307 fifo_state->plane[plane_id]);
1308 }
1309
1310 wm_state->sr[level].plane =
1311 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001312 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001313 raw->plane[PLANE_SPRITE1]),
1314 sr_fifo_size);
1315
1316 wm_state->sr[level].cursor =
1317 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1318 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001319 }
1320
Ville Syrjäläff32c542017-03-02 19:14:57 +02001321 if (level == 0)
1322 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001323
Ville Syrjäläff32c542017-03-02 19:14:57 +02001324 /* limit to only levels we can actually handle */
1325 wm_state->num_levels = level;
1326
1327 /* invalidate the higher levels */
1328 vlv_invalidate_wms(crtc, wm_state, level);
1329
1330 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001331}
1332
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001333#define VLV_FIFO(plane, value) \
1334 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1335
Ville Syrjäläff32c542017-03-02 19:14:57 +02001336static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1337 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001338{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001339 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001340 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001341 const struct vlv_fifo_state *fifo_state =
1342 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001343 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001344
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001345 if (!crtc_state->fifo_changed)
1346 return;
1347
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001348 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1349 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1350 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001351
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001352 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1353 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001354
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001355 spin_lock(&dev_priv->wm.dsparb_lock);
1356
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001357 switch (crtc->pipe) {
1358 uint32_t dsparb, dsparb2, dsparb3;
1359 case PIPE_A:
1360 dsparb = I915_READ(DSPARB);
1361 dsparb2 = I915_READ(DSPARB2);
1362
1363 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1364 VLV_FIFO(SPRITEB, 0xff));
1365 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1366 VLV_FIFO(SPRITEB, sprite1_start));
1367
1368 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1369 VLV_FIFO(SPRITEB_HI, 0x1));
1370 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1371 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1372
1373 I915_WRITE(DSPARB, dsparb);
1374 I915_WRITE(DSPARB2, dsparb2);
1375 break;
1376 case PIPE_B:
1377 dsparb = I915_READ(DSPARB);
1378 dsparb2 = I915_READ(DSPARB2);
1379
1380 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1381 VLV_FIFO(SPRITED, 0xff));
1382 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1383 VLV_FIFO(SPRITED, sprite1_start));
1384
1385 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1386 VLV_FIFO(SPRITED_HI, 0xff));
1387 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1388 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1389
1390 I915_WRITE(DSPARB, dsparb);
1391 I915_WRITE(DSPARB2, dsparb2);
1392 break;
1393 case PIPE_C:
1394 dsparb3 = I915_READ(DSPARB3);
1395 dsparb2 = I915_READ(DSPARB2);
1396
1397 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1398 VLV_FIFO(SPRITEF, 0xff));
1399 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1400 VLV_FIFO(SPRITEF, sprite1_start));
1401
1402 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1403 VLV_FIFO(SPRITEF_HI, 0xff));
1404 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1405 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1406
1407 I915_WRITE(DSPARB3, dsparb3);
1408 I915_WRITE(DSPARB2, dsparb2);
1409 break;
1410 default:
1411 break;
1412 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001413
1414 POSTING_READ(DSPARB);
1415
1416 spin_unlock(&dev_priv->wm.dsparb_lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001417}
1418
1419#undef VLV_FIFO
1420
Ville Syrjälä4841da52017-03-02 19:14:59 +02001421static int vlv_compute_intermediate_wm(struct drm_device *dev,
1422 struct intel_crtc *crtc,
1423 struct intel_crtc_state *crtc_state)
1424{
1425 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
1426 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
1427 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
1428 int level;
1429
1430 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001431 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1432 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001433
1434 for (level = 0; level < intermediate->num_levels; level++) {
1435 enum plane_id plane_id;
1436
1437 for_each_plane_id_on_crtc(crtc, plane_id) {
1438 intermediate->wm[level].plane[plane_id] =
1439 min(optimal->wm[level].plane[plane_id],
1440 active->wm[level].plane[plane_id]);
1441 }
1442
1443 intermediate->sr[level].plane = min(optimal->sr[level].plane,
1444 active->sr[level].plane);
1445 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
1446 active->sr[level].cursor);
1447 }
1448
1449 vlv_invalidate_wms(crtc, intermediate, level);
1450
1451 /*
1452 * If our intermediate WM are identical to the final WM, then we can
1453 * omit the post-vblank programming; only update if it's different.
1454 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001455 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1456 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001457
1458 return 0;
1459}
1460
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001461static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001462 struct vlv_wm_values *wm)
1463{
1464 struct intel_crtc *crtc;
1465 int num_active_crtcs = 0;
1466
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001467 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001468 wm->cxsr = true;
1469
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001470 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001471 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001472
1473 if (!crtc->active)
1474 continue;
1475
1476 if (!wm_state->cxsr)
1477 wm->cxsr = false;
1478
1479 num_active_crtcs++;
1480 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1481 }
1482
1483 if (num_active_crtcs != 1)
1484 wm->cxsr = false;
1485
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001486 if (num_active_crtcs > 1)
1487 wm->level = VLV_WM_LEVEL_PM2;
1488
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001489 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001490 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001491 enum pipe pipe = crtc->pipe;
1492
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001493 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001494 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001495 wm->sr = wm_state->sr[wm->level];
1496
Ville Syrjälä1b313892016-11-28 19:37:08 +02001497 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
1498 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
1499 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
1500 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001501 }
1502}
1503
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001504static bool is_disabling(int old, int new, int threshold)
1505{
1506 return old >= threshold && new < threshold;
1507}
1508
1509static bool is_enabling(int old, int new, int threshold)
1510{
1511 return old < threshold && new >= threshold;
1512}
1513
Ville Syrjäläff32c542017-03-02 19:14:57 +02001514static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001515{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001516 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
1517 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001518
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001519 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001520
Ville Syrjäläff32c542017-03-02 19:14:57 +02001521 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001522 return;
1523
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001524 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001525 chv_set_memory_dvfs(dev_priv, false);
1526
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001527 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001528 chv_set_memory_pm5(dev_priv, false);
1529
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001530 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001531 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001532
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001533 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001534
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001535 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001536 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001537
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001538 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001539 chv_set_memory_pm5(dev_priv, true);
1540
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001541 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001542 chv_set_memory_dvfs(dev_priv, true);
1543
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001544 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001545}
1546
Ville Syrjäläff32c542017-03-02 19:14:57 +02001547static void vlv_initial_watermarks(struct intel_atomic_state *state,
1548 struct intel_crtc_state *crtc_state)
1549{
1550 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1551 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1552
1553 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02001554 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
1555 vlv_program_watermarks(dev_priv);
1556 mutex_unlock(&dev_priv->wm.wm_mutex);
1557}
1558
1559static void vlv_optimize_watermarks(struct intel_atomic_state *state,
1560 struct intel_crtc_state *crtc_state)
1561{
1562 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1564
1565 if (!crtc_state->wm.need_postvbl_update)
1566 return;
1567
1568 mutex_lock(&dev_priv->wm.wm_mutex);
1569 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001570 vlv_program_watermarks(dev_priv);
1571 mutex_unlock(&dev_priv->wm.wm_mutex);
1572}
1573
Ville Syrjäläae801522015-03-05 21:19:49 +02001574#define single_plane_enabled(mask) is_power_of_2(mask)
1575
Ville Syrjälä432081b2016-10-31 22:37:03 +02001576static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001577{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001578 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001579 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001580 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1581 int plane_sr, cursor_sr;
1582 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001583 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001584
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001585 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001586 &g4x_wm_info, pessimal_latency_ns,
1587 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001588 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001589 enabled |= 1 << PIPE_A;
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_B,
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 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001595 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001596
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001597 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001598 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001599 sr_latency_ns,
1600 &g4x_wm_info,
1601 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001602 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001603 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001604 } else {
Imre Deak98584252014-06-13 14:54:20 +03001605 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001606 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001607 plane_sr = cursor_sr = 0;
1608 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001609
Ville Syrjäläa5043452014-06-28 02:04:18 +03001610 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1611 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001612 planea_wm, cursora_wm,
1613 planeb_wm, cursorb_wm,
1614 plane_sr, cursor_sr);
1615
1616 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001617 FW_WM(plane_sr, SR) |
1618 FW_WM(cursorb_wm, CURSORB) |
1619 FW_WM(planeb_wm, PLANEB) |
1620 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001621 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001622 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001623 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001624 /* HPLL off in SR has some issues on G4x... disable it */
1625 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001626 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001627 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001628
1629 if (cxsr_enabled)
1630 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001631}
1632
Ville Syrjälä432081b2016-10-31 22:37:03 +02001633static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001634{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001635 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001636 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001637 int srwm = 1;
1638 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001639 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001640
1641 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001642 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001643 if (crtc) {
1644 /* self-refresh has much higher latency */
1645 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001646 const struct drm_display_mode *adjusted_mode =
1647 &crtc->config->base.adjusted_mode;
1648 const struct drm_framebuffer *fb =
1649 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001650 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001651 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001652 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02001653 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001654 unsigned long line_time_us;
1655 int entries;
1656
Ville Syrjälä922044c2014-02-14 14:18:57 +02001657 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001658
1659 /* Use ns/us then divide to preserve precision */
1660 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001661 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001662 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1663 srwm = I965_FIFO_SIZE - entries;
1664 if (srwm < 0)
1665 srwm = 1;
1666 srwm &= 0x1ff;
1667 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1668 entries, srwm);
1669
1670 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001671 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001672 entries = DIV_ROUND_UP(entries,
1673 i965_cursor_wm_info.cacheline_size);
1674 cursor_sr = i965_cursor_wm_info.fifo_size -
1675 (entries + i965_cursor_wm_info.guard_size);
1676
1677 if (cursor_sr > i965_cursor_wm_info.max_wm)
1678 cursor_sr = i965_cursor_wm_info.max_wm;
1679
1680 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1681 "cursor %d\n", srwm, cursor_sr);
1682
Imre Deak98584252014-06-13 14:54:20 +03001683 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001684 } else {
Imre Deak98584252014-06-13 14:54:20 +03001685 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001686 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001687 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001688 }
1689
1690 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1691 srwm);
1692
1693 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001694 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1695 FW_WM(8, CURSORB) |
1696 FW_WM(8, PLANEB) |
1697 FW_WM(8, PLANEA));
1698 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1699 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001700 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001701 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001702
1703 if (cxsr_enabled)
1704 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001705}
1706
Ville Syrjäläf4998962015-03-10 17:02:21 +02001707#undef FW_WM
1708
Ville Syrjälä432081b2016-10-31 22:37:03 +02001709static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001710{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001711 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001712 const struct intel_watermark_params *wm_info;
1713 uint32_t fwater_lo;
1714 uint32_t fwater_hi;
1715 int cwm, srwm = 1;
1716 int fifo_size;
1717 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001718 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001719
Ville Syrjäläa9097be2016-10-31 22:37:20 +02001720 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001721 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001722 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001723 wm_info = &i915_wm_info;
1724 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001725 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001726
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001727 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001728 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001729 if (intel_crtc_active(crtc)) {
1730 const struct drm_display_mode *adjusted_mode =
1731 &crtc->config->base.adjusted_mode;
1732 const struct drm_framebuffer *fb =
1733 crtc->base.primary->state->fb;
1734 int cpp;
1735
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001736 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001737 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001738 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001739 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001740
Damien Lespiau241bfc32013-09-25 16:45:37 +01001741 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001742 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001743 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001744 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001745 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001746 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001747 if (planea_wm > (long)wm_info->max_wm)
1748 planea_wm = wm_info->max_wm;
1749 }
1750
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001751 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001752 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001753
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001754 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001755 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001756 if (intel_crtc_active(crtc)) {
1757 const struct drm_display_mode *adjusted_mode =
1758 &crtc->config->base.adjusted_mode;
1759 const struct drm_framebuffer *fb =
1760 crtc->base.primary->state->fb;
1761 int cpp;
1762
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001763 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001764 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001765 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001766 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001767
Damien Lespiau241bfc32013-09-25 16:45:37 +01001768 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001769 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001770 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001771 if (enabled == NULL)
1772 enabled = crtc;
1773 else
1774 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001775 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001776 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001777 if (planeb_wm > (long)wm_info->max_wm)
1778 planeb_wm = wm_info->max_wm;
1779 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001780
1781 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1782
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001783 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001784 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001785
Ville Syrjäläefc26112016-10-31 22:37:04 +02001786 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001787
1788 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001789 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001790 enabled = NULL;
1791 }
1792
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001793 /*
1794 * Overlay gets an aggressive default since video jitter is bad.
1795 */
1796 cwm = 2;
1797
1798 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001799 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001800
1801 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02001802 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001803 /* self-refresh has much higher latency */
1804 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001805 const struct drm_display_mode *adjusted_mode =
1806 &enabled->config->base.adjusted_mode;
1807 const struct drm_framebuffer *fb =
1808 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001809 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001810 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001811 int hdisplay = enabled->config->pipe_src_w;
1812 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001813 unsigned long line_time_us;
1814 int entries;
1815
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001816 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001817 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001818 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001819 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001820
Ville Syrjälä922044c2014-02-14 14:18:57 +02001821 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001822
1823 /* Use ns/us then divide to preserve precision */
1824 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001825 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001826 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1827 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1828 srwm = wm_info->fifo_size - entries;
1829 if (srwm < 0)
1830 srwm = 1;
1831
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001832 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001833 I915_WRITE(FW_BLC_SELF,
1834 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001835 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001836 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1837 }
1838
1839 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1840 planea_wm, planeb_wm, cwm, srwm);
1841
1842 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1843 fwater_hi = (cwm & 0x1f);
1844
1845 /* Set request length to 8 cachelines per fetch */
1846 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1847 fwater_hi = fwater_hi | (1 << 8);
1848
1849 I915_WRITE(FW_BLC, fwater_lo);
1850 I915_WRITE(FW_BLC2, fwater_hi);
1851
Imre Deak5209b1f2014-07-01 12:36:17 +03001852 if (enabled)
1853 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001854}
1855
Ville Syrjälä432081b2016-10-31 22:37:03 +02001856static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001857{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001858 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001859 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001860 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001861 uint32_t fwater_lo;
1862 int planea_wm;
1863
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001864 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001865 if (crtc == NULL)
1866 return;
1867
Ville Syrjäläefc26112016-10-31 22:37:04 +02001868 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001869 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001870 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001871 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001872 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001873 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1874 fwater_lo |= (3<<8) | planea_wm;
1875
1876 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1877
1878 I915_WRITE(FW_BLC, fwater_lo);
1879}
1880
Ville Syrjälä37126462013-08-01 16:18:55 +03001881/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001882static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001883{
1884 uint64_t ret;
1885
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001886 if (WARN(latency == 0, "Latency value missing\n"))
1887 return UINT_MAX;
1888
Ville Syrjäläac484962016-01-20 21:05:26 +02001889 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001890 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1891
1892 return ret;
1893}
1894
Ville Syrjälä37126462013-08-01 16:18:55 +03001895/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001896static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001897 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001898 uint32_t latency)
1899{
1900 uint32_t ret;
1901
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001902 if (WARN(latency == 0, "Latency value missing\n"))
1903 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001904 if (WARN_ON(!pipe_htotal))
1905 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001906
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001907 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001908 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001909 ret = DIV_ROUND_UP(ret, 64) + 2;
1910 return ret;
1911}
1912
Ville Syrjälä23297042013-07-05 11:57:17 +03001913static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001914 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001915{
Matt Roper15126882015-12-03 11:37:40 -08001916 /*
1917 * Neither of these should be possible since this function shouldn't be
1918 * called if the CRTC is off or the plane is invisible. But let's be
1919 * extra paranoid to avoid a potential divide-by-zero if we screw up
1920 * elsewhere in the driver.
1921 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001922 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001923 return 0;
1924 if (WARN_ON(!horiz_pixels))
1925 return 0;
1926
Ville Syrjäläac484962016-01-20 21:05:26 +02001927 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001928}
1929
Imre Deak820c1982013-12-17 14:46:36 +02001930struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001931 uint16_t pri;
1932 uint16_t spr;
1933 uint16_t cur;
1934 uint16_t fbc;
1935};
1936
Ville Syrjälä37126462013-08-01 16:18:55 +03001937/*
1938 * For both WM_PIPE and WM_LP.
1939 * mem_value must be in 0.1us units.
1940 */
Matt Roper7221fc32015-09-24 15:53:08 -07001941static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001942 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001943 uint32_t mem_value,
1944 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001945{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001946 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02001947 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001948
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001949 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001950 return 0;
1951
Ville Syrjälä353c8592016-12-14 23:30:57 +02001952 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02001953
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001954 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001955
1956 if (!is_lp)
1957 return method1;
1958
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001959 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07001960 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001961 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001962 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001963
1964 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001965}
1966
Ville Syrjälä37126462013-08-01 16:18:55 +03001967/*
1968 * For both WM_PIPE and WM_LP.
1969 * mem_value must be in 0.1us units.
1970 */
Matt Roper7221fc32015-09-24 15:53:08 -07001971static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001972 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001973 uint32_t mem_value)
1974{
1975 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02001976 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001977
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001978 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001979 return 0;
1980
Ville Syrjälä353c8592016-12-14 23:30:57 +02001981 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02001982
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001983 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
1984 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07001985 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001986 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001987 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001988 return min(method1, method2);
1989}
1990
Ville Syrjälä37126462013-08-01 16:18:55 +03001991/*
1992 * For both WM_PIPE and WM_LP.
1993 * mem_value must be in 0.1us units.
1994 */
Matt Roper7221fc32015-09-24 15:53:08 -07001995static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001996 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001997 uint32_t mem_value)
1998{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02001999 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002000
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002001 /*
2002 * Treat cursor with fb as always visible since cursor updates
2003 * can happen faster than the vrefresh rate, and the current
2004 * watermark code doesn't handle that correctly. Cursor updates
2005 * which set/clear the fb or change the cursor size are going
2006 * to get throttled by intel_legacy_cursor_update() to work
2007 * around this problem with the watermark code.
2008 */
2009 if (!cstate->base.active || !pstate->base.fb)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002010 return 0;
2011
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002012 cpp = pstate->base.fb->format->cpp[0];
2013
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002014 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002015 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002016 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002017}
2018
Paulo Zanonicca32e92013-05-31 11:45:06 -03002019/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002020static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002021 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002022 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002023{
Ville Syrjälä83054942016-11-18 21:53:00 +02002024 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002025
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002026 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002027 return 0;
2028
Ville Syrjälä353c8592016-12-14 23:30:57 +02002029 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002030
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002031 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002032}
2033
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002034static unsigned int
2035ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002036{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002037 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002038 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002039 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002040 return 768;
2041 else
2042 return 512;
2043}
2044
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002045static unsigned int
2046ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2047 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002048{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002049 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002050 /* BDW primary/sprite plane watermarks */
2051 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002052 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002053 /* IVB/HSW primary/sprite plane watermarks */
2054 return level == 0 ? 127 : 1023;
2055 else if (!is_sprite)
2056 /* ILK/SNB primary plane watermarks */
2057 return level == 0 ? 127 : 511;
2058 else
2059 /* ILK/SNB sprite plane watermarks */
2060 return level == 0 ? 63 : 255;
2061}
2062
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002063static unsigned int
2064ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002065{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002066 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002067 return level == 0 ? 63 : 255;
2068 else
2069 return level == 0 ? 31 : 63;
2070}
2071
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002072static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002073{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002074 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002075 return 31;
2076 else
2077 return 15;
2078}
2079
Ville Syrjälä158ae642013-08-07 13:28:19 +03002080/* Calculate the maximum primary/sprite plane watermark */
2081static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2082 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002083 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002084 enum intel_ddb_partitioning ddb_partitioning,
2085 bool is_sprite)
2086{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002087 struct drm_i915_private *dev_priv = to_i915(dev);
2088 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002089
2090 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002091 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002092 return 0;
2093
2094 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002095 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002096 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002097
2098 /*
2099 * For some reason the non self refresh
2100 * FIFO size is only half of the self
2101 * refresh FIFO size on ILK/SNB.
2102 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002103 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002104 fifo_size /= 2;
2105 }
2106
Ville Syrjälä240264f2013-08-07 13:29:12 +03002107 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002108 /* level 0 is always calculated with 1:1 split */
2109 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2110 if (is_sprite)
2111 fifo_size *= 5;
2112 fifo_size /= 6;
2113 } else {
2114 fifo_size /= 2;
2115 }
2116 }
2117
2118 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002119 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002120}
2121
2122/* Calculate the maximum cursor plane watermark */
2123static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002124 int level,
2125 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002126{
2127 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002128 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002129 return 64;
2130
2131 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002132 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002133}
2134
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002135static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002136 int level,
2137 const struct intel_wm_config *config,
2138 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002139 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002140{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002141 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2142 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2143 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002144 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002145}
2146
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002147static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002148 int level,
2149 struct ilk_wm_maximums *max)
2150{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002151 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2152 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2153 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2154 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002155}
2156
Ville Syrjäläd9395652013-10-09 19:18:10 +03002157static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002158 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002159 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002160{
2161 bool ret;
2162
2163 /* already determined to be invalid? */
2164 if (!result->enable)
2165 return false;
2166
2167 result->enable = result->pri_val <= max->pri &&
2168 result->spr_val <= max->spr &&
2169 result->cur_val <= max->cur;
2170
2171 ret = result->enable;
2172
2173 /*
2174 * HACK until we can pre-compute everything,
2175 * and thus fail gracefully if LP0 watermarks
2176 * are exceeded...
2177 */
2178 if (level == 0 && !result->enable) {
2179 if (result->pri_val > max->pri)
2180 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2181 level, result->pri_val, max->pri);
2182 if (result->spr_val > max->spr)
2183 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2184 level, result->spr_val, max->spr);
2185 if (result->cur_val > max->cur)
2186 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2187 level, result->cur_val, max->cur);
2188
2189 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2190 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2191 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2192 result->enable = true;
2193 }
2194
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002195 return ret;
2196}
2197
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002198static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002199 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002200 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002201 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002202 struct intel_plane_state *pristate,
2203 struct intel_plane_state *sprstate,
2204 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002205 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002206{
2207 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2208 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2209 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2210
2211 /* WM1+ latency values stored in 0.5us units */
2212 if (level > 0) {
2213 pri_latency *= 5;
2214 spr_latency *= 5;
2215 cur_latency *= 5;
2216 }
2217
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002218 if (pristate) {
2219 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2220 pri_latency, level);
2221 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2222 }
2223
2224 if (sprstate)
2225 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2226
2227 if (curstate)
2228 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2229
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002230 result->enable = true;
2231}
2232
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002233static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002234hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002235{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002236 const struct intel_atomic_state *intel_state =
2237 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002238 const struct drm_display_mode *adjusted_mode =
2239 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002240 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002241
Matt Roperee91a152015-12-03 11:37:39 -08002242 if (!cstate->base.active)
2243 return 0;
2244 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2245 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002246 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002247 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002248
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002249 /* The WM are computed with base on how long it takes to fill a single
2250 * row at the given clock rate, multiplied by 8.
2251 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002252 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2253 adjusted_mode->crtc_clock);
2254 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002255 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002256
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002257 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2258 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002259}
2260
Ville Syrjäläbb726512016-10-31 22:37:24 +02002261static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2262 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002263{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002264 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002265 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002266 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002267 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002268
2269 /* read the first set of memory latencies[0:3] */
2270 val = 0; /* data0 to be programmed to 0 for first set */
2271 mutex_lock(&dev_priv->rps.hw_lock);
2272 ret = sandybridge_pcode_read(dev_priv,
2273 GEN9_PCODE_READ_MEM_LATENCY,
2274 &val);
2275 mutex_unlock(&dev_priv->rps.hw_lock);
2276
2277 if (ret) {
2278 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2279 return;
2280 }
2281
2282 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2283 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2284 GEN9_MEM_LATENCY_LEVEL_MASK;
2285 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2286 GEN9_MEM_LATENCY_LEVEL_MASK;
2287 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2288 GEN9_MEM_LATENCY_LEVEL_MASK;
2289
2290 /* read the second set of memory latencies[4:7] */
2291 val = 1; /* data0 to be programmed to 1 for second set */
2292 mutex_lock(&dev_priv->rps.hw_lock);
2293 ret = sandybridge_pcode_read(dev_priv,
2294 GEN9_PCODE_READ_MEM_LATENCY,
2295 &val);
2296 mutex_unlock(&dev_priv->rps.hw_lock);
2297 if (ret) {
2298 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2299 return;
2300 }
2301
2302 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2303 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2304 GEN9_MEM_LATENCY_LEVEL_MASK;
2305 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2306 GEN9_MEM_LATENCY_LEVEL_MASK;
2307 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2308 GEN9_MEM_LATENCY_LEVEL_MASK;
2309
Vandana Kannan367294b2014-11-04 17:06:46 +00002310 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002311 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2312 * need to be disabled. We make sure to sanitize the values out
2313 * of the punit to satisfy this requirement.
2314 */
2315 for (level = 1; level <= max_level; level++) {
2316 if (wm[level] == 0) {
2317 for (i = level + 1; i <= max_level; i++)
2318 wm[i] = 0;
2319 break;
2320 }
2321 }
2322
2323 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002324 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002325 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002326 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002327 * to add 2us to the various latency levels we retrieve from the
2328 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002329 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002330 if (wm[0] == 0) {
2331 wm[0] += 2;
2332 for (level = 1; level <= max_level; level++) {
2333 if (wm[level] == 0)
2334 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002335 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002336 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002337 }
2338
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002339 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002340 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2341
2342 wm[0] = (sskpd >> 56) & 0xFF;
2343 if (wm[0] == 0)
2344 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002345 wm[1] = (sskpd >> 4) & 0xFF;
2346 wm[2] = (sskpd >> 12) & 0xFF;
2347 wm[3] = (sskpd >> 20) & 0x1FF;
2348 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002349 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002350 uint32_t sskpd = I915_READ(MCH_SSKPD);
2351
2352 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2353 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2354 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2355 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002356 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002357 uint32_t mltr = I915_READ(MLTR_ILK);
2358
2359 /* ILK primary LP0 latency is 700 ns */
2360 wm[0] = 7;
2361 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2362 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002363 }
2364}
2365
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002366static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2367 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002368{
2369 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002370 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002371 wm[0] = 13;
2372}
2373
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002374static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2375 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002376{
2377 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002378 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002379 wm[0] = 13;
2380
2381 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002382 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002383 wm[3] *= 2;
2384}
2385
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002386int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002387{
2388 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002389 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002390 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002391 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002392 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002393 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002394 return 3;
2395 else
2396 return 2;
2397}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002398
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002399static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002400 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002401 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002402{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002403 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002404
2405 for (level = 0; level <= max_level; level++) {
2406 unsigned int latency = wm[level];
2407
2408 if (latency == 0) {
2409 DRM_ERROR("%s WM%d latency not provided\n",
2410 name, level);
2411 continue;
2412 }
2413
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002414 /*
2415 * - latencies are in us on gen9.
2416 * - before then, WM1+ latency values are in 0.5us units
2417 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002418 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002419 latency *= 10;
2420 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002421 latency *= 5;
2422
2423 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2424 name, level, wm[level],
2425 latency / 10, latency % 10);
2426 }
2427}
2428
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002429static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2430 uint16_t wm[5], uint16_t min)
2431{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002432 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002433
2434 if (wm[0] >= min)
2435 return false;
2436
2437 wm[0] = max(wm[0], min);
2438 for (level = 1; level <= max_level; level++)
2439 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2440
2441 return true;
2442}
2443
Ville Syrjäläbb726512016-10-31 22:37:24 +02002444static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002445{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002446 bool changed;
2447
2448 /*
2449 * The BIOS provided WM memory latency values are often
2450 * inadequate for high resolution displays. Adjust them.
2451 */
2452 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2453 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2454 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2455
2456 if (!changed)
2457 return;
2458
2459 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002460 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2461 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2462 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002463}
2464
Ville Syrjäläbb726512016-10-31 22:37:24 +02002465static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002466{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002467 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002468
2469 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2470 sizeof(dev_priv->wm.pri_latency));
2471 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2472 sizeof(dev_priv->wm.pri_latency));
2473
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002474 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002475 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002476
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002477 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2478 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2479 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002480
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002481 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002482 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002483}
2484
Ville Syrjäläbb726512016-10-31 22:37:24 +02002485static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002486{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002487 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002488 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002489}
2490
Matt Ropered4a6a72016-02-23 17:20:13 -08002491static bool ilk_validate_pipe_wm(struct drm_device *dev,
2492 struct intel_pipe_wm *pipe_wm)
2493{
2494 /* LP0 watermark maximums depend on this pipe alone */
2495 const struct intel_wm_config config = {
2496 .num_pipes_active = 1,
2497 .sprites_enabled = pipe_wm->sprites_enabled,
2498 .sprites_scaled = pipe_wm->sprites_scaled,
2499 };
2500 struct ilk_wm_maximums max;
2501
2502 /* LP0 watermarks always use 1/2 DDB partitioning */
2503 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2504
2505 /* At least LP0 must be valid */
2506 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2507 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2508 return false;
2509 }
2510
2511 return true;
2512}
2513
Matt Roper261a27d2015-10-08 15:28:25 -07002514/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002515static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002516{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002517 struct drm_atomic_state *state = cstate->base.state;
2518 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002519 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002520 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002521 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002522 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002523 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002524 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002525 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002526 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002527 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002528
Matt Ropere8f1f022016-05-12 07:05:55 -07002529 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002530
Matt Roper43d59ed2015-09-24 15:53:07 -07002531 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002532 struct intel_plane_state *ps;
2533
2534 ps = intel_atomic_get_existing_plane_state(state,
2535 intel_plane);
2536 if (!ps)
2537 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002538
2539 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002540 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002541 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002542 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002543 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002544 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002545 }
2546
Matt Ropered4a6a72016-02-23 17:20:13 -08002547 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002548 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002549 pipe_wm->sprites_enabled = sprstate->base.visible;
2550 pipe_wm->sprites_scaled = sprstate->base.visible &&
2551 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2552 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002553 }
2554
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002555 usable_level = max_level;
2556
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002557 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002558 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002559 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002560
2561 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002562 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002563 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002564
Matt Roper86c8bbb2015-09-24 15:53:16 -07002565 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002566 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2567
2568 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2569 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002570
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002571 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002572 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002573
Matt Ropered4a6a72016-02-23 17:20:13 -08002574 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002575 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002576
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002577 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002578
2579 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002580 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002581
Matt Roper86c8bbb2015-09-24 15:53:16 -07002582 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002583 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002584
2585 /*
2586 * Disable any watermark level that exceeds the
2587 * register maximums since such watermarks are
2588 * always invalid.
2589 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002590 if (level > usable_level)
2591 continue;
2592
2593 if (ilk_validate_wm_level(level, &max, wm))
2594 pipe_wm->wm[level] = *wm;
2595 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002596 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002597 }
2598
Matt Roper86c8bbb2015-09-24 15:53:16 -07002599 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002600}
2601
2602/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002603 * Build a set of 'intermediate' watermark values that satisfy both the old
2604 * state and the new state. These can be programmed to the hardware
2605 * immediately.
2606 */
2607static int ilk_compute_intermediate_wm(struct drm_device *dev,
2608 struct intel_crtc *intel_crtc,
2609 struct intel_crtc_state *newstate)
2610{
Matt Ropere8f1f022016-05-12 07:05:55 -07002611 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002612 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002613 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002614
2615 /*
2616 * Start with the final, target watermarks, then combine with the
2617 * currently active watermarks to get values that are safe both before
2618 * and after the vblank.
2619 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002620 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002621 a->pipe_enabled |= b->pipe_enabled;
2622 a->sprites_enabled |= b->sprites_enabled;
2623 a->sprites_scaled |= b->sprites_scaled;
2624
2625 for (level = 0; level <= max_level; level++) {
2626 struct intel_wm_level *a_wm = &a->wm[level];
2627 const struct intel_wm_level *b_wm = &b->wm[level];
2628
2629 a_wm->enable &= b_wm->enable;
2630 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2631 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2632 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2633 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2634 }
2635
2636 /*
2637 * We need to make sure that these merged watermark values are
2638 * actually a valid configuration themselves. If they're not,
2639 * there's no safe way to transition from the old state to
2640 * the new state, so we need to fail the atomic transaction.
2641 */
2642 if (!ilk_validate_pipe_wm(dev, a))
2643 return -EINVAL;
2644
2645 /*
2646 * If our intermediate WM are identical to the final WM, then we can
2647 * omit the post-vblank programming; only update if it's different.
2648 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002649 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
2650 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08002651
2652 return 0;
2653}
2654
2655/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002656 * Merge the watermarks from all active pipes for a specific level.
2657 */
2658static void ilk_merge_wm_level(struct drm_device *dev,
2659 int level,
2660 struct intel_wm_level *ret_wm)
2661{
2662 const struct intel_crtc *intel_crtc;
2663
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002664 ret_wm->enable = true;
2665
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002666 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002667 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002668 const struct intel_wm_level *wm = &active->wm[level];
2669
2670 if (!active->pipe_enabled)
2671 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002672
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002673 /*
2674 * The watermark values may have been used in the past,
2675 * so we must maintain them in the registers for some
2676 * time even if the level is now disabled.
2677 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002678 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002679 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002680
2681 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2682 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2683 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2684 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2685 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002686}
2687
2688/*
2689 * Merge all low power watermarks for all active pipes.
2690 */
2691static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002692 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002693 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002694 struct intel_pipe_wm *merged)
2695{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002696 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002697 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002698 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002699
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002700 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002701 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002702 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002703 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002704
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002705 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002706 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002707
2708 /* merge each WM1+ level */
2709 for (level = 1; level <= max_level; level++) {
2710 struct intel_wm_level *wm = &merged->wm[level];
2711
2712 ilk_merge_wm_level(dev, level, wm);
2713
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002714 if (level > last_enabled_level)
2715 wm->enable = false;
2716 else if (!ilk_validate_wm_level(level, max, wm))
2717 /* make sure all following levels get disabled */
2718 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002719
2720 /*
2721 * The spec says it is preferred to disable
2722 * FBC WMs instead of disabling a WM level.
2723 */
2724 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002725 if (wm->enable)
2726 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002727 wm->fbc_val = 0;
2728 }
2729 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002730
2731 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2732 /*
2733 * FIXME this is racy. FBC might get enabled later.
2734 * What we should check here is whether FBC can be
2735 * enabled sometime later.
2736 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002737 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002738 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002739 for (level = 2; level <= max_level; level++) {
2740 struct intel_wm_level *wm = &merged->wm[level];
2741
2742 wm->enable = false;
2743 }
2744 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002745}
2746
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002747static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2748{
2749 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2750 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2751}
2752
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002753/* The value we need to program into the WM_LPx latency field */
2754static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2755{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002756 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002757
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002758 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002759 return 2 * level;
2760 else
2761 return dev_priv->wm.pri_latency[level];
2762}
2763
Imre Deak820c1982013-12-17 14:46:36 +02002764static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002765 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002766 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002767 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002768{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002769 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002770 struct intel_crtc *intel_crtc;
2771 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002772
Ville Syrjälä0362c782013-10-09 19:17:57 +03002773 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002774 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002775
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002776 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002777 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002778 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002779
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002780 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002781
Ville Syrjälä0362c782013-10-09 19:17:57 +03002782 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002783
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002784 /*
2785 * Maintain the watermark values even if the level is
2786 * disabled. Doing otherwise could cause underruns.
2787 */
2788 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002789 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002790 (r->pri_val << WM1_LP_SR_SHIFT) |
2791 r->cur_val;
2792
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002793 if (r->enable)
2794 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2795
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002796 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002797 results->wm_lp[wm_lp - 1] |=
2798 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2799 else
2800 results->wm_lp[wm_lp - 1] |=
2801 r->fbc_val << WM1_LP_FBC_SHIFT;
2802
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002803 /*
2804 * Always set WM1S_LP_EN when spr_val != 0, even if the
2805 * level is disabled. Doing otherwise could cause underruns.
2806 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002807 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002808 WARN_ON(wm_lp != 1);
2809 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2810 } else
2811 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002812 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002813
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002814 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002815 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002816 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002817 const struct intel_wm_level *r =
2818 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002819
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002820 if (WARN_ON(!r->enable))
2821 continue;
2822
Matt Ropered4a6a72016-02-23 17:20:13 -08002823 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002824
2825 results->wm_pipe[pipe] =
2826 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2827 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2828 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002829 }
2830}
2831
Paulo Zanoni861f3382013-05-31 10:19:21 -03002832/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2833 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002834static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002835 struct intel_pipe_wm *r1,
2836 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002837{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002838 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002839 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002840
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002841 for (level = 1; level <= max_level; level++) {
2842 if (r1->wm[level].enable)
2843 level1 = level;
2844 if (r2->wm[level].enable)
2845 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002846 }
2847
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002848 if (level1 == level2) {
2849 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002850 return r2;
2851 else
2852 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002853 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002854 return r1;
2855 } else {
2856 return r2;
2857 }
2858}
2859
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002860/* dirty bits used to track which watermarks need changes */
2861#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2862#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2863#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2864#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2865#define WM_DIRTY_FBC (1 << 24)
2866#define WM_DIRTY_DDB (1 << 25)
2867
Damien Lespiau055e3932014-08-18 13:49:10 +01002868static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002869 const struct ilk_wm_values *old,
2870 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002871{
2872 unsigned int dirty = 0;
2873 enum pipe pipe;
2874 int wm_lp;
2875
Damien Lespiau055e3932014-08-18 13:49:10 +01002876 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002877 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2878 dirty |= WM_DIRTY_LINETIME(pipe);
2879 /* Must disable LP1+ watermarks too */
2880 dirty |= WM_DIRTY_LP_ALL;
2881 }
2882
2883 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2884 dirty |= WM_DIRTY_PIPE(pipe);
2885 /* Must disable LP1+ watermarks too */
2886 dirty |= WM_DIRTY_LP_ALL;
2887 }
2888 }
2889
2890 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2891 dirty |= WM_DIRTY_FBC;
2892 /* Must disable LP1+ watermarks too */
2893 dirty |= WM_DIRTY_LP_ALL;
2894 }
2895
2896 if (old->partitioning != new->partitioning) {
2897 dirty |= WM_DIRTY_DDB;
2898 /* Must disable LP1+ watermarks too */
2899 dirty |= WM_DIRTY_LP_ALL;
2900 }
2901
2902 /* LP1+ watermarks already deemed dirty, no need to continue */
2903 if (dirty & WM_DIRTY_LP_ALL)
2904 return dirty;
2905
2906 /* Find the lowest numbered LP1+ watermark in need of an update... */
2907 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2908 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2909 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2910 break;
2911 }
2912
2913 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2914 for (; wm_lp <= 3; wm_lp++)
2915 dirty |= WM_DIRTY_LP(wm_lp);
2916
2917 return dirty;
2918}
2919
Ville Syrjälä8553c182013-12-05 15:51:39 +02002920static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2921 unsigned int dirty)
2922{
Imre Deak820c1982013-12-17 14:46:36 +02002923 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002924 bool changed = false;
2925
2926 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2927 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2928 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2929 changed = true;
2930 }
2931 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2932 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2933 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2934 changed = true;
2935 }
2936 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2937 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2938 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2939 changed = true;
2940 }
2941
2942 /*
2943 * Don't touch WM1S_LP_EN here.
2944 * Doing so could cause underruns.
2945 */
2946
2947 return changed;
2948}
2949
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002950/*
2951 * The spec says we shouldn't write when we don't need, because every write
2952 * causes WMs to be re-evaluated, expending some power.
2953 */
Imre Deak820c1982013-12-17 14:46:36 +02002954static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2955 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002956{
Imre Deak820c1982013-12-17 14:46:36 +02002957 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002958 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002959 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002960
Damien Lespiau055e3932014-08-18 13:49:10 +01002961 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002962 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002963 return;
2964
Ville Syrjälä8553c182013-12-05 15:51:39 +02002965 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002966
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002967 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002968 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002969 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002970 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002971 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002972 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2973
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002974 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002975 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002976 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002977 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002978 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002979 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2980
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002981 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002982 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002983 val = I915_READ(WM_MISC);
2984 if (results->partitioning == INTEL_DDB_PART_1_2)
2985 val &= ~WM_MISC_DATA_PARTITION_5_6;
2986 else
2987 val |= WM_MISC_DATA_PARTITION_5_6;
2988 I915_WRITE(WM_MISC, val);
2989 } else {
2990 val = I915_READ(DISP_ARB_CTL2);
2991 if (results->partitioning == INTEL_DDB_PART_1_2)
2992 val &= ~DISP_DATA_PARTITION_5_6;
2993 else
2994 val |= DISP_DATA_PARTITION_5_6;
2995 I915_WRITE(DISP_ARB_CTL2, val);
2996 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002997 }
2998
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002999 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003000 val = I915_READ(DISP_ARB_CTL);
3001 if (results->enable_fbc_wm)
3002 val &= ~DISP_FBC_WM_DIS;
3003 else
3004 val |= DISP_FBC_WM_DIS;
3005 I915_WRITE(DISP_ARB_CTL, val);
3006 }
3007
Imre Deak954911e2013-12-17 14:46:34 +02003008 if (dirty & WM_DIRTY_LP(1) &&
3009 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3010 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3011
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003012 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003013 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3014 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3015 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3016 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3017 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003018
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003019 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003020 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003021 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003022 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003023 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003024 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003025
3026 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003027}
3028
Matt Ropered4a6a72016-02-23 17:20:13 -08003029bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003030{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003031 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003032
3033 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3034}
3035
Lyude656d1b82016-08-17 15:55:54 -04003036#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003037
Matt Roper024c9042015-09-24 15:53:11 -07003038/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003039 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3040 * so assume we'll always need it in order to avoid underruns.
3041 */
3042static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3043{
3044 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3045
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003046 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003047 return true;
3048
3049 return false;
3050}
3051
Paulo Zanoni56feca92016-09-22 18:00:28 -03003052static bool
3053intel_has_sagv(struct drm_i915_private *dev_priv)
3054{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003055 if (IS_KABYLAKE(dev_priv))
3056 return true;
3057
3058 if (IS_SKYLAKE(dev_priv) &&
3059 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3060 return true;
3061
3062 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003063}
3064
Lyude656d1b82016-08-17 15:55:54 -04003065/*
3066 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3067 * depending on power and performance requirements. The display engine access
3068 * to system memory is blocked during the adjustment time. Because of the
3069 * blocking time, having this enabled can cause full system hangs and/or pipe
3070 * underruns if we don't meet all of the following requirements:
3071 *
3072 * - <= 1 pipe enabled
3073 * - All planes can enable watermarks for latencies >= SAGV engine block time
3074 * - We're not using an interlaced display configuration
3075 */
3076int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003077intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003078{
3079 int ret;
3080
Paulo Zanoni56feca92016-09-22 18:00:28 -03003081 if (!intel_has_sagv(dev_priv))
3082 return 0;
3083
3084 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003085 return 0;
3086
3087 DRM_DEBUG_KMS("Enabling the SAGV\n");
3088 mutex_lock(&dev_priv->rps.hw_lock);
3089
3090 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3091 GEN9_SAGV_ENABLE);
3092
3093 /* We don't need to wait for the SAGV when enabling */
3094 mutex_unlock(&dev_priv->rps.hw_lock);
3095
3096 /*
3097 * Some skl systems, pre-release machines in particular,
3098 * don't actually have an SAGV.
3099 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003100 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003101 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003102 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003103 return 0;
3104 } else if (ret < 0) {
3105 DRM_ERROR("Failed to enable the SAGV\n");
3106 return ret;
3107 }
3108
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003109 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003110 return 0;
3111}
3112
Lyude656d1b82016-08-17 15:55:54 -04003113int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003114intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003115{
Imre Deakb3b8e992016-12-05 18:27:38 +02003116 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003117
Paulo Zanoni56feca92016-09-22 18:00:28 -03003118 if (!intel_has_sagv(dev_priv))
3119 return 0;
3120
3121 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003122 return 0;
3123
3124 DRM_DEBUG_KMS("Disabling the SAGV\n");
3125 mutex_lock(&dev_priv->rps.hw_lock);
3126
3127 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003128 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3129 GEN9_SAGV_DISABLE,
3130 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3131 1);
Lyude656d1b82016-08-17 15:55:54 -04003132 mutex_unlock(&dev_priv->rps.hw_lock);
3133
Lyude656d1b82016-08-17 15:55:54 -04003134 /*
3135 * Some skl systems, pre-release machines in particular,
3136 * don't actually have an SAGV.
3137 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003138 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003139 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003140 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003141 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003142 } else if (ret < 0) {
3143 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3144 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003145 }
3146
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003147 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003148 return 0;
3149}
3150
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003151bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003152{
3153 struct drm_device *dev = state->dev;
3154 struct drm_i915_private *dev_priv = to_i915(dev);
3155 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003156 struct intel_crtc *crtc;
3157 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003158 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003159 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003160 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003161
Paulo Zanoni56feca92016-09-22 18:00:28 -03003162 if (!intel_has_sagv(dev_priv))
3163 return false;
3164
Lyude656d1b82016-08-17 15:55:54 -04003165 /*
3166 * SKL workaround: bspec recommends we disable the SAGV when we have
3167 * more then one pipe enabled
3168 *
3169 * If there are no active CRTCs, no additional checks need be performed
3170 */
3171 if (hweight32(intel_state->active_crtcs) == 0)
3172 return true;
3173 else if (hweight32(intel_state->active_crtcs) > 1)
3174 return false;
3175
3176 /* Since we're now guaranteed to only have one active CRTC... */
3177 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003178 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003179 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003180
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003181 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003182 return false;
3183
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003184 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003185 struct skl_plane_wm *wm =
3186 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003187
Lyude656d1b82016-08-17 15:55:54 -04003188 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003189 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003190 continue;
3191
3192 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003193 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003194 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003195 { }
3196
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003197 latency = dev_priv->wm.skl_latency[level];
3198
3199 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003200 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003201 I915_FORMAT_MOD_X_TILED)
3202 latency += 15;
3203
Lyude656d1b82016-08-17 15:55:54 -04003204 /*
3205 * If any of the planes on this pipe don't enable wm levels
3206 * that incur memory latencies higher then 30µs we can't enable
3207 * the SAGV
3208 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003209 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003210 return false;
3211 }
3212
3213 return true;
3214}
3215
Damien Lespiaub9cec072014-11-04 17:06:43 +00003216static void
3217skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003218 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003219 struct skl_ddb_entry *alloc, /* out */
3220 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003221{
Matt Roperc107acf2016-05-12 07:06:01 -07003222 struct drm_atomic_state *state = cstate->base.state;
3223 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3224 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003225 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003226 unsigned int pipe_size, ddb_size;
3227 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003228
Matt Ropera6d3460e2016-05-12 07:06:04 -07003229 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003230 alloc->start = 0;
3231 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003232 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003233 return;
3234 }
3235
Matt Ropera6d3460e2016-05-12 07:06:04 -07003236 if (intel_state->active_pipe_changes)
3237 *num_active = hweight32(intel_state->active_crtcs);
3238 else
3239 *num_active = hweight32(dev_priv->active_crtcs);
3240
Deepak M6f3fff62016-09-15 15:01:10 +05303241 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3242 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003243
3244 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3245
Matt Roperc107acf2016-05-12 07:06:01 -07003246 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003247 * If the state doesn't change the active CRTC's, then there's
3248 * no need to recalculate; the existing pipe allocation limits
3249 * should remain unchanged. Note that we're safe from racing
3250 * commits since any racing commit that changes the active CRTC
3251 * list would need to grab _all_ crtc locks, including the one
3252 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003253 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003254 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003255 /*
3256 * alloc may be cleared by clear_intel_crtc_state,
3257 * copy from old state to be sure
3258 */
3259 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003260 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003261 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003262
3263 nth_active_pipe = hweight32(intel_state->active_crtcs &
3264 (drm_crtc_mask(for_crtc) - 1));
3265 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3266 alloc->start = nth_active_pipe * ddb_size / *num_active;
3267 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003268}
3269
Matt Roperc107acf2016-05-12 07:06:01 -07003270static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003271{
Matt Roperc107acf2016-05-12 07:06:01 -07003272 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003273 return 32;
3274
3275 return 8;
3276}
3277
Damien Lespiaua269c582014-11-04 17:06:49 +00003278static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3279{
3280 entry->start = reg & 0x3ff;
3281 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003282 if (entry->end)
3283 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003284}
3285
Damien Lespiau08db6652014-11-04 17:06:52 +00003286void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3287 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003288{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003289 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003290
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003291 memset(ddb, 0, sizeof(*ddb));
3292
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003293 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003294 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003295 enum plane_id plane_id;
3296 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003297
3298 power_domain = POWER_DOMAIN_PIPE(pipe);
3299 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003300 continue;
3301
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003302 for_each_plane_id_on_crtc(crtc, plane_id) {
3303 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003304
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003305 if (plane_id != PLANE_CURSOR)
3306 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3307 else
3308 val = I915_READ(CUR_BUF_CFG(pipe));
3309
3310 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3311 }
Imre Deak4d800032016-02-17 16:31:29 +02003312
3313 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003314 }
3315}
3316
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003317/*
3318 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3319 * The bspec defines downscale amount as:
3320 *
3321 * """
3322 * Horizontal down scale amount = maximum[1, Horizontal source size /
3323 * Horizontal destination size]
3324 * Vertical down scale amount = maximum[1, Vertical source size /
3325 * Vertical destination size]
3326 * Total down scale amount = Horizontal down scale amount *
3327 * Vertical down scale amount
3328 * """
3329 *
3330 * Return value is provided in 16.16 fixed point form to retain fractional part.
3331 * Caller should take care of dividing & rounding off the value.
3332 */
3333static uint32_t
3334skl_plane_downscale_amount(const struct intel_plane_state *pstate)
3335{
3336 uint32_t downscale_h, downscale_w;
3337 uint32_t src_w, src_h, dst_w, dst_h;
3338
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003339 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003340 return DRM_PLANE_HELPER_NO_SCALING;
3341
3342 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003343 src_w = drm_rect_width(&pstate->base.src);
3344 src_h = drm_rect_height(&pstate->base.src);
3345 dst_w = drm_rect_width(&pstate->base.dst);
3346 dst_h = drm_rect_height(&pstate->base.dst);
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003347 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003348 swap(dst_w, dst_h);
3349
3350 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3351 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3352
3353 /* Provide result in 16.16 fixed point */
3354 return (uint64_t)downscale_w * downscale_h >> 16;
3355}
3356
Damien Lespiaub9cec072014-11-04 17:06:43 +00003357static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003358skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3359 const struct drm_plane_state *pstate,
3360 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003361{
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003362 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003363 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003364 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003365 struct drm_framebuffer *fb;
3366 u32 format;
Matt Ropera1de91e2016-05-12 07:05:57 -07003367
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003368 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003369 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003370
3371 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003372 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003373
Matt Ropera1de91e2016-05-12 07:05:57 -07003374 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3375 return 0;
3376 if (y && format != DRM_FORMAT_NV12)
3377 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003378
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003379 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3380 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003381
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003382 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003383 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003384
3385 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003386 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003387 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003388 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003389 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003390 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003391 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003392 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003393 } else {
3394 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02003395 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003396 }
3397
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003398 down_scale_amount = skl_plane_downscale_amount(intel_pstate);
3399
3400 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003401}
3402
3403/*
3404 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3405 * a 8192x4096@32bpp framebuffer:
3406 * 3 * 4096 * 8192 * 4 < 2^32
3407 */
3408static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003409skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3410 unsigned *plane_data_rate,
3411 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003412{
Matt Roper9c74d822016-05-12 07:05:58 -07003413 struct drm_crtc_state *cstate = &intel_cstate->base;
3414 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003415 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003416 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003417 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003418
3419 if (WARN_ON(!state))
3420 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003421
Matt Ropera1de91e2016-05-12 07:05:57 -07003422 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003423 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003424 enum plane_id plane_id = to_intel_plane(plane)->id;
3425 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003426
Matt Ropera6d3460e2016-05-12 07:06:04 -07003427 /* packed/uv */
3428 rate = skl_plane_relative_data_rate(intel_cstate,
3429 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003430 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003431
3432 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003433
Matt Ropera6d3460e2016-05-12 07:06:04 -07003434 /* y-plane */
3435 rate = skl_plane_relative_data_rate(intel_cstate,
3436 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003437 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003438
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003439 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003440 }
3441
3442 return total_data_rate;
3443}
3444
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003445static uint16_t
3446skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3447 const int y)
3448{
3449 struct drm_framebuffer *fb = pstate->fb;
3450 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3451 uint32_t src_w, src_h;
3452 uint32_t min_scanlines = 8;
3453 uint8_t plane_bpp;
3454
3455 if (WARN_ON(!fb))
3456 return 0;
3457
3458 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003459 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003460 return 0;
3461
3462 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003463 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3464 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003465 return 8;
3466
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003467 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3468 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003469
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003470 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003471 swap(src_w, src_h);
3472
3473 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003474 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003475 src_w /= 2;
3476 src_h /= 2;
3477 }
3478
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003479 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02003480 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003481 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02003482 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003483
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003484 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003485 switch (plane_bpp) {
3486 case 1:
3487 min_scanlines = 32;
3488 break;
3489 case 2:
3490 min_scanlines = 16;
3491 break;
3492 case 4:
3493 min_scanlines = 8;
3494 break;
3495 case 8:
3496 min_scanlines = 4;
3497 break;
3498 default:
3499 WARN(1, "Unsupported pixel depth %u for rotation",
3500 plane_bpp);
3501 min_scanlines = 32;
3502 }
3503 }
3504
3505 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3506}
3507
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003508static void
3509skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3510 uint16_t *minimum, uint16_t *y_minimum)
3511{
3512 const struct drm_plane_state *pstate;
3513 struct drm_plane *plane;
3514
3515 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003516 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003517
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003518 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003519 continue;
3520
3521 if (!pstate->visible)
3522 continue;
3523
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003524 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
3525 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003526 }
3527
3528 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3529}
3530
Matt Roperc107acf2016-05-12 07:06:01 -07003531static int
Matt Roper024c9042015-09-24 15:53:11 -07003532skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003533 struct skl_ddb_allocation *ddb /* out */)
3534{
Matt Roperc107acf2016-05-12 07:06:01 -07003535 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003536 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003537 struct drm_device *dev = crtc->dev;
3538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3539 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003540 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003541 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003542 uint16_t minimum[I915_MAX_PLANES] = {};
3543 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003544 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003545 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07003546 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003547 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3548 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003549
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003550 /* Clear the partitioning for disabled planes. */
3551 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3552 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3553
Matt Ropera6d3460e2016-05-12 07:06:04 -07003554 if (WARN_ON(!state))
3555 return 0;
3556
Matt Roperc107acf2016-05-12 07:06:01 -07003557 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003558 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003559 return 0;
3560 }
3561
Matt Ropera6d3460e2016-05-12 07:06:04 -07003562 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003563 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003564 if (alloc_size == 0) {
3565 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003566 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003567 }
3568
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003569 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003570
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003571 /*
3572 * 1. Allocate the mininum required blocks for each active plane
3573 * and allocate the cursor, it doesn't require extra allocation
3574 * proportional to the data rate.
3575 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003576
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003577 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
3578 alloc_size -= minimum[plane_id];
3579 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003580 }
3581
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003582 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3583 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3584
Damien Lespiaub9cec072014-11-04 17:06:43 +00003585 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003586 * 2. Distribute the remaining space in proportion to the amount of
3587 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003588 *
3589 * FIXME: we may not allocate every single block here.
3590 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003591 total_data_rate = skl_get_total_relative_data_rate(cstate,
3592 plane_data_rate,
3593 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003594 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003595 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003596
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003597 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003598 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003599 unsigned int data_rate, y_data_rate;
3600 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003601
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003602 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003603 continue;
3604
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003605 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003606
3607 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003608 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003609 * promote the expression to 64 bits to avoid overflowing, the
3610 * result is < available as data_rate / total_data_rate < 1
3611 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003612 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003613 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3614 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003615
Matt Roperc107acf2016-05-12 07:06:01 -07003616 /* Leave disabled planes at (0,0) */
3617 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003618 ddb->plane[pipe][plane_id].start = start;
3619 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003620 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003621
3622 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003623
3624 /*
3625 * allocation for y_plane part of planar format:
3626 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003627 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003628
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003629 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07003630 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3631 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003632
Matt Roperc107acf2016-05-12 07:06:01 -07003633 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003634 ddb->y_plane[pipe][plane_id].start = start;
3635 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003636 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003637
Matt Ropera1de91e2016-05-12 07:05:57 -07003638 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003639 }
3640
Matt Roperc107acf2016-05-12 07:06:01 -07003641 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003642}
3643
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003644/*
3645 * 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 +02003646 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003647 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3648 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3649*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303650static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
3651 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003652{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303653 uint32_t wm_intermediate_val;
3654 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003655
3656 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303657 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003658
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303659 wm_intermediate_val = latency * pixel_rate * cpp;
3660 ret = fixed_16_16_div_round_up_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003661 return ret;
3662}
3663
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303664static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
3665 uint32_t pipe_htotal,
3666 uint32_t latency,
3667 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003668{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003669 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303670 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003671
3672 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303673 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003674
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003675 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303676 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
3677 pipe_htotal * 1000);
3678 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003679 return ret;
3680}
3681
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003682static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3683 struct intel_plane_state *pstate)
3684{
3685 uint64_t adjusted_pixel_rate;
3686 uint64_t downscale_amount;
3687 uint64_t pixel_rate;
3688
3689 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003690 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003691 return 0;
3692
3693 /*
3694 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3695 * with additional adjustments for plane-specific scaling.
3696 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003697 adjusted_pixel_rate = cstate->pixel_rate;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003698 downscale_amount = skl_plane_downscale_amount(pstate);
3699
3700 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3701 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3702
3703 return pixel_rate;
3704}
3705
Matt Roper55994c22016-05-12 07:06:08 -07003706static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3707 struct intel_crtc_state *cstate,
3708 struct intel_plane_state *intel_pstate,
3709 uint16_t ddb_allocation,
3710 int level,
3711 uint16_t *out_blocks, /* out */
3712 uint8_t *out_lines, /* out */
3713 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003714{
Matt Roper33815fa2016-05-12 07:06:05 -07003715 struct drm_plane_state *pstate = &intel_pstate->base;
3716 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003717 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303718 uint_fixed_16_16_t method1, method2;
3719 uint_fixed_16_16_t plane_blocks_per_line;
3720 uint_fixed_16_16_t selected_result;
3721 uint32_t interm_pbpl;
3722 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003723 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02003724 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003725 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003726 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303727 uint_fixed_16_16_t y_tile_minimum;
3728 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003729 struct intel_atomic_state *state =
3730 to_intel_atomic_state(cstate->base.state);
3731 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303732 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003733
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003734 if (latency == 0 || !cstate->base.active || !intel_pstate->base.visible) {
Matt Roper55994c22016-05-12 07:06:08 -07003735 *enabled = false;
3736 return 0;
3737 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003738
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303739 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3740 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
3741 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
3742
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05303743 /* Display WA #1141: kbl. */
3744 if (IS_KABYLAKE(dev_priv) && dev_priv->ipc_enabled)
3745 latency += 4;
3746
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303747 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003748 latency += 15;
3749
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003750 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3751 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003752
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003753 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003754 swap(width, height);
3755
Ville Syrjälä353c8592016-12-14 23:30:57 +02003756 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003757 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3758
Dave Airlie61d0a042016-10-25 16:35:20 +10003759 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003760 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02003761 fb->format->cpp[1] :
3762 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003763
3764 switch (cpp) {
3765 case 1:
3766 y_min_scanlines = 16;
3767 break;
3768 case 2:
3769 y_min_scanlines = 8;
3770 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003771 case 4:
3772 y_min_scanlines = 4;
3773 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003774 default:
3775 MISSING_CASE(cpp);
3776 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003777 }
3778 } else {
3779 y_min_scanlines = 4;
3780 }
3781
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02003782 if (apply_memory_bw_wa)
3783 y_min_scanlines *= 2;
3784
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003785 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303786 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303787 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
3788 y_min_scanlines, 512);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003789 plane_blocks_per_line =
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303790 fixed_16_16_div_round_up(interm_pbpl, y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303791 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303792 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
3793 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303794 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303795 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
3796 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003797 }
3798
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003799 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3800 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003801 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003802 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003803 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003804
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303805 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
3806 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003807
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303808 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303809 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003810 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003811 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3812 (plane_bytes_per_line / 512 < 1))
3813 selected_result = method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303814 else if ((ddb_allocation /
3815 fixed_16_16_to_u32_round_up(plane_blocks_per_line)) >= 1)
3816 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003817 else
3818 selected_result = method1;
3819 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003820
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303821 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
3822 res_lines = DIV_ROUND_UP(selected_result.val,
3823 plane_blocks_per_line.val);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003824
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003825 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303826 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303827 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003828 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003829 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003830 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003831 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003832 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003833
Matt Roper55994c22016-05-12 07:06:08 -07003834 if (res_blocks >= ddb_allocation || res_lines > 31) {
3835 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003836
3837 /*
3838 * If there are no valid level 0 watermarks, then we can't
3839 * support this display configuration.
3840 */
3841 if (level) {
3842 return 0;
3843 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003844 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07003845
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003846 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3847 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
3848 plane->base.id, plane->name,
3849 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07003850 return -EINVAL;
3851 }
Matt Roper55994c22016-05-12 07:06:08 -07003852 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003853
3854 *out_blocks = res_blocks;
3855 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003856 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003857
Matt Roper55994c22016-05-12 07:06:08 -07003858 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003859}
3860
Matt Roperf4a96752016-05-12 07:06:06 -07003861static int
3862skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3863 struct skl_ddb_allocation *ddb,
3864 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003865 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003866 int level,
3867 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003868{
Matt Roperf4a96752016-05-12 07:06:06 -07003869 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003870 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003871 struct drm_plane *plane = &intel_plane->base;
3872 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003873 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003874 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003875 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003876
3877 if (state)
3878 intel_pstate =
3879 intel_atomic_get_existing_plane_state(state,
3880 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003881
Matt Roperf4a96752016-05-12 07:06:06 -07003882 /*
Lyudea62163e2016-10-04 14:28:20 -04003883 * Note: If we start supporting multiple pending atomic commits against
3884 * the same planes/CRTC's in the future, plane->state will no longer be
3885 * the correct pre-state to use for the calculations here and we'll
3886 * need to change where we get the 'unchanged' plane data from.
3887 *
3888 * For now this is fine because we only allow one queued commit against
3889 * a CRTC. Even if the plane isn't modified by this transaction and we
3890 * don't have a plane lock, we still have the CRTC's lock, so we know
3891 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003892 */
Lyudea62163e2016-10-04 14:28:20 -04003893 if (!intel_pstate)
3894 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003895
Lyudea62163e2016-10-04 14:28:20 -04003896 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003897
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003898 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07003899
Lyudea62163e2016-10-04 14:28:20 -04003900 ret = skl_compute_plane_wm(dev_priv,
3901 cstate,
3902 intel_pstate,
3903 ddb_blocks,
3904 level,
3905 &result->plane_res_b,
3906 &result->plane_res_l,
3907 &result->plane_en);
3908 if (ret)
3909 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003910
3911 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003912}
3913
Damien Lespiau407b50f2014-11-04 17:06:57 +00003914static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003915skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003916{
Mahesh Kumara3a89862016-12-01 21:19:34 +05303917 struct drm_atomic_state *state = cstate->base.state;
3918 struct drm_i915_private *dev_priv = to_i915(state->dev);
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003919 uint32_t pixel_rate;
Mahesh Kumara3a89862016-12-01 21:19:34 +05303920 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003921
Matt Roper024c9042015-09-24 15:53:11 -07003922 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003923 return 0;
3924
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003925 pixel_rate = cstate->pixel_rate;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003926
3927 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003928 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003929
Mahesh Kumara3a89862016-12-01 21:19:34 +05303930 linetime_wm = DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal *
3931 1000, pixel_rate);
3932
3933 /* Display WA #1135: bxt. */
3934 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
3935 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
3936
3937 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003938}
3939
Matt Roper024c9042015-09-24 15:53:11 -07003940static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003941 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003942{
Matt Roper024c9042015-09-24 15:53:11 -07003943 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003944 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003945
3946 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04003947 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003948}
3949
Matt Roper55994c22016-05-12 07:06:08 -07003950static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3951 struct skl_ddb_allocation *ddb,
3952 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003953{
Matt Roper024c9042015-09-24 15:53:11 -07003954 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003955 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04003956 struct intel_plane *intel_plane;
3957 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003958 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07003959 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003960
Lyudea62163e2016-10-04 14:28:20 -04003961 /*
3962 * We'll only calculate watermarks for planes that are actually
3963 * enabled, so make sure all other planes are set as disabled.
3964 */
3965 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
3966
3967 for_each_intel_plane_mask(&dev_priv->drm,
3968 intel_plane,
3969 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003970 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04003971
3972 for (level = 0; level <= max_level; level++) {
3973 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3974 intel_plane, level,
3975 &wm->wm[level]);
3976 if (ret)
3977 return ret;
3978 }
3979 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003980 }
Matt Roper024c9042015-09-24 15:53:11 -07003981 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003982
Matt Roper55994c22016-05-12 07:06:08 -07003983 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003984}
3985
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003986static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3987 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003988 const struct skl_ddb_entry *entry)
3989{
3990 if (entry->end)
3991 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3992 else
3993 I915_WRITE(reg, 0);
3994}
3995
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003996static void skl_write_wm_level(struct drm_i915_private *dev_priv,
3997 i915_reg_t reg,
3998 const struct skl_wm_level *level)
3999{
4000 uint32_t val = 0;
4001
4002 if (level->plane_en) {
4003 val |= PLANE_WM_EN;
4004 val |= level->plane_res_b;
4005 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4006 }
4007
4008 I915_WRITE(reg, val);
4009}
4010
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004011static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4012 const struct skl_plane_wm *wm,
4013 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004014 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004015{
4016 struct drm_crtc *crtc = &intel_crtc->base;
4017 struct drm_device *dev = crtc->dev;
4018 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004019 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004020 enum pipe pipe = intel_crtc->pipe;
4021
4022 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004023 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004024 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004025 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004026 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004027 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004028
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004029 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4030 &ddb->plane[pipe][plane_id]);
4031 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4032 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004033}
4034
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004035static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4036 const struct skl_plane_wm *wm,
4037 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004038{
4039 struct drm_crtc *crtc = &intel_crtc->base;
4040 struct drm_device *dev = crtc->dev;
4041 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004042 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004043 enum pipe pipe = intel_crtc->pipe;
4044
4045 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004046 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4047 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004048 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004049 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004050
4051 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004052 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004053}
4054
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004055bool skl_wm_level_equals(const struct skl_wm_level *l1,
4056 const struct skl_wm_level *l2)
4057{
4058 if (l1->plane_en != l2->plane_en)
4059 return false;
4060
4061 /* If both planes aren't enabled, the rest shouldn't matter */
4062 if (!l1->plane_en)
4063 return true;
4064
4065 return (l1->plane_res_l == l2->plane_res_l &&
4066 l1->plane_res_b == l2->plane_res_b);
4067}
4068
Lyude27082492016-08-24 07:48:10 +02004069static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4070 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004071{
Lyude27082492016-08-24 07:48:10 +02004072 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004073}
4074
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004075bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4076 const struct skl_ddb_entry *ddb,
4077 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004078{
Lyudece0ba282016-09-15 10:46:35 -04004079 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004080
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004081 for (i = 0; i < I915_MAX_PIPES; i++)
4082 if (i != ignore && entries[i] &&
4083 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004084 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004085
Lyude27082492016-08-24 07:48:10 +02004086 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004087}
4088
Matt Roper55994c22016-05-12 07:06:08 -07004089static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004090 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004091 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004092 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004093 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004094{
Matt Roperf4a96752016-05-12 07:06:06 -07004095 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004096 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004097
Matt Roper55994c22016-05-12 07:06:08 -07004098 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4099 if (ret)
4100 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004101
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004102 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004103 *changed = false;
4104 else
4105 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004106
Matt Roper55994c22016-05-12 07:06:08 -07004107 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004108}
4109
Matt Roper9b613022016-06-27 16:42:44 -07004110static uint32_t
4111pipes_modified(struct drm_atomic_state *state)
4112{
4113 struct drm_crtc *crtc;
4114 struct drm_crtc_state *cstate;
4115 uint32_t i, ret = 0;
4116
4117 for_each_crtc_in_state(state, crtc, cstate, i)
4118 ret |= drm_crtc_mask(crtc);
4119
4120 return ret;
4121}
4122
Jani Nikulabb7791b2016-10-04 12:29:17 +03004123static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004124skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4125{
4126 struct drm_atomic_state *state = cstate->base.state;
4127 struct drm_device *dev = state->dev;
4128 struct drm_crtc *crtc = cstate->base.crtc;
4129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4130 struct drm_i915_private *dev_priv = to_i915(dev);
4131 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4132 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4133 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4134 struct drm_plane_state *plane_state;
4135 struct drm_plane *plane;
4136 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004137
4138 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4139
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004140 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004141 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004142
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004143 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4144 &new_ddb->plane[pipe][plane_id]) &&
4145 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4146 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004147 continue;
4148
4149 plane_state = drm_atomic_get_plane_state(state, plane);
4150 if (IS_ERR(plane_state))
4151 return PTR_ERR(plane_state);
4152 }
4153
4154 return 0;
4155}
4156
Matt Roper98d39492016-05-12 07:06:03 -07004157static int
4158skl_compute_ddb(struct drm_atomic_state *state)
4159{
4160 struct drm_device *dev = state->dev;
4161 struct drm_i915_private *dev_priv = to_i915(dev);
4162 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4163 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004164 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004165 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004166 int ret;
4167
4168 /*
4169 * If this is our first atomic update following hardware readout,
4170 * we can't trust the DDB that the BIOS programmed for us. Let's
4171 * pretend that all pipes switched active status so that we'll
4172 * ensure a full DDB recompute.
4173 */
Matt Roper1b54a882016-06-17 13:42:18 -07004174 if (dev_priv->wm.distrust_bios_wm) {
4175 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4176 state->acquire_ctx);
4177 if (ret)
4178 return ret;
4179
Matt Roper98d39492016-05-12 07:06:03 -07004180 intel_state->active_pipe_changes = ~0;
4181
Matt Roper1b54a882016-06-17 13:42:18 -07004182 /*
4183 * We usually only initialize intel_state->active_crtcs if we
4184 * we're doing a modeset; make sure this field is always
4185 * initialized during the sanitization process that happens
4186 * on the first commit too.
4187 */
4188 if (!intel_state->modeset)
4189 intel_state->active_crtcs = dev_priv->active_crtcs;
4190 }
4191
Matt Roper98d39492016-05-12 07:06:03 -07004192 /*
4193 * If the modeset changes which CRTC's are active, we need to
4194 * recompute the DDB allocation for *all* active pipes, even
4195 * those that weren't otherwise being modified in any way by this
4196 * atomic commit. Due to the shrinking of the per-pipe allocations
4197 * when new active CRTC's are added, it's possible for a pipe that
4198 * we were already using and aren't changing at all here to suddenly
4199 * become invalid if its DDB needs exceeds its new allocation.
4200 *
4201 * Note that if we wind up doing a full DDB recompute, we can't let
4202 * any other display updates race with this transaction, so we need
4203 * to grab the lock on *all* CRTC's.
4204 */
Matt Roper734fa012016-05-12 15:11:40 -07004205 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004206 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004207 intel_state->wm_results.dirty_pipes = ~0;
4208 }
Matt Roper98d39492016-05-12 07:06:03 -07004209
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004210 /*
4211 * We're not recomputing for the pipes not included in the commit, so
4212 * make sure we start with the current state.
4213 */
4214 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4215
Matt Roper98d39492016-05-12 07:06:03 -07004216 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4217 struct intel_crtc_state *cstate;
4218
4219 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4220 if (IS_ERR(cstate))
4221 return PTR_ERR(cstate);
4222
Matt Roper734fa012016-05-12 15:11:40 -07004223 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004224 if (ret)
4225 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004226
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004227 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004228 if (ret)
4229 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004230 }
4231
4232 return 0;
4233}
4234
Matt Roper2722efb2016-08-17 15:55:55 -04004235static void
4236skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4237 struct skl_wm_values *src,
4238 enum pipe pipe)
4239{
Matt Roper2722efb2016-08-17 15:55:55 -04004240 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4241 sizeof(dst->ddb.y_plane[pipe]));
4242 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4243 sizeof(dst->ddb.plane[pipe]));
4244}
4245
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004246static void
4247skl_print_wm_changes(const struct drm_atomic_state *state)
4248{
4249 const struct drm_device *dev = state->dev;
4250 const struct drm_i915_private *dev_priv = to_i915(dev);
4251 const struct intel_atomic_state *intel_state =
4252 to_intel_atomic_state(state);
4253 const struct drm_crtc *crtc;
4254 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004255 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004256 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4257 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004258 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004259
4260 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004261 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4262 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004263
Maarten Lankhorst75704982016-11-01 12:04:10 +01004264 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004265 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004266 const struct skl_ddb_entry *old, *new;
4267
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004268 old = &old_ddb->plane[pipe][plane_id];
4269 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004270
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004271 if (skl_ddb_entry_equal(old, new))
4272 continue;
4273
Maarten Lankhorst75704982016-11-01 12:04:10 +01004274 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4275 intel_plane->base.base.id,
4276 intel_plane->base.name,
4277 old->start, old->end,
4278 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004279 }
4280 }
4281}
4282
Matt Roper98d39492016-05-12 07:06:03 -07004283static int
4284skl_compute_wm(struct drm_atomic_state *state)
4285{
4286 struct drm_crtc *crtc;
4287 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004288 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4289 struct skl_wm_values *results = &intel_state->wm_results;
4290 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004291 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004292 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004293
4294 /*
4295 * If this transaction isn't actually touching any CRTC's, don't
4296 * bother with watermark calculation. Note that if we pass this
4297 * test, we're guaranteed to hold at least one CRTC state mutex,
4298 * which means we can safely use values like dev_priv->active_crtcs
4299 * since any racing commits that want to update them would need to
4300 * hold _all_ CRTC state mutexes.
4301 */
4302 for_each_crtc_in_state(state, crtc, cstate, i)
4303 changed = true;
4304 if (!changed)
4305 return 0;
4306
Matt Roper734fa012016-05-12 15:11:40 -07004307 /* Clear all dirty flags */
4308 results->dirty_pipes = 0;
4309
Matt Roper98d39492016-05-12 07:06:03 -07004310 ret = skl_compute_ddb(state);
4311 if (ret)
4312 return ret;
4313
Matt Roper734fa012016-05-12 15:11:40 -07004314 /*
4315 * Calculate WM's for all pipes that are part of this transaction.
4316 * Note that the DDB allocation above may have added more CRTC's that
4317 * weren't otherwise being modified (and set bits in dirty_pipes) if
4318 * pipe allocations had to change.
4319 *
4320 * FIXME: Now that we're doing this in the atomic check phase, we
4321 * should allow skl_update_pipe_wm() to return failure in cases where
4322 * no suitable watermark values can be found.
4323 */
4324 for_each_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004325 struct intel_crtc_state *intel_cstate =
4326 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004327 const struct skl_pipe_wm *old_pipe_wm =
4328 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004329
4330 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004331 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4332 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004333 if (ret)
4334 return ret;
4335
4336 if (changed)
4337 results->dirty_pipes |= drm_crtc_mask(crtc);
4338
4339 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4340 /* This pipe's WM's did not change */
4341 continue;
4342
4343 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004344 }
4345
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004346 skl_print_wm_changes(state);
4347
Matt Roper98d39492016-05-12 07:06:03 -07004348 return 0;
4349}
4350
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004351static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4352 struct intel_crtc_state *cstate)
4353{
4354 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4355 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4356 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004357 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004358 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004359 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004360
4361 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4362 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004363
4364 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004365
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004366 for_each_plane_id_on_crtc(crtc, plane_id) {
4367 if (plane_id != PLANE_CURSOR)
4368 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4369 ddb, plane_id);
4370 else
4371 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4372 ddb);
4373 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004374}
4375
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004376static void skl_initial_wm(struct intel_atomic_state *state,
4377 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004378{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004379 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004380 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004381 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004382 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004383 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004384 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004385
Ville Syrjälä432081b2016-10-31 22:37:03 +02004386 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004387 return;
4388
Matt Roper734fa012016-05-12 15:11:40 -07004389 mutex_lock(&dev_priv->wm.wm_mutex);
4390
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004391 if (cstate->base.active_changed)
4392 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004393
4394 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004395
4396 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004397}
4398
Ville Syrjäläd8905652016-01-14 14:53:35 +02004399static void ilk_compute_wm_config(struct drm_device *dev,
4400 struct intel_wm_config *config)
4401{
4402 struct intel_crtc *crtc;
4403
4404 /* Compute the currently _active_ config */
4405 for_each_intel_crtc(dev, crtc) {
4406 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4407
4408 if (!wm->pipe_enabled)
4409 continue;
4410
4411 config->sprites_enabled |= wm->sprites_enabled;
4412 config->sprites_scaled |= wm->sprites_scaled;
4413 config->num_pipes_active++;
4414 }
4415}
4416
Matt Ropered4a6a72016-02-23 17:20:13 -08004417static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004418{
Chris Wilson91c8a322016-07-05 10:40:23 +01004419 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004420 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004421 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004422 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004423 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004424 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004425
Ville Syrjäläd8905652016-01-14 14:53:35 +02004426 ilk_compute_wm_config(dev, &config);
4427
4428 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4429 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004430
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004431 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004432 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004433 config.num_pipes_active == 1 && config.sprites_enabled) {
4434 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4435 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004436
Imre Deak820c1982013-12-17 14:46:36 +02004437 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004438 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004439 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004440 }
4441
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004442 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004443 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004444
Imre Deak820c1982013-12-17 14:46:36 +02004445 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004446
Imre Deak820c1982013-12-17 14:46:36 +02004447 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004448}
4449
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004450static void ilk_initial_watermarks(struct intel_atomic_state *state,
4451 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004452{
Matt Ropered4a6a72016-02-23 17:20:13 -08004453 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4454 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004455
Matt Ropered4a6a72016-02-23 17:20:13 -08004456 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004457 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004458 ilk_program_watermarks(dev_priv);
4459 mutex_unlock(&dev_priv->wm.wm_mutex);
4460}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004461
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004462static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4463 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004464{
4465 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4466 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4467
4468 mutex_lock(&dev_priv->wm.wm_mutex);
4469 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004470 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004471 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004472 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004473 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004474}
4475
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004476static inline void skl_wm_level_from_reg_val(uint32_t val,
4477 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004478{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004479 level->plane_en = val & PLANE_WM_EN;
4480 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4481 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4482 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004483}
4484
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004485void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4486 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004487{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004488 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004490 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004491 int level, max_level;
4492 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004493 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004494
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004495 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004496
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004497 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4498 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004499
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004500 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004501 if (plane_id != PLANE_CURSOR)
4502 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004503 else
4504 val = I915_READ(CUR_WM(pipe, level));
4505
4506 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4507 }
4508
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004509 if (plane_id != PLANE_CURSOR)
4510 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004511 else
4512 val = I915_READ(CUR_WM_TRANS(pipe));
4513
4514 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4515 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004516
Matt Roper3ef00282015-03-09 10:19:24 -07004517 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004518 return;
4519
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004520 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004521}
4522
4523void skl_wm_get_hw_state(struct drm_device *dev)
4524{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004525 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004526 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004527 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004528 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004529 struct intel_crtc *intel_crtc;
4530 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004531
Damien Lespiaua269c582014-11-04 17:06:49 +00004532 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004533 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4534 intel_crtc = to_intel_crtc(crtc);
4535 cstate = to_intel_crtc_state(crtc->state);
4536
4537 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4538
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004539 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004540 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004541 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004542
Matt Roper279e99d2016-05-12 07:06:02 -07004543 if (dev_priv->active_crtcs) {
4544 /* Fully recompute DDB on first atomic commit */
4545 dev_priv->wm.distrust_bios_wm = true;
4546 } else {
4547 /* Easy/common case; just sanitize DDB now if everything off */
4548 memset(ddb, 0, sizeof(*ddb));
4549 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004550}
4551
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004552static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4553{
4554 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004555 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004556 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004558 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004559 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004560 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004561 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004562 [PIPE_A] = WM0_PIPEA_ILK,
4563 [PIPE_B] = WM0_PIPEB_ILK,
4564 [PIPE_C] = WM0_PIPEC_IVB,
4565 };
4566
4567 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004568 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004569 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004570
Ville Syrjälä15606532016-05-13 17:55:17 +03004571 memset(active, 0, sizeof(*active));
4572
Matt Roper3ef00282015-03-09 10:19:24 -07004573 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004574
4575 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004576 u32 tmp = hw->wm_pipe[pipe];
4577
4578 /*
4579 * For active pipes LP0 watermark is marked as
4580 * enabled, and LP1+ watermaks as disabled since
4581 * we can't really reverse compute them in case
4582 * multiple pipes are active.
4583 */
4584 active->wm[0].enable = true;
4585 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4586 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4587 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4588 active->linetime = hw->wm_linetime[pipe];
4589 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004590 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004591
4592 /*
4593 * For inactive pipes, all watermark levels
4594 * should be marked as enabled but zeroed,
4595 * which is what we'd compute them to.
4596 */
4597 for (level = 0; level <= max_level; level++)
4598 active->wm[level].enable = true;
4599 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004600
4601 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004602}
4603
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004604#define _FW_WM(value, plane) \
4605 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4606#define _FW_WM_VLV(value, plane) \
4607 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4608
4609static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4610 struct vlv_wm_values *wm)
4611{
4612 enum pipe pipe;
4613 uint32_t tmp;
4614
4615 for_each_pipe(dev_priv, pipe) {
4616 tmp = I915_READ(VLV_DDL(pipe));
4617
Ville Syrjälä1b313892016-11-28 19:37:08 +02004618 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004619 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004620 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004621 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004622 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004623 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004624 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004625 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4626 }
4627
4628 tmp = I915_READ(DSPFW1);
4629 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004630 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
4631 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
4632 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004633
4634 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004635 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
4636 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
4637 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004638
4639 tmp = I915_READ(DSPFW3);
4640 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4641
4642 if (IS_CHERRYVIEW(dev_priv)) {
4643 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004644 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4645 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004646
4647 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004648 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
4649 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004650
4651 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004652 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
4653 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004654
4655 tmp = I915_READ(DSPHOWM);
4656 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004657 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4658 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4659 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
4660 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4661 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4662 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4663 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4664 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4665 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004666 } else {
4667 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004668 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4669 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004670
4671 tmp = I915_READ(DSPHOWM);
4672 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004673 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4674 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4675 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4676 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4677 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4678 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004679 }
4680}
4681
4682#undef _FW_WM
4683#undef _FW_WM_VLV
4684
4685void vlv_wm_get_hw_state(struct drm_device *dev)
4686{
4687 struct drm_i915_private *dev_priv = to_i915(dev);
4688 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02004689 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004690 u32 val;
4691
4692 vlv_read_wm_values(dev_priv, wm);
4693
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004694 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4695 wm->level = VLV_WM_LEVEL_PM2;
4696
4697 if (IS_CHERRYVIEW(dev_priv)) {
4698 mutex_lock(&dev_priv->rps.hw_lock);
4699
4700 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4701 if (val & DSP_MAXFIFO_PM5_ENABLE)
4702 wm->level = VLV_WM_LEVEL_PM5;
4703
Ville Syrjälä58590c12015-09-08 21:05:12 +03004704 /*
4705 * If DDR DVFS is disabled in the BIOS, Punit
4706 * will never ack the request. So if that happens
4707 * assume we don't have to enable/disable DDR DVFS
4708 * dynamically. To test that just set the REQ_ACK
4709 * bit to poke the Punit, but don't change the
4710 * HIGH/LOW bits so that we don't actually change
4711 * the current state.
4712 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004713 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004714 val |= FORCE_DDR_FREQ_REQ_ACK;
4715 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4716
4717 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4718 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4719 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4720 "assuming DDR DVFS is disabled\n");
4721 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4722 } else {
4723 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4724 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4725 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4726 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004727
4728 mutex_unlock(&dev_priv->rps.hw_lock);
4729 }
4730
Ville Syrjäläff32c542017-03-02 19:14:57 +02004731 for_each_intel_crtc(dev, crtc) {
4732 struct intel_crtc_state *crtc_state =
4733 to_intel_crtc_state(crtc->base.state);
4734 struct vlv_wm_state *active = &crtc->wm.active.vlv;
4735 const struct vlv_fifo_state *fifo_state =
4736 &crtc_state->wm.vlv.fifo_state;
4737 enum pipe pipe = crtc->pipe;
4738 enum plane_id plane_id;
4739 int level;
4740
4741 vlv_get_fifo_size(crtc_state);
4742
4743 active->num_levels = wm->level + 1;
4744 active->cxsr = wm->cxsr;
4745
Ville Syrjäläff32c542017-03-02 19:14:57 +02004746 for (level = 0; level < active->num_levels; level++) {
4747 struct vlv_pipe_wm *raw =
4748 &crtc_state->wm.vlv.raw[level];
4749
4750 active->sr[level].plane = wm->sr.plane;
4751 active->sr[level].cursor = wm->sr.cursor;
4752
4753 for_each_plane_id_on_crtc(crtc, plane_id) {
4754 active->wm[level].plane[plane_id] =
4755 wm->pipe[pipe].plane[plane_id];
4756
4757 raw->plane[plane_id] =
4758 vlv_invert_wm_value(active->wm[level].plane[plane_id],
4759 fifo_state->plane[plane_id]);
4760 }
4761 }
4762
4763 for_each_plane_id_on_crtc(crtc, plane_id)
4764 vlv_raw_plane_wm_set(crtc_state, level,
4765 plane_id, USHRT_MAX);
4766 vlv_invalidate_wms(crtc, active, level);
4767
4768 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02004769 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02004770
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004771 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 +02004772 pipe_name(pipe),
4773 wm->pipe[pipe].plane[PLANE_PRIMARY],
4774 wm->pipe[pipe].plane[PLANE_CURSOR],
4775 wm->pipe[pipe].plane[PLANE_SPRITE0],
4776 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02004777 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004778
4779 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4780 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4781}
4782
Ville Syrjälä602ae832017-03-02 19:15:02 +02004783void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
4784{
4785 struct intel_plane *plane;
4786 struct intel_crtc *crtc;
4787
4788 mutex_lock(&dev_priv->wm.wm_mutex);
4789
4790 for_each_intel_plane(&dev_priv->drm, plane) {
4791 struct intel_crtc *crtc =
4792 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
4793 struct intel_crtc_state *crtc_state =
4794 to_intel_crtc_state(crtc->base.state);
4795 struct intel_plane_state *plane_state =
4796 to_intel_plane_state(plane->base.state);
4797 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
4798 const struct vlv_fifo_state *fifo_state =
4799 &crtc_state->wm.vlv.fifo_state;
4800 enum plane_id plane_id = plane->id;
4801 int level;
4802
4803 if (plane_state->base.visible)
4804 continue;
4805
4806 for (level = 0; level < wm_state->num_levels; level++) {
4807 struct vlv_pipe_wm *raw =
4808 &crtc_state->wm.vlv.raw[level];
4809
4810 raw->plane[plane_id] = 0;
4811
4812 wm_state->wm[level].plane[plane_id] =
4813 vlv_invert_wm_value(raw->plane[plane_id],
4814 fifo_state->plane[plane_id]);
4815 }
4816 }
4817
4818 for_each_intel_crtc(&dev_priv->drm, crtc) {
4819 struct intel_crtc_state *crtc_state =
4820 to_intel_crtc_state(crtc->base.state);
4821
4822 crtc_state->wm.vlv.intermediate =
4823 crtc_state->wm.vlv.optimal;
4824 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
4825 }
4826
4827 vlv_program_watermarks(dev_priv);
4828
4829 mutex_unlock(&dev_priv->wm.wm_mutex);
4830}
4831
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004832void ilk_wm_get_hw_state(struct drm_device *dev)
4833{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004834 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004835 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004836 struct drm_crtc *crtc;
4837
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004838 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004839 ilk_pipe_wm_get_hw_state(crtc);
4840
4841 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4842 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4843 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4844
4845 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004846 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004847 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4848 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4849 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004850
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004851 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004852 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4853 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004854 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004855 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4856 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004857
4858 hw->enable_fbc_wm =
4859 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4860}
4861
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004862/**
4863 * intel_update_watermarks - update FIFO watermark values based on current modes
4864 *
4865 * Calculate watermark values for the various WM regs based on current mode
4866 * and plane configuration.
4867 *
4868 * There are several cases to deal with here:
4869 * - normal (i.e. non-self-refresh)
4870 * - self-refresh (SR) mode
4871 * - lines are large relative to FIFO size (buffer can hold up to 2)
4872 * - lines are small relative to FIFO size (buffer can hold more than 2
4873 * lines), so need to account for TLB latency
4874 *
4875 * The normal calculation is:
4876 * watermark = dotclock * bytes per pixel * latency
4877 * where latency is platform & configuration dependent (we assume pessimal
4878 * values here).
4879 *
4880 * The SR calculation is:
4881 * watermark = (trunc(latency/line time)+1) * surface width *
4882 * bytes per pixel
4883 * where
4884 * line time = htotal / dotclock
4885 * surface width = hdisplay for normal plane and 64 for cursor
4886 * and latency is assumed to be high, as above.
4887 *
4888 * The final value programmed to the register should always be rounded up,
4889 * and include an extra 2 entries to account for clock crossings.
4890 *
4891 * We don't use the sprite, so we can ignore that. And on Crestline we have
4892 * to set the non-SR watermarks to 8.
4893 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004894void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004895{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004896 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004897
4898 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004899 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004900}
4901
Jani Nikulae2828912016-01-18 09:19:47 +02004902/*
Daniel Vetter92703882012-08-09 16:46:01 +02004903 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004904 */
4905DEFINE_SPINLOCK(mchdev_lock);
4906
4907/* Global for IPS driver to get at the current i915 device. Protected by
4908 * mchdev_lock. */
4909static struct drm_i915_private *i915_mch_dev;
4910
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004911bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004912{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004913 u16 rgvswctl;
4914
Chris Wilson67520412017-03-02 13:28:01 +00004915 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02004916
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004917 rgvswctl = I915_READ16(MEMSWCTL);
4918 if (rgvswctl & MEMCTL_CMD_STS) {
4919 DRM_DEBUG("gpu busy, RCS change rejected\n");
4920 return false; /* still busy with another command */
4921 }
4922
4923 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4924 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4925 I915_WRITE16(MEMSWCTL, rgvswctl);
4926 POSTING_READ16(MEMSWCTL);
4927
4928 rgvswctl |= MEMCTL_CMD_STS;
4929 I915_WRITE16(MEMSWCTL, rgvswctl);
4930
4931 return true;
4932}
4933
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004934static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004935{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004936 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004937 u8 fmax, fmin, fstart, vstart;
4938
Daniel Vetter92703882012-08-09 16:46:01 +02004939 spin_lock_irq(&mchdev_lock);
4940
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004941 rgvmodectl = I915_READ(MEMMODECTL);
4942
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004943 /* Enable temp reporting */
4944 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4945 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4946
4947 /* 100ms RC evaluation intervals */
4948 I915_WRITE(RCUPEI, 100000);
4949 I915_WRITE(RCDNEI, 100000);
4950
4951 /* Set max/min thresholds to 90ms and 80ms respectively */
4952 I915_WRITE(RCBMAXAVG, 90000);
4953 I915_WRITE(RCBMINAVG, 80000);
4954
4955 I915_WRITE(MEMIHYST, 1);
4956
4957 /* Set up min, max, and cur for interrupt handling */
4958 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4959 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4960 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4961 MEMMODE_FSTART_SHIFT;
4962
Ville Syrjälä616847e2015-09-18 20:03:19 +03004963 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004964 PXVFREQ_PX_SHIFT;
4965
Daniel Vetter20e4d402012-08-08 23:35:39 +02004966 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4967 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004968
Daniel Vetter20e4d402012-08-08 23:35:39 +02004969 dev_priv->ips.max_delay = fstart;
4970 dev_priv->ips.min_delay = fmin;
4971 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004972
4973 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4974 fmax, fmin, fstart);
4975
4976 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4977
4978 /*
4979 * Interrupts will be enabled in ironlake_irq_postinstall
4980 */
4981
4982 I915_WRITE(VIDSTART, vstart);
4983 POSTING_READ(VIDSTART);
4984
4985 rgvmodectl |= MEMMODE_SWMODE_EN;
4986 I915_WRITE(MEMMODECTL, rgvmodectl);
4987
Daniel Vetter92703882012-08-09 16:46:01 +02004988 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004989 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004990 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004991
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004992 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004993
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004994 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4995 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004996 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004997 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004998 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004999
5000 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005001}
5002
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005003static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005004{
Daniel Vetter92703882012-08-09 16:46:01 +02005005 u16 rgvswctl;
5006
5007 spin_lock_irq(&mchdev_lock);
5008
5009 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005010
5011 /* Ack interrupts, disable EFC interrupt */
5012 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5013 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5014 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5015 I915_WRITE(DEIIR, DE_PCU_EVENT);
5016 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5017
5018 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005019 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005020 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005021 rgvswctl |= MEMCTL_CMD_STS;
5022 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005023 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005024
Daniel Vetter92703882012-08-09 16:46:01 +02005025 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005026}
5027
Daniel Vetteracbe9472012-07-26 11:50:05 +02005028/* There's a funny hw issue where the hw returns all 0 when reading from
5029 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5030 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5031 * all limits and the gpu stuck at whatever frequency it is at atm).
5032 */
Akash Goel74ef1172015-03-06 11:07:19 +05305033static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005034{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005035 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005036
Daniel Vetter20b46e52012-07-26 11:16:14 +02005037 /* Only set the down limit when we've reached the lowest level to avoid
5038 * getting more interrupts, otherwise leave this clear. This prevents a
5039 * race in the hw when coming out of rc6: There's a tiny window where
5040 * the hw runs at the minimal clock before selecting the desired
5041 * frequency, if the down threshold expires in that window we will not
5042 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005043 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305044 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5045 if (val <= dev_priv->rps.min_freq_softlimit)
5046 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5047 } else {
5048 limits = dev_priv->rps.max_freq_softlimit << 24;
5049 if (val <= dev_priv->rps.min_freq_softlimit)
5050 limits |= dev_priv->rps.min_freq_softlimit << 16;
5051 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005052
5053 return limits;
5054}
5055
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005056static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5057{
5058 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305059 u32 threshold_up = 0, threshold_down = 0; /* in % */
5060 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005061
5062 new_power = dev_priv->rps.power;
5063 switch (dev_priv->rps.power) {
5064 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005065 if (val > dev_priv->rps.efficient_freq + 1 &&
5066 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005067 new_power = BETWEEN;
5068 break;
5069
5070 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005071 if (val <= dev_priv->rps.efficient_freq &&
5072 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005073 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005074 else if (val >= dev_priv->rps.rp0_freq &&
5075 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005076 new_power = HIGH_POWER;
5077 break;
5078
5079 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005080 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5081 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005082 new_power = BETWEEN;
5083 break;
5084 }
5085 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005086 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005087 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005088 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005089 new_power = HIGH_POWER;
5090 if (new_power == dev_priv->rps.power)
5091 return;
5092
5093 /* Note the units here are not exactly 1us, but 1280ns. */
5094 switch (new_power) {
5095 case LOW_POWER:
5096 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305097 ei_up = 16000;
5098 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005099
5100 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305101 ei_down = 32000;
5102 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005103 break;
5104
5105 case BETWEEN:
5106 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305107 ei_up = 13000;
5108 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005109
5110 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305111 ei_down = 32000;
5112 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005113 break;
5114
5115 case HIGH_POWER:
5116 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305117 ei_up = 10000;
5118 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005119
5120 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305121 ei_down = 32000;
5122 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005123 break;
5124 }
5125
Mika Kuoppala6067a272017-02-15 15:52:59 +02005126 /* When byt can survive without system hang with dynamic
5127 * sw freq adjustments, this restriction can be lifted.
5128 */
5129 if (IS_VALLEYVIEW(dev_priv))
5130 goto skip_hw_write;
5131
Akash Goel8a586432015-03-06 11:07:18 +05305132 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005133 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305134 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005135 GT_INTERVAL_FROM_US(dev_priv,
5136 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305137
5138 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005139 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305140 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005141 GT_INTERVAL_FROM_US(dev_priv,
5142 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305143
Chris Wilsona72b5622016-07-02 15:35:59 +01005144 I915_WRITE(GEN6_RP_CONTROL,
5145 GEN6_RP_MEDIA_TURBO |
5146 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5147 GEN6_RP_MEDIA_IS_GFX |
5148 GEN6_RP_ENABLE |
5149 GEN6_RP_UP_BUSY_AVG |
5150 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305151
Mika Kuoppala6067a272017-02-15 15:52:59 +02005152skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005153 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005154 dev_priv->rps.up_threshold = threshold_up;
5155 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005156 dev_priv->rps.last_adj = 0;
5157}
5158
Chris Wilson2876ce72014-03-28 08:03:34 +00005159static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5160{
5161 u32 mask = 0;
5162
5163 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005164 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005165 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005166 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005167
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005168 mask &= dev_priv->pm_rps_events;
5169
Imre Deak59d02a12014-12-19 19:33:26 +02005170 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005171}
5172
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005173/* gen6_set_rps is called to update the frequency request, but should also be
5174 * called when the range (min_delay and max_delay) is modified so that we can
5175 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005176static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005177{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005178 /* min/max delay may still have been modified so be sure to
5179 * write the limits value.
5180 */
5181 if (val != dev_priv->rps.cur_freq) {
5182 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005183
Chris Wilsondc979972016-05-10 14:10:04 +01005184 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05305185 I915_WRITE(GEN6_RPNSWREQ,
5186 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01005187 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005188 I915_WRITE(GEN6_RPNSWREQ,
5189 HSW_FREQUENCY(val));
5190 else
5191 I915_WRITE(GEN6_RPNSWREQ,
5192 GEN6_FREQUENCY(val) |
5193 GEN6_OFFSET(0) |
5194 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005195 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005196
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005197 /* Make sure we continue to get interrupts
5198 * until we hit the minimum or maximum frequencies.
5199 */
Akash Goel74ef1172015-03-06 11:07:19 +05305200 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00005201 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005202
Ben Widawskyb39fb292014-03-19 18:31:11 -07005203 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02005204 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005205
5206 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005207}
5208
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005209static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005210{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005211 int err;
5212
Chris Wilsondc979972016-05-10 14:10:04 +01005213 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005214 "Odd GPU freq value\n"))
5215 val &= ~1;
5216
Deepak Scd25dd52015-07-10 18:31:40 +05305217 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5218
Chris Wilson8fb55192015-04-07 16:20:28 +01005219 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005220 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
5221 if (err)
5222 return err;
5223
Chris Wilsondb4c5e02017-02-10 15:03:46 +00005224 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005225 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005226
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005227 dev_priv->rps.cur_freq = val;
5228 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005229
5230 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005231}
5232
Deepak Sa7f6e232015-05-09 18:04:44 +05305233/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305234 *
5235 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305236 * 1. Forcewake Media well.
5237 * 2. Request idle freq.
5238 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305239*/
5240static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5241{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005242 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005243 int err;
Deepak S5549d252014-06-28 11:26:11 +05305244
Chris Wilsonaed242f2015-03-18 09:48:21 +00005245 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305246 return;
5247
Chris Wilsonc9efef72017-01-02 15:28:45 +00005248 /* The punit delays the write of the frequency and voltage until it
5249 * determines the GPU is awake. During normal usage we don't want to
5250 * waste power changing the frequency if the GPU is sleeping (rc6).
5251 * However, the GPU and driver is now idle and we do not want to delay
5252 * switching to minimum voltage (reducing power whilst idle) as we do
5253 * not expect to be woken in the near future and so must flush the
5254 * change by waking the device.
5255 *
5256 * We choose to take the media powerwell (either would do to trick the
5257 * punit into committing the voltage change) as that takes a lot less
5258 * power than the render powerwell.
5259 */
Deepak Sa7f6e232015-05-09 18:04:44 +05305260 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005261 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305262 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005263
5264 if (err)
5265 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05305266}
5267
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005268void gen6_rps_busy(struct drm_i915_private *dev_priv)
5269{
5270 mutex_lock(&dev_priv->rps.hw_lock);
5271 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00005272 u8 freq;
5273
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005274 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
5275 gen6_rps_reset_ei(dev_priv);
5276 I915_WRITE(GEN6_PMINTRMSK,
5277 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005278
Chris Wilsonc33d2472016-07-04 08:08:36 +01005279 gen6_enable_rps_interrupts(dev_priv);
5280
Chris Wilsonbd648182017-02-10 15:03:48 +00005281 /* Use the user's desired frequency as a guide, but for better
5282 * performance, jump directly to RPe as our starting frequency.
5283 */
5284 freq = max(dev_priv->rps.cur_freq,
5285 dev_priv->rps.efficient_freq);
5286
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005287 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00005288 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005289 dev_priv->rps.min_freq_softlimit,
5290 dev_priv->rps.max_freq_softlimit)))
5291 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005292 }
5293 mutex_unlock(&dev_priv->rps.hw_lock);
5294}
5295
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005296void gen6_rps_idle(struct drm_i915_private *dev_priv)
5297{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005298 /* Flush our bottom-half so that it does not race with us
5299 * setting the idle frequency and so that it is bounded by
5300 * our rpm wakeref. And then disable the interrupts to stop any
5301 * futher RPS reclocking whilst we are asleep.
5302 */
5303 gen6_disable_rps_interrupts(dev_priv);
5304
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005305 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005306 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005307 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305308 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005309 else
Chris Wilsondc979972016-05-10 14:10:04 +01005310 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005311 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005312 I915_WRITE(GEN6_PMINTRMSK,
5313 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005314 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005315 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005316
Chris Wilson8d3afd72015-05-21 21:01:47 +01005317 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005318 while (!list_empty(&dev_priv->rps.clients))
5319 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005320 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005321}
5322
Chris Wilson1854d5c2015-04-07 16:20:32 +01005323void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005324 struct intel_rps_client *rps,
5325 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005326{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005327 /* This is intentionally racy! We peek at the state here, then
5328 * validate inside the RPS worker.
5329 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005330 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005331 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005332 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005333 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005334
Chris Wilsone61b9952015-04-27 13:41:24 +01005335 /* Force a RPS boost (and don't count it against the client) if
5336 * the GPU is severely congested.
5337 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005338 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005339 rps = NULL;
5340
Chris Wilson8d3afd72015-05-21 21:01:47 +01005341 spin_lock(&dev_priv->rps.client_lock);
5342 if (rps == NULL || list_empty(&rps->link)) {
5343 spin_lock_irq(&dev_priv->irq_lock);
5344 if (dev_priv->rps.interrupts_enabled) {
5345 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005346 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005347 }
5348 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005349
Chris Wilson2e1b8732015-04-27 13:41:22 +01005350 if (rps != NULL) {
5351 list_add(&rps->link, &dev_priv->rps.clients);
5352 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005353 } else
5354 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005355 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005356 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005357}
5358
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005359int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005360{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005361 int err;
5362
Chris Wilsoncfd1c482017-02-20 09:47:07 +00005363 lockdep_assert_held(&dev_priv->rps.hw_lock);
5364 GEM_BUG_ON(val > dev_priv->rps.max_freq);
5365 GEM_BUG_ON(val < dev_priv->rps.min_freq);
5366
Chris Wilson76e4e4b2017-02-20 09:47:08 +00005367 if (!dev_priv->rps.enabled) {
5368 dev_priv->rps.cur_freq = val;
5369 return 0;
5370 }
5371
Chris Wilsondc979972016-05-10 14:10:04 +01005372 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005373 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005374 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005375 err = gen6_set_rps(dev_priv, val);
5376
5377 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005378}
5379
Chris Wilsondc979972016-05-10 14:10:04 +01005380static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005381{
Zhe Wang20e49362014-11-04 17:07:05 +00005382 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005383 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005384}
5385
Chris Wilsondc979972016-05-10 14:10:04 +01005386static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305387{
Akash Goel2030d682016-04-23 00:05:45 +05305388 I915_WRITE(GEN6_RP_CONTROL, 0);
5389}
5390
Chris Wilsondc979972016-05-10 14:10:04 +01005391static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005392{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005393 I915_WRITE(GEN6_RC_CONTROL, 0);
5394 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305395 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005396}
5397
Chris Wilsondc979972016-05-10 14:10:04 +01005398static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305399{
Deepak S38807742014-05-23 21:00:15 +05305400 I915_WRITE(GEN6_RC_CONTROL, 0);
5401}
5402
Chris Wilsondc979972016-05-10 14:10:04 +01005403static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005404{
Deepak S98a2e5f2014-08-18 10:35:27 -07005405 /* we're doing forcewake before Disabling RC6,
5406 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005407 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005408
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005409 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005410
Mika Kuoppala59bad942015-01-16 11:34:40 +02005411 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005412}
5413
Chris Wilsondc979972016-05-10 14:10:04 +01005414static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005415{
Chris Wilsondc979972016-05-10 14:10:04 +01005416 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005417 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5418 mode = GEN6_RC_CTL_RC6_ENABLE;
5419 else
5420 mode = 0;
5421 }
Chris Wilsondc979972016-05-10 14:10:04 +01005422 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005423 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5424 "RC6 %s RC6p %s RC6pp %s\n",
5425 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5426 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5427 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005428
5429 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005430 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5431 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005432}
5433
Chris Wilsondc979972016-05-10 14:10:04 +01005434static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305435{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005436 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305437 bool enable_rc6 = true;
5438 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005439 u32 rc_ctl;
5440 int rc_sw_target;
5441
5442 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5443 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5444 RC_SW_TARGET_STATE_SHIFT;
5445 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5446 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5447 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5448 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5449 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305450
5451 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005452 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305453 enable_rc6 = false;
5454 }
5455
5456 /*
5457 * The exact context size is not known for BXT, so assume a page size
5458 * for this check.
5459 */
5460 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005461 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5462 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5463 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005464 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305465 enable_rc6 = false;
5466 }
5467
5468 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5469 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5470 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5471 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005472 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305473 enable_rc6 = false;
5474 }
5475
Imre Deakfc619842016-06-29 19:13:55 +03005476 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5477 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5478 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5479 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5480 enable_rc6 = false;
5481 }
5482
5483 if (!I915_READ(GEN6_GFXPAUSE)) {
5484 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5485 enable_rc6 = false;
5486 }
5487
5488 if (!I915_READ(GEN8_MISC_CTRL0)) {
5489 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305490 enable_rc6 = false;
5491 }
5492
5493 return enable_rc6;
5494}
5495
Chris Wilsondc979972016-05-10 14:10:04 +01005496int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005497{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005498 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005499 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005500 return 0;
5501
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305502 if (!enable_rc6)
5503 return 0;
5504
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005505 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305506 DRM_INFO("RC6 disabled by BIOS\n");
5507 return 0;
5508 }
5509
Daniel Vetter456470e2012-08-08 23:35:40 +02005510 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005511 if (enable_rc6 >= 0) {
5512 int mask;
5513
Chris Wilsondc979972016-05-10 14:10:04 +01005514 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005515 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5516 INTEL_RC6pp_ENABLE;
5517 else
5518 mask = INTEL_RC6_ENABLE;
5519
5520 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005521 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5522 "(requested %d, valid %d)\n",
5523 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005524
5525 return enable_rc6 & mask;
5526 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005527
Chris Wilsondc979972016-05-10 14:10:04 +01005528 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005529 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005530
5531 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005532}
5533
Chris Wilsondc979972016-05-10 14:10:04 +01005534static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005535{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005536 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005537
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005538 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005539 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005540 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005541 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5542 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5543 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5544 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005545 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005546 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5547 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5548 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5549 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005550 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005551 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005552
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005553 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005554 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005555 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005556 u32 ddcc_status = 0;
5557
5558 if (sandybridge_pcode_read(dev_priv,
5559 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5560 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005561 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005562 clamp_t(u8,
5563 ((ddcc_status >> 8) & 0xff),
5564 dev_priv->rps.min_freq,
5565 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005566 }
5567
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005568 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305569 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005570 * the natural hardware unit for SKL
5571 */
Akash Goelc5e06882015-06-29 14:50:19 +05305572 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5573 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5574 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5575 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5576 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5577 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005578}
5579
Chris Wilson3a45b052016-07-13 09:10:32 +01005580static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005581 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01005582{
5583 u8 freq = dev_priv->rps.cur_freq;
5584
5585 /* force a reset */
5586 dev_priv->rps.power = -1;
5587 dev_priv->rps.cur_freq = -1;
5588
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005589 if (set(dev_priv, freq))
5590 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01005591}
5592
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005593/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005594static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005595{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005596 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5597
Akash Goel0beb0592015-03-06 11:07:20 +05305598 /* Program defaults and thresholds for RPS*/
5599 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5600 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005601
Akash Goel0beb0592015-03-06 11:07:20 +05305602 /* 1 second timeout*/
5603 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5604 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5605
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005606 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005607
Akash Goel0beb0592015-03-06 11:07:20 +05305608 /* Leaning on the below call to gen6_set_rps to program/setup the
5609 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5610 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005611 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005612
5613 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5614}
5615
Chris Wilsondc979972016-05-10 14:10:04 +01005616static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005617{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005618 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305619 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005620 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005621
5622 /* 1a: Software RC state - RC0 */
5623 I915_WRITE(GEN6_RC_STATE, 0);
5624
5625 /* 1b: Get forcewake during program sequence. Although the driver
5626 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005627 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005628
5629 /* 2a: Disable RC states. */
5630 I915_WRITE(GEN6_RC_CONTROL, 0);
5631
5632 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305633
5634 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005635 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305636 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5637 else
5638 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005639 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5640 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305641 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005642 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305643
Dave Gordon1a3d1892016-05-13 15:36:30 +01005644 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305645 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5646
Zhe Wang20e49362014-11-04 17:07:05 +00005647 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005648
Zhe Wang38c23522015-01-20 12:23:04 +00005649 /* 2c: Program Coarse Power Gating Policies. */
5650 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5651 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5652
Zhe Wang20e49362014-11-04 17:07:05 +00005653 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005654 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005655 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005656 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00005657 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
5658 I915_WRITE(GEN6_RC_CONTROL,
5659 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00005660
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305661 /*
5662 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305663 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305664 */
Chris Wilsondc979972016-05-10 14:10:04 +01005665 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305666 I915_WRITE(GEN9_PG_ENABLE, 0);
5667 else
5668 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5669 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005670
Mika Kuoppala59bad942015-01-16 11:34:40 +02005671 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005672}
5673
Chris Wilsondc979972016-05-10 14:10:04 +01005674static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005675{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005676 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305677 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005678 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005679
5680 /* 1a: Software RC state - RC0 */
5681 I915_WRITE(GEN6_RC_STATE, 0);
5682
5683 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5684 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005685 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005686
5687 /* 2a: Disable RC states. */
5688 I915_WRITE(GEN6_RC_CONTROL, 0);
5689
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005690 /* 2b: Program RC6 thresholds.*/
5691 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5692 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5693 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305694 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005695 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005696 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005697 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005698 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5699 else
5700 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005701
5702 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005703 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005704 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005705 intel_print_rc6_info(dev_priv, rc6_mask);
5706 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005707 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5708 GEN7_RC_CTL_TO_MODE |
5709 rc6_mask);
5710 else
5711 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5712 GEN6_RC_CTL_EI_MODE(1) |
5713 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005714
5715 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005716 I915_WRITE(GEN6_RPNSWREQ,
5717 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5718 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5719 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005720 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5721 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005722
Daniel Vetter7526ed72014-09-29 15:07:19 +02005723 /* Docs recommend 900MHz, and 300 MHz respectively */
5724 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5725 dev_priv->rps.max_freq_softlimit << 24 |
5726 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005727
Daniel Vetter7526ed72014-09-29 15:07:19 +02005728 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5729 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5730 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5731 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005732
Daniel Vetter7526ed72014-09-29 15:07:19 +02005733 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005734
5735 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005736 I915_WRITE(GEN6_RP_CONTROL,
5737 GEN6_RP_MEDIA_TURBO |
5738 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5739 GEN6_RP_MEDIA_IS_GFX |
5740 GEN6_RP_ENABLE |
5741 GEN6_RP_UP_BUSY_AVG |
5742 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005743
Daniel Vetter7526ed72014-09-29 15:07:19 +02005744 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005745
Chris Wilson3a45b052016-07-13 09:10:32 +01005746 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005747
Mika Kuoppala59bad942015-01-16 11:34:40 +02005748 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005749}
5750
Chris Wilsondc979972016-05-10 14:10:04 +01005751static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005752{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005753 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305754 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005755 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005756 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005757 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005758 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005759
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005760 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005761
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005762 /* Here begins a magic sequence of register writes to enable
5763 * auto-downclocking.
5764 *
5765 * Perhaps there might be some value in exposing these to
5766 * userspace...
5767 */
5768 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005769
5770 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005771 gtfifodbg = I915_READ(GTFIFODBG);
5772 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005773 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5774 I915_WRITE(GTFIFODBG, gtfifodbg);
5775 }
5776
Mika Kuoppala59bad942015-01-16 11:34:40 +02005777 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005778
5779 /* disable the counters and set deterministic thresholds */
5780 I915_WRITE(GEN6_RC_CONTROL, 0);
5781
5782 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5783 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5784 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5785 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5786 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5787
Akash Goel3b3f1652016-10-13 22:44:48 +05305788 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005789 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005790
5791 I915_WRITE(GEN6_RC_SLEEP, 0);
5792 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005793 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005794 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5795 else
5796 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005797 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005798 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5799
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005800 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005801 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005802 if (rc6_mode & INTEL_RC6_ENABLE)
5803 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5804
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005805 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005806 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005807 if (rc6_mode & INTEL_RC6p_ENABLE)
5808 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005809
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005810 if (rc6_mode & INTEL_RC6pp_ENABLE)
5811 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5812 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005813
Chris Wilsondc979972016-05-10 14:10:04 +01005814 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005815
5816 I915_WRITE(GEN6_RC_CONTROL,
5817 rc6_mask |
5818 GEN6_RC_CTL_EI_MODE(1) |
5819 GEN6_RC_CTL_HW_ENABLE);
5820
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005821 /* Power down if completely idle for over 50ms */
5822 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005823 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005824
Chris Wilson3a45b052016-07-13 09:10:32 +01005825 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005826
Ben Widawsky31643d52012-09-26 10:34:01 -07005827 rc6vids = 0;
5828 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005829 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005830 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005831 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005832 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5833 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5834 rc6vids &= 0xffff00;
5835 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5836 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5837 if (ret)
5838 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5839 }
5840
Mika Kuoppala59bad942015-01-16 11:34:40 +02005841 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005842}
5843
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005844static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005845{
5846 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005847 unsigned int gpu_freq;
5848 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305849 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005850 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005851 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005852
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005853 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005854
Ben Widawskyeda79642013-10-07 17:15:48 -03005855 policy = cpufreq_cpu_get(0);
5856 if (policy) {
5857 max_ia_freq = policy->cpuinfo.max_freq;
5858 cpufreq_cpu_put(policy);
5859 } else {
5860 /*
5861 * Default to measured freq if none found, PCU will ensure we
5862 * don't go over
5863 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005864 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005865 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005866
5867 /* Convert from kHz to MHz */
5868 max_ia_freq /= 1000;
5869
Ben Widawsky153b4b952013-10-22 22:05:09 -07005870 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005871 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5872 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005873
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005874 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305875 /* Convert GT frequency to 50 HZ units */
5876 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5877 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5878 } else {
5879 min_gpu_freq = dev_priv->rps.min_freq;
5880 max_gpu_freq = dev_priv->rps.max_freq;
5881 }
5882
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005883 /*
5884 * For each potential GPU frequency, load a ring frequency we'd like
5885 * to use for memory access. We do this by specifying the IA frequency
5886 * the PCU should use as a reference to determine the ring frequency.
5887 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305888 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5889 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005890 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005891
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005892 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305893 /*
5894 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5895 * No floor required for ring frequency on SKL.
5896 */
5897 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005898 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005899 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5900 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005901 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005902 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005903 ring_freq = max(min_ring_freq, ring_freq);
5904 /* leave ia_freq as the default, chosen by cpufreq */
5905 } else {
5906 /* On older processors, there is no separate ring
5907 * clock domain, so in order to boost the bandwidth
5908 * of the ring, we need to upclock the CPU (ia_freq).
5909 *
5910 * For GPU frequencies less than 750MHz,
5911 * just use the lowest ring freq.
5912 */
5913 if (gpu_freq < min_freq)
5914 ia_freq = 800;
5915 else
5916 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5917 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5918 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005919
Ben Widawsky42c05262012-09-26 10:34:00 -07005920 sandybridge_pcode_write(dev_priv,
5921 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005922 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5923 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5924 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005925 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005926}
5927
Ville Syrjälä03af2042014-06-28 02:03:53 +03005928static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305929{
5930 u32 val, rp0;
5931
Jani Nikula5b5929c2015-10-07 11:17:46 +03005932 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305933
Imre Deak43b67992016-08-31 19:13:02 +03005934 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005935 case 8:
5936 /* (2 * 4) config */
5937 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5938 break;
5939 case 12:
5940 /* (2 * 6) config */
5941 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5942 break;
5943 case 16:
5944 /* (2 * 8) config */
5945 default:
5946 /* Setting (2 * 8) Min RP0 for any other combination */
5947 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5948 break;
Deepak S095acd52015-01-17 11:05:59 +05305949 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005950
5951 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5952
Deepak S2b6b3a02014-05-27 15:59:30 +05305953 return rp0;
5954}
5955
5956static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5957{
5958 u32 val, rpe;
5959
5960 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5961 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5962
5963 return rpe;
5964}
5965
Deepak S7707df42014-07-12 18:46:14 +05305966static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5967{
5968 u32 val, rp1;
5969
Jani Nikula5b5929c2015-10-07 11:17:46 +03005970 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5971 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5972
Deepak S7707df42014-07-12 18:46:14 +05305973 return rp1;
5974}
5975
Deepak S96676fe2016-08-12 18:46:41 +05305976static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
5977{
5978 u32 val, rpn;
5979
5980 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
5981 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
5982 FB_GFX_FREQ_FUSE_MASK);
5983
5984 return rpn;
5985}
5986
Deepak Sf8f2b002014-07-10 13:16:21 +05305987static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5988{
5989 u32 val, rp1;
5990
5991 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5992
5993 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5994
5995 return rp1;
5996}
5997
Ville Syrjälä03af2042014-06-28 02:03:53 +03005998static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005999{
6000 u32 val, rp0;
6001
Jani Nikula64936252013-05-22 15:36:20 +03006002 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006003
6004 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6005 /* Clamp to max */
6006 rp0 = min_t(u32, rp0, 0xea);
6007
6008 return rp0;
6009}
6010
6011static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6012{
6013 u32 val, rpe;
6014
Jani Nikula64936252013-05-22 15:36:20 +03006015 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006016 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006017 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006018 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6019
6020 return rpe;
6021}
6022
Ville Syrjälä03af2042014-06-28 02:03:53 +03006023static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006024{
Imre Deak36146032014-12-04 18:39:35 +02006025 u32 val;
6026
6027 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6028 /*
6029 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6030 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6031 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6032 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6033 * to make sure it matches what Punit accepts.
6034 */
6035 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006036}
6037
Imre Deakae484342014-03-31 15:10:44 +03006038/* Check that the pctx buffer wasn't move under us. */
6039static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6040{
6041 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6042
6043 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6044 dev_priv->vlv_pctx->stolen->start);
6045}
6046
Deepak S38807742014-05-23 21:00:15 +05306047
6048/* Check that the pcbr address is not empty. */
6049static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6050{
6051 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6052
6053 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6054}
6055
Chris Wilsondc979972016-05-10 14:10:04 +01006056static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306057{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006058 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006059 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306060 u32 pcbr;
6061 int pctx_size = 32*1024;
6062
Deepak S38807742014-05-23 21:00:15 +05306063 pcbr = I915_READ(VLV_PCBR);
6064 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006065 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306066 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006067 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306068
6069 pctx_paddr = (paddr & (~4095));
6070 I915_WRITE(VLV_PCBR, pctx_paddr);
6071 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006072
6073 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306074}
6075
Chris Wilsondc979972016-05-10 14:10:04 +01006076static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006077{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006078 struct drm_i915_gem_object *pctx;
6079 unsigned long pctx_paddr;
6080 u32 pcbr;
6081 int pctx_size = 24*1024;
6082
6083 pcbr = I915_READ(VLV_PCBR);
6084 if (pcbr) {
6085 /* BIOS set it up already, grab the pre-alloc'd space */
6086 int pcbr_offset;
6087
6088 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006089 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006090 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006091 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006092 pctx_size);
6093 goto out;
6094 }
6095
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006096 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6097
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006098 /*
6099 * From the Gunit register HAS:
6100 * The Gfx driver is expected to program this register and ensure
6101 * proper allocation within Gfx stolen memory. For example, this
6102 * register should be programmed such than the PCBR range does not
6103 * overlap with other ranges, such as the frame buffer, protected
6104 * memory, or any other relevant ranges.
6105 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006106 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006107 if (!pctx) {
6108 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006109 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006110 }
6111
6112 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6113 I915_WRITE(VLV_PCBR, pctx_paddr);
6114
6115out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006116 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006117 dev_priv->vlv_pctx = pctx;
6118}
6119
Chris Wilsondc979972016-05-10 14:10:04 +01006120static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006121{
Imre Deakae484342014-03-31 15:10:44 +03006122 if (WARN_ON(!dev_priv->vlv_pctx))
6123 return;
6124
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006125 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006126 dev_priv->vlv_pctx = NULL;
6127}
6128
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006129static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6130{
6131 dev_priv->rps.gpll_ref_freq =
6132 vlv_get_cck_clock(dev_priv, "GPLL ref",
6133 CCK_GPLL_CLOCK_CONTROL,
6134 dev_priv->czclk_freq);
6135
6136 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6137 dev_priv->rps.gpll_ref_freq);
6138}
6139
Chris Wilsondc979972016-05-10 14:10:04 +01006140static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006141{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006142 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006143
Chris Wilsondc979972016-05-10 14:10:04 +01006144 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006145
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006146 vlv_init_gpll_ref_freq(dev_priv);
6147
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006148 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6149 switch ((val >> 6) & 3) {
6150 case 0:
6151 case 1:
6152 dev_priv->mem_freq = 800;
6153 break;
6154 case 2:
6155 dev_priv->mem_freq = 1066;
6156 break;
6157 case 3:
6158 dev_priv->mem_freq = 1333;
6159 break;
6160 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006161 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006162
Imre Deak4e805192014-04-14 20:24:41 +03006163 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6164 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6165 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006166 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006167 dev_priv->rps.max_freq);
6168
6169 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6170 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006171 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006172 dev_priv->rps.efficient_freq);
6173
Deepak Sf8f2b002014-07-10 13:16:21 +05306174 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6175 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006176 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306177 dev_priv->rps.rp1_freq);
6178
Imre Deak4e805192014-04-14 20:24:41 +03006179 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6180 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006181 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006182 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006183}
6184
Chris Wilsondc979972016-05-10 14:10:04 +01006185static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306186{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006187 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306188
Chris Wilsondc979972016-05-10 14:10:04 +01006189 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306190
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006191 vlv_init_gpll_ref_freq(dev_priv);
6192
Ville Syrjäläa5805162015-05-26 20:42:30 +03006193 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006194 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006195 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006196
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006197 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006198 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006199 dev_priv->mem_freq = 2000;
6200 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006201 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006202 dev_priv->mem_freq = 1600;
6203 break;
6204 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006205 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006206
Deepak S2b6b3a02014-05-27 15:59:30 +05306207 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
6208 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6209 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006210 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306211 dev_priv->rps.max_freq);
6212
6213 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
6214 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006215 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306216 dev_priv->rps.efficient_freq);
6217
Deepak S7707df42014-07-12 18:46:14 +05306218 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
6219 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006220 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05306221 dev_priv->rps.rp1_freq);
6222
Deepak S96676fe2016-08-12 18:46:41 +05306223 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306224 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006225 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306226 dev_priv->rps.min_freq);
6227
Ville Syrjälä1c147622014-08-18 14:42:43 +03006228 WARN_ONCE((dev_priv->rps.max_freq |
6229 dev_priv->rps.efficient_freq |
6230 dev_priv->rps.rp1_freq |
6231 dev_priv->rps.min_freq) & 1,
6232 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05306233}
6234
Chris Wilsondc979972016-05-10 14:10:04 +01006235static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006236{
Chris Wilsondc979972016-05-10 14:10:04 +01006237 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006238}
6239
Chris Wilsondc979972016-05-10 14:10:04 +01006240static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306241{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006242 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306243 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306244 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306245
6246 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6247
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006248 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6249 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306250 if (gtfifodbg) {
6251 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6252 gtfifodbg);
6253 I915_WRITE(GTFIFODBG, gtfifodbg);
6254 }
6255
6256 cherryview_check_pctx(dev_priv);
6257
6258 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6259 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006260 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306261
Ville Syrjälä160614a2015-01-19 13:50:47 +02006262 /* Disable RC states. */
6263 I915_WRITE(GEN6_RC_CONTROL, 0);
6264
Deepak S38807742014-05-23 21:00:15 +05306265 /* 2a: Program RC6 thresholds.*/
6266 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6267 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6268 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6269
Akash Goel3b3f1652016-10-13 22:44:48 +05306270 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006271 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306272 I915_WRITE(GEN6_RC_SLEEP, 0);
6273
Deepak Sf4f71c72015-03-28 15:23:35 +05306274 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6275 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306276
6277 /* allows RC6 residency counter to work */
6278 I915_WRITE(VLV_COUNTER_CONTROL,
6279 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6280 VLV_MEDIA_RC6_COUNT_EN |
6281 VLV_RENDER_RC6_COUNT_EN));
6282
6283 /* For now we assume BIOS is allocating and populating the PCBR */
6284 pcbr = I915_READ(VLV_PCBR);
6285
Deepak S38807742014-05-23 21:00:15 +05306286 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006287 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6288 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006289 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306290
6291 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6292
Deepak S2b6b3a02014-05-27 15:59:30 +05306293 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006294 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306295 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6296 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6297 I915_WRITE(GEN6_RP_UP_EI, 66000);
6298 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6299
6300 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6301
6302 /* 5: Enable RPS */
6303 I915_WRITE(GEN6_RP_CONTROL,
6304 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006305 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306306 GEN6_RP_ENABLE |
6307 GEN6_RP_UP_BUSY_AVG |
6308 GEN6_RP_DOWN_IDLE_AVG);
6309
Deepak S3ef62342015-04-29 08:36:24 +05306310 /* Setting Fixed Bias */
6311 val = VLV_OVERRIDE_EN |
6312 VLV_SOC_TDP_EN |
6313 CHV_BIAS_CPU_50_SOC_50;
6314 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6315
Deepak S2b6b3a02014-05-27 15:59:30 +05306316 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6317
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006318 /* RPS code assumes GPLL is used */
6319 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6320
Jani Nikula742f4912015-09-03 11:16:09 +03006321 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306322 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6323
Chris Wilson3a45b052016-07-13 09:10:32 +01006324 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306325
Mika Kuoppala59bad942015-01-16 11:34:40 +02006326 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306327}
6328
Chris Wilsondc979972016-05-10 14:10:04 +01006329static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006330{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006331 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306332 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006333 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006334
6335 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6336
Imre Deakae484342014-03-31 15:10:44 +03006337 valleyview_check_pctx(dev_priv);
6338
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006339 gtfifodbg = I915_READ(GTFIFODBG);
6340 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006341 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6342 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006343 I915_WRITE(GTFIFODBG, gtfifodbg);
6344 }
6345
Deepak Sc8d9a592013-11-23 14:55:42 +05306346 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006347 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006348
Ville Syrjälä160614a2015-01-19 13:50:47 +02006349 /* Disable RC states. */
6350 I915_WRITE(GEN6_RC_CONTROL, 0);
6351
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006352 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006353 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6354 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6355 I915_WRITE(GEN6_RP_UP_EI, 66000);
6356 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6357
6358 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6359
6360 I915_WRITE(GEN6_RP_CONTROL,
6361 GEN6_RP_MEDIA_TURBO |
6362 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6363 GEN6_RP_MEDIA_IS_GFX |
6364 GEN6_RP_ENABLE |
6365 GEN6_RP_UP_BUSY_AVG |
6366 GEN6_RP_DOWN_IDLE_CONT);
6367
6368 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6369 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6370 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6371
Akash Goel3b3f1652016-10-13 22:44:48 +05306372 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006373 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006374
Jesse Barnes2f0aa302013-11-15 09:32:11 -08006375 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006376
6377 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006378 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04006379 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
6380 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006381 VLV_MEDIA_RC6_COUNT_EN |
6382 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006383
Chris Wilsondc979972016-05-10 14:10:04 +01006384 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006385 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006386
Chris Wilsondc979972016-05-10 14:10:04 +01006387 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006388
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006389 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006390
Deepak S3ef62342015-04-29 08:36:24 +05306391 /* Setting Fixed Bias */
6392 val = VLV_OVERRIDE_EN |
6393 VLV_SOC_TDP_EN |
6394 VLV_BIAS_CPU_125_SOC_875;
6395 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6396
Jani Nikula64936252013-05-22 15:36:20 +03006397 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006398
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006399 /* RPS code assumes GPLL is used */
6400 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6401
Jani Nikula742f4912015-09-03 11:16:09 +03006402 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006403 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6404
Chris Wilson3a45b052016-07-13 09:10:32 +01006405 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006406
Mika Kuoppala59bad942015-01-16 11:34:40 +02006407 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006408}
6409
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006410static unsigned long intel_pxfreq(u32 vidfreq)
6411{
6412 unsigned long freq;
6413 int div = (vidfreq & 0x3f0000) >> 16;
6414 int post = (vidfreq & 0x3000) >> 12;
6415 int pre = (vidfreq & 0x7);
6416
6417 if (!pre)
6418 return 0;
6419
6420 freq = ((div * 133333) / ((1<<post) * pre));
6421
6422 return freq;
6423}
6424
Daniel Vettereb48eb02012-04-26 23:28:12 +02006425static const struct cparams {
6426 u16 i;
6427 u16 t;
6428 u16 m;
6429 u16 c;
6430} cparams[] = {
6431 { 1, 1333, 301, 28664 },
6432 { 1, 1066, 294, 24460 },
6433 { 1, 800, 294, 25192 },
6434 { 0, 1333, 276, 27605 },
6435 { 0, 1066, 276, 27605 },
6436 { 0, 800, 231, 23784 },
6437};
6438
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006439static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006440{
6441 u64 total_count, diff, ret;
6442 u32 count1, count2, count3, m = 0, c = 0;
6443 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6444 int i;
6445
Chris Wilson67520412017-03-02 13:28:01 +00006446 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006447
Daniel Vetter20e4d402012-08-08 23:35:39 +02006448 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006449
6450 /* Prevent division-by-zero if we are asking too fast.
6451 * Also, we don't get interesting results if we are polling
6452 * faster than once in 10ms, so just return the saved value
6453 * in such cases.
6454 */
6455 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006456 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006457
6458 count1 = I915_READ(DMIEC);
6459 count2 = I915_READ(DDREC);
6460 count3 = I915_READ(CSIEC);
6461
6462 total_count = count1 + count2 + count3;
6463
6464 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006465 if (total_count < dev_priv->ips.last_count1) {
6466 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006467 diff += total_count;
6468 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006469 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006470 }
6471
6472 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006473 if (cparams[i].i == dev_priv->ips.c_m &&
6474 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006475 m = cparams[i].m;
6476 c = cparams[i].c;
6477 break;
6478 }
6479 }
6480
6481 diff = div_u64(diff, diff1);
6482 ret = ((m * diff) + c);
6483 ret = div_u64(ret, 10);
6484
Daniel Vetter20e4d402012-08-08 23:35:39 +02006485 dev_priv->ips.last_count1 = total_count;
6486 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006487
Daniel Vetter20e4d402012-08-08 23:35:39 +02006488 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006489
6490 return ret;
6491}
6492
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006493unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6494{
6495 unsigned long val;
6496
Chris Wilsondc979972016-05-10 14:10:04 +01006497 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006498 return 0;
6499
6500 spin_lock_irq(&mchdev_lock);
6501
6502 val = __i915_chipset_val(dev_priv);
6503
6504 spin_unlock_irq(&mchdev_lock);
6505
6506 return val;
6507}
6508
Daniel Vettereb48eb02012-04-26 23:28:12 +02006509unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6510{
6511 unsigned long m, x, b;
6512 u32 tsfs;
6513
6514 tsfs = I915_READ(TSFS);
6515
6516 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6517 x = I915_READ8(TR1);
6518
6519 b = tsfs & TSFS_INTR_MASK;
6520
6521 return ((m * x) / 127) - b;
6522}
6523
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006524static int _pxvid_to_vd(u8 pxvid)
6525{
6526 if (pxvid == 0)
6527 return 0;
6528
6529 if (pxvid >= 8 && pxvid < 31)
6530 pxvid = 31;
6531
6532 return (pxvid + 2) * 125;
6533}
6534
6535static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006536{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006537 const int vd = _pxvid_to_vd(pxvid);
6538 const int vm = vd - 1125;
6539
Chris Wilsondc979972016-05-10 14:10:04 +01006540 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006541 return vm > 0 ? vm : 0;
6542
6543 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006544}
6545
Daniel Vetter02d71952012-08-09 16:44:54 +02006546static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006547{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006548 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006549 u32 count;
6550
Chris Wilson67520412017-03-02 13:28:01 +00006551 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006552
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006553 now = ktime_get_raw_ns();
6554 diffms = now - dev_priv->ips.last_time2;
6555 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006556
6557 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006558 if (!diffms)
6559 return;
6560
6561 count = I915_READ(GFXEC);
6562
Daniel Vetter20e4d402012-08-08 23:35:39 +02006563 if (count < dev_priv->ips.last_count2) {
6564 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006565 diff += count;
6566 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006567 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006568 }
6569
Daniel Vetter20e4d402012-08-08 23:35:39 +02006570 dev_priv->ips.last_count2 = count;
6571 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006572
6573 /* More magic constants... */
6574 diff = diff * 1181;
6575 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006576 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006577}
6578
Daniel Vetter02d71952012-08-09 16:44:54 +02006579void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6580{
Chris Wilsondc979972016-05-10 14:10:04 +01006581 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006582 return;
6583
Daniel Vetter92703882012-08-09 16:46:01 +02006584 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006585
6586 __i915_update_gfx_val(dev_priv);
6587
Daniel Vetter92703882012-08-09 16:46:01 +02006588 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006589}
6590
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006591static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006592{
6593 unsigned long t, corr, state1, corr2, state2;
6594 u32 pxvid, ext_v;
6595
Chris Wilson67520412017-03-02 13:28:01 +00006596 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006597
Ville Syrjälä616847e2015-09-18 20:03:19 +03006598 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006599 pxvid = (pxvid >> 24) & 0x7f;
6600 ext_v = pvid_to_extvid(dev_priv, pxvid);
6601
6602 state1 = ext_v;
6603
6604 t = i915_mch_val(dev_priv);
6605
6606 /* Revel in the empirically derived constants */
6607
6608 /* Correction factor in 1/100000 units */
6609 if (t > 80)
6610 corr = ((t * 2349) + 135940);
6611 else if (t >= 50)
6612 corr = ((t * 964) + 29317);
6613 else /* < 50 */
6614 corr = ((t * 301) + 1004);
6615
6616 corr = corr * ((150142 * state1) / 10000 - 78642);
6617 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006618 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006619
6620 state2 = (corr2 * state1) / 10000;
6621 state2 /= 100; /* convert to mW */
6622
Daniel Vetter02d71952012-08-09 16:44:54 +02006623 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006624
Daniel Vetter20e4d402012-08-08 23:35:39 +02006625 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006626}
6627
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006628unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6629{
6630 unsigned long val;
6631
Chris Wilsondc979972016-05-10 14:10:04 +01006632 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006633 return 0;
6634
6635 spin_lock_irq(&mchdev_lock);
6636
6637 val = __i915_gfx_val(dev_priv);
6638
6639 spin_unlock_irq(&mchdev_lock);
6640
6641 return val;
6642}
6643
Daniel Vettereb48eb02012-04-26 23:28:12 +02006644/**
6645 * i915_read_mch_val - return value for IPS use
6646 *
6647 * Calculate and return a value for the IPS driver to use when deciding whether
6648 * we have thermal and power headroom to increase CPU or GPU power budget.
6649 */
6650unsigned long i915_read_mch_val(void)
6651{
6652 struct drm_i915_private *dev_priv;
6653 unsigned long chipset_val, graphics_val, ret = 0;
6654
Daniel Vetter92703882012-08-09 16:46:01 +02006655 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006656 if (!i915_mch_dev)
6657 goto out_unlock;
6658 dev_priv = i915_mch_dev;
6659
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006660 chipset_val = __i915_chipset_val(dev_priv);
6661 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006662
6663 ret = chipset_val + graphics_val;
6664
6665out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006666 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006667
6668 return ret;
6669}
6670EXPORT_SYMBOL_GPL(i915_read_mch_val);
6671
6672/**
6673 * i915_gpu_raise - raise GPU frequency limit
6674 *
6675 * Raise the limit; IPS indicates we have thermal headroom.
6676 */
6677bool i915_gpu_raise(void)
6678{
6679 struct drm_i915_private *dev_priv;
6680 bool ret = true;
6681
Daniel Vetter92703882012-08-09 16:46:01 +02006682 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006683 if (!i915_mch_dev) {
6684 ret = false;
6685 goto out_unlock;
6686 }
6687 dev_priv = i915_mch_dev;
6688
Daniel Vetter20e4d402012-08-08 23:35:39 +02006689 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6690 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006691
6692out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006693 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006694
6695 return ret;
6696}
6697EXPORT_SYMBOL_GPL(i915_gpu_raise);
6698
6699/**
6700 * i915_gpu_lower - lower GPU frequency limit
6701 *
6702 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6703 * frequency maximum.
6704 */
6705bool i915_gpu_lower(void)
6706{
6707 struct drm_i915_private *dev_priv;
6708 bool ret = true;
6709
Daniel Vetter92703882012-08-09 16:46:01 +02006710 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006711 if (!i915_mch_dev) {
6712 ret = false;
6713 goto out_unlock;
6714 }
6715 dev_priv = i915_mch_dev;
6716
Daniel Vetter20e4d402012-08-08 23:35:39 +02006717 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6718 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006719
6720out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006721 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006722
6723 return ret;
6724}
6725EXPORT_SYMBOL_GPL(i915_gpu_lower);
6726
6727/**
6728 * i915_gpu_busy - indicate GPU business to IPS
6729 *
6730 * Tell the IPS driver whether or not the GPU is busy.
6731 */
6732bool i915_gpu_busy(void)
6733{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006734 bool ret = false;
6735
Daniel Vetter92703882012-08-09 16:46:01 +02006736 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006737 if (i915_mch_dev)
6738 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006739 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006740
6741 return ret;
6742}
6743EXPORT_SYMBOL_GPL(i915_gpu_busy);
6744
6745/**
6746 * i915_gpu_turbo_disable - disable graphics turbo
6747 *
6748 * Disable graphics turbo by resetting the max frequency and setting the
6749 * current frequency to the default.
6750 */
6751bool i915_gpu_turbo_disable(void)
6752{
6753 struct drm_i915_private *dev_priv;
6754 bool ret = true;
6755
Daniel Vetter92703882012-08-09 16:46:01 +02006756 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006757 if (!i915_mch_dev) {
6758 ret = false;
6759 goto out_unlock;
6760 }
6761 dev_priv = i915_mch_dev;
6762
Daniel Vetter20e4d402012-08-08 23:35:39 +02006763 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006764
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006765 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006766 ret = false;
6767
6768out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006769 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006770
6771 return ret;
6772}
6773EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6774
6775/**
6776 * Tells the intel_ips driver that the i915 driver is now loaded, if
6777 * IPS got loaded first.
6778 *
6779 * This awkward dance is so that neither module has to depend on the
6780 * other in order for IPS to do the appropriate communication of
6781 * GPU turbo limits to i915.
6782 */
6783static void
6784ips_ping_for_i915_load(void)
6785{
6786 void (*link)(void);
6787
6788 link = symbol_get(ips_link_to_i915_driver);
6789 if (link) {
6790 link();
6791 symbol_put(ips_link_to_i915_driver);
6792 }
6793}
6794
6795void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6796{
Daniel Vetter02d71952012-08-09 16:44:54 +02006797 /* We only register the i915 ips part with intel-ips once everything is
6798 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006799 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006800 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006801 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006802
6803 ips_ping_for_i915_load();
6804}
6805
6806void intel_gpu_ips_teardown(void)
6807{
Daniel Vetter92703882012-08-09 16:46:01 +02006808 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006809 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006810 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006811}
Deepak S76c3552f2014-01-30 23:08:16 +05306812
Chris Wilsondc979972016-05-10 14:10:04 +01006813static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006814{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006815 u32 lcfuse;
6816 u8 pxw[16];
6817 int i;
6818
6819 /* Disable to program */
6820 I915_WRITE(ECR, 0);
6821 POSTING_READ(ECR);
6822
6823 /* Program energy weights for various events */
6824 I915_WRITE(SDEW, 0x15040d00);
6825 I915_WRITE(CSIEW0, 0x007f0000);
6826 I915_WRITE(CSIEW1, 0x1e220004);
6827 I915_WRITE(CSIEW2, 0x04000004);
6828
6829 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006830 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006831 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006832 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006833
6834 /* Program P-state weights to account for frequency power adjustment */
6835 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006836 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006837 unsigned long freq = intel_pxfreq(pxvidfreq);
6838 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6839 PXVFREQ_PX_SHIFT;
6840 unsigned long val;
6841
6842 val = vid * vid;
6843 val *= (freq / 1000);
6844 val *= 255;
6845 val /= (127*127*900);
6846 if (val > 0xff)
6847 DRM_ERROR("bad pxval: %ld\n", val);
6848 pxw[i] = val;
6849 }
6850 /* Render standby states get 0 weight */
6851 pxw[14] = 0;
6852 pxw[15] = 0;
6853
6854 for (i = 0; i < 4; i++) {
6855 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6856 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006857 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006858 }
6859
6860 /* Adjust magic regs to magic values (more experimental results) */
6861 I915_WRITE(OGW0, 0);
6862 I915_WRITE(OGW1, 0);
6863 I915_WRITE(EG0, 0x00007f00);
6864 I915_WRITE(EG1, 0x0000000e);
6865 I915_WRITE(EG2, 0x000e0000);
6866 I915_WRITE(EG3, 0x68000300);
6867 I915_WRITE(EG4, 0x42000000);
6868 I915_WRITE(EG5, 0x00140031);
6869 I915_WRITE(EG6, 0);
6870 I915_WRITE(EG7, 0);
6871
6872 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006873 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006874
6875 /* Enable PMON + select events */
6876 I915_WRITE(ECR, 0x80000019);
6877
6878 lcfuse = I915_READ(LCFUSE02);
6879
Daniel Vetter20e4d402012-08-08 23:35:39 +02006880 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006881}
6882
Chris Wilsondc979972016-05-10 14:10:04 +01006883void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006884{
Imre Deakb268c692015-12-15 20:10:31 +02006885 /*
6886 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6887 * requirement.
6888 */
6889 if (!i915.enable_rc6) {
6890 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6891 intel_runtime_pm_get(dev_priv);
6892 }
Imre Deake6069ca2014-04-18 16:01:02 +03006893
Chris Wilsonb5163db2016-08-10 13:58:24 +01006894 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006895 mutex_lock(&dev_priv->rps.hw_lock);
6896
6897 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006898 if (IS_CHERRYVIEW(dev_priv))
6899 cherryview_init_gt_powersave(dev_priv);
6900 else if (IS_VALLEYVIEW(dev_priv))
6901 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006902 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006903 gen6_init_rps_frequencies(dev_priv);
6904
6905 /* Derive initial user preferences/limits from the hardware limits */
6906 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6907 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6908
6909 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6910 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6911
6912 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6913 dev_priv->rps.min_freq_softlimit =
6914 max_t(int,
6915 dev_priv->rps.efficient_freq,
6916 intel_freq_opcode(dev_priv, 450));
6917
Chris Wilson99ac9612016-07-13 09:10:34 +01006918 /* After setting max-softlimit, find the overclock max freq */
6919 if (IS_GEN6(dev_priv) ||
6920 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
6921 u32 params = 0;
6922
6923 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
6924 if (params & BIT(31)) { /* OC supported */
6925 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
6926 (dev_priv->rps.max_freq & 0xff) * 50,
6927 (params & 0xff) * 50);
6928 dev_priv->rps.max_freq = params & 0xff;
6929 }
6930 }
6931
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006932 /* Finally allow us to boost to max by default */
6933 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
6934
Chris Wilson773ea9a2016-07-13 09:10:33 +01006935 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01006936 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01006937
6938 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006939}
6940
Chris Wilsondc979972016-05-10 14:10:04 +01006941void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006942{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03006943 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01006944 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006945
6946 if (!i915.enable_rc6)
6947 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006948}
6949
Chris Wilson54b4f682016-07-21 21:16:19 +01006950/**
6951 * intel_suspend_gt_powersave - suspend PM work and helper threads
6952 * @dev_priv: i915 device
6953 *
6954 * We don't want to disable RC6 or other features here, we just want
6955 * to make sure any work we've queued has finished and won't bother
6956 * us while we're suspended.
6957 */
6958void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
6959{
6960 if (INTEL_GEN(dev_priv) < 6)
6961 return;
6962
6963 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
6964 intel_runtime_pm_put(dev_priv);
6965
6966 /* gen6_rps_idle() will be called later to disable interrupts */
6967}
6968
Chris Wilsonb7137e02016-07-13 09:10:37 +01006969void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
6970{
6971 dev_priv->rps.enabled = true; /* force disabling */
6972 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006973
6974 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006975}
6976
Chris Wilsondc979972016-05-10 14:10:04 +01006977void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006978{
Chris Wilsonb7137e02016-07-13 09:10:37 +01006979 if (!READ_ONCE(dev_priv->rps.enabled))
6980 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006981
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006982 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006983
Chris Wilsonb7137e02016-07-13 09:10:37 +01006984 if (INTEL_GEN(dev_priv) >= 9) {
6985 gen9_disable_rc6(dev_priv);
6986 gen9_disable_rps(dev_priv);
6987 } else if (IS_CHERRYVIEW(dev_priv)) {
6988 cherryview_disable_rps(dev_priv);
6989 } else if (IS_VALLEYVIEW(dev_priv)) {
6990 valleyview_disable_rps(dev_priv);
6991 } else if (INTEL_GEN(dev_priv) >= 6) {
6992 gen6_disable_rps(dev_priv);
6993 } else if (IS_IRONLAKE_M(dev_priv)) {
6994 ironlake_disable_drps(dev_priv);
6995 }
6996
6997 dev_priv->rps.enabled = false;
6998 mutex_unlock(&dev_priv->rps.hw_lock);
6999}
7000
7001void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7002{
Chris Wilson54b4f682016-07-21 21:16:19 +01007003 /* We shouldn't be disabling as we submit, so this should be less
7004 * racy than it appears!
7005 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007006 if (READ_ONCE(dev_priv->rps.enabled))
7007 return;
7008
7009 /* Powersaving is controlled by the host when inside a VM */
7010 if (intel_vgpu_active(dev_priv))
7011 return;
7012
7013 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007014
Chris Wilsondc979972016-05-10 14:10:04 +01007015 if (IS_CHERRYVIEW(dev_priv)) {
7016 cherryview_enable_rps(dev_priv);
7017 } else if (IS_VALLEYVIEW(dev_priv)) {
7018 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007019 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007020 gen9_enable_rc6(dev_priv);
7021 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007022 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007023 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007024 } else if (IS_BROADWELL(dev_priv)) {
7025 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007026 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007027 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007028 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007029 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007030 } else if (IS_IRONLAKE_M(dev_priv)) {
7031 ironlake_enable_drps(dev_priv);
7032 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007033 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007034
7035 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7036 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7037
7038 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7039 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7040
Chris Wilson54b4f682016-07-21 21:16:19 +01007041 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007042 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007043}
Imre Deakc6df39b2014-04-14 20:24:29 +03007044
Chris Wilson54b4f682016-07-21 21:16:19 +01007045static void __intel_autoenable_gt_powersave(struct work_struct *work)
7046{
7047 struct drm_i915_private *dev_priv =
7048 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7049 struct intel_engine_cs *rcs;
7050 struct drm_i915_gem_request *req;
7051
7052 if (READ_ONCE(dev_priv->rps.enabled))
7053 goto out;
7054
Akash Goel3b3f1652016-10-13 22:44:48 +05307055 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007056 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007057 goto out;
7058
7059 if (!rcs->init_context)
7060 goto out;
7061
7062 mutex_lock(&dev_priv->drm.struct_mutex);
7063
7064 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7065 if (IS_ERR(req))
7066 goto unlock;
7067
7068 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7069 rcs->init_context(req);
7070
7071 /* Mark the device busy, calling intel_enable_gt_powersave() */
7072 i915_add_request_no_flush(req);
7073
7074unlock:
7075 mutex_unlock(&dev_priv->drm.struct_mutex);
7076out:
7077 intel_runtime_pm_put(dev_priv);
7078}
7079
7080void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7081{
7082 if (READ_ONCE(dev_priv->rps.enabled))
7083 return;
7084
7085 if (IS_IRONLAKE_M(dev_priv)) {
7086 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007087 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007088 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7089 /*
7090 * PCU communication is slow and this doesn't need to be
7091 * done at any specific time, so do this out of our fast path
7092 * to make resume and init faster.
7093 *
7094 * We depend on the HW RC6 power context save/restore
7095 * mechanism when entering D3 through runtime PM suspend. So
7096 * disable RPM until RPS/RC6 is properly setup. We can only
7097 * get here via the driver load/system resume/runtime resume
7098 * paths, so the _noresume version is enough (and in case of
7099 * runtime resume it's necessary).
7100 */
7101 if (queue_delayed_work(dev_priv->wq,
7102 &dev_priv->rps.autoenable_work,
7103 round_jiffies_up_relative(HZ)))
7104 intel_runtime_pm_get_noresume(dev_priv);
7105 }
7106}
7107
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007108static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007109{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007110 /*
7111 * On Ibex Peak and Cougar Point, we need to disable clock
7112 * gating for the panel power sequencer or it will fail to
7113 * start up when no ports are active.
7114 */
7115 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7116}
7117
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007118static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007119{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007120 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007121
Damien Lespiau055e3932014-08-18 13:49:10 +01007122 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007123 I915_WRITE(DSPCNTR(pipe),
7124 I915_READ(DSPCNTR(pipe)) |
7125 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007126
7127 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7128 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007129 }
7130}
7131
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007132static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007133{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007134 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7135 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7136 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7137
7138 /*
7139 * Don't touch WM1S_LP_EN here.
7140 * Doing so could cause underruns.
7141 */
7142}
7143
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007144static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007145{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007146 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007147
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007148 /*
7149 * Required for FBC
7150 * WaFbcDisableDpfcClockGating:ilk
7151 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007152 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7153 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7154 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007155
7156 I915_WRITE(PCH_3DCGDIS0,
7157 MARIUNIT_CLOCK_GATE_DISABLE |
7158 SVSMUNIT_CLOCK_GATE_DISABLE);
7159 I915_WRITE(PCH_3DCGDIS1,
7160 VFMUNIT_CLOCK_GATE_DISABLE);
7161
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007162 /*
7163 * According to the spec the following bits should be set in
7164 * order to enable memory self-refresh
7165 * The bit 22/21 of 0x42004
7166 * The bit 5 of 0x42020
7167 * The bit 15 of 0x45000
7168 */
7169 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7170 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7171 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007172 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007173 I915_WRITE(DISP_ARB_CTL,
7174 (I915_READ(DISP_ARB_CTL) |
7175 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007176
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007177 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007178
7179 /*
7180 * Based on the document from hardware guys the following bits
7181 * should be set unconditionally in order to enable FBC.
7182 * The bit 22 of 0x42000
7183 * The bit 22 of 0x42004
7184 * The bit 7,8,9 of 0x42020.
7185 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007186 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007187 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007188 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7189 I915_READ(ILK_DISPLAY_CHICKEN1) |
7190 ILK_FBCQ_DIS);
7191 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7192 I915_READ(ILK_DISPLAY_CHICKEN2) |
7193 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007194 }
7195
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007196 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
7197
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007198 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7199 I915_READ(ILK_DISPLAY_CHICKEN2) |
7200 ILK_ELPIN_409_SELECT);
7201 I915_WRITE(_3D_CHICKEN2,
7202 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
7203 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02007204
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007205 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02007206 I915_WRITE(CACHE_MODE_0,
7207 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01007208
Akash Goel4e046322014-04-04 17:14:38 +05307209 /* WaDisable_RenderCache_OperationalFlush:ilk */
7210 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7211
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007212 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007213
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007214 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007215}
7216
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007217static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007218{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007219 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007220 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007221
7222 /*
7223 * On Ibex Peak and Cougar Point, we need to disable clock
7224 * gating for the panel power sequencer or it will fail to
7225 * start up when no ports are active.
7226 */
Jesse Barnescd664072013-10-02 10:34:19 -07007227 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7228 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7229 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007230 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7231 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007232 /* The below fixes the weird display corruption, a few pixels shifted
7233 * downward, on (only) LVDS of some HP laptops with IVY.
7234 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007235 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007236 val = I915_READ(TRANS_CHICKEN2(pipe));
7237 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7238 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007239 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007240 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007241 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7242 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7243 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007244 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7245 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007246 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007247 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007248 I915_WRITE(TRANS_CHICKEN1(pipe),
7249 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7250 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007251}
7252
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007253static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007254{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007255 uint32_t tmp;
7256
7257 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007258 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7259 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7260 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007261}
7262
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007263static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007264{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007265 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007266
Damien Lespiau231e54f2012-10-19 17:55:41 +01007267 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007268
7269 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7270 I915_READ(ILK_DISPLAY_CHICKEN2) |
7271 ILK_ELPIN_409_SELECT);
7272
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007273 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007274 I915_WRITE(_3D_CHICKEN,
7275 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7276
Akash Goel4e046322014-04-04 17:14:38 +05307277 /* WaDisable_RenderCache_OperationalFlush:snb */
7278 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7279
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007280 /*
7281 * BSpec recoomends 8x4 when MSAA is used,
7282 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007283 *
7284 * Note that PS/WM thread counts depend on the WIZ hashing
7285 * disable bit, which we don't touch here, but it's good
7286 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007287 */
7288 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007289 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007290
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007291 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007292
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007293 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007294 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007295
7296 I915_WRITE(GEN6_UCGCTL1,
7297 I915_READ(GEN6_UCGCTL1) |
7298 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7299 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7300
7301 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7302 * gating disable must be set. Failure to set it results in
7303 * flickering pixels due to Z write ordering failures after
7304 * some amount of runtime in the Mesa "fire" demo, and Unigine
7305 * Sanctuary and Tropics, and apparently anything else with
7306 * alpha test or pixel discard.
7307 *
7308 * According to the spec, bit 11 (RCCUNIT) must also be set,
7309 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007310 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007311 * WaDisableRCCUnitClockGating:snb
7312 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007313 */
7314 I915_WRITE(GEN6_UCGCTL2,
7315 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7316 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7317
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007318 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007319 I915_WRITE(_3D_CHICKEN3,
7320 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007321
7322 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007323 * Bspec says:
7324 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7325 * 3DSTATE_SF number of SF output attributes is more than 16."
7326 */
7327 I915_WRITE(_3D_CHICKEN3,
7328 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7329
7330 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007331 * According to the spec the following bits should be
7332 * set in order to enable memory self-refresh and fbc:
7333 * The bit21 and bit22 of 0x42000
7334 * The bit21 and bit22 of 0x42004
7335 * The bit5 and bit7 of 0x42020
7336 * The bit14 of 0x70180
7337 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007338 *
7339 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007340 */
7341 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7342 I915_READ(ILK_DISPLAY_CHICKEN1) |
7343 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7344 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7345 I915_READ(ILK_DISPLAY_CHICKEN2) |
7346 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007347 I915_WRITE(ILK_DSPCLK_GATE_D,
7348 I915_READ(ILK_DSPCLK_GATE_D) |
7349 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7350 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007351
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007352 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007353
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007354 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007355
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007356 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007357}
7358
7359static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7360{
7361 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7362
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007363 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007364 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007365 *
7366 * This actually overrides the dispatch
7367 * mode for all thread types.
7368 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007369 reg &= ~GEN7_FF_SCHED_MASK;
7370 reg |= GEN7_FF_TS_SCHED_HW;
7371 reg |= GEN7_FF_VS_SCHED_HW;
7372 reg |= GEN7_FF_DS_SCHED_HW;
7373
7374 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7375}
7376
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007377static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007378{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007379 /*
7380 * TODO: this bit should only be enabled when really needed, then
7381 * disabled when not needed anymore in order to save power.
7382 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007383 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007384 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7385 I915_READ(SOUTH_DSPCLK_GATE_D) |
7386 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007387
7388 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007389 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7390 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007391 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007392}
7393
Ville Syrjälä712bf362016-10-31 22:37:23 +02007394static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007395{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007396 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007397 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7398
7399 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7400 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7401 }
7402}
7403
Imre Deak450174f2016-05-03 15:54:21 +03007404static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7405 int general_prio_credits,
7406 int high_prio_credits)
7407{
7408 u32 misccpctl;
7409
7410 /* WaTempDisableDOPClkGating:bdw */
7411 misccpctl = I915_READ(GEN7_MISCCPCTL);
7412 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7413
7414 I915_WRITE(GEN8_L3SQCREG1,
7415 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7416 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7417
7418 /*
7419 * Wait at least 100 clocks before re-enabling clock gating.
7420 * See the definition of L3SQCREG1 in BSpec.
7421 */
7422 POSTING_READ(GEN8_L3SQCREG1);
7423 udelay(1);
7424 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7425}
7426
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007427static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007428{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007429 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007430
7431 /* WaDisableSDEUnitClockGating:kbl */
7432 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7433 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7434 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007435
7436 /* WaDisableGamClockGating:kbl */
7437 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7438 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7439 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007440
7441 /* WaFbcNukeOnHostModify:kbl */
7442 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7443 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007444}
7445
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007446static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007447{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007448 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007449
7450 /* WAC6entrylatency:skl */
7451 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7452 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007453
7454 /* WaFbcNukeOnHostModify:skl */
7455 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7456 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007457}
7458
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007459static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007460{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007461 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007462
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007463 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007464
Ben Widawskyab57fff2013-12-12 15:28:04 -08007465 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007466 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007467
Ben Widawskyab57fff2013-12-12 15:28:04 -08007468 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007469 I915_WRITE(CHICKEN_PAR1_1,
7470 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7471
Ben Widawskyab57fff2013-12-12 15:28:04 -08007472 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007473 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007474 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007475 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007476 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007477 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007478
Ben Widawskyab57fff2013-12-12 15:28:04 -08007479 /* WaVSRefCountFullforceMissDisable:bdw */
7480 /* WaDSRefCountFullforceMissDisable:bdw */
7481 I915_WRITE(GEN7_FF_THREAD_MODE,
7482 I915_READ(GEN7_FF_THREAD_MODE) &
7483 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007484
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007485 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7486 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007487
7488 /* WaDisableSDEUnitClockGating:bdw */
7489 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7490 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007491
Imre Deak450174f2016-05-03 15:54:21 +03007492 /* WaProgramL3SqcReg1Default:bdw */
7493 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007494
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007495 /*
7496 * WaGttCachingOffByDefault:bdw
7497 * GTT cache may not work with big pages, so if those
7498 * are ever enabled GTT cache may need to be disabled.
7499 */
7500 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7501
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007502 /* WaKVMNotificationOnConfigChange:bdw */
7503 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7504 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7505
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007506 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007507
7508 /* WaDisableDopClockGating:bdw
7509 *
7510 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7511 * clock gating.
7512 */
7513 I915_WRITE(GEN6_UCGCTL1,
7514 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007515}
7516
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007517static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007518{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007519 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007520
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007521 /* L3 caching of data atomics doesn't work -- disable it. */
7522 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7523 I915_WRITE(HSW_ROW_CHICKEN3,
7524 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7525
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007526 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007527 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7528 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7529 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7530
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007531 /* WaVSRefCountFullforceMissDisable:hsw */
7532 I915_WRITE(GEN7_FF_THREAD_MODE,
7533 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007534
Akash Goel4e046322014-04-04 17:14:38 +05307535 /* WaDisable_RenderCache_OperationalFlush:hsw */
7536 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7537
Chia-I Wufe27c602014-01-28 13:29:33 +08007538 /* enable HiZ Raw Stall Optimization */
7539 I915_WRITE(CACHE_MODE_0_GEN7,
7540 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7541
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007542 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007543 I915_WRITE(CACHE_MODE_1,
7544 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007545
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007546 /*
7547 * BSpec recommends 8x4 when MSAA is used,
7548 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007549 *
7550 * Note that PS/WM thread counts depend on the WIZ hashing
7551 * disable bit, which we don't touch here, but it's good
7552 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007553 */
7554 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007555 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007556
Kenneth Graunke94411592014-12-31 16:23:00 -08007557 /* WaSampleCChickenBitEnable:hsw */
7558 I915_WRITE(HALF_SLICE_CHICKEN3,
7559 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7560
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007561 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007562 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7563
Paulo Zanoni90a88642013-05-03 17:23:45 -03007564 /* WaRsPkgCStateDisplayPMReq:hsw */
7565 I915_WRITE(CHICKEN_PAR1_1,
7566 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007567
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007568 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007569}
7570
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007571static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007572{
Ben Widawsky20848222012-05-04 18:58:59 -07007573 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007574
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007575 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007576
Damien Lespiau231e54f2012-10-19 17:55:41 +01007577 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007578
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007579 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007580 I915_WRITE(_3D_CHICKEN3,
7581 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7582
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007583 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007584 I915_WRITE(IVB_CHICKEN3,
7585 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7586 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7587
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007588 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007589 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007590 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7591 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007592
Akash Goel4e046322014-04-04 17:14:38 +05307593 /* WaDisable_RenderCache_OperationalFlush:ivb */
7594 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7595
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007596 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007597 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7598 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7599
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007600 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007601 I915_WRITE(GEN7_L3CNTLREG1,
7602 GEN7_WA_FOR_GEN7_L3_CONTROL);
7603 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007604 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007605 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007606 I915_WRITE(GEN7_ROW_CHICKEN2,
7607 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007608 else {
7609 /* must write both registers */
7610 I915_WRITE(GEN7_ROW_CHICKEN2,
7611 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007612 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7613 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007614 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007615
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007616 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007617 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7618 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7619
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007620 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007621 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007622 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007623 */
7624 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007625 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007626
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007627 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007628 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7629 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7630 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7631
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007632 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007633
7634 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007635
Chris Wilson22721342014-03-04 09:41:43 +00007636 if (0) { /* causes HiZ corruption on ivb:gt1 */
7637 /* enable HiZ Raw Stall Optimization */
7638 I915_WRITE(CACHE_MODE_0_GEN7,
7639 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7640 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007641
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007642 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007643 I915_WRITE(CACHE_MODE_1,
7644 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007645
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007646 /*
7647 * BSpec recommends 8x4 when MSAA is used,
7648 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007649 *
7650 * Note that PS/WM thread counts depend on the WIZ hashing
7651 * disable bit, which we don't touch here, but it's good
7652 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007653 */
7654 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007655 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007656
Ben Widawsky20848222012-05-04 18:58:59 -07007657 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7658 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7659 snpcr |= GEN6_MBC_SNPCR_MED;
7660 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007661
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007662 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007663 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007664
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007665 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007666}
7667
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007668static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007669{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007670 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007671 I915_WRITE(_3D_CHICKEN3,
7672 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7673
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007674 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007675 I915_WRITE(IVB_CHICKEN3,
7676 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7677 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7678
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007679 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007680 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007681 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007682 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7683 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007684
Akash Goel4e046322014-04-04 17:14:38 +05307685 /* WaDisable_RenderCache_OperationalFlush:vlv */
7686 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7687
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007688 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007689 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7690 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7691
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007692 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007693 I915_WRITE(GEN7_ROW_CHICKEN2,
7694 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7695
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007696 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007697 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7698 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7699 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7700
Ville Syrjälä46680e02014-01-22 21:33:01 +02007701 gen7_setup_fixed_func_scheduler(dev_priv);
7702
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007703 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007704 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007705 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007706 */
7707 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007708 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007709
Akash Goelc98f5062014-03-24 23:00:07 +05307710 /* WaDisableL3Bank2xClockGate:vlv
7711 * Disabling L3 clock gating- MMIO 940c[25] = 1
7712 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7713 I915_WRITE(GEN7_UCGCTL4,
7714 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007715
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007716 /*
7717 * BSpec says this must be set, even though
7718 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7719 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007720 I915_WRITE(CACHE_MODE_1,
7721 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007722
7723 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007724 * BSpec recommends 8x4 when MSAA is used,
7725 * however in practice 16x4 seems fastest.
7726 *
7727 * Note that PS/WM thread counts depend on the WIZ hashing
7728 * disable bit, which we don't touch here, but it's good
7729 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7730 */
7731 I915_WRITE(GEN7_GT_MODE,
7732 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7733
7734 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007735 * WaIncreaseL3CreditsForVLVB0:vlv
7736 * This is the hardware default actually.
7737 */
7738 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7739
7740 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007741 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007742 * Disable clock gating on th GCFG unit to prevent a delay
7743 * in the reporting of vblank events.
7744 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007745 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007746}
7747
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007748static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007749{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007750 /* WaVSRefCountFullforceMissDisable:chv */
7751 /* WaDSRefCountFullforceMissDisable:chv */
7752 I915_WRITE(GEN7_FF_THREAD_MODE,
7753 I915_READ(GEN7_FF_THREAD_MODE) &
7754 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007755
7756 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7757 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7758 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007759
7760 /* WaDisableCSUnitClockGating:chv */
7761 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7762 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007763
7764 /* WaDisableSDEUnitClockGating:chv */
7765 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7766 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007767
7768 /*
Imre Deak450174f2016-05-03 15:54:21 +03007769 * WaProgramL3SqcReg1Default:chv
7770 * See gfxspecs/Related Documents/Performance Guide/
7771 * LSQC Setting Recommendations.
7772 */
7773 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7774
7775 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007776 * GTT cache may not work with big pages, so if those
7777 * are ever enabled GTT cache may need to be disabled.
7778 */
7779 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007780}
7781
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007782static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007783{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007784 uint32_t dspclk_gate;
7785
7786 I915_WRITE(RENCLK_GATE_D1, 0);
7787 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7788 GS_UNIT_CLOCK_GATE_DISABLE |
7789 CL_UNIT_CLOCK_GATE_DISABLE);
7790 I915_WRITE(RAMCLK_GATE_D, 0);
7791 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7792 OVRUNIT_CLOCK_GATE_DISABLE |
7793 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007794 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007795 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7796 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007797
7798 /* WaDisableRenderCachePipelinedFlush */
7799 I915_WRITE(CACHE_MODE_0,
7800 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007801
Akash Goel4e046322014-04-04 17:14:38 +05307802 /* WaDisable_RenderCache_OperationalFlush:g4x */
7803 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7804
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007805 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007806}
7807
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007808static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007809{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007810 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7811 I915_WRITE(RENCLK_GATE_D2, 0);
7812 I915_WRITE(DSPCLK_GATE_D, 0);
7813 I915_WRITE(RAMCLK_GATE_D, 0);
7814 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007815 I915_WRITE(MI_ARB_STATE,
7816 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307817
7818 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7819 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007820}
7821
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007822static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007823{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007824 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7825 I965_RCC_CLOCK_GATE_DISABLE |
7826 I965_RCPB_CLOCK_GATE_DISABLE |
7827 I965_ISC_CLOCK_GATE_DISABLE |
7828 I965_FBC_CLOCK_GATE_DISABLE);
7829 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007830 I915_WRITE(MI_ARB_STATE,
7831 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307832
7833 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7834 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007835}
7836
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007837static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007838{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007839 u32 dstate = I915_READ(D_STATE);
7840
7841 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7842 DSTATE_DOT_CLOCK_GATING;
7843 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007844
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007845 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007846 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007847
7848 /* IIR "flip pending" means done if this bit is set */
7849 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007850
7851 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007852 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007853
7854 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7855 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007856
7857 I915_WRITE(MI_ARB_STATE,
7858 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007859}
7860
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007861static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007862{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007863 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007864
7865 /* interrupts should cause a wake up from C3 */
7866 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7867 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007868
7869 I915_WRITE(MEM_MODE,
7870 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007871}
7872
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007873static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007874{
Ville Syrjälä10383922014-08-15 01:21:54 +03007875 I915_WRITE(MEM_MODE,
7876 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7877 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007878}
7879
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007880void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007881{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007882 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007883}
7884
Ville Syrjälä712bf362016-10-31 22:37:23 +02007885void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007886{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007887 if (HAS_PCH_LPT(dev_priv))
7888 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007889}
7890
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007891static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007892{
7893 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7894}
7895
7896/**
7897 * intel_init_clock_gating_hooks - setup the clock gating hooks
7898 * @dev_priv: device private
7899 *
7900 * Setup the hooks that configure which clocks of a given platform can be
7901 * gated and also apply various GT and display specific workarounds for these
7902 * platforms. Note that some GT specific workarounds are applied separately
7903 * when GPU contexts or batchbuffers start their execution.
7904 */
7905void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7906{
7907 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007908 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007909 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007910 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007911 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007912 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007913 else if (IS_GEMINILAKE(dev_priv))
7914 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007915 else if (IS_BROADWELL(dev_priv))
7916 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7917 else if (IS_CHERRYVIEW(dev_priv))
7918 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7919 else if (IS_HASWELL(dev_priv))
7920 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7921 else if (IS_IVYBRIDGE(dev_priv))
7922 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7923 else if (IS_VALLEYVIEW(dev_priv))
7924 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7925 else if (IS_GEN6(dev_priv))
7926 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7927 else if (IS_GEN5(dev_priv))
7928 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7929 else if (IS_G4X(dev_priv))
7930 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007931 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007932 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007933 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007934 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7935 else if (IS_GEN3(dev_priv))
7936 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7937 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7938 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7939 else if (IS_GEN2(dev_priv))
7940 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7941 else {
7942 MISSING_CASE(INTEL_DEVID(dev_priv));
7943 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7944 }
7945}
7946
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007947/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007948void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007949{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007950 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007951
Daniel Vetterc921aba2012-04-26 23:28:17 +02007952 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007953 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007954 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007955 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007956 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007957
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007958 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007959 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007960 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01007961 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01007962 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007963 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007964 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007965 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007966
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007967 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007968 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007969 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007970 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007971 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007972 dev_priv->display.compute_intermediate_wm =
7973 ilk_compute_intermediate_wm;
7974 dev_priv->display.initial_watermarks =
7975 ilk_initial_watermarks;
7976 dev_priv->display.optimize_watermarks =
7977 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007978 } else {
7979 DRM_DEBUG_KMS("Failed to read display plane latency. "
7980 "Disable CxSR\n");
7981 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007982 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007983 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007984 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007985 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007986 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007987 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007988 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007989 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007990 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007991 dev_priv->is_ddr3,
7992 dev_priv->fsb_freq,
7993 dev_priv->mem_freq)) {
7994 DRM_INFO("failed to find known CxSR latency "
7995 "(found ddr%s fsb freq %d, mem freq %d), "
7996 "disabling CxSR\n",
7997 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7998 dev_priv->fsb_freq, dev_priv->mem_freq);
7999 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008000 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008001 dev_priv->display.update_wm = NULL;
8002 } else
8003 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008004 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008005 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008006 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008007 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008008 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008009 dev_priv->display.update_wm = i9xx_update_wm;
8010 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008011 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008012 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008013 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008014 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008015 } else {
8016 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008017 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008018 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008019 } else {
8020 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008021 }
8022}
8023
Lyude87660502016-08-17 15:55:53 -04008024static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8025{
8026 uint32_t flags =
8027 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8028
8029 switch (flags) {
8030 case GEN6_PCODE_SUCCESS:
8031 return 0;
8032 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8033 case GEN6_PCODE_ILLEGAL_CMD:
8034 return -ENXIO;
8035 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008036 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008037 return -EOVERFLOW;
8038 case GEN6_PCODE_TIMEOUT:
8039 return -ETIMEDOUT;
8040 default:
8041 MISSING_CASE(flags)
8042 return 0;
8043 }
8044}
8045
8046static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8047{
8048 uint32_t flags =
8049 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8050
8051 switch (flags) {
8052 case GEN6_PCODE_SUCCESS:
8053 return 0;
8054 case GEN6_PCODE_ILLEGAL_CMD:
8055 return -ENXIO;
8056 case GEN7_PCODE_TIMEOUT:
8057 return -ETIMEDOUT;
8058 case GEN7_PCODE_ILLEGAL_DATA:
8059 return -EINVAL;
8060 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8061 return -EOVERFLOW;
8062 default:
8063 MISSING_CASE(flags);
8064 return 0;
8065 }
8066}
8067
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008068int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008069{
Lyude87660502016-08-17 15:55:53 -04008070 int status;
8071
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008072 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008073
Chris Wilson3f5582d2016-06-30 15:32:45 +01008074 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8075 * use te fw I915_READ variants to reduce the amount of work
8076 * required when reading/writing.
8077 */
8078
8079 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008080 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8081 return -EAGAIN;
8082 }
8083
Chris Wilson3f5582d2016-06-30 15:32:45 +01008084 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8085 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8086 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008087
Chris Wilson3f5582d2016-06-30 15:32:45 +01008088 if (intel_wait_for_register_fw(dev_priv,
8089 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8090 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008091 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8092 return -ETIMEDOUT;
8093 }
8094
Chris Wilson3f5582d2016-06-30 15:32:45 +01008095 *val = I915_READ_FW(GEN6_PCODE_DATA);
8096 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008097
Lyude87660502016-08-17 15:55:53 -04008098 if (INTEL_GEN(dev_priv) > 6)
8099 status = gen7_check_mailbox_status(dev_priv);
8100 else
8101 status = gen6_check_mailbox_status(dev_priv);
8102
8103 if (status) {
8104 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8105 status);
8106 return status;
8107 }
8108
Ben Widawsky42c05262012-09-26 10:34:00 -07008109 return 0;
8110}
8111
Chris Wilson3f5582d2016-06-30 15:32:45 +01008112int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008113 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008114{
Lyude87660502016-08-17 15:55:53 -04008115 int status;
8116
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008117 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008118
Chris Wilson3f5582d2016-06-30 15:32:45 +01008119 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8120 * use te fw I915_READ variants to reduce the amount of work
8121 * required when reading/writing.
8122 */
8123
8124 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008125 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
8126 return -EAGAIN;
8127 }
8128
Chris Wilson3f5582d2016-06-30 15:32:45 +01008129 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008130 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008131 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008132
Chris Wilson3f5582d2016-06-30 15:32:45 +01008133 if (intel_wait_for_register_fw(dev_priv,
8134 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8135 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008136 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
8137 return -ETIMEDOUT;
8138 }
8139
Chris Wilson3f5582d2016-06-30 15:32:45 +01008140 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008141
Lyude87660502016-08-17 15:55:53 -04008142 if (INTEL_GEN(dev_priv) > 6)
8143 status = gen7_check_mailbox_status(dev_priv);
8144 else
8145 status = gen6_check_mailbox_status(dev_priv);
8146
8147 if (status) {
8148 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
8149 status);
8150 return status;
8151 }
8152
Ben Widawsky42c05262012-09-26 10:34:00 -07008153 return 0;
8154}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008155
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008156static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8157 u32 request, u32 reply_mask, u32 reply,
8158 u32 *status)
8159{
8160 u32 val = request;
8161
8162 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8163
8164 return *status || ((val & reply_mask) == reply);
8165}
8166
8167/**
8168 * skl_pcode_request - send PCODE request until acknowledgment
8169 * @dev_priv: device private
8170 * @mbox: PCODE mailbox ID the request is targeted for
8171 * @request: request ID
8172 * @reply_mask: mask used to check for request acknowledgment
8173 * @reply: value used to check for request acknowledgment
8174 * @timeout_base_ms: timeout for polling with preemption enabled
8175 *
8176 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008177 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008178 * The request is acknowledged once the PCODE reply dword equals @reply after
8179 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008180 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008181 * preemption disabled.
8182 *
8183 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8184 * other error as reported by PCODE.
8185 */
8186int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8187 u32 reply_mask, u32 reply, int timeout_base_ms)
8188{
8189 u32 status;
8190 int ret;
8191
8192 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
8193
8194#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
8195 &status)
8196
8197 /*
8198 * Prime the PCODE by doing a request first. Normally it guarantees
8199 * that a subsequent request, at most @timeout_base_ms later, succeeds.
8200 * _wait_for() doesn't guarantee when its passed condition is evaluated
8201 * first, so send the first request explicitly.
8202 */
8203 if (COND) {
8204 ret = 0;
8205 goto out;
8206 }
8207 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
8208 if (!ret)
8209 goto out;
8210
8211 /*
8212 * The above can time out if the number of requests was low (2 in the
8213 * worst case) _and_ PCODE was busy for some reason even after a
8214 * (queued) request and @timeout_base_ms delay. As a workaround retry
8215 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02008216 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008217 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02008218 * requests, and for any quirks of the PCODE firmware that delays
8219 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008220 */
8221 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
8222 WARN_ON_ONCE(timeout_base_ms > 3);
8223 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02008224 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008225 preempt_enable();
8226
8227out:
8228 return ret ? ret : status;
8229#undef COND
8230}
8231
Ville Syrjälädd06f882014-11-10 22:55:12 +02008232static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8233{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008234 /*
8235 * N = val - 0xb7
8236 * Slow = Fast = GPLL ref * N
8237 */
8238 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008239}
8240
Fengguang Wub55dd642014-07-12 11:21:39 +02008241static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008242{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008243 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008244}
8245
Fengguang Wub55dd642014-07-12 11:21:39 +02008246static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308247{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008248 /*
8249 * N = val / 2
8250 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8251 */
8252 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308253}
8254
Fengguang Wub55dd642014-07-12 11:21:39 +02008255static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308256{
Ville Syrjälä1c147622014-08-18 14:42:43 +03008257 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008258 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308259}
8260
Ville Syrjälä616bc822015-01-23 21:04:25 +02008261int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8262{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008263 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008264 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8265 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008266 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008267 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008268 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008269 return byt_gpu_freq(dev_priv, val);
8270 else
8271 return val * GT_FREQUENCY_MULTIPLIER;
8272}
8273
Ville Syrjälä616bc822015-01-23 21:04:25 +02008274int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8275{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008276 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008277 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8278 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008279 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008280 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008281 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008282 return byt_freq_opcode(dev_priv, val);
8283 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008284 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308285}
8286
Chris Wilson6ad790c2015-04-07 16:20:31 +01008287struct request_boost {
8288 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008289 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008290};
8291
8292static void __intel_rps_boost_work(struct work_struct *work)
8293{
8294 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008295 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008296
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008297 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008298 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008299
Chris Wilsone8a261e2016-07-20 13:31:49 +01008300 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008301 kfree(boost);
8302}
8303
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008304void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008305{
8306 struct request_boost *boost;
8307
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008308 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008309 return;
8310
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008311 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008312 return;
8313
Chris Wilson6ad790c2015-04-07 16:20:31 +01008314 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8315 if (boost == NULL)
8316 return;
8317
Chris Wilsone8a261e2016-07-20 13:31:49 +01008318 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008319
8320 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008321 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008322}
8323
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008324void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008325{
Daniel Vetterf742a552013-12-06 10:17:53 +01008326 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01008327 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01008328
Chris Wilson54b4f682016-07-21 21:16:19 +01008329 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
8330 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01008331 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03008332
Paulo Zanoni33688d92014-03-07 20:08:19 -03008333 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02008334 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008335}