blob: 7bd4c8688acbc8347ce9210154e39222e3db71ef [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +020034#include <drm/drm_atomic_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030035
Ben Widawskydc39fff2013-10-18 12:32:07 -070036/**
Jani Nikula18afd442016-01-18 09:19:48 +020037 * DOC: RC6
38 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070039 * RC6 is a special power stage which allows the GPU to enter an very
40 * low-voltage mode when idle, using down to 0V while at this stage. This
41 * stage is entered automatically when the GPU is idle when RC6 support is
42 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43 *
44 * There are different RC6 modes available in Intel GPU, which differentiate
45 * among each other with the latency required to enter and leave RC6 and
46 * voltage consumed by the GPU in different states.
47 *
48 * The combination of the following flags define which states GPU is allowed
49 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50 * RC6pp is deepest RC6. Their support by hardware varies according to the
51 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52 * which brings the most power savings; deeper states save more power, but
53 * require higher latency to switch to and wake up.
54 */
55#define INTEL_RC6_ENABLE (1<<0)
56#define INTEL_RC6p_ENABLE (1<<1)
57#define INTEL_RC6pp_ENABLE (1<<2)
58
Ville Syrjälä46f16e62016-10-31 22:37:22 +020059static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030060{
Mika Kuoppalab033bb62016-06-07 17:19:04 +030061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl */
62 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
65 I915_WRITE(GEN8_CONFIG0,
66 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030067
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
72 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020073 /* WaFbcWakeMemOn:skl,bxt,kbl,glk */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030074 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
78 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl */
79 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030081}
82
Ville Syrjälä46f16e62016-10-31 22:37:22 +020083static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020084{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020085 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020086
Nick Hoatha7546152015-06-29 14:07:32 +010087 /* WaDisableSDEUnitClockGating:bxt */
88 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
89 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
90
Imre Deak32608ca2015-03-11 11:10:27 +020091 /*
92 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020093 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020094 */
Imre Deak32608ca2015-03-11 11:10:27 +020095 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020096 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +020097
98 /*
99 * Wa: Backlight PWM may stop in the asserted state, causing backlight
100 * to stay fully on.
101 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200102 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
103 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200104}
105
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200106static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
107{
108 gen9_init_clock_gating(dev_priv);
109
110 /*
111 * WaDisablePWMClockGating:glk
112 * Backlight PWM may stop in the asserted state, causing backlight
113 * to stay fully on.
114 */
115 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
116 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200117
118 /* WaDDIIOTimeout:glk */
119 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
120 u32 val = I915_READ(CHICKEN_MISC_2);
121 val &= ~(GLK_CL0_PWR_DOWN |
122 GLK_CL1_PWR_DOWN |
123 GLK_CL2_PWR_DOWN);
124 I915_WRITE(CHICKEN_MISC_2, val);
125 }
126
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200127}
128
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200129static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200130{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200131 u32 tmp;
132
133 tmp = I915_READ(CLKCFG);
134
135 switch (tmp & CLKCFG_FSB_MASK) {
136 case CLKCFG_FSB_533:
137 dev_priv->fsb_freq = 533; /* 133*4 */
138 break;
139 case CLKCFG_FSB_800:
140 dev_priv->fsb_freq = 800; /* 200*4 */
141 break;
142 case CLKCFG_FSB_667:
143 dev_priv->fsb_freq = 667; /* 167*4 */
144 break;
145 case CLKCFG_FSB_400:
146 dev_priv->fsb_freq = 400; /* 100*4 */
147 break;
148 }
149
150 switch (tmp & CLKCFG_MEM_MASK) {
151 case CLKCFG_MEM_533:
152 dev_priv->mem_freq = 533;
153 break;
154 case CLKCFG_MEM_667:
155 dev_priv->mem_freq = 667;
156 break;
157 case CLKCFG_MEM_800:
158 dev_priv->mem_freq = 800;
159 break;
160 }
161
162 /* detect pineview DDR3 setting */
163 tmp = I915_READ(CSHRDDR3CTL);
164 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
165}
166
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200167static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200168{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200169 u16 ddrpll, csipll;
170
171 ddrpll = I915_READ16(DDRMPLL1);
172 csipll = I915_READ16(CSIPLL0);
173
174 switch (ddrpll & 0xff) {
175 case 0xc:
176 dev_priv->mem_freq = 800;
177 break;
178 case 0x10:
179 dev_priv->mem_freq = 1066;
180 break;
181 case 0x14:
182 dev_priv->mem_freq = 1333;
183 break;
184 case 0x18:
185 dev_priv->mem_freq = 1600;
186 break;
187 default:
188 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
189 ddrpll & 0xff);
190 dev_priv->mem_freq = 0;
191 break;
192 }
193
Daniel Vetter20e4d402012-08-08 23:35:39 +0200194 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200195
196 switch (csipll & 0x3ff) {
197 case 0x00c:
198 dev_priv->fsb_freq = 3200;
199 break;
200 case 0x00e:
201 dev_priv->fsb_freq = 3733;
202 break;
203 case 0x010:
204 dev_priv->fsb_freq = 4266;
205 break;
206 case 0x012:
207 dev_priv->fsb_freq = 4800;
208 break;
209 case 0x014:
210 dev_priv->fsb_freq = 5333;
211 break;
212 case 0x016:
213 dev_priv->fsb_freq = 5866;
214 break;
215 case 0x018:
216 dev_priv->fsb_freq = 6400;
217 break;
218 default:
219 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
220 csipll & 0x3ff);
221 dev_priv->fsb_freq = 0;
222 break;
223 }
224
225 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200226 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200227 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200228 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200229 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200230 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200231 }
232}
233
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300234static const struct cxsr_latency cxsr_latency_table[] = {
235 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
236 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
237 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
238 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
239 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
240
241 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
242 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
243 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
244 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
245 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
246
247 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
248 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
249 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
250 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
251 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
252
253 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
254 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
255 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
256 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
257 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
258
259 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
260 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
261 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
262 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
263 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
264
265 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
266 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
267 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
268 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
269 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
270};
271
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100272static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
273 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300274 int fsb,
275 int mem)
276{
277 const struct cxsr_latency *latency;
278 int i;
279
280 if (fsb == 0 || mem == 0)
281 return NULL;
282
283 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
284 latency = &cxsr_latency_table[i];
285 if (is_desktop == latency->is_desktop &&
286 is_ddr3 == latency->is_ddr3 &&
287 fsb == latency->fsb_freq && mem == latency->mem_freq)
288 return latency;
289 }
290
291 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
292
293 return NULL;
294}
295
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200296static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
297{
298 u32 val;
299
300 mutex_lock(&dev_priv->rps.hw_lock);
301
302 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
303 if (enable)
304 val &= ~FORCE_DDR_HIGH_FREQ;
305 else
306 val |= FORCE_DDR_HIGH_FREQ;
307 val &= ~FORCE_DDR_LOW_FREQ;
308 val |= FORCE_DDR_FREQ_REQ_ACK;
309 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
310
311 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
312 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
313 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
314
315 mutex_unlock(&dev_priv->rps.hw_lock);
316}
317
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200318static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
319{
320 u32 val;
321
322 mutex_lock(&dev_priv->rps.hw_lock);
323
324 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
325 if (enable)
326 val |= DSP_MAXFIFO_PM5_ENABLE;
327 else
328 val &= ~DSP_MAXFIFO_PM5_ENABLE;
329 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
330
331 mutex_unlock(&dev_priv->rps.hw_lock);
332}
333
Ville Syrjäläf4998962015-03-10 17:02:21 +0200334#define FW_WM(value, plane) \
335 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
336
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200337static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300338{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200339 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300340 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300341
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100342 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200343 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300344 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300345 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200346 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200347 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300348 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300349 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200350 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200351 val = I915_READ(DSPFW3);
352 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
353 if (enable)
354 val |= PINEVIEW_SELF_REFRESH_EN;
355 else
356 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300357 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300358 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100359 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200360 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300361 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
362 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
363 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300364 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100365 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300366 /*
367 * FIXME can't find a bit like this for 915G, and
368 * and yet it does have the related watermark in
369 * FW_BLC_SELF. What's going on?
370 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200371 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300372 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
373 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
374 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300375 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300376 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300378 }
379
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200380 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
381
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200382 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
383 enableddisabled(enable),
384 enableddisabled(was_enabled));
385
386 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300387}
388
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300389/**
390 * intel_set_memory_cxsr - Configure CxSR state
391 * @dev_priv: i915 device
392 * @enable: Allow vs. disallow CxSR
393 *
394 * Allow or disallow the system to enter a special CxSR
395 * (C-state self refresh) state. What typically happens in CxSR mode
396 * is that several display FIFOs may get combined into a single larger
397 * FIFO for a particular plane (so called max FIFO mode) to allow the
398 * system to defer memory fetches longer, and the memory will enter
399 * self refresh.
400 *
401 * Note that enabling CxSR does not guarantee that the system enter
402 * this special mode, nor does it guarantee that the system stays
403 * in that mode once entered. So this just allows/disallows the system
404 * to autonomously utilize the CxSR mode. Other factors such as core
405 * C-states will affect when/if the system actually enters/exits the
406 * CxSR mode.
407 *
408 * Note that on VLV/CHV this actually only controls the max FIFO mode,
409 * and the system is free to enter/exit memory self refresh at any time
410 * even when the use of CxSR has been disallowed.
411 *
412 * While the system is actually in the CxSR/max FIFO mode, some plane
413 * control registers will not get latched on vblank. Thus in order to
414 * guarantee the system will respond to changes in the plane registers
415 * we must always disallow CxSR prior to making changes to those registers.
416 * Unfortunately the system will re-evaluate the CxSR conditions at
417 * frame start which happens after vblank start (which is when the plane
418 * registers would get latched), so we can't proceed with the plane update
419 * during the same frame where we disallowed CxSR.
420 *
421 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
422 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
423 * the hardware w.r.t. HPLL SR when writing to plane registers.
424 * Disallowing just CxSR is sufficient.
425 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200426bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200427{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200428 bool ret;
429
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200430 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200431 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200432 dev_priv->wm.vlv.cxsr = enable;
433 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200434
435 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200436}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200437
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300438/*
439 * Latency for FIFO fetches is dependent on several factors:
440 * - memory configuration (speed, channels)
441 * - chipset
442 * - current MCH state
443 * It can be fairly high in some situations, so here we assume a fairly
444 * pessimal value. It's a tradeoff between extra memory fetches (if we
445 * set this value too high, the FIFO will fetch frequently to stay full)
446 * and power consumption (set it too low to save power and we might see
447 * FIFO underruns and display "flicker").
448 *
449 * A value of 5us seems to be a good balance; safe for very low end
450 * platforms but not overly aggressive on lower latency configs.
451 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100452static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300453
Ville Syrjäläb5004722015-03-05 21:19:47 +0200454#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
455 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
456
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200457static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200458{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200459 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200460 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200461 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200462 enum pipe pipe = crtc->pipe;
463 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200464
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200465 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200466 uint32_t dsparb, dsparb2, dsparb3;
467 case PIPE_A:
468 dsparb = I915_READ(DSPARB);
469 dsparb2 = I915_READ(DSPARB2);
470 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
471 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
472 break;
473 case PIPE_B:
474 dsparb = I915_READ(DSPARB);
475 dsparb2 = I915_READ(DSPARB2);
476 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
477 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
478 break;
479 case PIPE_C:
480 dsparb2 = I915_READ(DSPARB2);
481 dsparb3 = I915_READ(DSPARB3);
482 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
483 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
484 break;
485 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200486 MISSING_CASE(pipe);
487 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200488 }
489
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200490 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
491 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
492 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
493 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200494}
495
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200496static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300497{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300498 uint32_t dsparb = I915_READ(DSPARB);
499 int size;
500
501 size = dsparb & 0x7f;
502 if (plane)
503 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
504
505 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
506 plane ? "B" : "A", size);
507
508 return size;
509}
510
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200511static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300512{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300513 uint32_t dsparb = I915_READ(DSPARB);
514 int size;
515
516 size = dsparb & 0x1ff;
517 if (plane)
518 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
519 size >>= 1; /* Convert to cachelines */
520
521 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
522 plane ? "B" : "A", size);
523
524 return size;
525}
526
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200527static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300528{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300529 uint32_t dsparb = I915_READ(DSPARB);
530 int size;
531
532 size = dsparb & 0x7f;
533 size >>= 2; /* Convert to cachelines */
534
535 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
536 plane ? "B" : "A",
537 size);
538
539 return size;
540}
541
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542/* Pineview has different values for various configs */
543static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300544 .fifo_size = PINEVIEW_DISPLAY_FIFO,
545 .max_wm = PINEVIEW_MAX_WM,
546 .default_wm = PINEVIEW_DFT_WM,
547 .guard_size = PINEVIEW_GUARD_WM,
548 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300549};
550static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300551 .fifo_size = PINEVIEW_DISPLAY_FIFO,
552 .max_wm = PINEVIEW_MAX_WM,
553 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
554 .guard_size = PINEVIEW_GUARD_WM,
555 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300556};
557static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300558 .fifo_size = PINEVIEW_CURSOR_FIFO,
559 .max_wm = PINEVIEW_CURSOR_MAX_WM,
560 .default_wm = PINEVIEW_CURSOR_DFT_WM,
561 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
562 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300563};
564static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300565 .fifo_size = PINEVIEW_CURSOR_FIFO,
566 .max_wm = PINEVIEW_CURSOR_MAX_WM,
567 .default_wm = PINEVIEW_CURSOR_DFT_WM,
568 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
569 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300570};
571static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300572 .fifo_size = G4X_FIFO_SIZE,
573 .max_wm = G4X_MAX_WM,
574 .default_wm = G4X_MAX_WM,
575 .guard_size = 2,
576 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300577};
578static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300579 .fifo_size = I965_CURSOR_FIFO,
580 .max_wm = I965_CURSOR_MAX_WM,
581 .default_wm = I965_CURSOR_DFT_WM,
582 .guard_size = 2,
583 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300584};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300585static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300586 .fifo_size = I965_CURSOR_FIFO,
587 .max_wm = I965_CURSOR_MAX_WM,
588 .default_wm = I965_CURSOR_DFT_WM,
589 .guard_size = 2,
590 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300591};
592static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300593 .fifo_size = I945_FIFO_SIZE,
594 .max_wm = I915_MAX_WM,
595 .default_wm = 1,
596 .guard_size = 2,
597 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300598};
599static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300600 .fifo_size = I915_FIFO_SIZE,
601 .max_wm = I915_MAX_WM,
602 .default_wm = 1,
603 .guard_size = 2,
604 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300605};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300606static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300607 .fifo_size = I855GM_FIFO_SIZE,
608 .max_wm = I915_MAX_WM,
609 .default_wm = 1,
610 .guard_size = 2,
611 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300612};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300613static const struct intel_watermark_params i830_bc_wm_info = {
614 .fifo_size = I855GM_FIFO_SIZE,
615 .max_wm = I915_MAX_WM/2,
616 .default_wm = 1,
617 .guard_size = 2,
618 .cacheline_size = I830_FIFO_LINE_SIZE,
619};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200620static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300621 .fifo_size = I830_FIFO_SIZE,
622 .max_wm = I915_MAX_WM,
623 .default_wm = 1,
624 .guard_size = 2,
625 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300626};
627
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300628/**
629 * intel_calculate_wm - calculate watermark level
630 * @clock_in_khz: pixel clock
631 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200632 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300633 * @latency_ns: memory latency for the platform
634 *
635 * Calculate the watermark level (the level at which the display plane will
636 * start fetching from memory again). Each chip has a different display
637 * FIFO size and allocation, so the caller needs to figure that out and pass
638 * in the correct intel_watermark_params structure.
639 *
640 * As the pixel clock runs, the FIFO will be drained at a rate that depends
641 * on the pixel size. When it reaches the watermark level, it'll start
642 * fetching FIFO line sized based chunks from memory until the FIFO fills
643 * past the watermark point. If the FIFO drains completely, a FIFO underrun
644 * will occur, and a display engine hang could result.
645 */
646static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
647 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200648 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300649 unsigned long latency_ns)
650{
651 long entries_required, wm_size;
652
653 /*
654 * Note: we need to make sure we don't overflow for various clock &
655 * latency values.
656 * clocks go from a few thousand to several hundred thousand.
657 * latency is usually a few thousand
658 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200659 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300660 1000;
661 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
662
663 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
664
665 wm_size = fifo_size - (entries_required + wm->guard_size);
666
667 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
668
669 /* Don't promote wm_size to unsigned... */
670 if (wm_size > (long)wm->max_wm)
671 wm_size = wm->max_wm;
672 if (wm_size <= 0)
673 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300674
675 /*
676 * Bspec seems to indicate that the value shouldn't be lower than
677 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
678 * Lets go for 8 which is the burst size since certain platforms
679 * already use a hardcoded 8 (which is what the spec says should be
680 * done).
681 */
682 if (wm_size <= 8)
683 wm_size = 8;
684
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300685 return wm_size;
686}
687
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300688static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
689{
690 return dev_priv->wm.max_level + 1;
691}
692
Ville Syrjälä24304d812017-03-14 17:10:49 +0200693static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
694 const struct intel_plane_state *plane_state)
695{
696 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
697
698 /* FIXME check the 'enable' instead */
699 if (!crtc_state->base.active)
700 return false;
701
702 /*
703 * Treat cursor with fb as always visible since cursor updates
704 * can happen faster than the vrefresh rate, and the current
705 * watermark code doesn't handle that correctly. Cursor updates
706 * which set/clear the fb or change the cursor size are going
707 * to get throttled by intel_legacy_cursor_update() to work
708 * around this problem with the watermark code.
709 */
710 if (plane->id == PLANE_CURSOR)
711 return plane_state->base.fb != NULL;
712 else
713 return plane_state->base.visible;
714}
715
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200716static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300717{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200718 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300719
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200720 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200721 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722 if (enabled)
723 return NULL;
724 enabled = crtc;
725 }
726 }
727
728 return enabled;
729}
730
Ville Syrjälä432081b2016-10-31 22:37:03 +0200731static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300732{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200733 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200734 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300735 const struct cxsr_latency *latency;
736 u32 reg;
737 unsigned long wm;
738
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100739 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
740 dev_priv->is_ddr3,
741 dev_priv->fsb_freq,
742 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300743 if (!latency) {
744 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300745 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746 return;
747 }
748
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200749 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300750 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200751 const struct drm_display_mode *adjusted_mode =
752 &crtc->config->base.adjusted_mode;
753 const struct drm_framebuffer *fb =
754 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200755 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300756 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300757
758 /* Display SR */
759 wm = intel_calculate_wm(clock, &pineview_display_wm,
760 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200761 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300762 reg = I915_READ(DSPFW1);
763 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200764 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300765 I915_WRITE(DSPFW1, reg);
766 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
767
768 /* cursor SR */
769 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
770 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200771 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300772 reg = I915_READ(DSPFW3);
773 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200774 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775 I915_WRITE(DSPFW3, reg);
776
777 /* Display HPLL off SR */
778 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
779 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200780 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300781 reg = I915_READ(DSPFW3);
782 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200783 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300784 I915_WRITE(DSPFW3, reg);
785
786 /* cursor HPLL off SR */
787 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
788 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200789 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300790 reg = I915_READ(DSPFW3);
791 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200792 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300793 I915_WRITE(DSPFW3, reg);
794 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
795
Imre Deak5209b1f2014-07-01 12:36:17 +0300796 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300797 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300798 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300799 }
800}
801
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200802static bool g4x_compute_wm0(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300803 int plane,
804 const struct intel_watermark_params *display,
805 int display_latency_ns,
806 const struct intel_watermark_params *cursor,
807 int cursor_latency_ns,
808 int *plane_wm,
809 int *cursor_wm)
810{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200811 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300812 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200813 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200814 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300815 int line_time_us, line_count;
816 int entries, tlb_miss;
817
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200818 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200819 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300820 *cursor_wm = cursor->guard_size;
821 *plane_wm = display->guard_size;
822 return false;
823 }
824
Ville Syrjäläefc26112016-10-31 22:37:04 +0200825 adjusted_mode = &crtc->config->base.adjusted_mode;
826 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100827 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800828 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200829 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200830 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300831
832 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200833 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300834 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
835 if (tlb_miss > 0)
836 entries += tlb_miss;
837 entries = DIV_ROUND_UP(entries, display->cacheline_size);
838 *plane_wm = entries + display->guard_size;
839 if (*plane_wm > (int)display->max_wm)
840 *plane_wm = display->max_wm;
841
842 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200843 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300844 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200845 entries = line_count * crtc->base.cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300846 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
847 if (tlb_miss > 0)
848 entries += tlb_miss;
849 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
850 *cursor_wm = entries + cursor->guard_size;
851 if (*cursor_wm > (int)cursor->max_wm)
852 *cursor_wm = (int)cursor->max_wm;
853
854 return true;
855}
856
857/*
858 * Check the wm result.
859 *
860 * If any calculated watermark values is larger than the maximum value that
861 * can be programmed into the associated watermark register, that watermark
862 * must be disabled.
863 */
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200864static bool g4x_check_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300865 int display_wm, int cursor_wm,
866 const struct intel_watermark_params *display,
867 const struct intel_watermark_params *cursor)
868{
869 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
870 display_wm, cursor_wm);
871
872 if (display_wm > display->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100873 DRM_DEBUG_KMS("display watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300874 display_wm, display->max_wm);
875 return false;
876 }
877
878 if (cursor_wm > cursor->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100879 DRM_DEBUG_KMS("cursor watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300880 cursor_wm, cursor->max_wm);
881 return false;
882 }
883
884 if (!(display_wm || cursor_wm)) {
885 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
886 return false;
887 }
888
889 return true;
890}
891
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200892static bool g4x_compute_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300893 int plane,
894 int latency_ns,
895 const struct intel_watermark_params *display,
896 const struct intel_watermark_params *cursor,
897 int *display_wm, int *cursor_wm)
898{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200899 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300900 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200901 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200902 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300903 unsigned long line_time_us;
904 int line_count, line_size;
905 int small, large;
906 int entries;
907
908 if (!latency_ns) {
909 *display_wm = *cursor_wm = 0;
910 return false;
911 }
912
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200913 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200914 adjusted_mode = &crtc->config->base.adjusted_mode;
915 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100916 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800917 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200918 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200919 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300920
Ville Syrjälä922044c2014-02-14 14:18:57 +0200921 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300922 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200923 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300924
925 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200926 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300927 large = line_count * line_size;
928
929 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
930 *display_wm = entries + display->guard_size;
931
932 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläefc26112016-10-31 22:37:04 +0200933 entries = line_count * cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300934 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
935 *cursor_wm = entries + cursor->guard_size;
936
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200937 return g4x_check_srwm(dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938 *display_wm, *cursor_wm,
939 display, cursor);
940}
941
Ville Syrjälä15665972015-03-10 16:16:28 +0200942#define FW_WM_VLV(value, plane) \
943 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
944
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200945static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200946 const struct vlv_wm_values *wm)
947{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200948 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200949
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200950 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200951 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
952
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200953 I915_WRITE(VLV_DDL(pipe),
954 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
955 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
956 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
957 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
958 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200959
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200960 /*
961 * Zero the (unused) WM1 watermarks, and also clear all the
962 * high order bits so that there are no out of bounds values
963 * present in the registers during the reprogramming.
964 */
965 I915_WRITE(DSPHOWM, 0);
966 I915_WRITE(DSPHOWM1, 0);
967 I915_WRITE(DSPFW4, 0);
968 I915_WRITE(DSPFW5, 0);
969 I915_WRITE(DSPFW6, 0);
970
Ville Syrjäläae801522015-03-05 21:19:49 +0200971 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200972 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200973 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
974 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
975 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200976 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200977 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
978 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
979 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200980 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200981 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200982
983 if (IS_CHERRYVIEW(dev_priv)) {
984 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200985 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
986 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200987 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200988 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
989 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200990 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200991 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
992 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200993 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200994 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200995 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
996 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
997 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
998 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
999 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1000 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1001 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1002 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1003 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001004 } else {
1005 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001006 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1007 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001008 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001009 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001010 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1011 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1012 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1013 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1014 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1015 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001016 }
1017
1018 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001019}
1020
Ville Syrjälä15665972015-03-10 16:16:28 +02001021#undef FW_WM_VLV
1022
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001023/* latency must be in 0.1us units. */
1024static unsigned int vlv_wm_method2(unsigned int pixel_rate,
1025 unsigned int pipe_htotal,
1026 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001027 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001028 unsigned int latency)
1029{
1030 unsigned int ret;
1031
1032 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001033 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001034 ret = DIV_ROUND_UP(ret, 64);
1035
1036 return ret;
1037}
1038
Ville Syrjäläbb726512016-10-31 22:37:24 +02001039static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001040{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001041 /* all latencies in usec */
1042 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1043
Ville Syrjälä58590c12015-09-08 21:05:12 +03001044 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1045
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001046 if (IS_CHERRYVIEW(dev_priv)) {
1047 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1048 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001049
1050 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001051 }
1052}
1053
Ville Syrjäläe339d672016-11-28 19:37:17 +02001054static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1055 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001056 int level)
1057{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001058 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001059 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001060 const struct drm_display_mode *adjusted_mode =
1061 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001062 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001063
1064 if (dev_priv->wm.pri_latency[level] == 0)
1065 return USHRT_MAX;
1066
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001067 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001068 return 0;
1069
Daniel Vetteref426c12017-01-04 11:41:10 +01001070 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001071 clock = adjusted_mode->crtc_clock;
1072 htotal = adjusted_mode->crtc_htotal;
1073 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001074 if (WARN_ON(htotal == 0))
1075 htotal = 1;
1076
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001077 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001078 /*
1079 * FIXME the formula gives values that are
1080 * too big for the cursor FIFO, and hence we
1081 * would never be able to use cursors. For
1082 * now just hardcode the watermark.
1083 */
1084 wm = 63;
1085 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001086 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001087 dev_priv->wm.pri_latency[level] * 10);
1088 }
1089
1090 return min_t(int, wm, USHRT_MAX);
1091}
1092
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001093static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1094{
1095 return (active_planes & (BIT(PLANE_SPRITE0) |
1096 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1097}
1098
Ville Syrjälä5012e602017-03-02 19:14:56 +02001099static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001100{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001101 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001102 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001103 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001104 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001105 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1106 int num_active_planes = hweight32(active_planes);
1107 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001108 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001109 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001110 unsigned int total_rate;
1111 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001112
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001113 /*
1114 * When enabling sprite0 after sprite1 has already been enabled
1115 * we tend to get an underrun unless sprite0 already has some
1116 * FIFO space allcoated. Hence we always allocate at least one
1117 * cacheline for sprite0 whenever sprite1 is enabled.
1118 *
1119 * All other plane enable sequences appear immune to this problem.
1120 */
1121 if (vlv_need_sprite0_fifo_workaround(active_planes))
1122 sprite0_fifo_extra = 1;
1123
Ville Syrjälä5012e602017-03-02 19:14:56 +02001124 total_rate = raw->plane[PLANE_PRIMARY] +
1125 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001126 raw->plane[PLANE_SPRITE1] +
1127 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001128
Ville Syrjälä5012e602017-03-02 19:14:56 +02001129 if (total_rate > fifo_size)
1130 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001131
Ville Syrjälä5012e602017-03-02 19:14:56 +02001132 if (total_rate == 0)
1133 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001134
Ville Syrjälä5012e602017-03-02 19:14:56 +02001135 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001136 unsigned int rate;
1137
Ville Syrjälä5012e602017-03-02 19:14:56 +02001138 if ((active_planes & BIT(plane_id)) == 0) {
1139 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001140 continue;
1141 }
1142
Ville Syrjälä5012e602017-03-02 19:14:56 +02001143 rate = raw->plane[plane_id];
1144 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1145 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001146 }
1147
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001148 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1149 fifo_left -= sprite0_fifo_extra;
1150
Ville Syrjälä5012e602017-03-02 19:14:56 +02001151 fifo_state->plane[PLANE_CURSOR] = 63;
1152
1153 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001154
1155 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001156 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001157 int plane_extra;
1158
1159 if (fifo_left == 0)
1160 break;
1161
Ville Syrjälä5012e602017-03-02 19:14:56 +02001162 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001163 continue;
1164
1165 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001166 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001167 fifo_left -= plane_extra;
1168 }
1169
Ville Syrjälä5012e602017-03-02 19:14:56 +02001170 WARN_ON(active_planes != 0 && fifo_left != 0);
1171
1172 /* give it all to the first plane if none are active */
1173 if (active_planes == 0) {
1174 WARN_ON(fifo_left != fifo_size);
1175 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1176 }
1177
1178 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001179}
1180
Ville Syrjäläff32c542017-03-02 19:14:57 +02001181/* mark all levels starting from 'level' as invalid */
1182static void vlv_invalidate_wms(struct intel_crtc *crtc,
1183 struct vlv_wm_state *wm_state, int level)
1184{
1185 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1186
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001187 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001188 enum plane_id plane_id;
1189
1190 for_each_plane_id_on_crtc(crtc, plane_id)
1191 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1192
1193 wm_state->sr[level].cursor = USHRT_MAX;
1194 wm_state->sr[level].plane = USHRT_MAX;
1195 }
1196}
1197
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001198static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1199{
1200 if (wm > fifo_size)
1201 return USHRT_MAX;
1202 else
1203 return fifo_size - wm;
1204}
1205
Ville Syrjäläff32c542017-03-02 19:14:57 +02001206/*
1207 * Starting from 'level' set all higher
1208 * levels to 'value' in the "raw" watermarks.
1209 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001210static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001211 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001212{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001213 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001214 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001215 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001216
Ville Syrjäläff32c542017-03-02 19:14:57 +02001217 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001218 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001219
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001220 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001221 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001222 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001223
1224 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001225}
1226
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001227static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1228 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001229{
1230 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1231 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001232 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001233 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001234 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001235
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001236 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001237 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1238 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001239 }
1240
1241 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001242 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001243 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1244 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1245
Ville Syrjäläff32c542017-03-02 19:14:57 +02001246 if (wm > max_wm)
1247 break;
1248
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001249 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001250 raw->plane[plane_id] = wm;
1251 }
1252
1253 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001254 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001255
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001256out:
1257 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001258 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001259 plane->base.name,
1260 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1261 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1262 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1263
1264 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001265}
1266
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001267static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1268 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001269{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001270 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001271 &crtc_state->wm.vlv.raw[level];
1272 const struct vlv_fifo_state *fifo_state =
1273 &crtc_state->wm.vlv.fifo_state;
1274
1275 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1276}
1277
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001278static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001279{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001280 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1281 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1282 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1283 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001284}
1285
1286static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001287{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001288 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001289 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001290 struct intel_atomic_state *state =
1291 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001292 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001293 const struct vlv_fifo_state *fifo_state =
1294 &crtc_state->wm.vlv.fifo_state;
1295 int num_active_planes = hweight32(crtc_state->active_planes &
1296 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001297 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001298 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001299 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001300 enum plane_id plane_id;
1301 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001302 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001303
Ville Syrjäläff32c542017-03-02 19:14:57 +02001304 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1305 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001306 to_intel_plane_state(plane->base.state);
1307
Ville Syrjäläff32c542017-03-02 19:14:57 +02001308 if (plane_state->base.crtc != &crtc->base &&
1309 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001310 continue;
1311
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001312 if (vlv_raw_plane_wm_compute(crtc_state, plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001313 dirty |= BIT(plane->id);
1314 }
1315
1316 /*
1317 * DSPARB registers may have been reset due to the
1318 * power well being turned off. Make sure we restore
1319 * them to a consistent state even if no primary/sprite
1320 * planes are initially active.
1321 */
1322 if (needs_modeset)
1323 crtc_state->fifo_changed = true;
1324
1325 if (!dirty)
1326 return 0;
1327
1328 /* cursor changes don't warrant a FIFO recompute */
1329 if (dirty & ~BIT(PLANE_CURSOR)) {
1330 const struct intel_crtc_state *old_crtc_state =
1331 to_intel_crtc_state(crtc->base.state);
1332 const struct vlv_fifo_state *old_fifo_state =
1333 &old_crtc_state->wm.vlv.fifo_state;
1334
1335 ret = vlv_compute_fifo(crtc_state);
1336 if (ret)
1337 return ret;
1338
1339 if (needs_modeset ||
1340 memcmp(old_fifo_state, fifo_state,
1341 sizeof(*fifo_state)) != 0)
1342 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001343 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001344
Ville Syrjäläff32c542017-03-02 19:14:57 +02001345 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001346 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001347 /*
1348 * Note that enabling cxsr with no primary/sprite planes
1349 * enabled can wedge the pipe. Hence we only allow cxsr
1350 * with exactly one enabled primary/sprite plane.
1351 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001352 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001353
Ville Syrjälä5012e602017-03-02 19:14:56 +02001354 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001355 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001356 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001357
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001358 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001359 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001360
Ville Syrjäläff32c542017-03-02 19:14:57 +02001361 for_each_plane_id_on_crtc(crtc, plane_id) {
1362 wm_state->wm[level].plane[plane_id] =
1363 vlv_invert_wm_value(raw->plane[plane_id],
1364 fifo_state->plane[plane_id]);
1365 }
1366
1367 wm_state->sr[level].plane =
1368 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001369 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001370 raw->plane[PLANE_SPRITE1]),
1371 sr_fifo_size);
1372
1373 wm_state->sr[level].cursor =
1374 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1375 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001376 }
1377
Ville Syrjäläff32c542017-03-02 19:14:57 +02001378 if (level == 0)
1379 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001380
Ville Syrjäläff32c542017-03-02 19:14:57 +02001381 /* limit to only levels we can actually handle */
1382 wm_state->num_levels = level;
1383
1384 /* invalidate the higher levels */
1385 vlv_invalidate_wms(crtc, wm_state, level);
1386
1387 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001388}
1389
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001390#define VLV_FIFO(plane, value) \
1391 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1392
Ville Syrjäläff32c542017-03-02 19:14:57 +02001393static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1394 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001395{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001396 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001397 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001398 const struct vlv_fifo_state *fifo_state =
1399 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001400 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001401
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001402 if (!crtc_state->fifo_changed)
1403 return;
1404
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001405 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1406 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1407 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001408
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001409 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1410 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001411
Ville Syrjäläc137d662017-03-02 19:15:06 +02001412 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1413
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001414 /*
1415 * uncore.lock serves a double purpose here. It allows us to
1416 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1417 * it protects the DSPARB registers from getting clobbered by
1418 * parallel updates from multiple pipes.
1419 *
1420 * intel_pipe_update_start() has already disabled interrupts
1421 * for us, so a plain spin_lock() is sufficient here.
1422 */
1423 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001424
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001425 switch (crtc->pipe) {
1426 uint32_t dsparb, dsparb2, dsparb3;
1427 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001428 dsparb = I915_READ_FW(DSPARB);
1429 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001430
1431 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1432 VLV_FIFO(SPRITEB, 0xff));
1433 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1434 VLV_FIFO(SPRITEB, sprite1_start));
1435
1436 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1437 VLV_FIFO(SPRITEB_HI, 0x1));
1438 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1439 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1440
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001441 I915_WRITE_FW(DSPARB, dsparb);
1442 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001443 break;
1444 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001445 dsparb = I915_READ_FW(DSPARB);
1446 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001447
1448 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1449 VLV_FIFO(SPRITED, 0xff));
1450 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1451 VLV_FIFO(SPRITED, sprite1_start));
1452
1453 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1454 VLV_FIFO(SPRITED_HI, 0xff));
1455 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1456 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1457
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001458 I915_WRITE_FW(DSPARB, dsparb);
1459 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001460 break;
1461 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001462 dsparb3 = I915_READ_FW(DSPARB3);
1463 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001464
1465 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1466 VLV_FIFO(SPRITEF, 0xff));
1467 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1468 VLV_FIFO(SPRITEF, sprite1_start));
1469
1470 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1471 VLV_FIFO(SPRITEF_HI, 0xff));
1472 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1473 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1474
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001475 I915_WRITE_FW(DSPARB3, dsparb3);
1476 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001477 break;
1478 default:
1479 break;
1480 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001481
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001482 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001483
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001484 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001485}
1486
1487#undef VLV_FIFO
1488
Ville Syrjälä4841da52017-03-02 19:14:59 +02001489static int vlv_compute_intermediate_wm(struct drm_device *dev,
1490 struct intel_crtc *crtc,
1491 struct intel_crtc_state *crtc_state)
1492{
1493 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
1494 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
1495 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
1496 int level;
1497
1498 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001499 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1500 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001501
1502 for (level = 0; level < intermediate->num_levels; level++) {
1503 enum plane_id plane_id;
1504
1505 for_each_plane_id_on_crtc(crtc, plane_id) {
1506 intermediate->wm[level].plane[plane_id] =
1507 min(optimal->wm[level].plane[plane_id],
1508 active->wm[level].plane[plane_id]);
1509 }
1510
1511 intermediate->sr[level].plane = min(optimal->sr[level].plane,
1512 active->sr[level].plane);
1513 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
1514 active->sr[level].cursor);
1515 }
1516
1517 vlv_invalidate_wms(crtc, intermediate, level);
1518
1519 /*
1520 * If our intermediate WM are identical to the final WM, then we can
1521 * omit the post-vblank programming; only update if it's different.
1522 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001523 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1524 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001525
1526 return 0;
1527}
1528
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001529static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001530 struct vlv_wm_values *wm)
1531{
1532 struct intel_crtc *crtc;
1533 int num_active_crtcs = 0;
1534
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001535 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001536 wm->cxsr = true;
1537
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001538 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001539 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001540
1541 if (!crtc->active)
1542 continue;
1543
1544 if (!wm_state->cxsr)
1545 wm->cxsr = false;
1546
1547 num_active_crtcs++;
1548 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1549 }
1550
1551 if (num_active_crtcs != 1)
1552 wm->cxsr = false;
1553
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001554 if (num_active_crtcs > 1)
1555 wm->level = VLV_WM_LEVEL_PM2;
1556
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001557 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001558 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001559 enum pipe pipe = crtc->pipe;
1560
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001561 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001562 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001563 wm->sr = wm_state->sr[wm->level];
1564
Ville Syrjälä1b313892016-11-28 19:37:08 +02001565 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
1566 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
1567 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
1568 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001569 }
1570}
1571
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001572static bool is_disabling(int old, int new, int threshold)
1573{
1574 return old >= threshold && new < threshold;
1575}
1576
1577static bool is_enabling(int old, int new, int threshold)
1578{
1579 return old < threshold && new >= threshold;
1580}
1581
Ville Syrjäläff32c542017-03-02 19:14:57 +02001582static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001583{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001584 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
1585 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001586
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001587 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001588
Ville Syrjäläff32c542017-03-02 19:14:57 +02001589 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001590 return;
1591
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001592 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001593 chv_set_memory_dvfs(dev_priv, false);
1594
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001595 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001596 chv_set_memory_pm5(dev_priv, false);
1597
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001598 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001599 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001600
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001601 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001602
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001603 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001604 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001605
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001606 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001607 chv_set_memory_pm5(dev_priv, true);
1608
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001609 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001610 chv_set_memory_dvfs(dev_priv, true);
1611
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001612 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001613}
1614
Ville Syrjäläff32c542017-03-02 19:14:57 +02001615static void vlv_initial_watermarks(struct intel_atomic_state *state,
1616 struct intel_crtc_state *crtc_state)
1617{
1618 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1619 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1620
1621 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02001622 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
1623 vlv_program_watermarks(dev_priv);
1624 mutex_unlock(&dev_priv->wm.wm_mutex);
1625}
1626
1627static void vlv_optimize_watermarks(struct intel_atomic_state *state,
1628 struct intel_crtc_state *crtc_state)
1629{
1630 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1631 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1632
1633 if (!crtc_state->wm.need_postvbl_update)
1634 return;
1635
1636 mutex_lock(&dev_priv->wm.wm_mutex);
1637 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001638 vlv_program_watermarks(dev_priv);
1639 mutex_unlock(&dev_priv->wm.wm_mutex);
1640}
1641
Ville Syrjäläae801522015-03-05 21:19:49 +02001642#define single_plane_enabled(mask) is_power_of_2(mask)
1643
Ville Syrjälä432081b2016-10-31 22:37:03 +02001644static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001645{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001646 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001647 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001648 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1649 int plane_sr, cursor_sr;
1650 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001651 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001652
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001653 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001654 &g4x_wm_info, pessimal_latency_ns,
1655 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001656 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001657 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001658
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001659 if (g4x_compute_wm0(dev_priv, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001660 &g4x_wm_info, pessimal_latency_ns,
1661 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001662 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001663 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001664
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001665 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001666 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001667 sr_latency_ns,
1668 &g4x_wm_info,
1669 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001670 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001671 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001672 } else {
Imre Deak98584252014-06-13 14:54:20 +03001673 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001674 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001675 plane_sr = cursor_sr = 0;
1676 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001677
Ville Syrjäläa5043452014-06-28 02:04:18 +03001678 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1679 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001680 planea_wm, cursora_wm,
1681 planeb_wm, cursorb_wm,
1682 plane_sr, cursor_sr);
1683
1684 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001685 FW_WM(plane_sr, SR) |
1686 FW_WM(cursorb_wm, CURSORB) |
1687 FW_WM(planeb_wm, PLANEB) |
1688 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001689 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001690 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001691 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001692 /* HPLL off in SR has some issues on G4x... disable it */
1693 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001694 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001695 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001696
1697 if (cxsr_enabled)
1698 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001699}
1700
Ville Syrjälä432081b2016-10-31 22:37:03 +02001701static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001702{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001703 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001704 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001705 int srwm = 1;
1706 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001707 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001708
1709 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001710 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001711 if (crtc) {
1712 /* self-refresh has much higher latency */
1713 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001714 const struct drm_display_mode *adjusted_mode =
1715 &crtc->config->base.adjusted_mode;
1716 const struct drm_framebuffer *fb =
1717 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001718 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001719 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001720 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02001721 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001722 unsigned long line_time_us;
1723 int entries;
1724
Ville Syrjälä922044c2014-02-14 14:18:57 +02001725 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001726
1727 /* Use ns/us then divide to preserve precision */
1728 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001729 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001730 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1731 srwm = I965_FIFO_SIZE - entries;
1732 if (srwm < 0)
1733 srwm = 1;
1734 srwm &= 0x1ff;
1735 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1736 entries, srwm);
1737
1738 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001739 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001740 entries = DIV_ROUND_UP(entries,
1741 i965_cursor_wm_info.cacheline_size);
1742 cursor_sr = i965_cursor_wm_info.fifo_size -
1743 (entries + i965_cursor_wm_info.guard_size);
1744
1745 if (cursor_sr > i965_cursor_wm_info.max_wm)
1746 cursor_sr = i965_cursor_wm_info.max_wm;
1747
1748 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1749 "cursor %d\n", srwm, cursor_sr);
1750
Imre Deak98584252014-06-13 14:54:20 +03001751 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001752 } else {
Imre Deak98584252014-06-13 14:54:20 +03001753 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001754 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001755 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001756 }
1757
1758 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1759 srwm);
1760
1761 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001762 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1763 FW_WM(8, CURSORB) |
1764 FW_WM(8, PLANEB) |
1765 FW_WM(8, PLANEA));
1766 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1767 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001768 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001769 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001770
1771 if (cxsr_enabled)
1772 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001773}
1774
Ville Syrjäläf4998962015-03-10 17:02:21 +02001775#undef FW_WM
1776
Ville Syrjälä432081b2016-10-31 22:37:03 +02001777static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001778{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001779 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001780 const struct intel_watermark_params *wm_info;
1781 uint32_t fwater_lo;
1782 uint32_t fwater_hi;
1783 int cwm, srwm = 1;
1784 int fifo_size;
1785 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001786 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001787
Ville Syrjäläa9097be2016-10-31 22:37:20 +02001788 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001789 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001790 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001791 wm_info = &i915_wm_info;
1792 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001793 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001794
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001795 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001796 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001797 if (intel_crtc_active(crtc)) {
1798 const struct drm_display_mode *adjusted_mode =
1799 &crtc->config->base.adjusted_mode;
1800 const struct drm_framebuffer *fb =
1801 crtc->base.primary->state->fb;
1802 int cpp;
1803
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001804 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001805 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001806 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001807 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001808
Damien Lespiau241bfc32013-09-25 16:45:37 +01001809 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001810 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001811 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001812 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001813 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001814 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001815 if (planea_wm > (long)wm_info->max_wm)
1816 planea_wm = wm_info->max_wm;
1817 }
1818
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001819 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001820 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001821
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001822 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001823 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001824 if (intel_crtc_active(crtc)) {
1825 const struct drm_display_mode *adjusted_mode =
1826 &crtc->config->base.adjusted_mode;
1827 const struct drm_framebuffer *fb =
1828 crtc->base.primary->state->fb;
1829 int cpp;
1830
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001831 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001832 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001833 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001834 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001835
Damien Lespiau241bfc32013-09-25 16:45:37 +01001836 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001837 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001838 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001839 if (enabled == NULL)
1840 enabled = crtc;
1841 else
1842 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001843 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001844 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001845 if (planeb_wm > (long)wm_info->max_wm)
1846 planeb_wm = wm_info->max_wm;
1847 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001848
1849 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1850
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001851 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001852 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001853
Ville Syrjäläefc26112016-10-31 22:37:04 +02001854 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001855
1856 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001857 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001858 enabled = NULL;
1859 }
1860
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001861 /*
1862 * Overlay gets an aggressive default since video jitter is bad.
1863 */
1864 cwm = 2;
1865
1866 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001867 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001868
1869 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02001870 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001871 /* self-refresh has much higher latency */
1872 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001873 const struct drm_display_mode *adjusted_mode =
1874 &enabled->config->base.adjusted_mode;
1875 const struct drm_framebuffer *fb =
1876 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001877 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001878 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001879 int hdisplay = enabled->config->pipe_src_w;
1880 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001881 unsigned long line_time_us;
1882 int entries;
1883
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001884 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001885 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001886 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001887 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001888
Ville Syrjälä922044c2014-02-14 14:18:57 +02001889 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001890
1891 /* Use ns/us then divide to preserve precision */
1892 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001893 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001894 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1895 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1896 srwm = wm_info->fifo_size - entries;
1897 if (srwm < 0)
1898 srwm = 1;
1899
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001900 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001901 I915_WRITE(FW_BLC_SELF,
1902 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001903 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001904 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1905 }
1906
1907 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1908 planea_wm, planeb_wm, cwm, srwm);
1909
1910 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1911 fwater_hi = (cwm & 0x1f);
1912
1913 /* Set request length to 8 cachelines per fetch */
1914 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1915 fwater_hi = fwater_hi | (1 << 8);
1916
1917 I915_WRITE(FW_BLC, fwater_lo);
1918 I915_WRITE(FW_BLC2, fwater_hi);
1919
Imre Deak5209b1f2014-07-01 12:36:17 +03001920 if (enabled)
1921 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001922}
1923
Ville Syrjälä432081b2016-10-31 22:37:03 +02001924static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001925{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001926 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001927 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001928 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001929 uint32_t fwater_lo;
1930 int planea_wm;
1931
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001932 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001933 if (crtc == NULL)
1934 return;
1935
Ville Syrjäläefc26112016-10-31 22:37:04 +02001936 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001937 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001938 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001939 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001940 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001941 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1942 fwater_lo |= (3<<8) | planea_wm;
1943
1944 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1945
1946 I915_WRITE(FW_BLC, fwater_lo);
1947}
1948
Ville Syrjälä37126462013-08-01 16:18:55 +03001949/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001950static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001951{
1952 uint64_t ret;
1953
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001954 if (WARN(latency == 0, "Latency value missing\n"))
1955 return UINT_MAX;
1956
Ville Syrjäläac484962016-01-20 21:05:26 +02001957 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001958 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1959
1960 return ret;
1961}
1962
Ville Syrjälä37126462013-08-01 16:18:55 +03001963/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001964static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001965 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001966 uint32_t latency)
1967{
1968 uint32_t ret;
1969
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001970 if (WARN(latency == 0, "Latency value missing\n"))
1971 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001972 if (WARN_ON(!pipe_htotal))
1973 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001974
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001975 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001976 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001977 ret = DIV_ROUND_UP(ret, 64) + 2;
1978 return ret;
1979}
1980
Ville Syrjälä23297042013-07-05 11:57:17 +03001981static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001982 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001983{
Matt Roper15126882015-12-03 11:37:40 -08001984 /*
1985 * Neither of these should be possible since this function shouldn't be
1986 * called if the CRTC is off or the plane is invisible. But let's be
1987 * extra paranoid to avoid a potential divide-by-zero if we screw up
1988 * elsewhere in the driver.
1989 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001990 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001991 return 0;
1992 if (WARN_ON(!horiz_pixels))
1993 return 0;
1994
Ville Syrjäläac484962016-01-20 21:05:26 +02001995 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001996}
1997
Imre Deak820c1982013-12-17 14:46:36 +02001998struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001999 uint16_t pri;
2000 uint16_t spr;
2001 uint16_t cur;
2002 uint16_t fbc;
2003};
2004
Ville Syrjälä37126462013-08-01 16:18:55 +03002005/*
2006 * For both WM_PIPE and WM_LP.
2007 * mem_value must be in 0.1us units.
2008 */
Matt Roper7221fc32015-09-24 15:53:08 -07002009static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002010 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002011 uint32_t mem_value,
2012 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002013{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002014 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002015 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002016
Ville Syrjälä24304d812017-03-14 17:10:49 +02002017 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002018 return 0;
2019
Ville Syrjälä353c8592016-12-14 23:30:57 +02002020 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002021
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002022 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002023
2024 if (!is_lp)
2025 return method1;
2026
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002027 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002028 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002029 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002030 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002031
2032 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002033}
2034
Ville Syrjälä37126462013-08-01 16:18:55 +03002035/*
2036 * For both WM_PIPE and WM_LP.
2037 * mem_value must be in 0.1us units.
2038 */
Matt Roper7221fc32015-09-24 15:53:08 -07002039static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002040 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002041 uint32_t mem_value)
2042{
2043 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002044 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002045
Ville Syrjälä24304d812017-03-14 17:10:49 +02002046 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002047 return 0;
2048
Ville Syrjälä353c8592016-12-14 23:30:57 +02002049 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002050
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002051 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2052 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002053 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002054 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002055 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002056 return min(method1, method2);
2057}
2058
Ville Syrjälä37126462013-08-01 16:18:55 +03002059/*
2060 * For both WM_PIPE and WM_LP.
2061 * mem_value must be in 0.1us units.
2062 */
Matt Roper7221fc32015-09-24 15:53:08 -07002063static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002064 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002065 uint32_t mem_value)
2066{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002067 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002068
Ville Syrjälä24304d812017-03-14 17:10:49 +02002069 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002070 return 0;
2071
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002072 cpp = pstate->base.fb->format->cpp[0];
2073
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002074 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002075 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002076 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002077}
2078
Paulo Zanonicca32e92013-05-31 11:45:06 -03002079/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002080static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002081 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002082 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002083{
Ville Syrjälä83054942016-11-18 21:53:00 +02002084 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002085
Ville Syrjälä24304d812017-03-14 17:10:49 +02002086 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002087 return 0;
2088
Ville Syrjälä353c8592016-12-14 23:30:57 +02002089 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002090
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002091 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002092}
2093
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002094static unsigned int
2095ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002096{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002097 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002098 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002099 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002100 return 768;
2101 else
2102 return 512;
2103}
2104
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002105static unsigned int
2106ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2107 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002108{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002109 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002110 /* BDW primary/sprite plane watermarks */
2111 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002112 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002113 /* IVB/HSW primary/sprite plane watermarks */
2114 return level == 0 ? 127 : 1023;
2115 else if (!is_sprite)
2116 /* ILK/SNB primary plane watermarks */
2117 return level == 0 ? 127 : 511;
2118 else
2119 /* ILK/SNB sprite plane watermarks */
2120 return level == 0 ? 63 : 255;
2121}
2122
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002123static unsigned int
2124ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002125{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002126 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002127 return level == 0 ? 63 : 255;
2128 else
2129 return level == 0 ? 31 : 63;
2130}
2131
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002132static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002133{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002134 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002135 return 31;
2136 else
2137 return 15;
2138}
2139
Ville Syrjälä158ae642013-08-07 13:28:19 +03002140/* Calculate the maximum primary/sprite plane watermark */
2141static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2142 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002143 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002144 enum intel_ddb_partitioning ddb_partitioning,
2145 bool is_sprite)
2146{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002147 struct drm_i915_private *dev_priv = to_i915(dev);
2148 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002149
2150 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002151 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002152 return 0;
2153
2154 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002155 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002156 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002157
2158 /*
2159 * For some reason the non self refresh
2160 * FIFO size is only half of the self
2161 * refresh FIFO size on ILK/SNB.
2162 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002163 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002164 fifo_size /= 2;
2165 }
2166
Ville Syrjälä240264f2013-08-07 13:29:12 +03002167 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002168 /* level 0 is always calculated with 1:1 split */
2169 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2170 if (is_sprite)
2171 fifo_size *= 5;
2172 fifo_size /= 6;
2173 } else {
2174 fifo_size /= 2;
2175 }
2176 }
2177
2178 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002179 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002180}
2181
2182/* Calculate the maximum cursor plane watermark */
2183static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002184 int level,
2185 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002186{
2187 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002188 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002189 return 64;
2190
2191 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002192 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002193}
2194
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002195static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002196 int level,
2197 const struct intel_wm_config *config,
2198 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002199 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002200{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002201 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2202 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2203 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002204 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002205}
2206
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002207static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002208 int level,
2209 struct ilk_wm_maximums *max)
2210{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002211 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2212 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2213 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2214 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002215}
2216
Ville Syrjäläd9395652013-10-09 19:18:10 +03002217static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002218 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002219 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002220{
2221 bool ret;
2222
2223 /* already determined to be invalid? */
2224 if (!result->enable)
2225 return false;
2226
2227 result->enable = result->pri_val <= max->pri &&
2228 result->spr_val <= max->spr &&
2229 result->cur_val <= max->cur;
2230
2231 ret = result->enable;
2232
2233 /*
2234 * HACK until we can pre-compute everything,
2235 * and thus fail gracefully if LP0 watermarks
2236 * are exceeded...
2237 */
2238 if (level == 0 && !result->enable) {
2239 if (result->pri_val > max->pri)
2240 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2241 level, result->pri_val, max->pri);
2242 if (result->spr_val > max->spr)
2243 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2244 level, result->spr_val, max->spr);
2245 if (result->cur_val > max->cur)
2246 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2247 level, result->cur_val, max->cur);
2248
2249 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2250 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2251 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2252 result->enable = true;
2253 }
2254
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002255 return ret;
2256}
2257
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002258static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002259 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002260 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002261 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002262 struct intel_plane_state *pristate,
2263 struct intel_plane_state *sprstate,
2264 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002265 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002266{
2267 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2268 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2269 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2270
2271 /* WM1+ latency values stored in 0.5us units */
2272 if (level > 0) {
2273 pri_latency *= 5;
2274 spr_latency *= 5;
2275 cur_latency *= 5;
2276 }
2277
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002278 if (pristate) {
2279 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2280 pri_latency, level);
2281 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2282 }
2283
2284 if (sprstate)
2285 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2286
2287 if (curstate)
2288 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2289
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002290 result->enable = true;
2291}
2292
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002293static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002294hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002295{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002296 const struct intel_atomic_state *intel_state =
2297 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002298 const struct drm_display_mode *adjusted_mode =
2299 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002300 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002301
Matt Roperee91a152015-12-03 11:37:39 -08002302 if (!cstate->base.active)
2303 return 0;
2304 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2305 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002306 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002307 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002308
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002309 /* The WM are computed with base on how long it takes to fill a single
2310 * row at the given clock rate, multiplied by 8.
2311 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002312 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2313 adjusted_mode->crtc_clock);
2314 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002315 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002316
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002317 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2318 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002319}
2320
Ville Syrjäläbb726512016-10-31 22:37:24 +02002321static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2322 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002323{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002324 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002325 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002326 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002327 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002328
2329 /* read the first set of memory latencies[0:3] */
2330 val = 0; /* data0 to be programmed to 0 for first set */
2331 mutex_lock(&dev_priv->rps.hw_lock);
2332 ret = sandybridge_pcode_read(dev_priv,
2333 GEN9_PCODE_READ_MEM_LATENCY,
2334 &val);
2335 mutex_unlock(&dev_priv->rps.hw_lock);
2336
2337 if (ret) {
2338 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2339 return;
2340 }
2341
2342 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2343 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2344 GEN9_MEM_LATENCY_LEVEL_MASK;
2345 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2346 GEN9_MEM_LATENCY_LEVEL_MASK;
2347 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2348 GEN9_MEM_LATENCY_LEVEL_MASK;
2349
2350 /* read the second set of memory latencies[4:7] */
2351 val = 1; /* data0 to be programmed to 1 for second set */
2352 mutex_lock(&dev_priv->rps.hw_lock);
2353 ret = sandybridge_pcode_read(dev_priv,
2354 GEN9_PCODE_READ_MEM_LATENCY,
2355 &val);
2356 mutex_unlock(&dev_priv->rps.hw_lock);
2357 if (ret) {
2358 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2359 return;
2360 }
2361
2362 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2363 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2364 GEN9_MEM_LATENCY_LEVEL_MASK;
2365 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2366 GEN9_MEM_LATENCY_LEVEL_MASK;
2367 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2368 GEN9_MEM_LATENCY_LEVEL_MASK;
2369
Vandana Kannan367294b2014-11-04 17:06:46 +00002370 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002371 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2372 * need to be disabled. We make sure to sanitize the values out
2373 * of the punit to satisfy this requirement.
2374 */
2375 for (level = 1; level <= max_level; level++) {
2376 if (wm[level] == 0) {
2377 for (i = level + 1; i <= max_level; i++)
2378 wm[i] = 0;
2379 break;
2380 }
2381 }
2382
2383 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002384 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002385 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002386 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002387 * to add 2us to the various latency levels we retrieve from the
2388 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002389 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002390 if (wm[0] == 0) {
2391 wm[0] += 2;
2392 for (level = 1; level <= max_level; level++) {
2393 if (wm[level] == 0)
2394 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002395 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002396 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002397 }
2398
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002399 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002400 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2401
2402 wm[0] = (sskpd >> 56) & 0xFF;
2403 if (wm[0] == 0)
2404 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002405 wm[1] = (sskpd >> 4) & 0xFF;
2406 wm[2] = (sskpd >> 12) & 0xFF;
2407 wm[3] = (sskpd >> 20) & 0x1FF;
2408 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002409 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002410 uint32_t sskpd = I915_READ(MCH_SSKPD);
2411
2412 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2413 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2414 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2415 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002416 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002417 uint32_t mltr = I915_READ(MLTR_ILK);
2418
2419 /* ILK primary LP0 latency is 700 ns */
2420 wm[0] = 7;
2421 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2422 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002423 }
2424}
2425
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002426static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2427 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002428{
2429 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002430 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002431 wm[0] = 13;
2432}
2433
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002434static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2435 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002436{
2437 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002438 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002439 wm[0] = 13;
2440
2441 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002442 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002443 wm[3] *= 2;
2444}
2445
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002446int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002447{
2448 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002449 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002450 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002451 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002452 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002453 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002454 return 3;
2455 else
2456 return 2;
2457}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002458
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002459static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002460 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002461 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002462{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002463 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002464
2465 for (level = 0; level <= max_level; level++) {
2466 unsigned int latency = wm[level];
2467
2468 if (latency == 0) {
2469 DRM_ERROR("%s WM%d latency not provided\n",
2470 name, level);
2471 continue;
2472 }
2473
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002474 /*
2475 * - latencies are in us on gen9.
2476 * - before then, WM1+ latency values are in 0.5us units
2477 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002478 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002479 latency *= 10;
2480 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002481 latency *= 5;
2482
2483 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2484 name, level, wm[level],
2485 latency / 10, latency % 10);
2486 }
2487}
2488
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002489static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2490 uint16_t wm[5], uint16_t min)
2491{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002492 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002493
2494 if (wm[0] >= min)
2495 return false;
2496
2497 wm[0] = max(wm[0], min);
2498 for (level = 1; level <= max_level; level++)
2499 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2500
2501 return true;
2502}
2503
Ville Syrjäläbb726512016-10-31 22:37:24 +02002504static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002505{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002506 bool changed;
2507
2508 /*
2509 * The BIOS provided WM memory latency values are often
2510 * inadequate for high resolution displays. Adjust them.
2511 */
2512 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2513 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2514 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2515
2516 if (!changed)
2517 return;
2518
2519 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002520 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2521 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2522 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002523}
2524
Ville Syrjäläbb726512016-10-31 22:37:24 +02002525static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002526{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002527 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002528
2529 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2530 sizeof(dev_priv->wm.pri_latency));
2531 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2532 sizeof(dev_priv->wm.pri_latency));
2533
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002534 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002535 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002536
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002537 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2538 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2539 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002540
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002541 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002542 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002543}
2544
Ville Syrjäläbb726512016-10-31 22:37:24 +02002545static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002546{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002547 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002548 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002549}
2550
Matt Ropered4a6a72016-02-23 17:20:13 -08002551static bool ilk_validate_pipe_wm(struct drm_device *dev,
2552 struct intel_pipe_wm *pipe_wm)
2553{
2554 /* LP0 watermark maximums depend on this pipe alone */
2555 const struct intel_wm_config config = {
2556 .num_pipes_active = 1,
2557 .sprites_enabled = pipe_wm->sprites_enabled,
2558 .sprites_scaled = pipe_wm->sprites_scaled,
2559 };
2560 struct ilk_wm_maximums max;
2561
2562 /* LP0 watermarks always use 1/2 DDB partitioning */
2563 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2564
2565 /* At least LP0 must be valid */
2566 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2567 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2568 return false;
2569 }
2570
2571 return true;
2572}
2573
Matt Roper261a27d2015-10-08 15:28:25 -07002574/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002575static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002576{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002577 struct drm_atomic_state *state = cstate->base.state;
2578 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002579 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002580 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002581 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002582 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002583 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002584 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002585 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002586 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002587 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002588
Matt Ropere8f1f022016-05-12 07:05:55 -07002589 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002590
Matt Roper43d59ed2015-09-24 15:53:07 -07002591 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002592 struct intel_plane_state *ps;
2593
2594 ps = intel_atomic_get_existing_plane_state(state,
2595 intel_plane);
2596 if (!ps)
2597 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002598
2599 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002600 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002601 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002602 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002603 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002604 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002605 }
2606
Matt Ropered4a6a72016-02-23 17:20:13 -08002607 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002608 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002609 pipe_wm->sprites_enabled = sprstate->base.visible;
2610 pipe_wm->sprites_scaled = sprstate->base.visible &&
2611 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2612 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002613 }
2614
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002615 usable_level = max_level;
2616
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002617 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002618 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002619 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002620
2621 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002622 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002623 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002624
Matt Roper86c8bbb2015-09-24 15:53:16 -07002625 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002626 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2627
2628 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2629 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002630
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002631 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002632 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002633
Matt Ropered4a6a72016-02-23 17:20:13 -08002634 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002635 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002636
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002637 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002638
2639 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002640 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002641
Matt Roper86c8bbb2015-09-24 15:53:16 -07002642 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002643 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002644
2645 /*
2646 * Disable any watermark level that exceeds the
2647 * register maximums since such watermarks are
2648 * always invalid.
2649 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002650 if (level > usable_level)
2651 continue;
2652
2653 if (ilk_validate_wm_level(level, &max, wm))
2654 pipe_wm->wm[level] = *wm;
2655 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002656 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002657 }
2658
Matt Roper86c8bbb2015-09-24 15:53:16 -07002659 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002660}
2661
2662/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002663 * Build a set of 'intermediate' watermark values that satisfy both the old
2664 * state and the new state. These can be programmed to the hardware
2665 * immediately.
2666 */
2667static int ilk_compute_intermediate_wm(struct drm_device *dev,
2668 struct intel_crtc *intel_crtc,
2669 struct intel_crtc_state *newstate)
2670{
Matt Ropere8f1f022016-05-12 07:05:55 -07002671 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002672 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002673 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002674
2675 /*
2676 * Start with the final, target watermarks, then combine with the
2677 * currently active watermarks to get values that are safe both before
2678 * and after the vblank.
2679 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002680 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002681 a->pipe_enabled |= b->pipe_enabled;
2682 a->sprites_enabled |= b->sprites_enabled;
2683 a->sprites_scaled |= b->sprites_scaled;
2684
2685 for (level = 0; level <= max_level; level++) {
2686 struct intel_wm_level *a_wm = &a->wm[level];
2687 const struct intel_wm_level *b_wm = &b->wm[level];
2688
2689 a_wm->enable &= b_wm->enable;
2690 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2691 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2692 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2693 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2694 }
2695
2696 /*
2697 * We need to make sure that these merged watermark values are
2698 * actually a valid configuration themselves. If they're not,
2699 * there's no safe way to transition from the old state to
2700 * the new state, so we need to fail the atomic transaction.
2701 */
2702 if (!ilk_validate_pipe_wm(dev, a))
2703 return -EINVAL;
2704
2705 /*
2706 * If our intermediate WM are identical to the final WM, then we can
2707 * omit the post-vblank programming; only update if it's different.
2708 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002709 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
2710 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08002711
2712 return 0;
2713}
2714
2715/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002716 * Merge the watermarks from all active pipes for a specific level.
2717 */
2718static void ilk_merge_wm_level(struct drm_device *dev,
2719 int level,
2720 struct intel_wm_level *ret_wm)
2721{
2722 const struct intel_crtc *intel_crtc;
2723
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002724 ret_wm->enable = true;
2725
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002726 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002727 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002728 const struct intel_wm_level *wm = &active->wm[level];
2729
2730 if (!active->pipe_enabled)
2731 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002732
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002733 /*
2734 * The watermark values may have been used in the past,
2735 * so we must maintain them in the registers for some
2736 * time even if the level is now disabled.
2737 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002738 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002739 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002740
2741 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2742 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2743 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2744 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2745 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002746}
2747
2748/*
2749 * Merge all low power watermarks for all active pipes.
2750 */
2751static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002752 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002753 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002754 struct intel_pipe_wm *merged)
2755{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002756 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002757 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002758 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002759
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002760 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002761 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002762 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002763 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002764
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002765 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002766 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002767
2768 /* merge each WM1+ level */
2769 for (level = 1; level <= max_level; level++) {
2770 struct intel_wm_level *wm = &merged->wm[level];
2771
2772 ilk_merge_wm_level(dev, level, wm);
2773
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002774 if (level > last_enabled_level)
2775 wm->enable = false;
2776 else if (!ilk_validate_wm_level(level, max, wm))
2777 /* make sure all following levels get disabled */
2778 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002779
2780 /*
2781 * The spec says it is preferred to disable
2782 * FBC WMs instead of disabling a WM level.
2783 */
2784 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002785 if (wm->enable)
2786 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002787 wm->fbc_val = 0;
2788 }
2789 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002790
2791 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2792 /*
2793 * FIXME this is racy. FBC might get enabled later.
2794 * What we should check here is whether FBC can be
2795 * enabled sometime later.
2796 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002797 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002798 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002799 for (level = 2; level <= max_level; level++) {
2800 struct intel_wm_level *wm = &merged->wm[level];
2801
2802 wm->enable = false;
2803 }
2804 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002805}
2806
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002807static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2808{
2809 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2810 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2811}
2812
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002813/* The value we need to program into the WM_LPx latency field */
2814static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2815{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002816 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002817
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002818 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002819 return 2 * level;
2820 else
2821 return dev_priv->wm.pri_latency[level];
2822}
2823
Imre Deak820c1982013-12-17 14:46:36 +02002824static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002825 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002826 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002827 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002828{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002829 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002830 struct intel_crtc *intel_crtc;
2831 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002832
Ville Syrjälä0362c782013-10-09 19:17:57 +03002833 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002834 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002835
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002836 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002837 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002838 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002839
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002840 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002841
Ville Syrjälä0362c782013-10-09 19:17:57 +03002842 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002843
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002844 /*
2845 * Maintain the watermark values even if the level is
2846 * disabled. Doing otherwise could cause underruns.
2847 */
2848 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002849 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002850 (r->pri_val << WM1_LP_SR_SHIFT) |
2851 r->cur_val;
2852
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002853 if (r->enable)
2854 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2855
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002856 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002857 results->wm_lp[wm_lp - 1] |=
2858 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2859 else
2860 results->wm_lp[wm_lp - 1] |=
2861 r->fbc_val << WM1_LP_FBC_SHIFT;
2862
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002863 /*
2864 * Always set WM1S_LP_EN when spr_val != 0, even if the
2865 * level is disabled. Doing otherwise could cause underruns.
2866 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002867 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002868 WARN_ON(wm_lp != 1);
2869 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2870 } else
2871 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002872 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002873
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002874 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002875 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002876 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002877 const struct intel_wm_level *r =
2878 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002879
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002880 if (WARN_ON(!r->enable))
2881 continue;
2882
Matt Ropered4a6a72016-02-23 17:20:13 -08002883 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002884
2885 results->wm_pipe[pipe] =
2886 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2887 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2888 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002889 }
2890}
2891
Paulo Zanoni861f3382013-05-31 10:19:21 -03002892/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2893 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002894static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002895 struct intel_pipe_wm *r1,
2896 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002897{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002898 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002899 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002900
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002901 for (level = 1; level <= max_level; level++) {
2902 if (r1->wm[level].enable)
2903 level1 = level;
2904 if (r2->wm[level].enable)
2905 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002906 }
2907
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002908 if (level1 == level2) {
2909 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002910 return r2;
2911 else
2912 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002913 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002914 return r1;
2915 } else {
2916 return r2;
2917 }
2918}
2919
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002920/* dirty bits used to track which watermarks need changes */
2921#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2922#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2923#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2924#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2925#define WM_DIRTY_FBC (1 << 24)
2926#define WM_DIRTY_DDB (1 << 25)
2927
Damien Lespiau055e3932014-08-18 13:49:10 +01002928static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002929 const struct ilk_wm_values *old,
2930 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002931{
2932 unsigned int dirty = 0;
2933 enum pipe pipe;
2934 int wm_lp;
2935
Damien Lespiau055e3932014-08-18 13:49:10 +01002936 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002937 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2938 dirty |= WM_DIRTY_LINETIME(pipe);
2939 /* Must disable LP1+ watermarks too */
2940 dirty |= WM_DIRTY_LP_ALL;
2941 }
2942
2943 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2944 dirty |= WM_DIRTY_PIPE(pipe);
2945 /* Must disable LP1+ watermarks too */
2946 dirty |= WM_DIRTY_LP_ALL;
2947 }
2948 }
2949
2950 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2951 dirty |= WM_DIRTY_FBC;
2952 /* Must disable LP1+ watermarks too */
2953 dirty |= WM_DIRTY_LP_ALL;
2954 }
2955
2956 if (old->partitioning != new->partitioning) {
2957 dirty |= WM_DIRTY_DDB;
2958 /* Must disable LP1+ watermarks too */
2959 dirty |= WM_DIRTY_LP_ALL;
2960 }
2961
2962 /* LP1+ watermarks already deemed dirty, no need to continue */
2963 if (dirty & WM_DIRTY_LP_ALL)
2964 return dirty;
2965
2966 /* Find the lowest numbered LP1+ watermark in need of an update... */
2967 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2968 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2969 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2970 break;
2971 }
2972
2973 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2974 for (; wm_lp <= 3; wm_lp++)
2975 dirty |= WM_DIRTY_LP(wm_lp);
2976
2977 return dirty;
2978}
2979
Ville Syrjälä8553c182013-12-05 15:51:39 +02002980static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2981 unsigned int dirty)
2982{
Imre Deak820c1982013-12-17 14:46:36 +02002983 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002984 bool changed = false;
2985
2986 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2987 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2988 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2989 changed = true;
2990 }
2991 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2992 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2993 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2994 changed = true;
2995 }
2996 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2997 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2998 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2999 changed = true;
3000 }
3001
3002 /*
3003 * Don't touch WM1S_LP_EN here.
3004 * Doing so could cause underruns.
3005 */
3006
3007 return changed;
3008}
3009
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003010/*
3011 * The spec says we shouldn't write when we don't need, because every write
3012 * causes WMs to be re-evaluated, expending some power.
3013 */
Imre Deak820c1982013-12-17 14:46:36 +02003014static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3015 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003016{
Imre Deak820c1982013-12-17 14:46:36 +02003017 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003018 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003019 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003020
Damien Lespiau055e3932014-08-18 13:49:10 +01003021 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003022 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003023 return;
3024
Ville Syrjälä8553c182013-12-05 15:51:39 +02003025 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003026
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003027 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003028 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003029 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003030 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003031 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003032 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3033
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003034 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003035 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003036 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003037 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003038 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003039 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3040
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003041 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003042 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003043 val = I915_READ(WM_MISC);
3044 if (results->partitioning == INTEL_DDB_PART_1_2)
3045 val &= ~WM_MISC_DATA_PARTITION_5_6;
3046 else
3047 val |= WM_MISC_DATA_PARTITION_5_6;
3048 I915_WRITE(WM_MISC, val);
3049 } else {
3050 val = I915_READ(DISP_ARB_CTL2);
3051 if (results->partitioning == INTEL_DDB_PART_1_2)
3052 val &= ~DISP_DATA_PARTITION_5_6;
3053 else
3054 val |= DISP_DATA_PARTITION_5_6;
3055 I915_WRITE(DISP_ARB_CTL2, val);
3056 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003057 }
3058
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003059 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003060 val = I915_READ(DISP_ARB_CTL);
3061 if (results->enable_fbc_wm)
3062 val &= ~DISP_FBC_WM_DIS;
3063 else
3064 val |= DISP_FBC_WM_DIS;
3065 I915_WRITE(DISP_ARB_CTL, val);
3066 }
3067
Imre Deak954911e2013-12-17 14:46:34 +02003068 if (dirty & WM_DIRTY_LP(1) &&
3069 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3070 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3071
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003072 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003073 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3074 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3075 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3076 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3077 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003078
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003079 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003080 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003081 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003082 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003083 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003084 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003085
3086 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003087}
3088
Matt Ropered4a6a72016-02-23 17:20:13 -08003089bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003090{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003091 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003092
3093 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3094}
3095
Lyude656d1b82016-08-17 15:55:54 -04003096#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003097
Matt Roper024c9042015-09-24 15:53:11 -07003098/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003099 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3100 * so assume we'll always need it in order to avoid underruns.
3101 */
3102static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3103{
3104 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3105
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003106 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003107 return true;
3108
3109 return false;
3110}
3111
Paulo Zanoni56feca92016-09-22 18:00:28 -03003112static bool
3113intel_has_sagv(struct drm_i915_private *dev_priv)
3114{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003115 if (IS_KABYLAKE(dev_priv))
3116 return true;
3117
3118 if (IS_SKYLAKE(dev_priv) &&
3119 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3120 return true;
3121
3122 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003123}
3124
Lyude656d1b82016-08-17 15:55:54 -04003125/*
3126 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3127 * depending on power and performance requirements. The display engine access
3128 * to system memory is blocked during the adjustment time. Because of the
3129 * blocking time, having this enabled can cause full system hangs and/or pipe
3130 * underruns if we don't meet all of the following requirements:
3131 *
3132 * - <= 1 pipe enabled
3133 * - All planes can enable watermarks for latencies >= SAGV engine block time
3134 * - We're not using an interlaced display configuration
3135 */
3136int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003137intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003138{
3139 int ret;
3140
Paulo Zanoni56feca92016-09-22 18:00:28 -03003141 if (!intel_has_sagv(dev_priv))
3142 return 0;
3143
3144 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003145 return 0;
3146
3147 DRM_DEBUG_KMS("Enabling the SAGV\n");
3148 mutex_lock(&dev_priv->rps.hw_lock);
3149
3150 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3151 GEN9_SAGV_ENABLE);
3152
3153 /* We don't need to wait for the SAGV when enabling */
3154 mutex_unlock(&dev_priv->rps.hw_lock);
3155
3156 /*
3157 * Some skl systems, pre-release machines in particular,
3158 * don't actually have an SAGV.
3159 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003160 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003161 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003162 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003163 return 0;
3164 } else if (ret < 0) {
3165 DRM_ERROR("Failed to enable the SAGV\n");
3166 return ret;
3167 }
3168
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003169 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003170 return 0;
3171}
3172
Lyude656d1b82016-08-17 15:55:54 -04003173int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003174intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003175{
Imre Deakb3b8e992016-12-05 18:27:38 +02003176 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003177
Paulo Zanoni56feca92016-09-22 18:00:28 -03003178 if (!intel_has_sagv(dev_priv))
3179 return 0;
3180
3181 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003182 return 0;
3183
3184 DRM_DEBUG_KMS("Disabling the SAGV\n");
3185 mutex_lock(&dev_priv->rps.hw_lock);
3186
3187 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003188 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3189 GEN9_SAGV_DISABLE,
3190 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3191 1);
Lyude656d1b82016-08-17 15:55:54 -04003192 mutex_unlock(&dev_priv->rps.hw_lock);
3193
Lyude656d1b82016-08-17 15:55:54 -04003194 /*
3195 * Some skl systems, pre-release machines in particular,
3196 * don't actually have an SAGV.
3197 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003198 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003199 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003200 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003201 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003202 } else if (ret < 0) {
3203 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3204 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003205 }
3206
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003207 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003208 return 0;
3209}
3210
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003211bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003212{
3213 struct drm_device *dev = state->dev;
3214 struct drm_i915_private *dev_priv = to_i915(dev);
3215 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003216 struct intel_crtc *crtc;
3217 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003218 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003219 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003220 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003221
Paulo Zanoni56feca92016-09-22 18:00:28 -03003222 if (!intel_has_sagv(dev_priv))
3223 return false;
3224
Lyude656d1b82016-08-17 15:55:54 -04003225 /*
3226 * SKL workaround: bspec recommends we disable the SAGV when we have
3227 * more then one pipe enabled
3228 *
3229 * If there are no active CRTCs, no additional checks need be performed
3230 */
3231 if (hweight32(intel_state->active_crtcs) == 0)
3232 return true;
3233 else if (hweight32(intel_state->active_crtcs) > 1)
3234 return false;
3235
3236 /* Since we're now guaranteed to only have one active CRTC... */
3237 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003238 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003239 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003240
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003241 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003242 return false;
3243
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003244 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003245 struct skl_plane_wm *wm =
3246 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003247
Lyude656d1b82016-08-17 15:55:54 -04003248 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003249 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003250 continue;
3251
3252 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003253 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003254 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003255 { }
3256
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003257 latency = dev_priv->wm.skl_latency[level];
3258
3259 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003260 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003261 I915_FORMAT_MOD_X_TILED)
3262 latency += 15;
3263
Lyude656d1b82016-08-17 15:55:54 -04003264 /*
3265 * If any of the planes on this pipe don't enable wm levels
3266 * that incur memory latencies higher then 30µs we can't enable
3267 * the SAGV
3268 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003269 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003270 return false;
3271 }
3272
3273 return true;
3274}
3275
Damien Lespiaub9cec072014-11-04 17:06:43 +00003276static void
3277skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003278 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003279 struct skl_ddb_entry *alloc, /* out */
3280 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003281{
Matt Roperc107acf2016-05-12 07:06:01 -07003282 struct drm_atomic_state *state = cstate->base.state;
3283 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3284 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003285 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003286 unsigned int pipe_size, ddb_size;
3287 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003288
Matt Ropera6d3460e2016-05-12 07:06:04 -07003289 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003290 alloc->start = 0;
3291 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003292 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003293 return;
3294 }
3295
Matt Ropera6d3460e2016-05-12 07:06:04 -07003296 if (intel_state->active_pipe_changes)
3297 *num_active = hweight32(intel_state->active_crtcs);
3298 else
3299 *num_active = hweight32(dev_priv->active_crtcs);
3300
Deepak M6f3fff62016-09-15 15:01:10 +05303301 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3302 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003303
3304 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3305
Matt Roperc107acf2016-05-12 07:06:01 -07003306 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003307 * If the state doesn't change the active CRTC's, then there's
3308 * no need to recalculate; the existing pipe allocation limits
3309 * should remain unchanged. Note that we're safe from racing
3310 * commits since any racing commit that changes the active CRTC
3311 * list would need to grab _all_ crtc locks, including the one
3312 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003313 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003314 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003315 /*
3316 * alloc may be cleared by clear_intel_crtc_state,
3317 * copy from old state to be sure
3318 */
3319 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003320 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003321 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003322
3323 nth_active_pipe = hweight32(intel_state->active_crtcs &
3324 (drm_crtc_mask(for_crtc) - 1));
3325 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3326 alloc->start = nth_active_pipe * ddb_size / *num_active;
3327 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003328}
3329
Matt Roperc107acf2016-05-12 07:06:01 -07003330static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003331{
Matt Roperc107acf2016-05-12 07:06:01 -07003332 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003333 return 32;
3334
3335 return 8;
3336}
3337
Damien Lespiaua269c582014-11-04 17:06:49 +00003338static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3339{
3340 entry->start = reg & 0x3ff;
3341 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003342 if (entry->end)
3343 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003344}
3345
Damien Lespiau08db6652014-11-04 17:06:52 +00003346void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3347 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003348{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003349 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003350
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003351 memset(ddb, 0, sizeof(*ddb));
3352
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003353 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003354 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003355 enum plane_id plane_id;
3356 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003357
3358 power_domain = POWER_DOMAIN_PIPE(pipe);
3359 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003360 continue;
3361
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003362 for_each_plane_id_on_crtc(crtc, plane_id) {
3363 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003364
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003365 if (plane_id != PLANE_CURSOR)
3366 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3367 else
3368 val = I915_READ(CUR_BUF_CFG(pipe));
3369
3370 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3371 }
Imre Deak4d800032016-02-17 16:31:29 +02003372
3373 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003374 }
3375}
3376
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003377/*
3378 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3379 * The bspec defines downscale amount as:
3380 *
3381 * """
3382 * Horizontal down scale amount = maximum[1, Horizontal source size /
3383 * Horizontal destination size]
3384 * Vertical down scale amount = maximum[1, Vertical source size /
3385 * Vertical destination size]
3386 * Total down scale amount = Horizontal down scale amount *
3387 * Vertical down scale amount
3388 * """
3389 *
3390 * Return value is provided in 16.16 fixed point form to retain fractional part.
3391 * Caller should take care of dividing & rounding off the value.
3392 */
3393static uint32_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003394skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3395 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003396{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003397 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003398 uint32_t downscale_h, downscale_w;
3399 uint32_t src_w, src_h, dst_w, dst_h;
3400
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003401 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003402 return DRM_PLANE_HELPER_NO_SCALING;
3403
3404 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003405 if (plane->id == PLANE_CURSOR) {
3406 src_w = pstate->base.src_w;
3407 src_h = pstate->base.src_h;
3408 dst_w = pstate->base.crtc_w;
3409 dst_h = pstate->base.crtc_h;
3410 } else {
3411 src_w = drm_rect_width(&pstate->base.src);
3412 src_h = drm_rect_height(&pstate->base.src);
3413 dst_w = drm_rect_width(&pstate->base.dst);
3414 dst_h = drm_rect_height(&pstate->base.dst);
3415 }
3416
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003417 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003418 swap(dst_w, dst_h);
3419
3420 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3421 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3422
3423 /* Provide result in 16.16 fixed point */
3424 return (uint64_t)downscale_w * downscale_h >> 16;
3425}
3426
Damien Lespiaub9cec072014-11-04 17:06:43 +00003427static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003428skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3429 const struct drm_plane_state *pstate,
3430 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003431{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003432 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003433 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003434 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003435 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003436 struct drm_framebuffer *fb;
3437 u32 format;
Matt Ropera1de91e2016-05-12 07:05:57 -07003438
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003439 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003440 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003441
3442 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003443 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003444
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003445 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07003446 return 0;
3447 if (y && format != DRM_FORMAT_NV12)
3448 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003449
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003450 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3451 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003452
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003453 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003454 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003455
3456 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003457 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003458 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003459 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003460 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003461 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003462 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003463 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003464 } else {
3465 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02003466 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003467 }
3468
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003469 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003470
3471 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003472}
3473
3474/*
3475 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3476 * a 8192x4096@32bpp framebuffer:
3477 * 3 * 4096 * 8192 * 4 < 2^32
3478 */
3479static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003480skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3481 unsigned *plane_data_rate,
3482 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003483{
Matt Roper9c74d822016-05-12 07:05:58 -07003484 struct drm_crtc_state *cstate = &intel_cstate->base;
3485 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003486 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003487 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003488 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003489
3490 if (WARN_ON(!state))
3491 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003492
Matt Ropera1de91e2016-05-12 07:05:57 -07003493 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003494 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003495 enum plane_id plane_id = to_intel_plane(plane)->id;
3496 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003497
Matt Ropera6d3460e2016-05-12 07:06:04 -07003498 /* packed/uv */
3499 rate = skl_plane_relative_data_rate(intel_cstate,
3500 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003501 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003502
3503 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003504
Matt Ropera6d3460e2016-05-12 07:06:04 -07003505 /* y-plane */
3506 rate = skl_plane_relative_data_rate(intel_cstate,
3507 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003508 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003509
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003510 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003511 }
3512
3513 return total_data_rate;
3514}
3515
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003516static uint16_t
3517skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3518 const int y)
3519{
3520 struct drm_framebuffer *fb = pstate->fb;
3521 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3522 uint32_t src_w, src_h;
3523 uint32_t min_scanlines = 8;
3524 uint8_t plane_bpp;
3525
3526 if (WARN_ON(!fb))
3527 return 0;
3528
3529 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003530 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003531 return 0;
3532
3533 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003534 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3535 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003536 return 8;
3537
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003538 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3539 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003540
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003541 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003542 swap(src_w, src_h);
3543
3544 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003545 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003546 src_w /= 2;
3547 src_h /= 2;
3548 }
3549
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003550 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02003551 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003552 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02003553 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003554
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003555 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003556 switch (plane_bpp) {
3557 case 1:
3558 min_scanlines = 32;
3559 break;
3560 case 2:
3561 min_scanlines = 16;
3562 break;
3563 case 4:
3564 min_scanlines = 8;
3565 break;
3566 case 8:
3567 min_scanlines = 4;
3568 break;
3569 default:
3570 WARN(1, "Unsupported pixel depth %u for rotation",
3571 plane_bpp);
3572 min_scanlines = 32;
3573 }
3574 }
3575
3576 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3577}
3578
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003579static void
3580skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3581 uint16_t *minimum, uint16_t *y_minimum)
3582{
3583 const struct drm_plane_state *pstate;
3584 struct drm_plane *plane;
3585
3586 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003587 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003588
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003589 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003590 continue;
3591
3592 if (!pstate->visible)
3593 continue;
3594
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003595 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
3596 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003597 }
3598
3599 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3600}
3601
Matt Roperc107acf2016-05-12 07:06:01 -07003602static int
Matt Roper024c9042015-09-24 15:53:11 -07003603skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003604 struct skl_ddb_allocation *ddb /* out */)
3605{
Matt Roperc107acf2016-05-12 07:06:01 -07003606 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003607 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003608 struct drm_device *dev = crtc->dev;
3609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3610 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003611 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003612 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003613 uint16_t minimum[I915_MAX_PLANES] = {};
3614 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003615 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003616 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07003617 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003618 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3619 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003620
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003621 /* Clear the partitioning for disabled planes. */
3622 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3623 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3624
Matt Ropera6d3460e2016-05-12 07:06:04 -07003625 if (WARN_ON(!state))
3626 return 0;
3627
Matt Roperc107acf2016-05-12 07:06:01 -07003628 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003629 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003630 return 0;
3631 }
3632
Matt Ropera6d3460e2016-05-12 07:06:04 -07003633 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003634 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003635 if (alloc_size == 0) {
3636 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003637 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003638 }
3639
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003640 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003641
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003642 /*
3643 * 1. Allocate the mininum required blocks for each active plane
3644 * and allocate the cursor, it doesn't require extra allocation
3645 * proportional to the data rate.
3646 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003647
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003648 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
3649 alloc_size -= minimum[plane_id];
3650 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003651 }
3652
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003653 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3654 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3655
Damien Lespiaub9cec072014-11-04 17:06:43 +00003656 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003657 * 2. Distribute the remaining space in proportion to the amount of
3658 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003659 *
3660 * FIXME: we may not allocate every single block here.
3661 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003662 total_data_rate = skl_get_total_relative_data_rate(cstate,
3663 plane_data_rate,
3664 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003665 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003666 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003667
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003668 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003669 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003670 unsigned int data_rate, y_data_rate;
3671 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003672
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003673 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003674 continue;
3675
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003676 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003677
3678 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003679 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003680 * promote the expression to 64 bits to avoid overflowing, the
3681 * result is < available as data_rate / total_data_rate < 1
3682 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003683 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003684 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3685 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003686
Matt Roperc107acf2016-05-12 07:06:01 -07003687 /* Leave disabled planes at (0,0) */
3688 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003689 ddb->plane[pipe][plane_id].start = start;
3690 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003691 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003692
3693 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003694
3695 /*
3696 * allocation for y_plane part of planar format:
3697 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003698 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003699
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003700 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07003701 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3702 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003703
Matt Roperc107acf2016-05-12 07:06:01 -07003704 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003705 ddb->y_plane[pipe][plane_id].start = start;
3706 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003707 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003708
Matt Ropera1de91e2016-05-12 07:05:57 -07003709 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003710 }
3711
Matt Roperc107acf2016-05-12 07:06:01 -07003712 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003713}
3714
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003715/*
3716 * 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 +02003717 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003718 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3719 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3720*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303721static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
3722 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003723{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303724 uint32_t wm_intermediate_val;
3725 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003726
3727 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303728 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003729
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303730 wm_intermediate_val = latency * pixel_rate * cpp;
3731 ret = fixed_16_16_div_round_up_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003732 return ret;
3733}
3734
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303735static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
3736 uint32_t pipe_htotal,
3737 uint32_t latency,
3738 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003739{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003740 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303741 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003742
3743 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303744 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003745
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003746 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303747 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
3748 pipe_htotal * 1000);
3749 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003750 return ret;
3751}
3752
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003753static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3754 struct intel_plane_state *pstate)
3755{
3756 uint64_t adjusted_pixel_rate;
3757 uint64_t downscale_amount;
3758 uint64_t pixel_rate;
3759
3760 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003761 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003762 return 0;
3763
3764 /*
3765 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3766 * with additional adjustments for plane-specific scaling.
3767 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003768 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003769 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003770
3771 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3772 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3773
3774 return pixel_rate;
3775}
3776
Matt Roper55994c22016-05-12 07:06:08 -07003777static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3778 struct intel_crtc_state *cstate,
3779 struct intel_plane_state *intel_pstate,
3780 uint16_t ddb_allocation,
3781 int level,
3782 uint16_t *out_blocks, /* out */
3783 uint8_t *out_lines, /* out */
3784 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003785{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003786 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
Matt Roper33815fa2016-05-12 07:06:05 -07003787 struct drm_plane_state *pstate = &intel_pstate->base;
3788 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003789 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303790 uint_fixed_16_16_t method1, method2;
3791 uint_fixed_16_16_t plane_blocks_per_line;
3792 uint_fixed_16_16_t selected_result;
3793 uint32_t interm_pbpl;
3794 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003795 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02003796 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003797 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003798 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303799 uint_fixed_16_16_t y_tile_minimum;
3800 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003801 struct intel_atomic_state *state =
3802 to_intel_atomic_state(cstate->base.state);
3803 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303804 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003805
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003806 if (latency == 0 ||
3807 !intel_wm_plane_visible(cstate, intel_pstate)) {
Matt Roper55994c22016-05-12 07:06:08 -07003808 *enabled = false;
3809 return 0;
3810 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003811
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303812 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3813 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
3814 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
3815
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05303816 /* Display WA #1141: kbl. */
3817 if (IS_KABYLAKE(dev_priv) && dev_priv->ipc_enabled)
3818 latency += 4;
3819
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303820 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003821 latency += 15;
3822
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003823 if (plane->id == PLANE_CURSOR) {
3824 width = intel_pstate->base.crtc_w;
3825 height = intel_pstate->base.crtc_h;
3826 } else {
3827 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3828 height = drm_rect_height(&intel_pstate->base.src) >> 16;
3829 }
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003830
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003831 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003832 swap(width, height);
3833
Ville Syrjälä353c8592016-12-14 23:30:57 +02003834 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003835 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3836
Dave Airlie61d0a042016-10-25 16:35:20 +10003837 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003838 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02003839 fb->format->cpp[1] :
3840 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003841
3842 switch (cpp) {
3843 case 1:
3844 y_min_scanlines = 16;
3845 break;
3846 case 2:
3847 y_min_scanlines = 8;
3848 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003849 case 4:
3850 y_min_scanlines = 4;
3851 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003852 default:
3853 MISSING_CASE(cpp);
3854 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003855 }
3856 } else {
3857 y_min_scanlines = 4;
3858 }
3859
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02003860 if (apply_memory_bw_wa)
3861 y_min_scanlines *= 2;
3862
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003863 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303864 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303865 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
3866 y_min_scanlines, 512);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003867 plane_blocks_per_line =
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303868 fixed_16_16_div_round_up(interm_pbpl, y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303869 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303870 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
3871 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303872 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303873 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
3874 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003875 }
3876
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003877 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3878 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003879 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003880 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003881 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003882
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303883 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
3884 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003885
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303886 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303887 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003888 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003889 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3890 (plane_bytes_per_line / 512 < 1))
3891 selected_result = method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303892 else if ((ddb_allocation /
3893 fixed_16_16_to_u32_round_up(plane_blocks_per_line)) >= 1)
3894 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003895 else
3896 selected_result = method1;
3897 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003898
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303899 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
3900 res_lines = DIV_ROUND_UP(selected_result.val,
3901 plane_blocks_per_line.val);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003902
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003903 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303904 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303905 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003906 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003907 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003908 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003909 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003910 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003911
Matt Roper55994c22016-05-12 07:06:08 -07003912 if (res_blocks >= ddb_allocation || res_lines > 31) {
3913 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003914
3915 /*
3916 * If there are no valid level 0 watermarks, then we can't
3917 * support this display configuration.
3918 */
3919 if (level) {
3920 return 0;
3921 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003922 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07003923
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003924 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3925 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
3926 plane->base.id, plane->name,
3927 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07003928 return -EINVAL;
3929 }
Matt Roper55994c22016-05-12 07:06:08 -07003930 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003931
3932 *out_blocks = res_blocks;
3933 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003934 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003935
Matt Roper55994c22016-05-12 07:06:08 -07003936 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003937}
3938
Matt Roperf4a96752016-05-12 07:06:06 -07003939static int
3940skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3941 struct skl_ddb_allocation *ddb,
3942 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003943 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003944 int level,
3945 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003946{
Matt Roperf4a96752016-05-12 07:06:06 -07003947 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003948 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003949 struct drm_plane *plane = &intel_plane->base;
3950 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003951 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003952 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003953 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003954
3955 if (state)
3956 intel_pstate =
3957 intel_atomic_get_existing_plane_state(state,
3958 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003959
Matt Roperf4a96752016-05-12 07:06:06 -07003960 /*
Lyudea62163e2016-10-04 14:28:20 -04003961 * Note: If we start supporting multiple pending atomic commits against
3962 * the same planes/CRTC's in the future, plane->state will no longer be
3963 * the correct pre-state to use for the calculations here and we'll
3964 * need to change where we get the 'unchanged' plane data from.
3965 *
3966 * For now this is fine because we only allow one queued commit against
3967 * a CRTC. Even if the plane isn't modified by this transaction and we
3968 * don't have a plane lock, we still have the CRTC's lock, so we know
3969 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003970 */
Lyudea62163e2016-10-04 14:28:20 -04003971 if (!intel_pstate)
3972 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003973
Lyudea62163e2016-10-04 14:28:20 -04003974 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003975
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003976 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07003977
Lyudea62163e2016-10-04 14:28:20 -04003978 ret = skl_compute_plane_wm(dev_priv,
3979 cstate,
3980 intel_pstate,
3981 ddb_blocks,
3982 level,
3983 &result->plane_res_b,
3984 &result->plane_res_l,
3985 &result->plane_en);
3986 if (ret)
3987 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003988
3989 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003990}
3991
Damien Lespiau407b50f2014-11-04 17:06:57 +00003992static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003993skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003994{
Mahesh Kumara3a89862016-12-01 21:19:34 +05303995 struct drm_atomic_state *state = cstate->base.state;
3996 struct drm_i915_private *dev_priv = to_i915(state->dev);
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003997 uint32_t pixel_rate;
Mahesh Kumara3a89862016-12-01 21:19:34 +05303998 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003999
Matt Roper024c9042015-09-24 15:53:11 -07004000 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004001 return 0;
4002
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004003 pixel_rate = cstate->pixel_rate;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004004
4005 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03004006 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004007
Mahesh Kumara3a89862016-12-01 21:19:34 +05304008 linetime_wm = DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal *
4009 1000, pixel_rate);
4010
4011 /* Display WA #1135: bxt. */
4012 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
4013 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
4014
4015 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004016}
4017
Matt Roper024c9042015-09-24 15:53:11 -07004018static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00004019 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004020{
Matt Roper024c9042015-09-24 15:53:11 -07004021 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004022 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00004023
4024 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04004025 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004026}
4027
Matt Roper55994c22016-05-12 07:06:08 -07004028static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4029 struct skl_ddb_allocation *ddb,
4030 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004031{
Matt Roper024c9042015-09-24 15:53:11 -07004032 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004033 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04004034 struct intel_plane *intel_plane;
4035 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004036 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004037 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004038
Lyudea62163e2016-10-04 14:28:20 -04004039 /*
4040 * We'll only calculate watermarks for planes that are actually
4041 * enabled, so make sure all other planes are set as disabled.
4042 */
4043 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4044
4045 for_each_intel_plane_mask(&dev_priv->drm,
4046 intel_plane,
4047 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004048 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04004049
4050 for (level = 0; level <= max_level; level++) {
4051 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
4052 intel_plane, level,
4053 &wm->wm[level]);
4054 if (ret)
4055 return ret;
4056 }
4057 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004058 }
Matt Roper024c9042015-09-24 15:53:11 -07004059 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004060
Matt Roper55994c22016-05-12 07:06:08 -07004061 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004062}
4063
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004064static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4065 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004066 const struct skl_ddb_entry *entry)
4067{
4068 if (entry->end)
4069 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4070 else
4071 I915_WRITE(reg, 0);
4072}
4073
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004074static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4075 i915_reg_t reg,
4076 const struct skl_wm_level *level)
4077{
4078 uint32_t val = 0;
4079
4080 if (level->plane_en) {
4081 val |= PLANE_WM_EN;
4082 val |= level->plane_res_b;
4083 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4084 }
4085
4086 I915_WRITE(reg, val);
4087}
4088
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004089static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4090 const struct skl_plane_wm *wm,
4091 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004092 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004093{
4094 struct drm_crtc *crtc = &intel_crtc->base;
4095 struct drm_device *dev = crtc->dev;
4096 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004097 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004098 enum pipe pipe = intel_crtc->pipe;
4099
4100 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004101 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004102 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004103 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004104 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004105 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004106
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004107 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4108 &ddb->plane[pipe][plane_id]);
4109 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4110 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004111}
4112
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004113static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4114 const struct skl_plane_wm *wm,
4115 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004116{
4117 struct drm_crtc *crtc = &intel_crtc->base;
4118 struct drm_device *dev = crtc->dev;
4119 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004120 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004121 enum pipe pipe = intel_crtc->pipe;
4122
4123 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004124 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4125 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004126 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004127 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004128
4129 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004130 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004131}
4132
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004133bool skl_wm_level_equals(const struct skl_wm_level *l1,
4134 const struct skl_wm_level *l2)
4135{
4136 if (l1->plane_en != l2->plane_en)
4137 return false;
4138
4139 /* If both planes aren't enabled, the rest shouldn't matter */
4140 if (!l1->plane_en)
4141 return true;
4142
4143 return (l1->plane_res_l == l2->plane_res_l &&
4144 l1->plane_res_b == l2->plane_res_b);
4145}
4146
Lyude27082492016-08-24 07:48:10 +02004147static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4148 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004149{
Lyude27082492016-08-24 07:48:10 +02004150 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004151}
4152
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004153bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4154 const struct skl_ddb_entry *ddb,
4155 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004156{
Lyudece0ba282016-09-15 10:46:35 -04004157 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004158
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004159 for (i = 0; i < I915_MAX_PIPES; i++)
4160 if (i != ignore && entries[i] &&
4161 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004162 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004163
Lyude27082492016-08-24 07:48:10 +02004164 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004165}
4166
Matt Roper55994c22016-05-12 07:06:08 -07004167static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004168 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004169 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004170 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004171 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004172{
Matt Roperf4a96752016-05-12 07:06:06 -07004173 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004174 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004175
Matt Roper55994c22016-05-12 07:06:08 -07004176 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4177 if (ret)
4178 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004179
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004180 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004181 *changed = false;
4182 else
4183 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004184
Matt Roper55994c22016-05-12 07:06:08 -07004185 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004186}
4187
Matt Roper9b613022016-06-27 16:42:44 -07004188static uint32_t
4189pipes_modified(struct drm_atomic_state *state)
4190{
4191 struct drm_crtc *crtc;
4192 struct drm_crtc_state *cstate;
4193 uint32_t i, ret = 0;
4194
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004195 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004196 ret |= drm_crtc_mask(crtc);
4197
4198 return ret;
4199}
4200
Jani Nikulabb7791b2016-10-04 12:29:17 +03004201static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004202skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4203{
4204 struct drm_atomic_state *state = cstate->base.state;
4205 struct drm_device *dev = state->dev;
4206 struct drm_crtc *crtc = cstate->base.crtc;
4207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4208 struct drm_i915_private *dev_priv = to_i915(dev);
4209 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4210 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4211 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4212 struct drm_plane_state *plane_state;
4213 struct drm_plane *plane;
4214 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004215
4216 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4217
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004218 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004219 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004220
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004221 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4222 &new_ddb->plane[pipe][plane_id]) &&
4223 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4224 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004225 continue;
4226
4227 plane_state = drm_atomic_get_plane_state(state, plane);
4228 if (IS_ERR(plane_state))
4229 return PTR_ERR(plane_state);
4230 }
4231
4232 return 0;
4233}
4234
Matt Roper98d39492016-05-12 07:06:03 -07004235static int
4236skl_compute_ddb(struct drm_atomic_state *state)
4237{
4238 struct drm_device *dev = state->dev;
4239 struct drm_i915_private *dev_priv = to_i915(dev);
4240 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4241 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004242 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004243 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004244 int ret;
4245
4246 /*
4247 * If this is our first atomic update following hardware readout,
4248 * we can't trust the DDB that the BIOS programmed for us. Let's
4249 * pretend that all pipes switched active status so that we'll
4250 * ensure a full DDB recompute.
4251 */
Matt Roper1b54a882016-06-17 13:42:18 -07004252 if (dev_priv->wm.distrust_bios_wm) {
4253 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4254 state->acquire_ctx);
4255 if (ret)
4256 return ret;
4257
Matt Roper98d39492016-05-12 07:06:03 -07004258 intel_state->active_pipe_changes = ~0;
4259
Matt Roper1b54a882016-06-17 13:42:18 -07004260 /*
4261 * We usually only initialize intel_state->active_crtcs if we
4262 * we're doing a modeset; make sure this field is always
4263 * initialized during the sanitization process that happens
4264 * on the first commit too.
4265 */
4266 if (!intel_state->modeset)
4267 intel_state->active_crtcs = dev_priv->active_crtcs;
4268 }
4269
Matt Roper98d39492016-05-12 07:06:03 -07004270 /*
4271 * If the modeset changes which CRTC's are active, we need to
4272 * recompute the DDB allocation for *all* active pipes, even
4273 * those that weren't otherwise being modified in any way by this
4274 * atomic commit. Due to the shrinking of the per-pipe allocations
4275 * when new active CRTC's are added, it's possible for a pipe that
4276 * we were already using and aren't changing at all here to suddenly
4277 * become invalid if its DDB needs exceeds its new allocation.
4278 *
4279 * Note that if we wind up doing a full DDB recompute, we can't let
4280 * any other display updates race with this transaction, so we need
4281 * to grab the lock on *all* CRTC's.
4282 */
Matt Roper734fa012016-05-12 15:11:40 -07004283 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004284 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004285 intel_state->wm_results.dirty_pipes = ~0;
4286 }
Matt Roper98d39492016-05-12 07:06:03 -07004287
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004288 /*
4289 * We're not recomputing for the pipes not included in the commit, so
4290 * make sure we start with the current state.
4291 */
4292 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4293
Matt Roper98d39492016-05-12 07:06:03 -07004294 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4295 struct intel_crtc_state *cstate;
4296
4297 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4298 if (IS_ERR(cstate))
4299 return PTR_ERR(cstate);
4300
Matt Roper734fa012016-05-12 15:11:40 -07004301 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004302 if (ret)
4303 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004304
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004305 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004306 if (ret)
4307 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004308 }
4309
4310 return 0;
4311}
4312
Matt Roper2722efb2016-08-17 15:55:55 -04004313static void
4314skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4315 struct skl_wm_values *src,
4316 enum pipe pipe)
4317{
Matt Roper2722efb2016-08-17 15:55:55 -04004318 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4319 sizeof(dst->ddb.y_plane[pipe]));
4320 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4321 sizeof(dst->ddb.plane[pipe]));
4322}
4323
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004324static void
4325skl_print_wm_changes(const struct drm_atomic_state *state)
4326{
4327 const struct drm_device *dev = state->dev;
4328 const struct drm_i915_private *dev_priv = to_i915(dev);
4329 const struct intel_atomic_state *intel_state =
4330 to_intel_atomic_state(state);
4331 const struct drm_crtc *crtc;
4332 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004333 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004334 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4335 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004336 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004337
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004338 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004339 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4340 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004341
Maarten Lankhorst75704982016-11-01 12:04:10 +01004342 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004343 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004344 const struct skl_ddb_entry *old, *new;
4345
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004346 old = &old_ddb->plane[pipe][plane_id];
4347 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004348
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004349 if (skl_ddb_entry_equal(old, new))
4350 continue;
4351
Maarten Lankhorst75704982016-11-01 12:04:10 +01004352 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4353 intel_plane->base.base.id,
4354 intel_plane->base.name,
4355 old->start, old->end,
4356 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004357 }
4358 }
4359}
4360
Matt Roper98d39492016-05-12 07:06:03 -07004361static int
4362skl_compute_wm(struct drm_atomic_state *state)
4363{
4364 struct drm_crtc *crtc;
4365 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004366 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4367 struct skl_wm_values *results = &intel_state->wm_results;
4368 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004369 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004370 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004371
4372 /*
4373 * If this transaction isn't actually touching any CRTC's, don't
4374 * bother with watermark calculation. Note that if we pass this
4375 * test, we're guaranteed to hold at least one CRTC state mutex,
4376 * which means we can safely use values like dev_priv->active_crtcs
4377 * since any racing commits that want to update them would need to
4378 * hold _all_ CRTC state mutexes.
4379 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004380 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07004381 changed = true;
4382 if (!changed)
4383 return 0;
4384
Matt Roper734fa012016-05-12 15:11:40 -07004385 /* Clear all dirty flags */
4386 results->dirty_pipes = 0;
4387
Matt Roper98d39492016-05-12 07:06:03 -07004388 ret = skl_compute_ddb(state);
4389 if (ret)
4390 return ret;
4391
Matt Roper734fa012016-05-12 15:11:40 -07004392 /*
4393 * Calculate WM's for all pipes that are part of this transaction.
4394 * Note that the DDB allocation above may have added more CRTC's that
4395 * weren't otherwise being modified (and set bits in dirty_pipes) if
4396 * pipe allocations had to change.
4397 *
4398 * FIXME: Now that we're doing this in the atomic check phase, we
4399 * should allow skl_update_pipe_wm() to return failure in cases where
4400 * no suitable watermark values can be found.
4401 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004402 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004403 struct intel_crtc_state *intel_cstate =
4404 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004405 const struct skl_pipe_wm *old_pipe_wm =
4406 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004407
4408 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004409 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4410 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004411 if (ret)
4412 return ret;
4413
4414 if (changed)
4415 results->dirty_pipes |= drm_crtc_mask(crtc);
4416
4417 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4418 /* This pipe's WM's did not change */
4419 continue;
4420
4421 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004422 }
4423
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004424 skl_print_wm_changes(state);
4425
Matt Roper98d39492016-05-12 07:06:03 -07004426 return 0;
4427}
4428
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004429static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4430 struct intel_crtc_state *cstate)
4431{
4432 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4433 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4434 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004435 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004436 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004437 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004438
4439 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4440 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004441
4442 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004443
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004444 for_each_plane_id_on_crtc(crtc, plane_id) {
4445 if (plane_id != PLANE_CURSOR)
4446 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4447 ddb, plane_id);
4448 else
4449 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4450 ddb);
4451 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004452}
4453
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004454static void skl_initial_wm(struct intel_atomic_state *state,
4455 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004456{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004457 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004458 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004459 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004460 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004461 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004462 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004463
Ville Syrjälä432081b2016-10-31 22:37:03 +02004464 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004465 return;
4466
Matt Roper734fa012016-05-12 15:11:40 -07004467 mutex_lock(&dev_priv->wm.wm_mutex);
4468
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004469 if (cstate->base.active_changed)
4470 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004471
4472 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004473
4474 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004475}
4476
Ville Syrjäläd8905652016-01-14 14:53:35 +02004477static void ilk_compute_wm_config(struct drm_device *dev,
4478 struct intel_wm_config *config)
4479{
4480 struct intel_crtc *crtc;
4481
4482 /* Compute the currently _active_ config */
4483 for_each_intel_crtc(dev, crtc) {
4484 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4485
4486 if (!wm->pipe_enabled)
4487 continue;
4488
4489 config->sprites_enabled |= wm->sprites_enabled;
4490 config->sprites_scaled |= wm->sprites_scaled;
4491 config->num_pipes_active++;
4492 }
4493}
4494
Matt Ropered4a6a72016-02-23 17:20:13 -08004495static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004496{
Chris Wilson91c8a322016-07-05 10:40:23 +01004497 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004498 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004499 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004500 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004501 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004502 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004503
Ville Syrjäläd8905652016-01-14 14:53:35 +02004504 ilk_compute_wm_config(dev, &config);
4505
4506 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4507 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004508
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004509 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004510 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004511 config.num_pipes_active == 1 && config.sprites_enabled) {
4512 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4513 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004514
Imre Deak820c1982013-12-17 14:46:36 +02004515 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004516 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004517 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004518 }
4519
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004520 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004521 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004522
Imre Deak820c1982013-12-17 14:46:36 +02004523 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004524
Imre Deak820c1982013-12-17 14:46:36 +02004525 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004526}
4527
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004528static void ilk_initial_watermarks(struct intel_atomic_state *state,
4529 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004530{
Matt Ropered4a6a72016-02-23 17:20:13 -08004531 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4532 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004533
Matt Ropered4a6a72016-02-23 17:20:13 -08004534 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004535 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004536 ilk_program_watermarks(dev_priv);
4537 mutex_unlock(&dev_priv->wm.wm_mutex);
4538}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004539
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004540static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4541 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004542{
4543 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4544 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4545
4546 mutex_lock(&dev_priv->wm.wm_mutex);
4547 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004548 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004549 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004550 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004551 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004552}
4553
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004554static inline void skl_wm_level_from_reg_val(uint32_t val,
4555 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004556{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004557 level->plane_en = val & PLANE_WM_EN;
4558 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4559 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4560 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004561}
4562
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004563void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4564 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004565{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004566 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004568 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004569 int level, max_level;
4570 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004571 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004572
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004573 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004574
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004575 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4576 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004577
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004578 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004579 if (plane_id != PLANE_CURSOR)
4580 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004581 else
4582 val = I915_READ(CUR_WM(pipe, level));
4583
4584 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4585 }
4586
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004587 if (plane_id != PLANE_CURSOR)
4588 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004589 else
4590 val = I915_READ(CUR_WM_TRANS(pipe));
4591
4592 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4593 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004594
Matt Roper3ef00282015-03-09 10:19:24 -07004595 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004596 return;
4597
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004598 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004599}
4600
4601void skl_wm_get_hw_state(struct drm_device *dev)
4602{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004603 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004604 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004605 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004606 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004607 struct intel_crtc *intel_crtc;
4608 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004609
Damien Lespiaua269c582014-11-04 17:06:49 +00004610 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004611 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4612 intel_crtc = to_intel_crtc(crtc);
4613 cstate = to_intel_crtc_state(crtc->state);
4614
4615 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4616
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004617 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004618 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004619 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004620
Matt Roper279e99d2016-05-12 07:06:02 -07004621 if (dev_priv->active_crtcs) {
4622 /* Fully recompute DDB on first atomic commit */
4623 dev_priv->wm.distrust_bios_wm = true;
4624 } else {
4625 /* Easy/common case; just sanitize DDB now if everything off */
4626 memset(ddb, 0, sizeof(*ddb));
4627 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004628}
4629
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004630static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4631{
4632 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004633 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004634 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004635 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004636 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004637 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004638 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004639 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004640 [PIPE_A] = WM0_PIPEA_ILK,
4641 [PIPE_B] = WM0_PIPEB_ILK,
4642 [PIPE_C] = WM0_PIPEC_IVB,
4643 };
4644
4645 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004646 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004647 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004648
Ville Syrjälä15606532016-05-13 17:55:17 +03004649 memset(active, 0, sizeof(*active));
4650
Matt Roper3ef00282015-03-09 10:19:24 -07004651 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004652
4653 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004654 u32 tmp = hw->wm_pipe[pipe];
4655
4656 /*
4657 * For active pipes LP0 watermark is marked as
4658 * enabled, and LP1+ watermaks as disabled since
4659 * we can't really reverse compute them in case
4660 * multiple pipes are active.
4661 */
4662 active->wm[0].enable = true;
4663 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4664 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4665 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4666 active->linetime = hw->wm_linetime[pipe];
4667 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004668 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004669
4670 /*
4671 * For inactive pipes, all watermark levels
4672 * should be marked as enabled but zeroed,
4673 * which is what we'd compute them to.
4674 */
4675 for (level = 0; level <= max_level; level++)
4676 active->wm[level].enable = true;
4677 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004678
4679 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004680}
4681
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004682#define _FW_WM(value, plane) \
4683 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4684#define _FW_WM_VLV(value, plane) \
4685 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4686
4687static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4688 struct vlv_wm_values *wm)
4689{
4690 enum pipe pipe;
4691 uint32_t tmp;
4692
4693 for_each_pipe(dev_priv, pipe) {
4694 tmp = I915_READ(VLV_DDL(pipe));
4695
Ville Syrjälä1b313892016-11-28 19:37:08 +02004696 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004697 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004698 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004699 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004700 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004701 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004702 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004703 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4704 }
4705
4706 tmp = I915_READ(DSPFW1);
4707 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004708 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
4709 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
4710 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004711
4712 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004713 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
4714 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
4715 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004716
4717 tmp = I915_READ(DSPFW3);
4718 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4719
4720 if (IS_CHERRYVIEW(dev_priv)) {
4721 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004722 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4723 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004724
4725 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004726 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
4727 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004728
4729 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004730 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
4731 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004732
4733 tmp = I915_READ(DSPHOWM);
4734 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004735 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4736 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4737 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
4738 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4739 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4740 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4741 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4742 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4743 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004744 } else {
4745 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004746 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4747 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004748
4749 tmp = I915_READ(DSPHOWM);
4750 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004751 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4752 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4753 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4754 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4755 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4756 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004757 }
4758}
4759
4760#undef _FW_WM
4761#undef _FW_WM_VLV
4762
4763void vlv_wm_get_hw_state(struct drm_device *dev)
4764{
4765 struct drm_i915_private *dev_priv = to_i915(dev);
4766 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02004767 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004768 u32 val;
4769
4770 vlv_read_wm_values(dev_priv, wm);
4771
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004772 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4773 wm->level = VLV_WM_LEVEL_PM2;
4774
4775 if (IS_CHERRYVIEW(dev_priv)) {
4776 mutex_lock(&dev_priv->rps.hw_lock);
4777
4778 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4779 if (val & DSP_MAXFIFO_PM5_ENABLE)
4780 wm->level = VLV_WM_LEVEL_PM5;
4781
Ville Syrjälä58590c12015-09-08 21:05:12 +03004782 /*
4783 * If DDR DVFS is disabled in the BIOS, Punit
4784 * will never ack the request. So if that happens
4785 * assume we don't have to enable/disable DDR DVFS
4786 * dynamically. To test that just set the REQ_ACK
4787 * bit to poke the Punit, but don't change the
4788 * HIGH/LOW bits so that we don't actually change
4789 * the current state.
4790 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004791 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004792 val |= FORCE_DDR_FREQ_REQ_ACK;
4793 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4794
4795 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4796 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4797 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4798 "assuming DDR DVFS is disabled\n");
4799 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4800 } else {
4801 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4802 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4803 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4804 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004805
4806 mutex_unlock(&dev_priv->rps.hw_lock);
4807 }
4808
Ville Syrjäläff32c542017-03-02 19:14:57 +02004809 for_each_intel_crtc(dev, crtc) {
4810 struct intel_crtc_state *crtc_state =
4811 to_intel_crtc_state(crtc->base.state);
4812 struct vlv_wm_state *active = &crtc->wm.active.vlv;
4813 const struct vlv_fifo_state *fifo_state =
4814 &crtc_state->wm.vlv.fifo_state;
4815 enum pipe pipe = crtc->pipe;
4816 enum plane_id plane_id;
4817 int level;
4818
4819 vlv_get_fifo_size(crtc_state);
4820
4821 active->num_levels = wm->level + 1;
4822 active->cxsr = wm->cxsr;
4823
Ville Syrjäläff32c542017-03-02 19:14:57 +02004824 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03004825 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02004826 &crtc_state->wm.vlv.raw[level];
4827
4828 active->sr[level].plane = wm->sr.plane;
4829 active->sr[level].cursor = wm->sr.cursor;
4830
4831 for_each_plane_id_on_crtc(crtc, plane_id) {
4832 active->wm[level].plane[plane_id] =
4833 wm->pipe[pipe].plane[plane_id];
4834
4835 raw->plane[plane_id] =
4836 vlv_invert_wm_value(active->wm[level].plane[plane_id],
4837 fifo_state->plane[plane_id]);
4838 }
4839 }
4840
4841 for_each_plane_id_on_crtc(crtc, plane_id)
4842 vlv_raw_plane_wm_set(crtc_state, level,
4843 plane_id, USHRT_MAX);
4844 vlv_invalidate_wms(crtc, active, level);
4845
4846 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02004847 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02004848
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004849 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 +02004850 pipe_name(pipe),
4851 wm->pipe[pipe].plane[PLANE_PRIMARY],
4852 wm->pipe[pipe].plane[PLANE_CURSOR],
4853 wm->pipe[pipe].plane[PLANE_SPRITE0],
4854 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02004855 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004856
4857 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4858 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4859}
4860
Ville Syrjälä602ae832017-03-02 19:15:02 +02004861void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
4862{
4863 struct intel_plane *plane;
4864 struct intel_crtc *crtc;
4865
4866 mutex_lock(&dev_priv->wm.wm_mutex);
4867
4868 for_each_intel_plane(&dev_priv->drm, plane) {
4869 struct intel_crtc *crtc =
4870 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
4871 struct intel_crtc_state *crtc_state =
4872 to_intel_crtc_state(crtc->base.state);
4873 struct intel_plane_state *plane_state =
4874 to_intel_plane_state(plane->base.state);
4875 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
4876 const struct vlv_fifo_state *fifo_state =
4877 &crtc_state->wm.vlv.fifo_state;
4878 enum plane_id plane_id = plane->id;
4879 int level;
4880
4881 if (plane_state->base.visible)
4882 continue;
4883
4884 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03004885 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02004886 &crtc_state->wm.vlv.raw[level];
4887
4888 raw->plane[plane_id] = 0;
4889
4890 wm_state->wm[level].plane[plane_id] =
4891 vlv_invert_wm_value(raw->plane[plane_id],
4892 fifo_state->plane[plane_id]);
4893 }
4894 }
4895
4896 for_each_intel_crtc(&dev_priv->drm, crtc) {
4897 struct intel_crtc_state *crtc_state =
4898 to_intel_crtc_state(crtc->base.state);
4899
4900 crtc_state->wm.vlv.intermediate =
4901 crtc_state->wm.vlv.optimal;
4902 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
4903 }
4904
4905 vlv_program_watermarks(dev_priv);
4906
4907 mutex_unlock(&dev_priv->wm.wm_mutex);
4908}
4909
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004910void ilk_wm_get_hw_state(struct drm_device *dev)
4911{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004912 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004913 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004914 struct drm_crtc *crtc;
4915
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004916 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004917 ilk_pipe_wm_get_hw_state(crtc);
4918
4919 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4920 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4921 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4922
4923 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004924 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004925 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4926 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4927 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004928
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004929 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004930 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4931 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004932 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004933 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4934 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004935
4936 hw->enable_fbc_wm =
4937 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4938}
4939
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004940/**
4941 * intel_update_watermarks - update FIFO watermark values based on current modes
4942 *
4943 * Calculate watermark values for the various WM regs based on current mode
4944 * and plane configuration.
4945 *
4946 * There are several cases to deal with here:
4947 * - normal (i.e. non-self-refresh)
4948 * - self-refresh (SR) mode
4949 * - lines are large relative to FIFO size (buffer can hold up to 2)
4950 * - lines are small relative to FIFO size (buffer can hold more than 2
4951 * lines), so need to account for TLB latency
4952 *
4953 * The normal calculation is:
4954 * watermark = dotclock * bytes per pixel * latency
4955 * where latency is platform & configuration dependent (we assume pessimal
4956 * values here).
4957 *
4958 * The SR calculation is:
4959 * watermark = (trunc(latency/line time)+1) * surface width *
4960 * bytes per pixel
4961 * where
4962 * line time = htotal / dotclock
4963 * surface width = hdisplay for normal plane and 64 for cursor
4964 * and latency is assumed to be high, as above.
4965 *
4966 * The final value programmed to the register should always be rounded up,
4967 * and include an extra 2 entries to account for clock crossings.
4968 *
4969 * We don't use the sprite, so we can ignore that. And on Crestline we have
4970 * to set the non-SR watermarks to 8.
4971 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004972void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004973{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004974 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004975
4976 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004977 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004978}
4979
Jani Nikulae2828912016-01-18 09:19:47 +02004980/*
Daniel Vetter92703882012-08-09 16:46:01 +02004981 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004982 */
4983DEFINE_SPINLOCK(mchdev_lock);
4984
4985/* Global for IPS driver to get at the current i915 device. Protected by
4986 * mchdev_lock. */
4987static struct drm_i915_private *i915_mch_dev;
4988
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004989bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004990{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004991 u16 rgvswctl;
4992
Chris Wilson67520412017-03-02 13:28:01 +00004993 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02004994
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004995 rgvswctl = I915_READ16(MEMSWCTL);
4996 if (rgvswctl & MEMCTL_CMD_STS) {
4997 DRM_DEBUG("gpu busy, RCS change rejected\n");
4998 return false; /* still busy with another command */
4999 }
5000
5001 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5002 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5003 I915_WRITE16(MEMSWCTL, rgvswctl);
5004 POSTING_READ16(MEMSWCTL);
5005
5006 rgvswctl |= MEMCTL_CMD_STS;
5007 I915_WRITE16(MEMSWCTL, rgvswctl);
5008
5009 return true;
5010}
5011
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005012static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005013{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005014 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005015 u8 fmax, fmin, fstart, vstart;
5016
Daniel Vetter92703882012-08-09 16:46:01 +02005017 spin_lock_irq(&mchdev_lock);
5018
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005019 rgvmodectl = I915_READ(MEMMODECTL);
5020
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005021 /* Enable temp reporting */
5022 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5023 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5024
5025 /* 100ms RC evaluation intervals */
5026 I915_WRITE(RCUPEI, 100000);
5027 I915_WRITE(RCDNEI, 100000);
5028
5029 /* Set max/min thresholds to 90ms and 80ms respectively */
5030 I915_WRITE(RCBMAXAVG, 90000);
5031 I915_WRITE(RCBMINAVG, 80000);
5032
5033 I915_WRITE(MEMIHYST, 1);
5034
5035 /* Set up min, max, and cur for interrupt handling */
5036 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5037 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5038 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5039 MEMMODE_FSTART_SHIFT;
5040
Ville Syrjälä616847e2015-09-18 20:03:19 +03005041 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005042 PXVFREQ_PX_SHIFT;
5043
Daniel Vetter20e4d402012-08-08 23:35:39 +02005044 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5045 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005046
Daniel Vetter20e4d402012-08-08 23:35:39 +02005047 dev_priv->ips.max_delay = fstart;
5048 dev_priv->ips.min_delay = fmin;
5049 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005050
5051 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5052 fmax, fmin, fstart);
5053
5054 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5055
5056 /*
5057 * Interrupts will be enabled in ironlake_irq_postinstall
5058 */
5059
5060 I915_WRITE(VIDSTART, vstart);
5061 POSTING_READ(VIDSTART);
5062
5063 rgvmodectl |= MEMMODE_SWMODE_EN;
5064 I915_WRITE(MEMMODECTL, rgvmodectl);
5065
Daniel Vetter92703882012-08-09 16:46:01 +02005066 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005067 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005068 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005069
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005070 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005071
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005072 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5073 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005074 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005075 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005076 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005077
5078 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005079}
5080
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005081static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005082{
Daniel Vetter92703882012-08-09 16:46:01 +02005083 u16 rgvswctl;
5084
5085 spin_lock_irq(&mchdev_lock);
5086
5087 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005088
5089 /* Ack interrupts, disable EFC interrupt */
5090 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5091 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5092 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5093 I915_WRITE(DEIIR, DE_PCU_EVENT);
5094 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5095
5096 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005097 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005098 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005099 rgvswctl |= MEMCTL_CMD_STS;
5100 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005101 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005102
Daniel Vetter92703882012-08-09 16:46:01 +02005103 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005104}
5105
Daniel Vetteracbe9472012-07-26 11:50:05 +02005106/* There's a funny hw issue where the hw returns all 0 when reading from
5107 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5108 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5109 * all limits and the gpu stuck at whatever frequency it is at atm).
5110 */
Akash Goel74ef1172015-03-06 11:07:19 +05305111static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005112{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005113 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005114
Daniel Vetter20b46e52012-07-26 11:16:14 +02005115 /* Only set the down limit when we've reached the lowest level to avoid
5116 * getting more interrupts, otherwise leave this clear. This prevents a
5117 * race in the hw when coming out of rc6: There's a tiny window where
5118 * the hw runs at the minimal clock before selecting the desired
5119 * frequency, if the down threshold expires in that window we will not
5120 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005121 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305122 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5123 if (val <= dev_priv->rps.min_freq_softlimit)
5124 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5125 } else {
5126 limits = dev_priv->rps.max_freq_softlimit << 24;
5127 if (val <= dev_priv->rps.min_freq_softlimit)
5128 limits |= dev_priv->rps.min_freq_softlimit << 16;
5129 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005130
5131 return limits;
5132}
5133
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005134static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5135{
5136 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305137 u32 threshold_up = 0, threshold_down = 0; /* in % */
5138 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005139
5140 new_power = dev_priv->rps.power;
5141 switch (dev_priv->rps.power) {
5142 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005143 if (val > dev_priv->rps.efficient_freq + 1 &&
5144 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005145 new_power = BETWEEN;
5146 break;
5147
5148 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005149 if (val <= dev_priv->rps.efficient_freq &&
5150 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005151 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005152 else if (val >= dev_priv->rps.rp0_freq &&
5153 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005154 new_power = HIGH_POWER;
5155 break;
5156
5157 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005158 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5159 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005160 new_power = BETWEEN;
5161 break;
5162 }
5163 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005164 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005165 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005166 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005167 new_power = HIGH_POWER;
5168 if (new_power == dev_priv->rps.power)
5169 return;
5170
5171 /* Note the units here are not exactly 1us, but 1280ns. */
5172 switch (new_power) {
5173 case LOW_POWER:
5174 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305175 ei_up = 16000;
5176 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005177
5178 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305179 ei_down = 32000;
5180 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005181 break;
5182
5183 case BETWEEN:
5184 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305185 ei_up = 13000;
5186 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005187
5188 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305189 ei_down = 32000;
5190 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005191 break;
5192
5193 case HIGH_POWER:
5194 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305195 ei_up = 10000;
5196 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005197
5198 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305199 ei_down = 32000;
5200 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005201 break;
5202 }
5203
Mika Kuoppala6067a272017-02-15 15:52:59 +02005204 /* When byt can survive without system hang with dynamic
5205 * sw freq adjustments, this restriction can be lifted.
5206 */
5207 if (IS_VALLEYVIEW(dev_priv))
5208 goto skip_hw_write;
5209
Akash Goel8a586432015-03-06 11:07:18 +05305210 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005211 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305212 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005213 GT_INTERVAL_FROM_US(dev_priv,
5214 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305215
5216 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005217 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305218 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005219 GT_INTERVAL_FROM_US(dev_priv,
5220 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305221
Chris Wilsona72b5622016-07-02 15:35:59 +01005222 I915_WRITE(GEN6_RP_CONTROL,
5223 GEN6_RP_MEDIA_TURBO |
5224 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5225 GEN6_RP_MEDIA_IS_GFX |
5226 GEN6_RP_ENABLE |
5227 GEN6_RP_UP_BUSY_AVG |
5228 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305229
Mika Kuoppala6067a272017-02-15 15:52:59 +02005230skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005231 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005232 dev_priv->rps.up_threshold = threshold_up;
5233 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005234 dev_priv->rps.last_adj = 0;
5235}
5236
Chris Wilson2876ce72014-03-28 08:03:34 +00005237static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5238{
5239 u32 mask = 0;
5240
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005241 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00005242 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005243 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005244 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005245 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005246
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005247 mask &= dev_priv->pm_rps_events;
5248
Imre Deak59d02a12014-12-19 19:33:26 +02005249 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005250}
5251
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005252/* gen6_set_rps is called to update the frequency request, but should also be
5253 * called when the range (min_delay and max_delay) is modified so that we can
5254 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005255static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005256{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005257 /* min/max delay may still have been modified so be sure to
5258 * write the limits value.
5259 */
5260 if (val != dev_priv->rps.cur_freq) {
5261 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005262
Chris Wilsondc979972016-05-10 14:10:04 +01005263 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05305264 I915_WRITE(GEN6_RPNSWREQ,
5265 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01005266 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005267 I915_WRITE(GEN6_RPNSWREQ,
5268 HSW_FREQUENCY(val));
5269 else
5270 I915_WRITE(GEN6_RPNSWREQ,
5271 GEN6_FREQUENCY(val) |
5272 GEN6_OFFSET(0) |
5273 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005274 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005275
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005276 /* Make sure we continue to get interrupts
5277 * until we hit the minimum or maximum frequencies.
5278 */
Akash Goel74ef1172015-03-06 11:07:19 +05305279 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00005280 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005281
Ben Widawskyb39fb292014-03-19 18:31:11 -07005282 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02005283 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005284
5285 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005286}
5287
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005288static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005289{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005290 int err;
5291
Chris Wilsondc979972016-05-10 14:10:04 +01005292 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005293 "Odd GPU freq value\n"))
5294 val &= ~1;
5295
Deepak Scd25dd52015-07-10 18:31:40 +05305296 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5297
Chris Wilson8fb55192015-04-07 16:20:28 +01005298 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005299 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
5300 if (err)
5301 return err;
5302
Chris Wilsondb4c5e02017-02-10 15:03:46 +00005303 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005304 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005305
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005306 dev_priv->rps.cur_freq = val;
5307 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005308
5309 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005310}
5311
Deepak Sa7f6e232015-05-09 18:04:44 +05305312/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305313 *
5314 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305315 * 1. Forcewake Media well.
5316 * 2. Request idle freq.
5317 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305318*/
5319static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5320{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005321 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005322 int err;
Deepak S5549d252014-06-28 11:26:11 +05305323
Chris Wilsonaed242f2015-03-18 09:48:21 +00005324 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305325 return;
5326
Chris Wilsonc9efef72017-01-02 15:28:45 +00005327 /* The punit delays the write of the frequency and voltage until it
5328 * determines the GPU is awake. During normal usage we don't want to
5329 * waste power changing the frequency if the GPU is sleeping (rc6).
5330 * However, the GPU and driver is now idle and we do not want to delay
5331 * switching to minimum voltage (reducing power whilst idle) as we do
5332 * not expect to be woken in the near future and so must flush the
5333 * change by waking the device.
5334 *
5335 * We choose to take the media powerwell (either would do to trick the
5336 * punit into committing the voltage change) as that takes a lot less
5337 * power than the render powerwell.
5338 */
Deepak Sa7f6e232015-05-09 18:04:44 +05305339 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005340 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305341 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005342
5343 if (err)
5344 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05305345}
5346
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005347void gen6_rps_busy(struct drm_i915_private *dev_priv)
5348{
5349 mutex_lock(&dev_priv->rps.hw_lock);
5350 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00005351 u8 freq;
5352
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005353 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005354 gen6_rps_reset_ei(dev_priv);
5355 I915_WRITE(GEN6_PMINTRMSK,
5356 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005357
Chris Wilsonc33d2472016-07-04 08:08:36 +01005358 gen6_enable_rps_interrupts(dev_priv);
5359
Chris Wilsonbd648182017-02-10 15:03:48 +00005360 /* Use the user's desired frequency as a guide, but for better
5361 * performance, jump directly to RPe as our starting frequency.
5362 */
5363 freq = max(dev_priv->rps.cur_freq,
5364 dev_priv->rps.efficient_freq);
5365
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005366 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00005367 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005368 dev_priv->rps.min_freq_softlimit,
5369 dev_priv->rps.max_freq_softlimit)))
5370 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005371 }
5372 mutex_unlock(&dev_priv->rps.hw_lock);
5373}
5374
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005375void gen6_rps_idle(struct drm_i915_private *dev_priv)
5376{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005377 /* Flush our bottom-half so that it does not race with us
5378 * setting the idle frequency and so that it is bounded by
5379 * our rpm wakeref. And then disable the interrupts to stop any
5380 * futher RPS reclocking whilst we are asleep.
5381 */
5382 gen6_disable_rps_interrupts(dev_priv);
5383
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005384 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005385 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005386 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305387 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005388 else
Chris Wilsondc979972016-05-10 14:10:04 +01005389 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005390 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005391 I915_WRITE(GEN6_PMINTRMSK,
5392 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005393 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005394 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005395
Chris Wilson8d3afd72015-05-21 21:01:47 +01005396 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005397 while (!list_empty(&dev_priv->rps.clients))
5398 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005399 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005400}
5401
Chris Wilson1854d5c2015-04-07 16:20:32 +01005402void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005403 struct intel_rps_client *rps,
5404 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005405{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005406 /* This is intentionally racy! We peek at the state here, then
5407 * validate inside the RPS worker.
5408 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005409 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005410 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005411 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005412 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005413
Chris Wilsone61b9952015-04-27 13:41:24 +01005414 /* Force a RPS boost (and don't count it against the client) if
5415 * the GPU is severely congested.
5416 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005417 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005418 rps = NULL;
5419
Chris Wilson8d3afd72015-05-21 21:01:47 +01005420 spin_lock(&dev_priv->rps.client_lock);
5421 if (rps == NULL || list_empty(&rps->link)) {
5422 spin_lock_irq(&dev_priv->irq_lock);
5423 if (dev_priv->rps.interrupts_enabled) {
5424 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005425 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005426 }
5427 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005428
Chris Wilson2e1b8732015-04-27 13:41:22 +01005429 if (rps != NULL) {
5430 list_add(&rps->link, &dev_priv->rps.clients);
5431 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005432 } else
5433 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005434 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005435 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005436}
5437
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005438int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005439{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005440 int err;
5441
Chris Wilsoncfd1c482017-02-20 09:47:07 +00005442 lockdep_assert_held(&dev_priv->rps.hw_lock);
5443 GEM_BUG_ON(val > dev_priv->rps.max_freq);
5444 GEM_BUG_ON(val < dev_priv->rps.min_freq);
5445
Chris Wilson76e4e4b2017-02-20 09:47:08 +00005446 if (!dev_priv->rps.enabled) {
5447 dev_priv->rps.cur_freq = val;
5448 return 0;
5449 }
5450
Chris Wilsondc979972016-05-10 14:10:04 +01005451 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005452 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005453 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005454 err = gen6_set_rps(dev_priv, val);
5455
5456 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005457}
5458
Chris Wilsondc979972016-05-10 14:10:04 +01005459static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005460{
Zhe Wang20e49362014-11-04 17:07:05 +00005461 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005462 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005463}
5464
Chris Wilsondc979972016-05-10 14:10:04 +01005465static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305466{
Akash Goel2030d682016-04-23 00:05:45 +05305467 I915_WRITE(GEN6_RP_CONTROL, 0);
5468}
5469
Chris Wilsondc979972016-05-10 14:10:04 +01005470static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005471{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005472 I915_WRITE(GEN6_RC_CONTROL, 0);
5473 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305474 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005475}
5476
Chris Wilsondc979972016-05-10 14:10:04 +01005477static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305478{
Deepak S38807742014-05-23 21:00:15 +05305479 I915_WRITE(GEN6_RC_CONTROL, 0);
5480}
5481
Chris Wilsondc979972016-05-10 14:10:04 +01005482static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005483{
Deepak S98a2e5f2014-08-18 10:35:27 -07005484 /* we're doing forcewake before Disabling RC6,
5485 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005486 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005487
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005488 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005489
Mika Kuoppala59bad942015-01-16 11:34:40 +02005490 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005491}
5492
Chris Wilsondc979972016-05-10 14:10:04 +01005493static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005494{
Chris Wilsondc979972016-05-10 14:10:04 +01005495 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005496 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5497 mode = GEN6_RC_CTL_RC6_ENABLE;
5498 else
5499 mode = 0;
5500 }
Chris Wilsondc979972016-05-10 14:10:04 +01005501 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005502 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5503 "RC6 %s RC6p %s RC6pp %s\n",
5504 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5505 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5506 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005507
5508 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005509 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5510 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005511}
5512
Chris Wilsondc979972016-05-10 14:10:04 +01005513static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305514{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005515 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305516 bool enable_rc6 = true;
5517 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005518 u32 rc_ctl;
5519 int rc_sw_target;
5520
5521 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5522 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5523 RC_SW_TARGET_STATE_SHIFT;
5524 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5525 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5526 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5527 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5528 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305529
5530 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005531 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305532 enable_rc6 = false;
5533 }
5534
5535 /*
5536 * The exact context size is not known for BXT, so assume a page size
5537 * for this check.
5538 */
5539 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005540 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5541 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5542 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005543 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305544 enable_rc6 = false;
5545 }
5546
5547 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5548 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5549 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5550 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005551 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305552 enable_rc6 = false;
5553 }
5554
Imre Deakfc619842016-06-29 19:13:55 +03005555 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5556 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5557 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5558 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5559 enable_rc6 = false;
5560 }
5561
5562 if (!I915_READ(GEN6_GFXPAUSE)) {
5563 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5564 enable_rc6 = false;
5565 }
5566
5567 if (!I915_READ(GEN8_MISC_CTRL0)) {
5568 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305569 enable_rc6 = false;
5570 }
5571
5572 return enable_rc6;
5573}
5574
Chris Wilsondc979972016-05-10 14:10:04 +01005575int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005576{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005577 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005578 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005579 return 0;
5580
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305581 if (!enable_rc6)
5582 return 0;
5583
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005584 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305585 DRM_INFO("RC6 disabled by BIOS\n");
5586 return 0;
5587 }
5588
Daniel Vetter456470e2012-08-08 23:35:40 +02005589 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005590 if (enable_rc6 >= 0) {
5591 int mask;
5592
Chris Wilsondc979972016-05-10 14:10:04 +01005593 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005594 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5595 INTEL_RC6pp_ENABLE;
5596 else
5597 mask = INTEL_RC6_ENABLE;
5598
5599 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005600 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5601 "(requested %d, valid %d)\n",
5602 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005603
5604 return enable_rc6 & mask;
5605 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005606
Chris Wilsondc979972016-05-10 14:10:04 +01005607 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005608 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005609
5610 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005611}
5612
Chris Wilsondc979972016-05-10 14:10:04 +01005613static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005614{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005615 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005616
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005617 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005618 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005619 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005620 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5621 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5622 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5623 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005624 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005625 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5626 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5627 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5628 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005629 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005630 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005631
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005632 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005633 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005634 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005635 u32 ddcc_status = 0;
5636
5637 if (sandybridge_pcode_read(dev_priv,
5638 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5639 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005640 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005641 clamp_t(u8,
5642 ((ddcc_status >> 8) & 0xff),
5643 dev_priv->rps.min_freq,
5644 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005645 }
5646
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005647 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305648 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005649 * the natural hardware unit for SKL
5650 */
Akash Goelc5e06882015-06-29 14:50:19 +05305651 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5652 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5653 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5654 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5655 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5656 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005657}
5658
Chris Wilson3a45b052016-07-13 09:10:32 +01005659static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005660 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01005661{
5662 u8 freq = dev_priv->rps.cur_freq;
5663
5664 /* force a reset */
5665 dev_priv->rps.power = -1;
5666 dev_priv->rps.cur_freq = -1;
5667
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005668 if (set(dev_priv, freq))
5669 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01005670}
5671
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005672/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005673static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005674{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005675 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5676
Akash Goel0beb0592015-03-06 11:07:20 +05305677 /* Program defaults and thresholds for RPS*/
5678 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5679 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005680
Akash Goel0beb0592015-03-06 11:07:20 +05305681 /* 1 second timeout*/
5682 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5683 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5684
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005685 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005686
Akash Goel0beb0592015-03-06 11:07:20 +05305687 /* Leaning on the below call to gen6_set_rps to program/setup the
5688 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5689 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005690 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005691
5692 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5693}
5694
Chris Wilsondc979972016-05-10 14:10:04 +01005695static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005696{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005697 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305698 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005699 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005700
5701 /* 1a: Software RC state - RC0 */
5702 I915_WRITE(GEN6_RC_STATE, 0);
5703
5704 /* 1b: Get forcewake during program sequence. Although the driver
5705 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005706 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005707
5708 /* 2a: Disable RC states. */
5709 I915_WRITE(GEN6_RC_CONTROL, 0);
5710
5711 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305712
5713 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005714 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305715 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5716 else
5717 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005718 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5719 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305720 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005721 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305722
Dave Gordon1a3d1892016-05-13 15:36:30 +01005723 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305724 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5725
Zhe Wang20e49362014-11-04 17:07:05 +00005726 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005727
Zhe Wang38c23522015-01-20 12:23:04 +00005728 /* 2c: Program Coarse Power Gating Policies. */
5729 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5730 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5731
Zhe Wang20e49362014-11-04 17:07:05 +00005732 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005733 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005734 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005735 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00005736 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
5737 I915_WRITE(GEN6_RC_CONTROL,
5738 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00005739
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305740 /*
5741 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305742 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305743 */
Chris Wilsondc979972016-05-10 14:10:04 +01005744 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305745 I915_WRITE(GEN9_PG_ENABLE, 0);
5746 else
5747 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5748 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005749
Mika Kuoppala59bad942015-01-16 11:34:40 +02005750 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005751}
5752
Chris Wilsondc979972016-05-10 14:10:04 +01005753static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005754{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005755 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305756 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005757 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005758
5759 /* 1a: Software RC state - RC0 */
5760 I915_WRITE(GEN6_RC_STATE, 0);
5761
5762 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5763 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005764 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005765
5766 /* 2a: Disable RC states. */
5767 I915_WRITE(GEN6_RC_CONTROL, 0);
5768
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005769 /* 2b: Program RC6 thresholds.*/
5770 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5771 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5772 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305773 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005774 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005775 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005776 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005777 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5778 else
5779 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005780
5781 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005782 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005783 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005784 intel_print_rc6_info(dev_priv, rc6_mask);
5785 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005786 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5787 GEN7_RC_CTL_TO_MODE |
5788 rc6_mask);
5789 else
5790 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5791 GEN6_RC_CTL_EI_MODE(1) |
5792 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005793
5794 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005795 I915_WRITE(GEN6_RPNSWREQ,
5796 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5797 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5798 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005799 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5800 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005801
Daniel Vetter7526ed72014-09-29 15:07:19 +02005802 /* Docs recommend 900MHz, and 300 MHz respectively */
5803 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5804 dev_priv->rps.max_freq_softlimit << 24 |
5805 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005806
Daniel Vetter7526ed72014-09-29 15:07:19 +02005807 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5808 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5809 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5810 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005811
Daniel Vetter7526ed72014-09-29 15:07:19 +02005812 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005813
5814 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005815 I915_WRITE(GEN6_RP_CONTROL,
5816 GEN6_RP_MEDIA_TURBO |
5817 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5818 GEN6_RP_MEDIA_IS_GFX |
5819 GEN6_RP_ENABLE |
5820 GEN6_RP_UP_BUSY_AVG |
5821 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005822
Daniel Vetter7526ed72014-09-29 15:07:19 +02005823 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005824
Chris Wilson3a45b052016-07-13 09:10:32 +01005825 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005826
Mika Kuoppala59bad942015-01-16 11:34:40 +02005827 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005828}
5829
Chris Wilsondc979972016-05-10 14:10:04 +01005830static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005831{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005832 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305833 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005834 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005835 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005836 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005837 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005838
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005839 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005840
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005841 /* Here begins a magic sequence of register writes to enable
5842 * auto-downclocking.
5843 *
5844 * Perhaps there might be some value in exposing these to
5845 * userspace...
5846 */
5847 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005848
5849 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005850 gtfifodbg = I915_READ(GTFIFODBG);
5851 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005852 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5853 I915_WRITE(GTFIFODBG, gtfifodbg);
5854 }
5855
Mika Kuoppala59bad942015-01-16 11:34:40 +02005856 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005857
5858 /* disable the counters and set deterministic thresholds */
5859 I915_WRITE(GEN6_RC_CONTROL, 0);
5860
5861 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5862 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5863 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5864 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5865 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5866
Akash Goel3b3f1652016-10-13 22:44:48 +05305867 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005868 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005869
5870 I915_WRITE(GEN6_RC_SLEEP, 0);
5871 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005872 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005873 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5874 else
5875 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005876 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005877 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5878
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005879 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005880 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005881 if (rc6_mode & INTEL_RC6_ENABLE)
5882 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5883
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005884 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005885 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005886 if (rc6_mode & INTEL_RC6p_ENABLE)
5887 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005888
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005889 if (rc6_mode & INTEL_RC6pp_ENABLE)
5890 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5891 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005892
Chris Wilsondc979972016-05-10 14:10:04 +01005893 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005894
5895 I915_WRITE(GEN6_RC_CONTROL,
5896 rc6_mask |
5897 GEN6_RC_CTL_EI_MODE(1) |
5898 GEN6_RC_CTL_HW_ENABLE);
5899
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005900 /* Power down if completely idle for over 50ms */
5901 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005902 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005903
Chris Wilson3a45b052016-07-13 09:10:32 +01005904 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005905
Ben Widawsky31643d52012-09-26 10:34:01 -07005906 rc6vids = 0;
5907 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005908 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005909 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005910 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005911 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5912 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5913 rc6vids &= 0xffff00;
5914 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5915 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5916 if (ret)
5917 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5918 }
5919
Mika Kuoppala59bad942015-01-16 11:34:40 +02005920 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005921}
5922
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005923static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005924{
5925 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005926 unsigned int gpu_freq;
5927 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305928 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005929 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005930 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005931
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005932 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005933
Ben Widawskyeda79642013-10-07 17:15:48 -03005934 policy = cpufreq_cpu_get(0);
5935 if (policy) {
5936 max_ia_freq = policy->cpuinfo.max_freq;
5937 cpufreq_cpu_put(policy);
5938 } else {
5939 /*
5940 * Default to measured freq if none found, PCU will ensure we
5941 * don't go over
5942 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005943 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005944 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005945
5946 /* Convert from kHz to MHz */
5947 max_ia_freq /= 1000;
5948
Ben Widawsky153b4b952013-10-22 22:05:09 -07005949 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005950 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5951 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005952
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005953 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305954 /* Convert GT frequency to 50 HZ units */
5955 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5956 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5957 } else {
5958 min_gpu_freq = dev_priv->rps.min_freq;
5959 max_gpu_freq = dev_priv->rps.max_freq;
5960 }
5961
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005962 /*
5963 * For each potential GPU frequency, load a ring frequency we'd like
5964 * to use for memory access. We do this by specifying the IA frequency
5965 * the PCU should use as a reference to determine the ring frequency.
5966 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305967 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5968 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005969 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005970
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005971 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305972 /*
5973 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5974 * No floor required for ring frequency on SKL.
5975 */
5976 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005977 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005978 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5979 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005980 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005981 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005982 ring_freq = max(min_ring_freq, ring_freq);
5983 /* leave ia_freq as the default, chosen by cpufreq */
5984 } else {
5985 /* On older processors, there is no separate ring
5986 * clock domain, so in order to boost the bandwidth
5987 * of the ring, we need to upclock the CPU (ia_freq).
5988 *
5989 * For GPU frequencies less than 750MHz,
5990 * just use the lowest ring freq.
5991 */
5992 if (gpu_freq < min_freq)
5993 ia_freq = 800;
5994 else
5995 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5996 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5997 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005998
Ben Widawsky42c05262012-09-26 10:34:00 -07005999 sandybridge_pcode_write(dev_priv,
6000 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006001 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6002 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6003 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006004 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006005}
6006
Ville Syrjälä03af2042014-06-28 02:03:53 +03006007static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306008{
6009 u32 val, rp0;
6010
Jani Nikula5b5929c2015-10-07 11:17:46 +03006011 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306012
Imre Deak43b67992016-08-31 19:13:02 +03006013 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006014 case 8:
6015 /* (2 * 4) config */
6016 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6017 break;
6018 case 12:
6019 /* (2 * 6) config */
6020 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6021 break;
6022 case 16:
6023 /* (2 * 8) config */
6024 default:
6025 /* Setting (2 * 8) Min RP0 for any other combination */
6026 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6027 break;
Deepak S095acd52015-01-17 11:05:59 +05306028 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006029
6030 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6031
Deepak S2b6b3a02014-05-27 15:59:30 +05306032 return rp0;
6033}
6034
6035static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6036{
6037 u32 val, rpe;
6038
6039 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6040 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6041
6042 return rpe;
6043}
6044
Deepak S7707df42014-07-12 18:46:14 +05306045static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6046{
6047 u32 val, rp1;
6048
Jani Nikula5b5929c2015-10-07 11:17:46 +03006049 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6050 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6051
Deepak S7707df42014-07-12 18:46:14 +05306052 return rp1;
6053}
6054
Deepak S96676fe2016-08-12 18:46:41 +05306055static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6056{
6057 u32 val, rpn;
6058
6059 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6060 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6061 FB_GFX_FREQ_FUSE_MASK);
6062
6063 return rpn;
6064}
6065
Deepak Sf8f2b002014-07-10 13:16:21 +05306066static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6067{
6068 u32 val, rp1;
6069
6070 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6071
6072 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6073
6074 return rp1;
6075}
6076
Ville Syrjälä03af2042014-06-28 02:03:53 +03006077static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006078{
6079 u32 val, rp0;
6080
Jani Nikula64936252013-05-22 15:36:20 +03006081 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006082
6083 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6084 /* Clamp to max */
6085 rp0 = min_t(u32, rp0, 0xea);
6086
6087 return rp0;
6088}
6089
6090static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6091{
6092 u32 val, rpe;
6093
Jani Nikula64936252013-05-22 15:36:20 +03006094 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006095 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006096 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006097 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6098
6099 return rpe;
6100}
6101
Ville Syrjälä03af2042014-06-28 02:03:53 +03006102static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006103{
Imre Deak36146032014-12-04 18:39:35 +02006104 u32 val;
6105
6106 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6107 /*
6108 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6109 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6110 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6111 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6112 * to make sure it matches what Punit accepts.
6113 */
6114 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006115}
6116
Imre Deakae484342014-03-31 15:10:44 +03006117/* Check that the pctx buffer wasn't move under us. */
6118static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6119{
6120 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6121
6122 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6123 dev_priv->vlv_pctx->stolen->start);
6124}
6125
Deepak S38807742014-05-23 21:00:15 +05306126
6127/* Check that the pcbr address is not empty. */
6128static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6129{
6130 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6131
6132 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6133}
6134
Chris Wilsondc979972016-05-10 14:10:04 +01006135static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306136{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006137 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006138 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306139 u32 pcbr;
6140 int pctx_size = 32*1024;
6141
Deepak S38807742014-05-23 21:00:15 +05306142 pcbr = I915_READ(VLV_PCBR);
6143 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006144 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306145 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006146 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306147
6148 pctx_paddr = (paddr & (~4095));
6149 I915_WRITE(VLV_PCBR, pctx_paddr);
6150 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006151
6152 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306153}
6154
Chris Wilsondc979972016-05-10 14:10:04 +01006155static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006156{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006157 struct drm_i915_gem_object *pctx;
6158 unsigned long pctx_paddr;
6159 u32 pcbr;
6160 int pctx_size = 24*1024;
6161
6162 pcbr = I915_READ(VLV_PCBR);
6163 if (pcbr) {
6164 /* BIOS set it up already, grab the pre-alloc'd space */
6165 int pcbr_offset;
6166
6167 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006168 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006169 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006170 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006171 pctx_size);
6172 goto out;
6173 }
6174
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006175 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6176
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006177 /*
6178 * From the Gunit register HAS:
6179 * The Gfx driver is expected to program this register and ensure
6180 * proper allocation within Gfx stolen memory. For example, this
6181 * register should be programmed such than the PCBR range does not
6182 * overlap with other ranges, such as the frame buffer, protected
6183 * memory, or any other relevant ranges.
6184 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006185 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006186 if (!pctx) {
6187 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006188 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006189 }
6190
6191 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6192 I915_WRITE(VLV_PCBR, pctx_paddr);
6193
6194out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006195 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006196 dev_priv->vlv_pctx = pctx;
6197}
6198
Chris Wilsondc979972016-05-10 14:10:04 +01006199static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006200{
Imre Deakae484342014-03-31 15:10:44 +03006201 if (WARN_ON(!dev_priv->vlv_pctx))
6202 return;
6203
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006204 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006205 dev_priv->vlv_pctx = NULL;
6206}
6207
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006208static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6209{
6210 dev_priv->rps.gpll_ref_freq =
6211 vlv_get_cck_clock(dev_priv, "GPLL ref",
6212 CCK_GPLL_CLOCK_CONTROL,
6213 dev_priv->czclk_freq);
6214
6215 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6216 dev_priv->rps.gpll_ref_freq);
6217}
6218
Chris Wilsondc979972016-05-10 14:10:04 +01006219static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006220{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006221 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006222
Chris Wilsondc979972016-05-10 14:10:04 +01006223 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006224
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006225 vlv_init_gpll_ref_freq(dev_priv);
6226
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006227 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6228 switch ((val >> 6) & 3) {
6229 case 0:
6230 case 1:
6231 dev_priv->mem_freq = 800;
6232 break;
6233 case 2:
6234 dev_priv->mem_freq = 1066;
6235 break;
6236 case 3:
6237 dev_priv->mem_freq = 1333;
6238 break;
6239 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006240 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006241
Imre Deak4e805192014-04-14 20:24:41 +03006242 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6243 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6244 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006245 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006246 dev_priv->rps.max_freq);
6247
6248 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6249 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006250 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006251 dev_priv->rps.efficient_freq);
6252
Deepak Sf8f2b002014-07-10 13:16:21 +05306253 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6254 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006255 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306256 dev_priv->rps.rp1_freq);
6257
Imre Deak4e805192014-04-14 20:24:41 +03006258 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6259 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006260 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006261 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006262}
6263
Chris Wilsondc979972016-05-10 14:10:04 +01006264static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306265{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006266 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306267
Chris Wilsondc979972016-05-10 14:10:04 +01006268 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306269
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006270 vlv_init_gpll_ref_freq(dev_priv);
6271
Ville Syrjäläa5805162015-05-26 20:42:30 +03006272 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006273 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006274 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006275
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006276 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006277 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006278 dev_priv->mem_freq = 2000;
6279 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006280 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006281 dev_priv->mem_freq = 1600;
6282 break;
6283 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006284 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006285
Deepak S2b6b3a02014-05-27 15:59:30 +05306286 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
6287 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6288 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006289 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306290 dev_priv->rps.max_freq);
6291
6292 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
6293 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006294 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306295 dev_priv->rps.efficient_freq);
6296
Deepak S7707df42014-07-12 18:46:14 +05306297 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
6298 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006299 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05306300 dev_priv->rps.rp1_freq);
6301
Deepak S96676fe2016-08-12 18:46:41 +05306302 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306303 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006304 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306305 dev_priv->rps.min_freq);
6306
Ville Syrjälä1c147622014-08-18 14:42:43 +03006307 WARN_ONCE((dev_priv->rps.max_freq |
6308 dev_priv->rps.efficient_freq |
6309 dev_priv->rps.rp1_freq |
6310 dev_priv->rps.min_freq) & 1,
6311 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05306312}
6313
Chris Wilsondc979972016-05-10 14:10:04 +01006314static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006315{
Chris Wilsondc979972016-05-10 14:10:04 +01006316 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006317}
6318
Chris Wilsondc979972016-05-10 14:10:04 +01006319static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306320{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006321 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306322 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306323 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306324
6325 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6326
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006327 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6328 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306329 if (gtfifodbg) {
6330 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6331 gtfifodbg);
6332 I915_WRITE(GTFIFODBG, gtfifodbg);
6333 }
6334
6335 cherryview_check_pctx(dev_priv);
6336
6337 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6338 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006339 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306340
Ville Syrjälä160614a2015-01-19 13:50:47 +02006341 /* Disable RC states. */
6342 I915_WRITE(GEN6_RC_CONTROL, 0);
6343
Deepak S38807742014-05-23 21:00:15 +05306344 /* 2a: Program RC6 thresholds.*/
6345 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6346 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6347 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6348
Akash Goel3b3f1652016-10-13 22:44:48 +05306349 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006350 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306351 I915_WRITE(GEN6_RC_SLEEP, 0);
6352
Deepak Sf4f71c72015-03-28 15:23:35 +05306353 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6354 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306355
6356 /* allows RC6 residency counter to work */
6357 I915_WRITE(VLV_COUNTER_CONTROL,
6358 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6359 VLV_MEDIA_RC6_COUNT_EN |
6360 VLV_RENDER_RC6_COUNT_EN));
6361
6362 /* For now we assume BIOS is allocating and populating the PCBR */
6363 pcbr = I915_READ(VLV_PCBR);
6364
Deepak S38807742014-05-23 21:00:15 +05306365 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006366 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6367 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006368 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306369
6370 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6371
Deepak S2b6b3a02014-05-27 15:59:30 +05306372 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006373 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306374 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6375 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6376 I915_WRITE(GEN6_RP_UP_EI, 66000);
6377 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6378
6379 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6380
6381 /* 5: Enable RPS */
6382 I915_WRITE(GEN6_RP_CONTROL,
6383 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006384 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306385 GEN6_RP_ENABLE |
6386 GEN6_RP_UP_BUSY_AVG |
6387 GEN6_RP_DOWN_IDLE_AVG);
6388
Deepak S3ef62342015-04-29 08:36:24 +05306389 /* Setting Fixed Bias */
6390 val = VLV_OVERRIDE_EN |
6391 VLV_SOC_TDP_EN |
6392 CHV_BIAS_CPU_50_SOC_50;
6393 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6394
Deepak S2b6b3a02014-05-27 15:59:30 +05306395 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6396
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006397 /* RPS code assumes GPLL is used */
6398 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6399
Jani Nikula742f4912015-09-03 11:16:09 +03006400 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306401 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6402
Chris Wilson3a45b052016-07-13 09:10:32 +01006403 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306404
Mika Kuoppala59bad942015-01-16 11:34:40 +02006405 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306406}
6407
Chris Wilsondc979972016-05-10 14:10:04 +01006408static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006409{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006410 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306411 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006412 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006413
6414 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6415
Imre Deakae484342014-03-31 15:10:44 +03006416 valleyview_check_pctx(dev_priv);
6417
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006418 gtfifodbg = I915_READ(GTFIFODBG);
6419 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006420 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6421 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006422 I915_WRITE(GTFIFODBG, gtfifodbg);
6423 }
6424
Deepak Sc8d9a592013-11-23 14:55:42 +05306425 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006426 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006427
Ville Syrjälä160614a2015-01-19 13:50:47 +02006428 /* Disable RC states. */
6429 I915_WRITE(GEN6_RC_CONTROL, 0);
6430
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006431 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006432 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6433 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6434 I915_WRITE(GEN6_RP_UP_EI, 66000);
6435 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6436
6437 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6438
6439 I915_WRITE(GEN6_RP_CONTROL,
6440 GEN6_RP_MEDIA_TURBO |
6441 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6442 GEN6_RP_MEDIA_IS_GFX |
6443 GEN6_RP_ENABLE |
6444 GEN6_RP_UP_BUSY_AVG |
6445 GEN6_RP_DOWN_IDLE_CONT);
6446
6447 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6448 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6449 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6450
Akash Goel3b3f1652016-10-13 22:44:48 +05306451 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006452 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006453
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08006454 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006455
6456 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006457 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02006458 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6459 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04006460 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006461 VLV_MEDIA_RC6_COUNT_EN |
6462 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006463
Chris Wilsondc979972016-05-10 14:10:04 +01006464 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006465 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006466
Chris Wilsondc979972016-05-10 14:10:04 +01006467 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006468
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006469 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006470
Deepak S3ef62342015-04-29 08:36:24 +05306471 /* Setting Fixed Bias */
6472 val = VLV_OVERRIDE_EN |
6473 VLV_SOC_TDP_EN |
6474 VLV_BIAS_CPU_125_SOC_875;
6475 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6476
Jani Nikula64936252013-05-22 15:36:20 +03006477 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006478
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006479 /* RPS code assumes GPLL is used */
6480 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6481
Jani Nikula742f4912015-09-03 11:16:09 +03006482 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006483 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6484
Chris Wilson3a45b052016-07-13 09:10:32 +01006485 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006486
Mika Kuoppala59bad942015-01-16 11:34:40 +02006487 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006488}
6489
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006490static unsigned long intel_pxfreq(u32 vidfreq)
6491{
6492 unsigned long freq;
6493 int div = (vidfreq & 0x3f0000) >> 16;
6494 int post = (vidfreq & 0x3000) >> 12;
6495 int pre = (vidfreq & 0x7);
6496
6497 if (!pre)
6498 return 0;
6499
6500 freq = ((div * 133333) / ((1<<post) * pre));
6501
6502 return freq;
6503}
6504
Daniel Vettereb48eb02012-04-26 23:28:12 +02006505static const struct cparams {
6506 u16 i;
6507 u16 t;
6508 u16 m;
6509 u16 c;
6510} cparams[] = {
6511 { 1, 1333, 301, 28664 },
6512 { 1, 1066, 294, 24460 },
6513 { 1, 800, 294, 25192 },
6514 { 0, 1333, 276, 27605 },
6515 { 0, 1066, 276, 27605 },
6516 { 0, 800, 231, 23784 },
6517};
6518
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006519static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006520{
6521 u64 total_count, diff, ret;
6522 u32 count1, count2, count3, m = 0, c = 0;
6523 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6524 int i;
6525
Chris Wilson67520412017-03-02 13:28:01 +00006526 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006527
Daniel Vetter20e4d402012-08-08 23:35:39 +02006528 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006529
6530 /* Prevent division-by-zero if we are asking too fast.
6531 * Also, we don't get interesting results if we are polling
6532 * faster than once in 10ms, so just return the saved value
6533 * in such cases.
6534 */
6535 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006536 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006537
6538 count1 = I915_READ(DMIEC);
6539 count2 = I915_READ(DDREC);
6540 count3 = I915_READ(CSIEC);
6541
6542 total_count = count1 + count2 + count3;
6543
6544 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006545 if (total_count < dev_priv->ips.last_count1) {
6546 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006547 diff += total_count;
6548 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006549 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006550 }
6551
6552 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006553 if (cparams[i].i == dev_priv->ips.c_m &&
6554 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006555 m = cparams[i].m;
6556 c = cparams[i].c;
6557 break;
6558 }
6559 }
6560
6561 diff = div_u64(diff, diff1);
6562 ret = ((m * diff) + c);
6563 ret = div_u64(ret, 10);
6564
Daniel Vetter20e4d402012-08-08 23:35:39 +02006565 dev_priv->ips.last_count1 = total_count;
6566 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006567
Daniel Vetter20e4d402012-08-08 23:35:39 +02006568 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006569
6570 return ret;
6571}
6572
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006573unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6574{
6575 unsigned long val;
6576
Chris Wilsondc979972016-05-10 14:10:04 +01006577 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006578 return 0;
6579
6580 spin_lock_irq(&mchdev_lock);
6581
6582 val = __i915_chipset_val(dev_priv);
6583
6584 spin_unlock_irq(&mchdev_lock);
6585
6586 return val;
6587}
6588
Daniel Vettereb48eb02012-04-26 23:28:12 +02006589unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6590{
6591 unsigned long m, x, b;
6592 u32 tsfs;
6593
6594 tsfs = I915_READ(TSFS);
6595
6596 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6597 x = I915_READ8(TR1);
6598
6599 b = tsfs & TSFS_INTR_MASK;
6600
6601 return ((m * x) / 127) - b;
6602}
6603
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006604static int _pxvid_to_vd(u8 pxvid)
6605{
6606 if (pxvid == 0)
6607 return 0;
6608
6609 if (pxvid >= 8 && pxvid < 31)
6610 pxvid = 31;
6611
6612 return (pxvid + 2) * 125;
6613}
6614
6615static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006616{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006617 const int vd = _pxvid_to_vd(pxvid);
6618 const int vm = vd - 1125;
6619
Chris Wilsondc979972016-05-10 14:10:04 +01006620 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006621 return vm > 0 ? vm : 0;
6622
6623 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006624}
6625
Daniel Vetter02d71952012-08-09 16:44:54 +02006626static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006627{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006628 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006629 u32 count;
6630
Chris Wilson67520412017-03-02 13:28:01 +00006631 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006632
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006633 now = ktime_get_raw_ns();
6634 diffms = now - dev_priv->ips.last_time2;
6635 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006636
6637 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006638 if (!diffms)
6639 return;
6640
6641 count = I915_READ(GFXEC);
6642
Daniel Vetter20e4d402012-08-08 23:35:39 +02006643 if (count < dev_priv->ips.last_count2) {
6644 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006645 diff += count;
6646 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006647 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006648 }
6649
Daniel Vetter20e4d402012-08-08 23:35:39 +02006650 dev_priv->ips.last_count2 = count;
6651 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006652
6653 /* More magic constants... */
6654 diff = diff * 1181;
6655 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006656 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006657}
6658
Daniel Vetter02d71952012-08-09 16:44:54 +02006659void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6660{
Chris Wilsondc979972016-05-10 14:10:04 +01006661 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006662 return;
6663
Daniel Vetter92703882012-08-09 16:46:01 +02006664 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006665
6666 __i915_update_gfx_val(dev_priv);
6667
Daniel Vetter92703882012-08-09 16:46:01 +02006668 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006669}
6670
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006671static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006672{
6673 unsigned long t, corr, state1, corr2, state2;
6674 u32 pxvid, ext_v;
6675
Chris Wilson67520412017-03-02 13:28:01 +00006676 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006677
Ville Syrjälä616847e2015-09-18 20:03:19 +03006678 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006679 pxvid = (pxvid >> 24) & 0x7f;
6680 ext_v = pvid_to_extvid(dev_priv, pxvid);
6681
6682 state1 = ext_v;
6683
6684 t = i915_mch_val(dev_priv);
6685
6686 /* Revel in the empirically derived constants */
6687
6688 /* Correction factor in 1/100000 units */
6689 if (t > 80)
6690 corr = ((t * 2349) + 135940);
6691 else if (t >= 50)
6692 corr = ((t * 964) + 29317);
6693 else /* < 50 */
6694 corr = ((t * 301) + 1004);
6695
6696 corr = corr * ((150142 * state1) / 10000 - 78642);
6697 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006698 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006699
6700 state2 = (corr2 * state1) / 10000;
6701 state2 /= 100; /* convert to mW */
6702
Daniel Vetter02d71952012-08-09 16:44:54 +02006703 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006704
Daniel Vetter20e4d402012-08-08 23:35:39 +02006705 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006706}
6707
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006708unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6709{
6710 unsigned long val;
6711
Chris Wilsondc979972016-05-10 14:10:04 +01006712 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006713 return 0;
6714
6715 spin_lock_irq(&mchdev_lock);
6716
6717 val = __i915_gfx_val(dev_priv);
6718
6719 spin_unlock_irq(&mchdev_lock);
6720
6721 return val;
6722}
6723
Daniel Vettereb48eb02012-04-26 23:28:12 +02006724/**
6725 * i915_read_mch_val - return value for IPS use
6726 *
6727 * Calculate and return a value for the IPS driver to use when deciding whether
6728 * we have thermal and power headroom to increase CPU or GPU power budget.
6729 */
6730unsigned long i915_read_mch_val(void)
6731{
6732 struct drm_i915_private *dev_priv;
6733 unsigned long chipset_val, graphics_val, ret = 0;
6734
Daniel Vetter92703882012-08-09 16:46:01 +02006735 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006736 if (!i915_mch_dev)
6737 goto out_unlock;
6738 dev_priv = i915_mch_dev;
6739
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006740 chipset_val = __i915_chipset_val(dev_priv);
6741 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006742
6743 ret = chipset_val + graphics_val;
6744
6745out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006746 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006747
6748 return ret;
6749}
6750EXPORT_SYMBOL_GPL(i915_read_mch_val);
6751
6752/**
6753 * i915_gpu_raise - raise GPU frequency limit
6754 *
6755 * Raise the limit; IPS indicates we have thermal headroom.
6756 */
6757bool i915_gpu_raise(void)
6758{
6759 struct drm_i915_private *dev_priv;
6760 bool ret = true;
6761
Daniel Vetter92703882012-08-09 16:46:01 +02006762 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006763 if (!i915_mch_dev) {
6764 ret = false;
6765 goto out_unlock;
6766 }
6767 dev_priv = i915_mch_dev;
6768
Daniel Vetter20e4d402012-08-08 23:35:39 +02006769 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6770 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006771
6772out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006773 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006774
6775 return ret;
6776}
6777EXPORT_SYMBOL_GPL(i915_gpu_raise);
6778
6779/**
6780 * i915_gpu_lower - lower GPU frequency limit
6781 *
6782 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6783 * frequency maximum.
6784 */
6785bool i915_gpu_lower(void)
6786{
6787 struct drm_i915_private *dev_priv;
6788 bool ret = true;
6789
Daniel Vetter92703882012-08-09 16:46:01 +02006790 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006791 if (!i915_mch_dev) {
6792 ret = false;
6793 goto out_unlock;
6794 }
6795 dev_priv = i915_mch_dev;
6796
Daniel Vetter20e4d402012-08-08 23:35:39 +02006797 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6798 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006799
6800out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006801 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006802
6803 return ret;
6804}
6805EXPORT_SYMBOL_GPL(i915_gpu_lower);
6806
6807/**
6808 * i915_gpu_busy - indicate GPU business to IPS
6809 *
6810 * Tell the IPS driver whether or not the GPU is busy.
6811 */
6812bool i915_gpu_busy(void)
6813{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006814 bool ret = false;
6815
Daniel Vetter92703882012-08-09 16:46:01 +02006816 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006817 if (i915_mch_dev)
6818 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006819 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006820
6821 return ret;
6822}
6823EXPORT_SYMBOL_GPL(i915_gpu_busy);
6824
6825/**
6826 * i915_gpu_turbo_disable - disable graphics turbo
6827 *
6828 * Disable graphics turbo by resetting the max frequency and setting the
6829 * current frequency to the default.
6830 */
6831bool i915_gpu_turbo_disable(void)
6832{
6833 struct drm_i915_private *dev_priv;
6834 bool ret = true;
6835
Daniel Vetter92703882012-08-09 16:46:01 +02006836 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006837 if (!i915_mch_dev) {
6838 ret = false;
6839 goto out_unlock;
6840 }
6841 dev_priv = i915_mch_dev;
6842
Daniel Vetter20e4d402012-08-08 23:35:39 +02006843 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006844
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006845 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006846 ret = false;
6847
6848out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006849 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006850
6851 return ret;
6852}
6853EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6854
6855/**
6856 * Tells the intel_ips driver that the i915 driver is now loaded, if
6857 * IPS got loaded first.
6858 *
6859 * This awkward dance is so that neither module has to depend on the
6860 * other in order for IPS to do the appropriate communication of
6861 * GPU turbo limits to i915.
6862 */
6863static void
6864ips_ping_for_i915_load(void)
6865{
6866 void (*link)(void);
6867
6868 link = symbol_get(ips_link_to_i915_driver);
6869 if (link) {
6870 link();
6871 symbol_put(ips_link_to_i915_driver);
6872 }
6873}
6874
6875void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6876{
Daniel Vetter02d71952012-08-09 16:44:54 +02006877 /* We only register the i915 ips part with intel-ips once everything is
6878 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006879 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006880 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006881 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006882
6883 ips_ping_for_i915_load();
6884}
6885
6886void intel_gpu_ips_teardown(void)
6887{
Daniel Vetter92703882012-08-09 16:46:01 +02006888 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006889 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006890 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006891}
Deepak S76c3552f2014-01-30 23:08:16 +05306892
Chris Wilsondc979972016-05-10 14:10:04 +01006893static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006894{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006895 u32 lcfuse;
6896 u8 pxw[16];
6897 int i;
6898
6899 /* Disable to program */
6900 I915_WRITE(ECR, 0);
6901 POSTING_READ(ECR);
6902
6903 /* Program energy weights for various events */
6904 I915_WRITE(SDEW, 0x15040d00);
6905 I915_WRITE(CSIEW0, 0x007f0000);
6906 I915_WRITE(CSIEW1, 0x1e220004);
6907 I915_WRITE(CSIEW2, 0x04000004);
6908
6909 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006910 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006911 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006912 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006913
6914 /* Program P-state weights to account for frequency power adjustment */
6915 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006916 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006917 unsigned long freq = intel_pxfreq(pxvidfreq);
6918 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6919 PXVFREQ_PX_SHIFT;
6920 unsigned long val;
6921
6922 val = vid * vid;
6923 val *= (freq / 1000);
6924 val *= 255;
6925 val /= (127*127*900);
6926 if (val > 0xff)
6927 DRM_ERROR("bad pxval: %ld\n", val);
6928 pxw[i] = val;
6929 }
6930 /* Render standby states get 0 weight */
6931 pxw[14] = 0;
6932 pxw[15] = 0;
6933
6934 for (i = 0; i < 4; i++) {
6935 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6936 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006937 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006938 }
6939
6940 /* Adjust magic regs to magic values (more experimental results) */
6941 I915_WRITE(OGW0, 0);
6942 I915_WRITE(OGW1, 0);
6943 I915_WRITE(EG0, 0x00007f00);
6944 I915_WRITE(EG1, 0x0000000e);
6945 I915_WRITE(EG2, 0x000e0000);
6946 I915_WRITE(EG3, 0x68000300);
6947 I915_WRITE(EG4, 0x42000000);
6948 I915_WRITE(EG5, 0x00140031);
6949 I915_WRITE(EG6, 0);
6950 I915_WRITE(EG7, 0);
6951
6952 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006953 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006954
6955 /* Enable PMON + select events */
6956 I915_WRITE(ECR, 0x80000019);
6957
6958 lcfuse = I915_READ(LCFUSE02);
6959
Daniel Vetter20e4d402012-08-08 23:35:39 +02006960 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006961}
6962
Chris Wilsondc979972016-05-10 14:10:04 +01006963void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006964{
Imre Deakb268c692015-12-15 20:10:31 +02006965 /*
6966 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6967 * requirement.
6968 */
6969 if (!i915.enable_rc6) {
6970 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6971 intel_runtime_pm_get(dev_priv);
6972 }
Imre Deake6069ca2014-04-18 16:01:02 +03006973
Chris Wilsonb5163db2016-08-10 13:58:24 +01006974 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006975 mutex_lock(&dev_priv->rps.hw_lock);
6976
6977 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006978 if (IS_CHERRYVIEW(dev_priv))
6979 cherryview_init_gt_powersave(dev_priv);
6980 else if (IS_VALLEYVIEW(dev_priv))
6981 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006982 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006983 gen6_init_rps_frequencies(dev_priv);
6984
6985 /* Derive initial user preferences/limits from the hardware limits */
6986 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6987 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6988
6989 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6990 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6991
6992 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6993 dev_priv->rps.min_freq_softlimit =
6994 max_t(int,
6995 dev_priv->rps.efficient_freq,
6996 intel_freq_opcode(dev_priv, 450));
6997
Chris Wilson99ac9612016-07-13 09:10:34 +01006998 /* After setting max-softlimit, find the overclock max freq */
6999 if (IS_GEN6(dev_priv) ||
7000 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7001 u32 params = 0;
7002
7003 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7004 if (params & BIT(31)) { /* OC supported */
7005 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7006 (dev_priv->rps.max_freq & 0xff) * 50,
7007 (params & 0xff) * 50);
7008 dev_priv->rps.max_freq = params & 0xff;
7009 }
7010 }
7011
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007012 /* Finally allow us to boost to max by default */
7013 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7014
Chris Wilson773ea9a2016-07-13 09:10:33 +01007015 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007016 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007017
7018 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007019}
7020
Chris Wilsondc979972016-05-10 14:10:04 +01007021void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007022{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007023 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007024 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007025
7026 if (!i915.enable_rc6)
7027 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007028}
7029
Chris Wilson54b4f682016-07-21 21:16:19 +01007030/**
7031 * intel_suspend_gt_powersave - suspend PM work and helper threads
7032 * @dev_priv: i915 device
7033 *
7034 * We don't want to disable RC6 or other features here, we just want
7035 * to make sure any work we've queued has finished and won't bother
7036 * us while we're suspended.
7037 */
7038void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7039{
7040 if (INTEL_GEN(dev_priv) < 6)
7041 return;
7042
7043 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7044 intel_runtime_pm_put(dev_priv);
7045
7046 /* gen6_rps_idle() will be called later to disable interrupts */
7047}
7048
Chris Wilsonb7137e02016-07-13 09:10:37 +01007049void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7050{
7051 dev_priv->rps.enabled = true; /* force disabling */
7052 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007053
7054 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007055}
7056
Chris Wilsondc979972016-05-10 14:10:04 +01007057void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007058{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007059 if (!READ_ONCE(dev_priv->rps.enabled))
7060 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007061
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007062 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007063
Chris Wilsonb7137e02016-07-13 09:10:37 +01007064 if (INTEL_GEN(dev_priv) >= 9) {
7065 gen9_disable_rc6(dev_priv);
7066 gen9_disable_rps(dev_priv);
7067 } else if (IS_CHERRYVIEW(dev_priv)) {
7068 cherryview_disable_rps(dev_priv);
7069 } else if (IS_VALLEYVIEW(dev_priv)) {
7070 valleyview_disable_rps(dev_priv);
7071 } else if (INTEL_GEN(dev_priv) >= 6) {
7072 gen6_disable_rps(dev_priv);
7073 } else if (IS_IRONLAKE_M(dev_priv)) {
7074 ironlake_disable_drps(dev_priv);
7075 }
7076
7077 dev_priv->rps.enabled = false;
7078 mutex_unlock(&dev_priv->rps.hw_lock);
7079}
7080
7081void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7082{
Chris Wilson54b4f682016-07-21 21:16:19 +01007083 /* We shouldn't be disabling as we submit, so this should be less
7084 * racy than it appears!
7085 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007086 if (READ_ONCE(dev_priv->rps.enabled))
7087 return;
7088
7089 /* Powersaving is controlled by the host when inside a VM */
7090 if (intel_vgpu_active(dev_priv))
7091 return;
7092
7093 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007094
Chris Wilsondc979972016-05-10 14:10:04 +01007095 if (IS_CHERRYVIEW(dev_priv)) {
7096 cherryview_enable_rps(dev_priv);
7097 } else if (IS_VALLEYVIEW(dev_priv)) {
7098 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007099 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007100 gen9_enable_rc6(dev_priv);
7101 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007102 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007103 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007104 } else if (IS_BROADWELL(dev_priv)) {
7105 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007106 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007107 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007108 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007109 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007110 } else if (IS_IRONLAKE_M(dev_priv)) {
7111 ironlake_enable_drps(dev_priv);
7112 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007113 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007114
7115 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7116 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7117
7118 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7119 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7120
Chris Wilson54b4f682016-07-21 21:16:19 +01007121 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007122 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007123}
Imre Deakc6df39b2014-04-14 20:24:29 +03007124
Chris Wilson54b4f682016-07-21 21:16:19 +01007125static void __intel_autoenable_gt_powersave(struct work_struct *work)
7126{
7127 struct drm_i915_private *dev_priv =
7128 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7129 struct intel_engine_cs *rcs;
7130 struct drm_i915_gem_request *req;
7131
7132 if (READ_ONCE(dev_priv->rps.enabled))
7133 goto out;
7134
Akash Goel3b3f1652016-10-13 22:44:48 +05307135 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007136 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007137 goto out;
7138
7139 if (!rcs->init_context)
7140 goto out;
7141
7142 mutex_lock(&dev_priv->drm.struct_mutex);
7143
7144 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7145 if (IS_ERR(req))
7146 goto unlock;
7147
7148 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7149 rcs->init_context(req);
7150
7151 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007152 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007153
7154unlock:
7155 mutex_unlock(&dev_priv->drm.struct_mutex);
7156out:
7157 intel_runtime_pm_put(dev_priv);
7158}
7159
7160void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7161{
7162 if (READ_ONCE(dev_priv->rps.enabled))
7163 return;
7164
7165 if (IS_IRONLAKE_M(dev_priv)) {
7166 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007167 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007168 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7169 /*
7170 * PCU communication is slow and this doesn't need to be
7171 * done at any specific time, so do this out of our fast path
7172 * to make resume and init faster.
7173 *
7174 * We depend on the HW RC6 power context save/restore
7175 * mechanism when entering D3 through runtime PM suspend. So
7176 * disable RPM until RPS/RC6 is properly setup. We can only
7177 * get here via the driver load/system resume/runtime resume
7178 * paths, so the _noresume version is enough (and in case of
7179 * runtime resume it's necessary).
7180 */
7181 if (queue_delayed_work(dev_priv->wq,
7182 &dev_priv->rps.autoenable_work,
7183 round_jiffies_up_relative(HZ)))
7184 intel_runtime_pm_get_noresume(dev_priv);
7185 }
7186}
7187
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007188static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007189{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007190 /*
7191 * On Ibex Peak and Cougar Point, we need to disable clock
7192 * gating for the panel power sequencer or it will fail to
7193 * start up when no ports are active.
7194 */
7195 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7196}
7197
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007198static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007199{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007200 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007201
Damien Lespiau055e3932014-08-18 13:49:10 +01007202 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007203 I915_WRITE(DSPCNTR(pipe),
7204 I915_READ(DSPCNTR(pipe)) |
7205 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007206
7207 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7208 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007209 }
7210}
7211
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007212static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007213{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007214 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7215 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7216 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7217
7218 /*
7219 * Don't touch WM1S_LP_EN here.
7220 * Doing so could cause underruns.
7221 */
7222}
7223
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007224static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007225{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007226 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007227
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007228 /*
7229 * Required for FBC
7230 * WaFbcDisableDpfcClockGating:ilk
7231 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007232 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7233 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7234 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007235
7236 I915_WRITE(PCH_3DCGDIS0,
7237 MARIUNIT_CLOCK_GATE_DISABLE |
7238 SVSMUNIT_CLOCK_GATE_DISABLE);
7239 I915_WRITE(PCH_3DCGDIS1,
7240 VFMUNIT_CLOCK_GATE_DISABLE);
7241
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007242 /*
7243 * According to the spec the following bits should be set in
7244 * order to enable memory self-refresh
7245 * The bit 22/21 of 0x42004
7246 * The bit 5 of 0x42020
7247 * The bit 15 of 0x45000
7248 */
7249 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7250 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7251 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007252 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007253 I915_WRITE(DISP_ARB_CTL,
7254 (I915_READ(DISP_ARB_CTL) |
7255 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007256
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007257 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007258
7259 /*
7260 * Based on the document from hardware guys the following bits
7261 * should be set unconditionally in order to enable FBC.
7262 * The bit 22 of 0x42000
7263 * The bit 22 of 0x42004
7264 * The bit 7,8,9 of 0x42020.
7265 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007266 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007267 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007268 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7269 I915_READ(ILK_DISPLAY_CHICKEN1) |
7270 ILK_FBCQ_DIS);
7271 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7272 I915_READ(ILK_DISPLAY_CHICKEN2) |
7273 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007274 }
7275
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007276 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
7277
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007278 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7279 I915_READ(ILK_DISPLAY_CHICKEN2) |
7280 ILK_ELPIN_409_SELECT);
7281 I915_WRITE(_3D_CHICKEN2,
7282 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
7283 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02007284
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007285 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02007286 I915_WRITE(CACHE_MODE_0,
7287 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01007288
Akash Goel4e046322014-04-04 17:14:38 +05307289 /* WaDisable_RenderCache_OperationalFlush:ilk */
7290 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7291
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007292 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007293
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007294 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007295}
7296
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007297static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007298{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007299 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007300 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007301
7302 /*
7303 * On Ibex Peak and Cougar Point, we need to disable clock
7304 * gating for the panel power sequencer or it will fail to
7305 * start up when no ports are active.
7306 */
Jesse Barnescd664072013-10-02 10:34:19 -07007307 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7308 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7309 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007310 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7311 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007312 /* The below fixes the weird display corruption, a few pixels shifted
7313 * downward, on (only) LVDS of some HP laptops with IVY.
7314 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007315 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007316 val = I915_READ(TRANS_CHICKEN2(pipe));
7317 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7318 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007319 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007320 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007321 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7322 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7323 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007324 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7325 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007326 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007327 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007328 I915_WRITE(TRANS_CHICKEN1(pipe),
7329 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7330 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007331}
7332
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007333static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007334{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007335 uint32_t tmp;
7336
7337 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007338 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7339 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7340 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007341}
7342
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007343static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007344{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007345 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007346
Damien Lespiau231e54f2012-10-19 17:55:41 +01007347 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007348
7349 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7350 I915_READ(ILK_DISPLAY_CHICKEN2) |
7351 ILK_ELPIN_409_SELECT);
7352
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007353 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007354 I915_WRITE(_3D_CHICKEN,
7355 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7356
Akash Goel4e046322014-04-04 17:14:38 +05307357 /* WaDisable_RenderCache_OperationalFlush:snb */
7358 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7359
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007360 /*
7361 * BSpec recoomends 8x4 when MSAA is used,
7362 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007363 *
7364 * Note that PS/WM thread counts depend on the WIZ hashing
7365 * disable bit, which we don't touch here, but it's good
7366 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007367 */
7368 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007369 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007370
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007371 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007372
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007373 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007374 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007375
7376 I915_WRITE(GEN6_UCGCTL1,
7377 I915_READ(GEN6_UCGCTL1) |
7378 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7379 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7380
7381 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7382 * gating disable must be set. Failure to set it results in
7383 * flickering pixels due to Z write ordering failures after
7384 * some amount of runtime in the Mesa "fire" demo, and Unigine
7385 * Sanctuary and Tropics, and apparently anything else with
7386 * alpha test or pixel discard.
7387 *
7388 * According to the spec, bit 11 (RCCUNIT) must also be set,
7389 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007390 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007391 * WaDisableRCCUnitClockGating:snb
7392 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007393 */
7394 I915_WRITE(GEN6_UCGCTL2,
7395 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7396 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7397
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007398 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007399 I915_WRITE(_3D_CHICKEN3,
7400 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007401
7402 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007403 * Bspec says:
7404 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7405 * 3DSTATE_SF number of SF output attributes is more than 16."
7406 */
7407 I915_WRITE(_3D_CHICKEN3,
7408 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7409
7410 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007411 * According to the spec the following bits should be
7412 * set in order to enable memory self-refresh and fbc:
7413 * The bit21 and bit22 of 0x42000
7414 * The bit21 and bit22 of 0x42004
7415 * The bit5 and bit7 of 0x42020
7416 * The bit14 of 0x70180
7417 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007418 *
7419 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007420 */
7421 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7422 I915_READ(ILK_DISPLAY_CHICKEN1) |
7423 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7424 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7425 I915_READ(ILK_DISPLAY_CHICKEN2) |
7426 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007427 I915_WRITE(ILK_DSPCLK_GATE_D,
7428 I915_READ(ILK_DSPCLK_GATE_D) |
7429 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7430 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007431
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007432 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007433
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007434 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007435
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007436 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007437}
7438
7439static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7440{
7441 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7442
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007443 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007444 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007445 *
7446 * This actually overrides the dispatch
7447 * mode for all thread types.
7448 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007449 reg &= ~GEN7_FF_SCHED_MASK;
7450 reg |= GEN7_FF_TS_SCHED_HW;
7451 reg |= GEN7_FF_VS_SCHED_HW;
7452 reg |= GEN7_FF_DS_SCHED_HW;
7453
7454 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7455}
7456
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007457static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007458{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007459 /*
7460 * TODO: this bit should only be enabled when really needed, then
7461 * disabled when not needed anymore in order to save power.
7462 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007463 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007464 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7465 I915_READ(SOUTH_DSPCLK_GATE_D) |
7466 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007467
7468 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007469 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7470 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007471 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007472}
7473
Ville Syrjälä712bf362016-10-31 22:37:23 +02007474static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007475{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007476 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007477 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7478
7479 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7480 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7481 }
7482}
7483
Imre Deak450174f2016-05-03 15:54:21 +03007484static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7485 int general_prio_credits,
7486 int high_prio_credits)
7487{
7488 u32 misccpctl;
7489
7490 /* WaTempDisableDOPClkGating:bdw */
7491 misccpctl = I915_READ(GEN7_MISCCPCTL);
7492 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7493
7494 I915_WRITE(GEN8_L3SQCREG1,
7495 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7496 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7497
7498 /*
7499 * Wait at least 100 clocks before re-enabling clock gating.
7500 * See the definition of L3SQCREG1 in BSpec.
7501 */
7502 POSTING_READ(GEN8_L3SQCREG1);
7503 udelay(1);
7504 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7505}
7506
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007507static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007508{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007509 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007510
7511 /* WaDisableSDEUnitClockGating:kbl */
7512 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7513 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7514 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007515
7516 /* WaDisableGamClockGating:kbl */
7517 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7518 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7519 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007520
7521 /* WaFbcNukeOnHostModify:kbl */
7522 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7523 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007524}
7525
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007526static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007527{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007528 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007529
7530 /* WAC6entrylatency:skl */
7531 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7532 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007533
7534 /* WaFbcNukeOnHostModify:skl */
7535 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7536 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007537}
7538
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007539static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007540{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007541 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007542
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007543 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007544
Ben Widawskyab57fff2013-12-12 15:28:04 -08007545 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007546 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007547
Ben Widawskyab57fff2013-12-12 15:28:04 -08007548 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007549 I915_WRITE(CHICKEN_PAR1_1,
7550 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7551
Ben Widawskyab57fff2013-12-12 15:28:04 -08007552 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007553 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007554 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007555 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007556 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007557 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007558
Ben Widawskyab57fff2013-12-12 15:28:04 -08007559 /* WaVSRefCountFullforceMissDisable:bdw */
7560 /* WaDSRefCountFullforceMissDisable:bdw */
7561 I915_WRITE(GEN7_FF_THREAD_MODE,
7562 I915_READ(GEN7_FF_THREAD_MODE) &
7563 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007564
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007565 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7566 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007567
7568 /* WaDisableSDEUnitClockGating:bdw */
7569 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7570 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007571
Imre Deak450174f2016-05-03 15:54:21 +03007572 /* WaProgramL3SqcReg1Default:bdw */
7573 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007574
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007575 /*
7576 * WaGttCachingOffByDefault:bdw
7577 * GTT cache may not work with big pages, so if those
7578 * are ever enabled GTT cache may need to be disabled.
7579 */
7580 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7581
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007582 /* WaKVMNotificationOnConfigChange:bdw */
7583 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7584 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7585
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007586 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007587
7588 /* WaDisableDopClockGating:bdw
7589 *
7590 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7591 * clock gating.
7592 */
7593 I915_WRITE(GEN6_UCGCTL1,
7594 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007595}
7596
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007597static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007598{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007599 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007600
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007601 /* L3 caching of data atomics doesn't work -- disable it. */
7602 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7603 I915_WRITE(HSW_ROW_CHICKEN3,
7604 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7605
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007606 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007607 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7608 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7609 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7610
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007611 /* WaVSRefCountFullforceMissDisable:hsw */
7612 I915_WRITE(GEN7_FF_THREAD_MODE,
7613 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007614
Akash Goel4e046322014-04-04 17:14:38 +05307615 /* WaDisable_RenderCache_OperationalFlush:hsw */
7616 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7617
Chia-I Wufe27c602014-01-28 13:29:33 +08007618 /* enable HiZ Raw Stall Optimization */
7619 I915_WRITE(CACHE_MODE_0_GEN7,
7620 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7621
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007622 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007623 I915_WRITE(CACHE_MODE_1,
7624 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007625
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007626 /*
7627 * BSpec recommends 8x4 when MSAA is used,
7628 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007629 *
7630 * Note that PS/WM thread counts depend on the WIZ hashing
7631 * disable bit, which we don't touch here, but it's good
7632 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007633 */
7634 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007635 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007636
Kenneth Graunke94411592014-12-31 16:23:00 -08007637 /* WaSampleCChickenBitEnable:hsw */
7638 I915_WRITE(HALF_SLICE_CHICKEN3,
7639 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7640
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007641 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007642 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7643
Paulo Zanoni90a88642013-05-03 17:23:45 -03007644 /* WaRsPkgCStateDisplayPMReq:hsw */
7645 I915_WRITE(CHICKEN_PAR1_1,
7646 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007647
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007648 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007649}
7650
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007651static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007652{
Ben Widawsky20848222012-05-04 18:58:59 -07007653 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007654
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007655 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007656
Damien Lespiau231e54f2012-10-19 17:55:41 +01007657 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007658
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007659 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007660 I915_WRITE(_3D_CHICKEN3,
7661 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7662
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007663 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007664 I915_WRITE(IVB_CHICKEN3,
7665 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7666 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7667
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007668 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007669 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007670 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7671 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007672
Akash Goel4e046322014-04-04 17:14:38 +05307673 /* WaDisable_RenderCache_OperationalFlush:ivb */
7674 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7675
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007676 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007677 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7678 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7679
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007680 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007681 I915_WRITE(GEN7_L3CNTLREG1,
7682 GEN7_WA_FOR_GEN7_L3_CONTROL);
7683 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007684 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007685 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007686 I915_WRITE(GEN7_ROW_CHICKEN2,
7687 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007688 else {
7689 /* must write both registers */
7690 I915_WRITE(GEN7_ROW_CHICKEN2,
7691 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007692 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7693 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007694 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007695
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007696 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007697 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7698 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7699
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007700 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007701 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007702 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007703 */
7704 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007705 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007706
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007707 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007708 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7709 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7710 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7711
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007712 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007713
7714 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007715
Chris Wilson22721342014-03-04 09:41:43 +00007716 if (0) { /* causes HiZ corruption on ivb:gt1 */
7717 /* enable HiZ Raw Stall Optimization */
7718 I915_WRITE(CACHE_MODE_0_GEN7,
7719 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7720 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007721
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007722 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007723 I915_WRITE(CACHE_MODE_1,
7724 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007725
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007726 /*
7727 * BSpec recommends 8x4 when MSAA is used,
7728 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007729 *
7730 * Note that PS/WM thread counts depend on the WIZ hashing
7731 * disable bit, which we don't touch here, but it's good
7732 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007733 */
7734 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007735 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007736
Ben Widawsky20848222012-05-04 18:58:59 -07007737 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7738 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7739 snpcr |= GEN6_MBC_SNPCR_MED;
7740 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007741
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007742 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007743 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007744
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007745 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007746}
7747
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007748static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007749{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007750 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007751 I915_WRITE(_3D_CHICKEN3,
7752 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7753
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007754 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007755 I915_WRITE(IVB_CHICKEN3,
7756 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7757 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7758
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007759 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007760 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007761 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007762 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7763 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007764
Akash Goel4e046322014-04-04 17:14:38 +05307765 /* WaDisable_RenderCache_OperationalFlush:vlv */
7766 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7767
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007768 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007769 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7770 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7771
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007772 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007773 I915_WRITE(GEN7_ROW_CHICKEN2,
7774 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7775
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007776 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007777 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7778 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7779 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7780
Ville Syrjälä46680e02014-01-22 21:33:01 +02007781 gen7_setup_fixed_func_scheduler(dev_priv);
7782
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007783 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007784 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007785 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007786 */
7787 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007788 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007789
Akash Goelc98f5062014-03-24 23:00:07 +05307790 /* WaDisableL3Bank2xClockGate:vlv
7791 * Disabling L3 clock gating- MMIO 940c[25] = 1
7792 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7793 I915_WRITE(GEN7_UCGCTL4,
7794 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007795
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007796 /*
7797 * BSpec says this must be set, even though
7798 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7799 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007800 I915_WRITE(CACHE_MODE_1,
7801 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007802
7803 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007804 * BSpec recommends 8x4 when MSAA is used,
7805 * however in practice 16x4 seems fastest.
7806 *
7807 * Note that PS/WM thread counts depend on the WIZ hashing
7808 * disable bit, which we don't touch here, but it's good
7809 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7810 */
7811 I915_WRITE(GEN7_GT_MODE,
7812 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7813
7814 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007815 * WaIncreaseL3CreditsForVLVB0:vlv
7816 * This is the hardware default actually.
7817 */
7818 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7819
7820 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007821 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007822 * Disable clock gating on th GCFG unit to prevent a delay
7823 * in the reporting of vblank events.
7824 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007825 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007826}
7827
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007828static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007829{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007830 /* WaVSRefCountFullforceMissDisable:chv */
7831 /* WaDSRefCountFullforceMissDisable:chv */
7832 I915_WRITE(GEN7_FF_THREAD_MODE,
7833 I915_READ(GEN7_FF_THREAD_MODE) &
7834 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007835
7836 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7837 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7838 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007839
7840 /* WaDisableCSUnitClockGating:chv */
7841 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7842 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007843
7844 /* WaDisableSDEUnitClockGating:chv */
7845 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7846 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007847
7848 /*
Imre Deak450174f2016-05-03 15:54:21 +03007849 * WaProgramL3SqcReg1Default:chv
7850 * See gfxspecs/Related Documents/Performance Guide/
7851 * LSQC Setting Recommendations.
7852 */
7853 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7854
7855 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007856 * GTT cache may not work with big pages, so if those
7857 * are ever enabled GTT cache may need to be disabled.
7858 */
7859 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007860}
7861
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007862static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007863{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007864 uint32_t dspclk_gate;
7865
7866 I915_WRITE(RENCLK_GATE_D1, 0);
7867 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7868 GS_UNIT_CLOCK_GATE_DISABLE |
7869 CL_UNIT_CLOCK_GATE_DISABLE);
7870 I915_WRITE(RAMCLK_GATE_D, 0);
7871 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7872 OVRUNIT_CLOCK_GATE_DISABLE |
7873 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007874 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007875 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7876 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007877
7878 /* WaDisableRenderCachePipelinedFlush */
7879 I915_WRITE(CACHE_MODE_0,
7880 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007881
Akash Goel4e046322014-04-04 17:14:38 +05307882 /* WaDisable_RenderCache_OperationalFlush:g4x */
7883 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7884
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007885 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007886}
7887
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007888static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007889{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007890 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7891 I915_WRITE(RENCLK_GATE_D2, 0);
7892 I915_WRITE(DSPCLK_GATE_D, 0);
7893 I915_WRITE(RAMCLK_GATE_D, 0);
7894 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007895 I915_WRITE(MI_ARB_STATE,
7896 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307897
7898 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7899 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007900}
7901
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007902static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007903{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007904 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7905 I965_RCC_CLOCK_GATE_DISABLE |
7906 I965_RCPB_CLOCK_GATE_DISABLE |
7907 I965_ISC_CLOCK_GATE_DISABLE |
7908 I965_FBC_CLOCK_GATE_DISABLE);
7909 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007910 I915_WRITE(MI_ARB_STATE,
7911 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307912
7913 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7914 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007915}
7916
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007917static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007918{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007919 u32 dstate = I915_READ(D_STATE);
7920
7921 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7922 DSTATE_DOT_CLOCK_GATING;
7923 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007924
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007925 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007926 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007927
7928 /* IIR "flip pending" means done if this bit is set */
7929 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007930
7931 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007932 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007933
7934 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7935 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007936
7937 I915_WRITE(MI_ARB_STATE,
7938 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007939}
7940
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007941static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007942{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007943 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007944
7945 /* interrupts should cause a wake up from C3 */
7946 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7947 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007948
7949 I915_WRITE(MEM_MODE,
7950 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007951}
7952
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007953static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007954{
Ville Syrjälä10383922014-08-15 01:21:54 +03007955 I915_WRITE(MEM_MODE,
7956 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7957 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007958}
7959
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007960void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007961{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007962 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007963}
7964
Ville Syrjälä712bf362016-10-31 22:37:23 +02007965void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007966{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007967 if (HAS_PCH_LPT(dev_priv))
7968 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007969}
7970
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007971static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007972{
7973 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7974}
7975
7976/**
7977 * intel_init_clock_gating_hooks - setup the clock gating hooks
7978 * @dev_priv: device private
7979 *
7980 * Setup the hooks that configure which clocks of a given platform can be
7981 * gated and also apply various GT and display specific workarounds for these
7982 * platforms. Note that some GT specific workarounds are applied separately
7983 * when GPU contexts or batchbuffers start their execution.
7984 */
7985void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7986{
7987 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007988 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007989 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007990 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007991 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007992 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007993 else if (IS_GEMINILAKE(dev_priv))
7994 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007995 else if (IS_BROADWELL(dev_priv))
7996 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7997 else if (IS_CHERRYVIEW(dev_priv))
7998 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7999 else if (IS_HASWELL(dev_priv))
8000 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
8001 else if (IS_IVYBRIDGE(dev_priv))
8002 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
8003 else if (IS_VALLEYVIEW(dev_priv))
8004 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
8005 else if (IS_GEN6(dev_priv))
8006 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8007 else if (IS_GEN5(dev_priv))
8008 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
8009 else if (IS_G4X(dev_priv))
8010 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008011 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008012 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008013 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008014 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
8015 else if (IS_GEN3(dev_priv))
8016 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8017 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8018 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8019 else if (IS_GEN2(dev_priv))
8020 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8021 else {
8022 MISSING_CASE(INTEL_DEVID(dev_priv));
8023 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8024 }
8025}
8026
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008027/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008028void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008029{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008030 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008031
Daniel Vetterc921aba2012-04-26 23:28:17 +02008032 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008033 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008034 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008035 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008036 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008037
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008038 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008039 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008040 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008041 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008042 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008043 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008044 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008045 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008046
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008047 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008048 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008049 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008050 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008051 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008052 dev_priv->display.compute_intermediate_wm =
8053 ilk_compute_intermediate_wm;
8054 dev_priv->display.initial_watermarks =
8055 ilk_initial_watermarks;
8056 dev_priv->display.optimize_watermarks =
8057 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008058 } else {
8059 DRM_DEBUG_KMS("Failed to read display plane latency. "
8060 "Disable CxSR\n");
8061 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008062 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008063 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008064 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008065 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008066 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008067 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008068 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008069 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008070 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008071 dev_priv->is_ddr3,
8072 dev_priv->fsb_freq,
8073 dev_priv->mem_freq)) {
8074 DRM_INFO("failed to find known CxSR latency "
8075 "(found ddr%s fsb freq %d, mem freq %d), "
8076 "disabling CxSR\n",
8077 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8078 dev_priv->fsb_freq, dev_priv->mem_freq);
8079 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008080 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008081 dev_priv->display.update_wm = NULL;
8082 } else
8083 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008084 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008085 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008086 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008087 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008088 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008089 dev_priv->display.update_wm = i9xx_update_wm;
8090 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008091 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008092 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008093 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008094 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008095 } else {
8096 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008097 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008098 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008099 } else {
8100 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008101 }
8102}
8103
Lyude87660502016-08-17 15:55:53 -04008104static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8105{
8106 uint32_t flags =
8107 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8108
8109 switch (flags) {
8110 case GEN6_PCODE_SUCCESS:
8111 return 0;
8112 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8113 case GEN6_PCODE_ILLEGAL_CMD:
8114 return -ENXIO;
8115 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008116 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008117 return -EOVERFLOW;
8118 case GEN6_PCODE_TIMEOUT:
8119 return -ETIMEDOUT;
8120 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00008121 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04008122 return 0;
8123 }
8124}
8125
8126static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8127{
8128 uint32_t flags =
8129 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8130
8131 switch (flags) {
8132 case GEN6_PCODE_SUCCESS:
8133 return 0;
8134 case GEN6_PCODE_ILLEGAL_CMD:
8135 return -ENXIO;
8136 case GEN7_PCODE_TIMEOUT:
8137 return -ETIMEDOUT;
8138 case GEN7_PCODE_ILLEGAL_DATA:
8139 return -EINVAL;
8140 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8141 return -EOVERFLOW;
8142 default:
8143 MISSING_CASE(flags);
8144 return 0;
8145 }
8146}
8147
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008148int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008149{
Lyude87660502016-08-17 15:55:53 -04008150 int status;
8151
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008152 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008153
Chris Wilson3f5582d2016-06-30 15:32:45 +01008154 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8155 * use te fw I915_READ variants to reduce the amount of work
8156 * required when reading/writing.
8157 */
8158
8159 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008160 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8161 return -EAGAIN;
8162 }
8163
Chris Wilson3f5582d2016-06-30 15:32:45 +01008164 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8165 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8166 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008167
Chris Wilsone09a3032017-04-11 11:13:39 +01008168 if (__intel_wait_for_register_fw(dev_priv,
8169 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8170 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008171 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8172 return -ETIMEDOUT;
8173 }
8174
Chris Wilson3f5582d2016-06-30 15:32:45 +01008175 *val = I915_READ_FW(GEN6_PCODE_DATA);
8176 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008177
Lyude87660502016-08-17 15:55:53 -04008178 if (INTEL_GEN(dev_priv) > 6)
8179 status = gen7_check_mailbox_status(dev_priv);
8180 else
8181 status = gen6_check_mailbox_status(dev_priv);
8182
8183 if (status) {
8184 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8185 status);
8186 return status;
8187 }
8188
Ben Widawsky42c05262012-09-26 10:34:00 -07008189 return 0;
8190}
8191
Chris Wilson3f5582d2016-06-30 15:32:45 +01008192int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008193 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008194{
Lyude87660502016-08-17 15:55:53 -04008195 int status;
8196
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008197 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008198
Chris Wilson3f5582d2016-06-30 15:32:45 +01008199 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8200 * use te fw I915_READ variants to reduce the amount of work
8201 * required when reading/writing.
8202 */
8203
8204 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008205 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
8206 return -EAGAIN;
8207 }
8208
Chris Wilson3f5582d2016-06-30 15:32:45 +01008209 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008210 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008211 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008212
Chris Wilsone09a3032017-04-11 11:13:39 +01008213 if (__intel_wait_for_register_fw(dev_priv,
8214 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8215 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008216 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
8217 return -ETIMEDOUT;
8218 }
8219
Chris Wilson3f5582d2016-06-30 15:32:45 +01008220 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008221
Lyude87660502016-08-17 15:55:53 -04008222 if (INTEL_GEN(dev_priv) > 6)
8223 status = gen7_check_mailbox_status(dev_priv);
8224 else
8225 status = gen6_check_mailbox_status(dev_priv);
8226
8227 if (status) {
8228 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
8229 status);
8230 return status;
8231 }
8232
Ben Widawsky42c05262012-09-26 10:34:00 -07008233 return 0;
8234}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008235
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008236static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8237 u32 request, u32 reply_mask, u32 reply,
8238 u32 *status)
8239{
8240 u32 val = request;
8241
8242 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8243
8244 return *status || ((val & reply_mask) == reply);
8245}
8246
8247/**
8248 * skl_pcode_request - send PCODE request until acknowledgment
8249 * @dev_priv: device private
8250 * @mbox: PCODE mailbox ID the request is targeted for
8251 * @request: request ID
8252 * @reply_mask: mask used to check for request acknowledgment
8253 * @reply: value used to check for request acknowledgment
8254 * @timeout_base_ms: timeout for polling with preemption enabled
8255 *
8256 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008257 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008258 * The request is acknowledged once the PCODE reply dword equals @reply after
8259 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008260 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008261 * preemption disabled.
8262 *
8263 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8264 * other error as reported by PCODE.
8265 */
8266int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8267 u32 reply_mask, u32 reply, int timeout_base_ms)
8268{
8269 u32 status;
8270 int ret;
8271
8272 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
8273
8274#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
8275 &status)
8276
8277 /*
8278 * Prime the PCODE by doing a request first. Normally it guarantees
8279 * that a subsequent request, at most @timeout_base_ms later, succeeds.
8280 * _wait_for() doesn't guarantee when its passed condition is evaluated
8281 * first, so send the first request explicitly.
8282 */
8283 if (COND) {
8284 ret = 0;
8285 goto out;
8286 }
8287 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
8288 if (!ret)
8289 goto out;
8290
8291 /*
8292 * The above can time out if the number of requests was low (2 in the
8293 * worst case) _and_ PCODE was busy for some reason even after a
8294 * (queued) request and @timeout_base_ms delay. As a workaround retry
8295 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02008296 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008297 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02008298 * requests, and for any quirks of the PCODE firmware that delays
8299 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008300 */
8301 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
8302 WARN_ON_ONCE(timeout_base_ms > 3);
8303 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02008304 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008305 preempt_enable();
8306
8307out:
8308 return ret ? ret : status;
8309#undef COND
8310}
8311
Ville Syrjälädd06f882014-11-10 22:55:12 +02008312static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8313{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008314 /*
8315 * N = val - 0xb7
8316 * Slow = Fast = GPLL ref * N
8317 */
8318 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008319}
8320
Fengguang Wub55dd642014-07-12 11:21:39 +02008321static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008322{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008323 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008324}
8325
Fengguang Wub55dd642014-07-12 11:21:39 +02008326static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308327{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008328 /*
8329 * N = val / 2
8330 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8331 */
8332 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308333}
8334
Fengguang Wub55dd642014-07-12 11:21:39 +02008335static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308336{
Ville Syrjälä1c147622014-08-18 14:42:43 +03008337 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008338 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308339}
8340
Ville Syrjälä616bc822015-01-23 21:04:25 +02008341int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8342{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008343 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008344 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8345 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008346 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008347 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008348 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008349 return byt_gpu_freq(dev_priv, val);
8350 else
8351 return val * GT_FREQUENCY_MULTIPLIER;
8352}
8353
Ville Syrjälä616bc822015-01-23 21:04:25 +02008354int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8355{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008356 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008357 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8358 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008359 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008360 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008361 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008362 return byt_freq_opcode(dev_priv, val);
8363 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008364 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308365}
8366
Chris Wilson6ad790c2015-04-07 16:20:31 +01008367struct request_boost {
8368 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008369 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008370};
8371
8372static void __intel_rps_boost_work(struct work_struct *work)
8373{
8374 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008375 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008376
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008377 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008378 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008379
Chris Wilsone8a261e2016-07-20 13:31:49 +01008380 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008381 kfree(boost);
8382}
8383
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008384void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008385{
8386 struct request_boost *boost;
8387
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008388 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008389 return;
8390
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008391 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008392 return;
8393
Chris Wilson6ad790c2015-04-07 16:20:31 +01008394 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8395 if (boost == NULL)
8396 return;
8397
Chris Wilsone8a261e2016-07-20 13:31:49 +01008398 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008399
8400 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008401 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008402}
8403
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008404void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008405{
Daniel Vetterf742a552013-12-06 10:17:53 +01008406 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01008407 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01008408
Chris Wilson54b4f682016-07-21 21:16:19 +01008409 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
8410 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01008411 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03008412
Paulo Zanoni33688d92014-03-07 20:08:19 -03008413 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02008414 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008415}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008416
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008417static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
8418 const i915_reg_t reg)
8419{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008420 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00008421 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008422
8423 /* The register accessed do not need forcewake. We borrow
8424 * uncore lock to prevent concurrent access to range reg.
8425 */
8426 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008427
8428 /* vlv and chv residency counters are 40 bits in width.
8429 * With a control bit, we can choose between upper or lower
8430 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008431 *
8432 * Although we always use the counter in high-range mode elsewhere,
8433 * userspace may attempt to read the value before rc6 is initialised,
8434 * before we have set the default VLV_COUNTER_CONTROL value. So always
8435 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008436 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008437 I915_WRITE_FW(VLV_COUNTER_CONTROL,
8438 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008439 upper = I915_READ_FW(reg);
8440 do {
8441 tmp = upper;
8442
8443 I915_WRITE_FW(VLV_COUNTER_CONTROL,
8444 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
8445 lower = I915_READ_FW(reg);
8446
8447 I915_WRITE_FW(VLV_COUNTER_CONTROL,
8448 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
8449 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00008450 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008451
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008452 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
8453 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
8454 * now.
8455 */
8456
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008457 spin_unlock_irq(&dev_priv->uncore.lock);
8458
8459 return lower | (u64)upper << 8;
8460}
8461
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02008462u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
8463 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008464{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008465 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008466
8467 if (!intel_enable_rc6())
8468 return 0;
8469
8470 intel_runtime_pm_get(dev_priv);
8471
8472 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
8473 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02008474 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008475 div = dev_priv->czclk_freq;
8476
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008477 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008478 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02008479 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008480 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008481
8482 time_hw = I915_READ(reg);
8483 } else {
8484 units = 128000; /* 1.28us */
8485 div = 100000;
8486
8487 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008488 }
8489
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008490 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008491 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008492}