blob: 0aed13dcedf06e3ee54e15d0ae039a89fdd0ee3d [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{
Rodrigo Vivi82525c12017-06-08 08:50:00 -070061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030062 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
Rodrigo Vivi82525c12017-06-08 08:50:00 -070068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
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
Rodrigo Vivi82525c12017-06-08 08:50:00 -070072 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
73 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
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
Rodrigo Vivi82525c12017-06-08 08:50:00 -070078 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030079 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ä04548cb2017-04-21 21:14:29 +0300432 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
433 dev_priv->wm.vlv.cxsr = enable;
434 else if (IS_G4X(dev_priv))
435 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200436 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200437
438 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200439}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200440
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300441/*
442 * Latency for FIFO fetches is dependent on several factors:
443 * - memory configuration (speed, channels)
444 * - chipset
445 * - current MCH state
446 * It can be fairly high in some situations, so here we assume a fairly
447 * pessimal value. It's a tradeoff between extra memory fetches (if we
448 * set this value too high, the FIFO will fetch frequently to stay full)
449 * and power consumption (set it too low to save power and we might see
450 * FIFO underruns and display "flicker").
451 *
452 * A value of 5us seems to be a good balance; safe for very low end
453 * platforms but not overly aggressive on lower latency configs.
454 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100455static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300456
Ville Syrjäläb5004722015-03-05 21:19:47 +0200457#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
458 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
459
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200460static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200461{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200462 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200463 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200464 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200465 enum pipe pipe = crtc->pipe;
466 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200467
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200468 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200469 uint32_t dsparb, dsparb2, dsparb3;
470 case PIPE_A:
471 dsparb = I915_READ(DSPARB);
472 dsparb2 = I915_READ(DSPARB2);
473 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
474 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
475 break;
476 case PIPE_B:
477 dsparb = I915_READ(DSPARB);
478 dsparb2 = I915_READ(DSPARB2);
479 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
480 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
481 break;
482 case PIPE_C:
483 dsparb2 = I915_READ(DSPARB2);
484 dsparb3 = I915_READ(DSPARB3);
485 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
486 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
487 break;
488 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200489 MISSING_CASE(pipe);
490 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200491 }
492
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200493 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
494 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
495 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
496 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200497}
498
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200499static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300500{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300501 uint32_t dsparb = I915_READ(DSPARB);
502 int size;
503
504 size = dsparb & 0x7f;
505 if (plane)
506 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
507
508 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
509 plane ? "B" : "A", size);
510
511 return size;
512}
513
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200514static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300516 uint32_t dsparb = I915_READ(DSPARB);
517 int size;
518
519 size = dsparb & 0x1ff;
520 if (plane)
521 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
522 size >>= 1; /* Convert to cachelines */
523
524 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
525 plane ? "B" : "A", size);
526
527 return size;
528}
529
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200530static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300532 uint32_t dsparb = I915_READ(DSPARB);
533 int size;
534
535 size = dsparb & 0x7f;
536 size >>= 2; /* Convert to cachelines */
537
538 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
539 plane ? "B" : "A",
540 size);
541
542 return size;
543}
544
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545/* Pineview has different values for various configs */
546static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300547 .fifo_size = PINEVIEW_DISPLAY_FIFO,
548 .max_wm = PINEVIEW_MAX_WM,
549 .default_wm = PINEVIEW_DFT_WM,
550 .guard_size = PINEVIEW_GUARD_WM,
551 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552};
553static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300554 .fifo_size = PINEVIEW_DISPLAY_FIFO,
555 .max_wm = PINEVIEW_MAX_WM,
556 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
557 .guard_size = PINEVIEW_GUARD_WM,
558 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559};
560static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300561 .fifo_size = PINEVIEW_CURSOR_FIFO,
562 .max_wm = PINEVIEW_CURSOR_MAX_WM,
563 .default_wm = PINEVIEW_CURSOR_DFT_WM,
564 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
565 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300566};
567static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300568 .fifo_size = PINEVIEW_CURSOR_FIFO,
569 .max_wm = PINEVIEW_CURSOR_MAX_WM,
570 .default_wm = PINEVIEW_CURSOR_DFT_WM,
571 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
572 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300574static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300575 .fifo_size = I965_CURSOR_FIFO,
576 .max_wm = I965_CURSOR_MAX_WM,
577 .default_wm = I965_CURSOR_DFT_WM,
578 .guard_size = 2,
579 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300580};
581static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300582 .fifo_size = I945_FIFO_SIZE,
583 .max_wm = I915_MAX_WM,
584 .default_wm = 1,
585 .guard_size = 2,
586 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300587};
588static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300589 .fifo_size = I915_FIFO_SIZE,
590 .max_wm = I915_MAX_WM,
591 .default_wm = 1,
592 .guard_size = 2,
593 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300594};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300595static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300596 .fifo_size = I855GM_FIFO_SIZE,
597 .max_wm = I915_MAX_WM,
598 .default_wm = 1,
599 .guard_size = 2,
600 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300602static const struct intel_watermark_params i830_bc_wm_info = {
603 .fifo_size = I855GM_FIFO_SIZE,
604 .max_wm = I915_MAX_WM/2,
605 .default_wm = 1,
606 .guard_size = 2,
607 .cacheline_size = I830_FIFO_LINE_SIZE,
608};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200609static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300610 .fifo_size = I830_FIFO_SIZE,
611 .max_wm = I915_MAX_WM,
612 .default_wm = 1,
613 .guard_size = 2,
614 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300615};
616
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300617/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300618 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
619 * @pixel_rate: Pipe pixel rate in kHz
620 * @cpp: Plane bytes per pixel
621 * @latency: Memory wakeup latency in 0.1us units
622 *
623 * Compute the watermark using the method 1 or "small buffer"
624 * formula. The caller may additonally add extra cachelines
625 * to account for TLB misses and clock crossings.
626 *
627 * This method is concerned with the short term drain rate
628 * of the FIFO, ie. it does not account for blanking periods
629 * which would effectively reduce the average drain rate across
630 * a longer period. The name "small" refers to the fact the
631 * FIFO is relatively small compared to the amount of data
632 * fetched.
633 *
634 * The FIFO level vs. time graph might look something like:
635 *
636 * |\ |\
637 * | \ | \
638 * __---__---__ (- plane active, _ blanking)
639 * -> time
640 *
641 * or perhaps like this:
642 *
643 * |\|\ |\|\
644 * __----__----__ (- plane active, _ blanking)
645 * -> time
646 *
647 * Returns:
648 * The watermark in bytes
649 */
650static unsigned int intel_wm_method1(unsigned int pixel_rate,
651 unsigned int cpp,
652 unsigned int latency)
653{
654 uint64_t ret;
655
656 ret = (uint64_t) pixel_rate * cpp * latency;
657 ret = DIV_ROUND_UP_ULL(ret, 10000);
658
659 return ret;
660}
661
662/**
663 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
664 * @pixel_rate: Pipe pixel rate in kHz
665 * @htotal: Pipe horizontal total
666 * @width: Plane width in pixels
667 * @cpp: Plane bytes per pixel
668 * @latency: Memory wakeup latency in 0.1us units
669 *
670 * Compute the watermark using the method 2 or "large buffer"
671 * formula. The caller may additonally add extra cachelines
672 * to account for TLB misses and clock crossings.
673 *
674 * This method is concerned with the long term drain rate
675 * of the FIFO, ie. it does account for blanking periods
676 * which effectively reduce the average drain rate across
677 * a longer period. The name "large" refers to the fact the
678 * FIFO is relatively large compared to the amount of data
679 * fetched.
680 *
681 * The FIFO level vs. time graph might look something like:
682 *
683 * |\___ |\___
684 * | \___ | \___
685 * | \ | \
686 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
687 * -> time
688 *
689 * Returns:
690 * The watermark in bytes
691 */
692static unsigned int intel_wm_method2(unsigned int pixel_rate,
693 unsigned int htotal,
694 unsigned int width,
695 unsigned int cpp,
696 unsigned int latency)
697{
698 unsigned int ret;
699
700 /*
701 * FIXME remove once all users are computing
702 * watermarks in the correct place.
703 */
704 if (WARN_ON_ONCE(htotal == 0))
705 htotal = 1;
706
707 ret = (latency * pixel_rate) / (htotal * 10000);
708 ret = (ret + 1) * width * cpp;
709
710 return ret;
711}
712
713/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300714 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300715 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300716 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200717 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300718 * @latency_ns: memory latency for the platform
719 *
720 * Calculate the watermark level (the level at which the display plane will
721 * start fetching from memory again). Each chip has a different display
722 * FIFO size and allocation, so the caller needs to figure that out and pass
723 * in the correct intel_watermark_params structure.
724 *
725 * As the pixel clock runs, the FIFO will be drained at a rate that depends
726 * on the pixel size. When it reaches the watermark level, it'll start
727 * fetching FIFO line sized based chunks from memory until the FIFO fills
728 * past the watermark point. If the FIFO drains completely, a FIFO underrun
729 * will occur, and a display engine hang could result.
730 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300731static unsigned int intel_calculate_wm(int pixel_rate,
732 const struct intel_watermark_params *wm,
733 int fifo_size, int cpp,
734 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300735{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300736 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300737
738 /*
739 * Note: we need to make sure we don't overflow for various clock &
740 * latency values.
741 * clocks go from a few thousand to several hundred thousand.
742 * latency is usually a few thousand
743 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300744 entries = intel_wm_method1(pixel_rate, cpp,
745 latency_ns / 100);
746 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
747 wm->guard_size;
748 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300749
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300750 wm_size = fifo_size - entries;
751 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300752
753 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300754 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755 wm_size = wm->max_wm;
756 if (wm_size <= 0)
757 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300758
759 /*
760 * Bspec seems to indicate that the value shouldn't be lower than
761 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
762 * Lets go for 8 which is the burst size since certain platforms
763 * already use a hardcoded 8 (which is what the spec says should be
764 * done).
765 */
766 if (wm_size <= 8)
767 wm_size = 8;
768
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300769 return wm_size;
770}
771
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300772static bool is_disabling(int old, int new, int threshold)
773{
774 return old >= threshold && new < threshold;
775}
776
777static bool is_enabling(int old, int new, int threshold)
778{
779 return old < threshold && new >= threshold;
780}
781
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300782static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
783{
784 return dev_priv->wm.max_level + 1;
785}
786
Ville Syrjälä24304d812017-03-14 17:10:49 +0200787static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
788 const struct intel_plane_state *plane_state)
789{
790 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
791
792 /* FIXME check the 'enable' instead */
793 if (!crtc_state->base.active)
794 return false;
795
796 /*
797 * Treat cursor with fb as always visible since cursor updates
798 * can happen faster than the vrefresh rate, and the current
799 * watermark code doesn't handle that correctly. Cursor updates
800 * which set/clear the fb or change the cursor size are going
801 * to get throttled by intel_legacy_cursor_update() to work
802 * around this problem with the watermark code.
803 */
804 if (plane->id == PLANE_CURSOR)
805 return plane_state->base.fb != NULL;
806 else
807 return plane_state->base.visible;
808}
809
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200810static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300811{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200812 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300813
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200814 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200815 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300816 if (enabled)
817 return NULL;
818 enabled = crtc;
819 }
820 }
821
822 return enabled;
823}
824
Ville Syrjälä432081b2016-10-31 22:37:03 +0200825static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300826{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200827 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200828 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300829 const struct cxsr_latency *latency;
830 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300831 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300832
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100833 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
834 dev_priv->is_ddr3,
835 dev_priv->fsb_freq,
836 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300837 if (!latency) {
838 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300839 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300840 return;
841 }
842
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200843 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300844 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200845 const struct drm_display_mode *adjusted_mode =
846 &crtc->config->base.adjusted_mode;
847 const struct drm_framebuffer *fb =
848 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200849 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300850 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300851
852 /* Display SR */
853 wm = intel_calculate_wm(clock, &pineview_display_wm,
854 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200855 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300856 reg = I915_READ(DSPFW1);
857 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200858 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300859 I915_WRITE(DSPFW1, reg);
860 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
861
862 /* cursor SR */
863 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
864 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300865 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866 reg = I915_READ(DSPFW3);
867 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200868 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300869 I915_WRITE(DSPFW3, reg);
870
871 /* Display HPLL off SR */
872 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
873 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200874 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300875 reg = I915_READ(DSPFW3);
876 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200877 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300878 I915_WRITE(DSPFW3, reg);
879
880 /* cursor HPLL off SR */
881 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
882 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300883 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884 reg = I915_READ(DSPFW3);
885 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200886 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887 I915_WRITE(DSPFW3, reg);
888 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
889
Imre Deak5209b1f2014-07-01 12:36:17 +0300890 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300891 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300892 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300893 }
894}
895
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300896/*
897 * Documentation says:
898 * "If the line size is small, the TLB fetches can get in the way of the
899 * data fetches, causing some lag in the pixel data return which is not
900 * accounted for in the above formulas. The following adjustment only
901 * needs to be applied if eight whole lines fit in the buffer at once.
902 * The WM is adjusted upwards by the difference between the FIFO size
903 * and the size of 8 whole lines. This adjustment is always performed
904 * in the actual pixel depth regardless of whether FBC is enabled or not."
905 */
906static int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
907{
908 int tlb_miss = fifo_size * 64 - width * cpp * 8;
909
910 return max(0, tlb_miss);
911}
912
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300913static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
914 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300915{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300916 enum pipe pipe;
917
918 for_each_pipe(dev_priv, pipe)
919 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
920
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300921 I915_WRITE(DSPFW1,
922 FW_WM(wm->sr.plane, SR) |
923 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
924 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
925 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
926 I915_WRITE(DSPFW2,
927 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
928 FW_WM(wm->sr.fbc, FBC_SR) |
929 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
930 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
931 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
932 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
933 I915_WRITE(DSPFW3,
934 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
935 FW_WM(wm->sr.cursor, CURSOR_SR) |
936 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
937 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300939 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300940}
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ä04548cb2017-04-21 21:14:29 +03001023static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1024{
1025 /* all latencies in usec */
1026 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1027 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001028 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001029
Ville Syrjälä79d94302017-04-21 21:14:30 +03001030 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001031}
1032
1033static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1034{
1035 /*
1036 * DSPCNTR[13] supposedly controls whether the
1037 * primary plane can use the FIFO space otherwise
1038 * reserved for the sprite plane. It's not 100% clear
1039 * what the actual FIFO size is, but it looks like we
1040 * can happily set both primary and sprite watermarks
1041 * up to 127 cachelines. So that would seem to mean
1042 * that either DSPCNTR[13] doesn't do anything, or that
1043 * the total FIFO is >= 256 cachelines in size. Either
1044 * way, we don't seem to have to worry about this
1045 * repartitioning as the maximum watermark value the
1046 * register can hold for each plane is lower than the
1047 * minimum FIFO size.
1048 */
1049 switch (plane_id) {
1050 case PLANE_CURSOR:
1051 return 63;
1052 case PLANE_PRIMARY:
1053 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1054 case PLANE_SPRITE0:
1055 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1056 default:
1057 MISSING_CASE(plane_id);
1058 return 0;
1059 }
1060}
1061
1062static int g4x_fbc_fifo_size(int level)
1063{
1064 switch (level) {
1065 case G4X_WM_LEVEL_SR:
1066 return 7;
1067 case G4X_WM_LEVEL_HPLL:
1068 return 15;
1069 default:
1070 MISSING_CASE(level);
1071 return 0;
1072 }
1073}
1074
1075static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1076 const struct intel_plane_state *plane_state,
1077 int level)
1078{
1079 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1080 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1081 const struct drm_display_mode *adjusted_mode =
1082 &crtc_state->base.adjusted_mode;
1083 int clock, htotal, cpp, width, wm;
1084 int latency = dev_priv->wm.pri_latency[level] * 10;
1085
1086 if (latency == 0)
1087 return USHRT_MAX;
1088
1089 if (!intel_wm_plane_visible(crtc_state, plane_state))
1090 return 0;
1091
1092 /*
1093 * Not 100% sure which way ELK should go here as the
1094 * spec only says CL/CTG should assume 32bpp and BW
1095 * doesn't need to. But as these things followed the
1096 * mobile vs. desktop lines on gen3 as well, let's
1097 * assume ELK doesn't need this.
1098 *
1099 * The spec also fails to list such a restriction for
1100 * the HPLL watermark, which seems a little strange.
1101 * Let's use 32bpp for the HPLL watermark as well.
1102 */
1103 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1104 level != G4X_WM_LEVEL_NORMAL)
1105 cpp = 4;
1106 else
1107 cpp = plane_state->base.fb->format->cpp[0];
1108
1109 clock = adjusted_mode->crtc_clock;
1110 htotal = adjusted_mode->crtc_htotal;
1111
1112 if (plane->id == PLANE_CURSOR)
1113 width = plane_state->base.crtc_w;
1114 else
1115 width = drm_rect_width(&plane_state->base.dst);
1116
1117 if (plane->id == PLANE_CURSOR) {
1118 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1119 } else if (plane->id == PLANE_PRIMARY &&
1120 level == G4X_WM_LEVEL_NORMAL) {
1121 wm = intel_wm_method1(clock, cpp, latency);
1122 } else {
1123 int small, large;
1124
1125 small = intel_wm_method1(clock, cpp, latency);
1126 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1127
1128 wm = min(small, large);
1129 }
1130
1131 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1132 width, cpp);
1133
1134 wm = DIV_ROUND_UP(wm, 64) + 2;
1135
1136 return min_t(int, wm, USHRT_MAX);
1137}
1138
1139static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1140 int level, enum plane_id plane_id, u16 value)
1141{
1142 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1143 bool dirty = false;
1144
1145 for (; level < intel_wm_num_levels(dev_priv); level++) {
1146 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1147
1148 dirty |= raw->plane[plane_id] != value;
1149 raw->plane[plane_id] = value;
1150 }
1151
1152 return dirty;
1153}
1154
1155static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1156 int level, u16 value)
1157{
1158 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1159 bool dirty = false;
1160
1161 /* NORMAL level doesn't have an FBC watermark */
1162 level = max(level, G4X_WM_LEVEL_SR);
1163
1164 for (; level < intel_wm_num_levels(dev_priv); level++) {
1165 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1166
1167 dirty |= raw->fbc != value;
1168 raw->fbc = value;
1169 }
1170
1171 return dirty;
1172}
1173
1174static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1175 const struct intel_plane_state *pstate,
1176 uint32_t pri_val);
1177
1178static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1179 const struct intel_plane_state *plane_state)
1180{
1181 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1182 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1183 enum plane_id plane_id = plane->id;
1184 bool dirty = false;
1185 int level;
1186
1187 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1188 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1189 if (plane_id == PLANE_PRIMARY)
1190 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1191 goto out;
1192 }
1193
1194 for (level = 0; level < num_levels; level++) {
1195 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1196 int wm, max_wm;
1197
1198 wm = g4x_compute_wm(crtc_state, plane_state, level);
1199 max_wm = g4x_plane_fifo_size(plane_id, level);
1200
1201 if (wm > max_wm)
1202 break;
1203
1204 dirty |= raw->plane[plane_id] != wm;
1205 raw->plane[plane_id] = wm;
1206
1207 if (plane_id != PLANE_PRIMARY ||
1208 level == G4X_WM_LEVEL_NORMAL)
1209 continue;
1210
1211 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1212 raw->plane[plane_id]);
1213 max_wm = g4x_fbc_fifo_size(level);
1214
1215 /*
1216 * FBC wm is not mandatory as we
1217 * can always just disable its use.
1218 */
1219 if (wm > max_wm)
1220 wm = USHRT_MAX;
1221
1222 dirty |= raw->fbc != wm;
1223 raw->fbc = wm;
1224 }
1225
1226 /* mark watermarks as invalid */
1227 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1228
1229 if (plane_id == PLANE_PRIMARY)
1230 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1231
1232 out:
1233 if (dirty) {
1234 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1235 plane->base.name,
1236 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1237 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1238 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1239
1240 if (plane_id == PLANE_PRIMARY)
1241 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1242 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1243 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1244 }
1245
1246 return dirty;
1247}
1248
1249static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1250 enum plane_id plane_id, int level)
1251{
1252 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1253
1254 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1255}
1256
1257static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1258 int level)
1259{
1260 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1261
1262 if (level > dev_priv->wm.max_level)
1263 return false;
1264
1265 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1266 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1267 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1268}
1269
1270/* mark all levels starting from 'level' as invalid */
1271static void g4x_invalidate_wms(struct intel_crtc *crtc,
1272 struct g4x_wm_state *wm_state, int level)
1273{
1274 if (level <= G4X_WM_LEVEL_NORMAL) {
1275 enum plane_id plane_id;
1276
1277 for_each_plane_id_on_crtc(crtc, plane_id)
1278 wm_state->wm.plane[plane_id] = USHRT_MAX;
1279 }
1280
1281 if (level <= G4X_WM_LEVEL_SR) {
1282 wm_state->cxsr = false;
1283 wm_state->sr.cursor = USHRT_MAX;
1284 wm_state->sr.plane = USHRT_MAX;
1285 wm_state->sr.fbc = USHRT_MAX;
1286 }
1287
1288 if (level <= G4X_WM_LEVEL_HPLL) {
1289 wm_state->hpll_en = false;
1290 wm_state->hpll.cursor = USHRT_MAX;
1291 wm_state->hpll.plane = USHRT_MAX;
1292 wm_state->hpll.fbc = USHRT_MAX;
1293 }
1294}
1295
1296static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1297{
1298 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1299 struct intel_atomic_state *state =
1300 to_intel_atomic_state(crtc_state->base.state);
1301 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1302 int num_active_planes = hweight32(crtc_state->active_planes &
1303 ~BIT(PLANE_CURSOR));
1304 const struct g4x_pipe_wm *raw;
1305 struct intel_plane_state *plane_state;
1306 struct intel_plane *plane;
1307 enum plane_id plane_id;
1308 int i, level;
1309 unsigned int dirty = 0;
1310
1311 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1312 const struct intel_plane_state *old_plane_state =
1313 to_intel_plane_state(plane->base.state);
1314
1315 if (plane_state->base.crtc != &crtc->base &&
1316 old_plane_state->base.crtc != &crtc->base)
1317 continue;
1318
1319 if (g4x_raw_plane_wm_compute(crtc_state, plane_state))
1320 dirty |= BIT(plane->id);
1321 }
1322
1323 if (!dirty)
1324 return 0;
1325
1326 level = G4X_WM_LEVEL_NORMAL;
1327 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1328 goto out;
1329
1330 raw = &crtc_state->wm.g4x.raw[level];
1331 for_each_plane_id_on_crtc(crtc, plane_id)
1332 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1333
1334 level = G4X_WM_LEVEL_SR;
1335
1336 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1337 goto out;
1338
1339 raw = &crtc_state->wm.g4x.raw[level];
1340 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1341 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1342 wm_state->sr.fbc = raw->fbc;
1343
1344 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1345
1346 level = G4X_WM_LEVEL_HPLL;
1347
1348 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1349 goto out;
1350
1351 raw = &crtc_state->wm.g4x.raw[level];
1352 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1353 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1354 wm_state->hpll.fbc = raw->fbc;
1355
1356 wm_state->hpll_en = wm_state->cxsr;
1357
1358 level++;
1359
1360 out:
1361 if (level == G4X_WM_LEVEL_NORMAL)
1362 return -EINVAL;
1363
1364 /* invalidate the higher levels */
1365 g4x_invalidate_wms(crtc, wm_state, level);
1366
1367 /*
1368 * Determine if the FBC watermark(s) can be used. IF
1369 * this isn't the case we prefer to disable the FBC
1370 ( watermark(s) rather than disable the SR/HPLL
1371 * level(s) entirely.
1372 */
1373 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1374
1375 if (level >= G4X_WM_LEVEL_SR &&
1376 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1377 wm_state->fbc_en = false;
1378 else if (level >= G4X_WM_LEVEL_HPLL &&
1379 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1380 wm_state->fbc_en = false;
1381
1382 return 0;
1383}
1384
1385static int g4x_compute_intermediate_wm(struct drm_device *dev,
1386 struct intel_crtc *crtc,
1387 struct intel_crtc_state *crtc_state)
1388{
1389 struct g4x_wm_state *intermediate = &crtc_state->wm.g4x.intermediate;
1390 const struct g4x_wm_state *optimal = &crtc_state->wm.g4x.optimal;
1391 const struct g4x_wm_state *active = &crtc->wm.active.g4x;
1392 enum plane_id plane_id;
1393
1394 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1395 !crtc_state->disable_cxsr;
1396 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1397 !crtc_state->disable_cxsr;
1398 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1399
1400 for_each_plane_id_on_crtc(crtc, plane_id) {
1401 intermediate->wm.plane[plane_id] =
1402 max(optimal->wm.plane[plane_id],
1403 active->wm.plane[plane_id]);
1404
1405 WARN_ON(intermediate->wm.plane[plane_id] >
1406 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1407 }
1408
1409 intermediate->sr.plane = max(optimal->sr.plane,
1410 active->sr.plane);
1411 intermediate->sr.cursor = max(optimal->sr.cursor,
1412 active->sr.cursor);
1413 intermediate->sr.fbc = max(optimal->sr.fbc,
1414 active->sr.fbc);
1415
1416 intermediate->hpll.plane = max(optimal->hpll.plane,
1417 active->hpll.plane);
1418 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1419 active->hpll.cursor);
1420 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1421 active->hpll.fbc);
1422
1423 WARN_ON((intermediate->sr.plane >
1424 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1425 intermediate->sr.cursor >
1426 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1427 intermediate->cxsr);
1428 WARN_ON((intermediate->sr.plane >
1429 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1430 intermediate->sr.cursor >
1431 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1432 intermediate->hpll_en);
1433
1434 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1435 intermediate->fbc_en && intermediate->cxsr);
1436 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1437 intermediate->fbc_en && intermediate->hpll_en);
1438
1439 /*
1440 * If our intermediate WM are identical to the final WM, then we can
1441 * omit the post-vblank programming; only update if it's different.
1442 */
1443 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1444 crtc_state->wm.need_postvbl_update = true;
1445
1446 return 0;
1447}
1448
1449static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1450 struct g4x_wm_values *wm)
1451{
1452 struct intel_crtc *crtc;
1453 int num_active_crtcs = 0;
1454
1455 wm->cxsr = true;
1456 wm->hpll_en = true;
1457 wm->fbc_en = true;
1458
1459 for_each_intel_crtc(&dev_priv->drm, crtc) {
1460 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1461
1462 if (!crtc->active)
1463 continue;
1464
1465 if (!wm_state->cxsr)
1466 wm->cxsr = false;
1467 if (!wm_state->hpll_en)
1468 wm->hpll_en = false;
1469 if (!wm_state->fbc_en)
1470 wm->fbc_en = false;
1471
1472 num_active_crtcs++;
1473 }
1474
1475 if (num_active_crtcs != 1) {
1476 wm->cxsr = false;
1477 wm->hpll_en = false;
1478 wm->fbc_en = false;
1479 }
1480
1481 for_each_intel_crtc(&dev_priv->drm, crtc) {
1482 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1483 enum pipe pipe = crtc->pipe;
1484
1485 wm->pipe[pipe] = wm_state->wm;
1486 if (crtc->active && wm->cxsr)
1487 wm->sr = wm_state->sr;
1488 if (crtc->active && wm->hpll_en)
1489 wm->hpll = wm_state->hpll;
1490 }
1491}
1492
1493static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1494{
1495 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1496 struct g4x_wm_values new_wm = {};
1497
1498 g4x_merge_wm(dev_priv, &new_wm);
1499
1500 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1501 return;
1502
1503 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1504 _intel_set_memory_cxsr(dev_priv, false);
1505
1506 g4x_write_wm_values(dev_priv, &new_wm);
1507
1508 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1509 _intel_set_memory_cxsr(dev_priv, true);
1510
1511 *old_wm = new_wm;
1512}
1513
1514static void g4x_initial_watermarks(struct intel_atomic_state *state,
1515 struct intel_crtc_state *crtc_state)
1516{
1517 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1518 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1519
1520 mutex_lock(&dev_priv->wm.wm_mutex);
1521 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1522 g4x_program_watermarks(dev_priv);
1523 mutex_unlock(&dev_priv->wm.wm_mutex);
1524}
1525
1526static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1527 struct intel_crtc_state *crtc_state)
1528{
1529 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1531
1532 if (!crtc_state->wm.need_postvbl_update)
1533 return;
1534
1535 mutex_lock(&dev_priv->wm.wm_mutex);
1536 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1537 g4x_program_watermarks(dev_priv);
1538 mutex_unlock(&dev_priv->wm.wm_mutex);
1539}
1540
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001541/* latency must be in 0.1us units. */
1542static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001543 unsigned int htotal,
1544 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001545 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001546 unsigned int latency)
1547{
1548 unsigned int ret;
1549
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001550 ret = intel_wm_method2(pixel_rate, htotal,
1551 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001552 ret = DIV_ROUND_UP(ret, 64);
1553
1554 return ret;
1555}
1556
Ville Syrjäläbb726512016-10-31 22:37:24 +02001557static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001558{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001559 /* all latencies in usec */
1560 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1561
Ville Syrjälä58590c12015-09-08 21:05:12 +03001562 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1563
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001564 if (IS_CHERRYVIEW(dev_priv)) {
1565 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1566 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001567
1568 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001569 }
1570}
1571
Ville Syrjäläe339d672016-11-28 19:37:17 +02001572static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1573 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001574 int level)
1575{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001576 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001577 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001578 const struct drm_display_mode *adjusted_mode =
1579 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001580 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001581
1582 if (dev_priv->wm.pri_latency[level] == 0)
1583 return USHRT_MAX;
1584
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001585 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001586 return 0;
1587
Daniel Vetteref426c12017-01-04 11:41:10 +01001588 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001589 clock = adjusted_mode->crtc_clock;
1590 htotal = adjusted_mode->crtc_htotal;
1591 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001592
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001593 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001594 /*
1595 * FIXME the formula gives values that are
1596 * too big for the cursor FIFO, and hence we
1597 * would never be able to use cursors. For
1598 * now just hardcode the watermark.
1599 */
1600 wm = 63;
1601 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001602 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001603 dev_priv->wm.pri_latency[level] * 10);
1604 }
1605
1606 return min_t(int, wm, USHRT_MAX);
1607}
1608
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001609static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1610{
1611 return (active_planes & (BIT(PLANE_SPRITE0) |
1612 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1613}
1614
Ville Syrjälä5012e602017-03-02 19:14:56 +02001615static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001616{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001617 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001618 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001619 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001620 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001621 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1622 int num_active_planes = hweight32(active_planes);
1623 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001624 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001625 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001626 unsigned int total_rate;
1627 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001628
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001629 /*
1630 * When enabling sprite0 after sprite1 has already been enabled
1631 * we tend to get an underrun unless sprite0 already has some
1632 * FIFO space allcoated. Hence we always allocate at least one
1633 * cacheline for sprite0 whenever sprite1 is enabled.
1634 *
1635 * All other plane enable sequences appear immune to this problem.
1636 */
1637 if (vlv_need_sprite0_fifo_workaround(active_planes))
1638 sprite0_fifo_extra = 1;
1639
Ville Syrjälä5012e602017-03-02 19:14:56 +02001640 total_rate = raw->plane[PLANE_PRIMARY] +
1641 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001642 raw->plane[PLANE_SPRITE1] +
1643 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001644
Ville Syrjälä5012e602017-03-02 19:14:56 +02001645 if (total_rate > fifo_size)
1646 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001647
Ville Syrjälä5012e602017-03-02 19:14:56 +02001648 if (total_rate == 0)
1649 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001650
Ville Syrjälä5012e602017-03-02 19:14:56 +02001651 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001652 unsigned int rate;
1653
Ville Syrjälä5012e602017-03-02 19:14:56 +02001654 if ((active_planes & BIT(plane_id)) == 0) {
1655 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001656 continue;
1657 }
1658
Ville Syrjälä5012e602017-03-02 19:14:56 +02001659 rate = raw->plane[plane_id];
1660 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1661 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001662 }
1663
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001664 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1665 fifo_left -= sprite0_fifo_extra;
1666
Ville Syrjälä5012e602017-03-02 19:14:56 +02001667 fifo_state->plane[PLANE_CURSOR] = 63;
1668
1669 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001670
1671 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001672 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001673 int plane_extra;
1674
1675 if (fifo_left == 0)
1676 break;
1677
Ville Syrjälä5012e602017-03-02 19:14:56 +02001678 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001679 continue;
1680
1681 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001682 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001683 fifo_left -= plane_extra;
1684 }
1685
Ville Syrjälä5012e602017-03-02 19:14:56 +02001686 WARN_ON(active_planes != 0 && fifo_left != 0);
1687
1688 /* give it all to the first plane if none are active */
1689 if (active_planes == 0) {
1690 WARN_ON(fifo_left != fifo_size);
1691 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1692 }
1693
1694 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001695}
1696
Ville Syrjäläff32c542017-03-02 19:14:57 +02001697/* mark all levels starting from 'level' as invalid */
1698static void vlv_invalidate_wms(struct intel_crtc *crtc,
1699 struct vlv_wm_state *wm_state, int level)
1700{
1701 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1702
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001703 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001704 enum plane_id plane_id;
1705
1706 for_each_plane_id_on_crtc(crtc, plane_id)
1707 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1708
1709 wm_state->sr[level].cursor = USHRT_MAX;
1710 wm_state->sr[level].plane = USHRT_MAX;
1711 }
1712}
1713
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001714static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1715{
1716 if (wm > fifo_size)
1717 return USHRT_MAX;
1718 else
1719 return fifo_size - wm;
1720}
1721
Ville Syrjäläff32c542017-03-02 19:14:57 +02001722/*
1723 * Starting from 'level' set all higher
1724 * levels to 'value' in the "raw" watermarks.
1725 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001726static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001727 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001728{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001729 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001730 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001731 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001732
Ville Syrjäläff32c542017-03-02 19:14:57 +02001733 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001734 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001735
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001736 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001737 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001738 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001739
1740 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001741}
1742
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001743static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1744 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001745{
1746 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1747 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001748 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001749 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001750 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001751
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001752 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001753 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1754 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001755 }
1756
1757 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001758 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001759 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1760 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1761
Ville Syrjäläff32c542017-03-02 19:14:57 +02001762 if (wm > max_wm)
1763 break;
1764
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001765 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001766 raw->plane[plane_id] = wm;
1767 }
1768
1769 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001770 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001771
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001772out:
1773 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001774 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001775 plane->base.name,
1776 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1777 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1778 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1779
1780 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001781}
1782
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001783static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1784 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001785{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001786 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001787 &crtc_state->wm.vlv.raw[level];
1788 const struct vlv_fifo_state *fifo_state =
1789 &crtc_state->wm.vlv.fifo_state;
1790
1791 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1792}
1793
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001794static 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 +02001795{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001796 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1797 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1798 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1799 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001800}
1801
1802static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001803{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001804 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001805 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001806 struct intel_atomic_state *state =
1807 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001808 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001809 const struct vlv_fifo_state *fifo_state =
1810 &crtc_state->wm.vlv.fifo_state;
1811 int num_active_planes = hweight32(crtc_state->active_planes &
1812 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001813 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001814 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001815 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001816 enum plane_id plane_id;
1817 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001818 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001819
Ville Syrjäläff32c542017-03-02 19:14:57 +02001820 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1821 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001822 to_intel_plane_state(plane->base.state);
1823
Ville Syrjäläff32c542017-03-02 19:14:57 +02001824 if (plane_state->base.crtc != &crtc->base &&
1825 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001826 continue;
1827
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001828 if (vlv_raw_plane_wm_compute(crtc_state, plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001829 dirty |= BIT(plane->id);
1830 }
1831
1832 /*
1833 * DSPARB registers may have been reset due to the
1834 * power well being turned off. Make sure we restore
1835 * them to a consistent state even if no primary/sprite
1836 * planes are initially active.
1837 */
1838 if (needs_modeset)
1839 crtc_state->fifo_changed = true;
1840
1841 if (!dirty)
1842 return 0;
1843
1844 /* cursor changes don't warrant a FIFO recompute */
1845 if (dirty & ~BIT(PLANE_CURSOR)) {
1846 const struct intel_crtc_state *old_crtc_state =
1847 to_intel_crtc_state(crtc->base.state);
1848 const struct vlv_fifo_state *old_fifo_state =
1849 &old_crtc_state->wm.vlv.fifo_state;
1850
1851 ret = vlv_compute_fifo(crtc_state);
1852 if (ret)
1853 return ret;
1854
1855 if (needs_modeset ||
1856 memcmp(old_fifo_state, fifo_state,
1857 sizeof(*fifo_state)) != 0)
1858 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001859 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001860
Ville Syrjäläff32c542017-03-02 19:14:57 +02001861 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001862 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001863 /*
1864 * Note that enabling cxsr with no primary/sprite planes
1865 * enabled can wedge the pipe. Hence we only allow cxsr
1866 * with exactly one enabled primary/sprite plane.
1867 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001868 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001869
Ville Syrjälä5012e602017-03-02 19:14:56 +02001870 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001871 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001872 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001873
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001874 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001875 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001876
Ville Syrjäläff32c542017-03-02 19:14:57 +02001877 for_each_plane_id_on_crtc(crtc, plane_id) {
1878 wm_state->wm[level].plane[plane_id] =
1879 vlv_invert_wm_value(raw->plane[plane_id],
1880 fifo_state->plane[plane_id]);
1881 }
1882
1883 wm_state->sr[level].plane =
1884 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001885 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001886 raw->plane[PLANE_SPRITE1]),
1887 sr_fifo_size);
1888
1889 wm_state->sr[level].cursor =
1890 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1891 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001892 }
1893
Ville Syrjäläff32c542017-03-02 19:14:57 +02001894 if (level == 0)
1895 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001896
Ville Syrjäläff32c542017-03-02 19:14:57 +02001897 /* limit to only levels we can actually handle */
1898 wm_state->num_levels = level;
1899
1900 /* invalidate the higher levels */
1901 vlv_invalidate_wms(crtc, wm_state, level);
1902
1903 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001904}
1905
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001906#define VLV_FIFO(plane, value) \
1907 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1908
Ville Syrjäläff32c542017-03-02 19:14:57 +02001909static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1910 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001911{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001912 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001913 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001914 const struct vlv_fifo_state *fifo_state =
1915 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001916 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001917
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001918 if (!crtc_state->fifo_changed)
1919 return;
1920
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001921 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1922 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1923 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001924
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001925 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1926 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001927
Ville Syrjäläc137d662017-03-02 19:15:06 +02001928 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1929
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001930 /*
1931 * uncore.lock serves a double purpose here. It allows us to
1932 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1933 * it protects the DSPARB registers from getting clobbered by
1934 * parallel updates from multiple pipes.
1935 *
1936 * intel_pipe_update_start() has already disabled interrupts
1937 * for us, so a plain spin_lock() is sufficient here.
1938 */
1939 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001940
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001941 switch (crtc->pipe) {
1942 uint32_t dsparb, dsparb2, dsparb3;
1943 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001944 dsparb = I915_READ_FW(DSPARB);
1945 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001946
1947 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1948 VLV_FIFO(SPRITEB, 0xff));
1949 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1950 VLV_FIFO(SPRITEB, sprite1_start));
1951
1952 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1953 VLV_FIFO(SPRITEB_HI, 0x1));
1954 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1955 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1956
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001957 I915_WRITE_FW(DSPARB, dsparb);
1958 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001959 break;
1960 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001961 dsparb = I915_READ_FW(DSPARB);
1962 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001963
1964 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1965 VLV_FIFO(SPRITED, 0xff));
1966 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1967 VLV_FIFO(SPRITED, sprite1_start));
1968
1969 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1970 VLV_FIFO(SPRITED_HI, 0xff));
1971 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1972 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1973
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001974 I915_WRITE_FW(DSPARB, dsparb);
1975 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001976 break;
1977 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001978 dsparb3 = I915_READ_FW(DSPARB3);
1979 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001980
1981 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1982 VLV_FIFO(SPRITEF, 0xff));
1983 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1984 VLV_FIFO(SPRITEF, sprite1_start));
1985
1986 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1987 VLV_FIFO(SPRITEF_HI, 0xff));
1988 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1989 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1990
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001991 I915_WRITE_FW(DSPARB3, dsparb3);
1992 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001993 break;
1994 default:
1995 break;
1996 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001997
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001998 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001999
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002000 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002001}
2002
2003#undef VLV_FIFO
2004
Ville Syrjälä4841da52017-03-02 19:14:59 +02002005static int vlv_compute_intermediate_wm(struct drm_device *dev,
2006 struct intel_crtc *crtc,
2007 struct intel_crtc_state *crtc_state)
2008{
2009 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
2010 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
2011 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
2012 int level;
2013
2014 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002015 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2016 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002017
2018 for (level = 0; level < intermediate->num_levels; level++) {
2019 enum plane_id plane_id;
2020
2021 for_each_plane_id_on_crtc(crtc, plane_id) {
2022 intermediate->wm[level].plane[plane_id] =
2023 min(optimal->wm[level].plane[plane_id],
2024 active->wm[level].plane[plane_id]);
2025 }
2026
2027 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2028 active->sr[level].plane);
2029 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2030 active->sr[level].cursor);
2031 }
2032
2033 vlv_invalidate_wms(crtc, intermediate, level);
2034
2035 /*
2036 * If our intermediate WM are identical to the final WM, then we can
2037 * omit the post-vblank programming; only update if it's different.
2038 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002039 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2040 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002041
2042 return 0;
2043}
2044
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002045static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002046 struct vlv_wm_values *wm)
2047{
2048 struct intel_crtc *crtc;
2049 int num_active_crtcs = 0;
2050
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002051 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002052 wm->cxsr = true;
2053
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002054 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002055 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002056
2057 if (!crtc->active)
2058 continue;
2059
2060 if (!wm_state->cxsr)
2061 wm->cxsr = false;
2062
2063 num_active_crtcs++;
2064 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2065 }
2066
2067 if (num_active_crtcs != 1)
2068 wm->cxsr = false;
2069
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002070 if (num_active_crtcs > 1)
2071 wm->level = VLV_WM_LEVEL_PM2;
2072
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002073 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002074 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002075 enum pipe pipe = crtc->pipe;
2076
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002077 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002078 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002079 wm->sr = wm_state->sr[wm->level];
2080
Ville Syrjälä1b313892016-11-28 19:37:08 +02002081 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2082 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2083 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2084 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002085 }
2086}
2087
Ville Syrjäläff32c542017-03-02 19:14:57 +02002088static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002089{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002090 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2091 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002092
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002093 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002094
Ville Syrjäläff32c542017-03-02 19:14:57 +02002095 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002096 return;
2097
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002098 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002099 chv_set_memory_dvfs(dev_priv, false);
2100
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002101 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002102 chv_set_memory_pm5(dev_priv, false);
2103
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002104 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002105 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002106
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002107 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002108
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002109 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002110 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002111
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002112 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002113 chv_set_memory_pm5(dev_priv, true);
2114
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002115 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002116 chv_set_memory_dvfs(dev_priv, true);
2117
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002118 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002119}
2120
Ville Syrjäläff32c542017-03-02 19:14:57 +02002121static void vlv_initial_watermarks(struct intel_atomic_state *state,
2122 struct intel_crtc_state *crtc_state)
2123{
2124 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2125 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2126
2127 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002128 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2129 vlv_program_watermarks(dev_priv);
2130 mutex_unlock(&dev_priv->wm.wm_mutex);
2131}
2132
2133static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2134 struct intel_crtc_state *crtc_state)
2135{
2136 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2138
2139 if (!crtc_state->wm.need_postvbl_update)
2140 return;
2141
2142 mutex_lock(&dev_priv->wm.wm_mutex);
2143 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002144 vlv_program_watermarks(dev_priv);
2145 mutex_unlock(&dev_priv->wm.wm_mutex);
2146}
2147
Ville Syrjälä432081b2016-10-31 22:37:03 +02002148static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002149{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002150 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002151 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002152 int srwm = 1;
2153 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002154 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002155
2156 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002157 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002158 if (crtc) {
2159 /* self-refresh has much higher latency */
2160 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002161 const struct drm_display_mode *adjusted_mode =
2162 &crtc->config->base.adjusted_mode;
2163 const struct drm_framebuffer *fb =
2164 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002165 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002166 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002167 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002168 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002169 int entries;
2170
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002171 entries = intel_wm_method2(clock, htotal,
2172 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002173 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2174 srwm = I965_FIFO_SIZE - entries;
2175 if (srwm < 0)
2176 srwm = 1;
2177 srwm &= 0x1ff;
2178 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2179 entries, srwm);
2180
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002181 entries = intel_wm_method2(clock, htotal,
2182 crtc->base.cursor->state->crtc_w, 4,
2183 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002184 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002185 i965_cursor_wm_info.cacheline_size) +
2186 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002187
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002188 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002189 if (cursor_sr > i965_cursor_wm_info.max_wm)
2190 cursor_sr = i965_cursor_wm_info.max_wm;
2191
2192 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2193 "cursor %d\n", srwm, cursor_sr);
2194
Imre Deak98584252014-06-13 14:54:20 +03002195 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002196 } else {
Imre Deak98584252014-06-13 14:54:20 +03002197 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002198 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002199 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002200 }
2201
2202 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2203 srwm);
2204
2205 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002206 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2207 FW_WM(8, CURSORB) |
2208 FW_WM(8, PLANEB) |
2209 FW_WM(8, PLANEA));
2210 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2211 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002212 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002213 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002214
2215 if (cxsr_enabled)
2216 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002217}
2218
Ville Syrjäläf4998962015-03-10 17:02:21 +02002219#undef FW_WM
2220
Ville Syrjälä432081b2016-10-31 22:37:03 +02002221static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002222{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002223 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002224 const struct intel_watermark_params *wm_info;
2225 uint32_t fwater_lo;
2226 uint32_t fwater_hi;
2227 int cwm, srwm = 1;
2228 int fifo_size;
2229 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002230 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002231
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002232 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002233 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002234 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002235 wm_info = &i915_wm_info;
2236 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002237 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002238
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002239 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002240 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002241 if (intel_crtc_active(crtc)) {
2242 const struct drm_display_mode *adjusted_mode =
2243 &crtc->config->base.adjusted_mode;
2244 const struct drm_framebuffer *fb =
2245 crtc->base.primary->state->fb;
2246 int cpp;
2247
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002248 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002249 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002250 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002251 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002252
Damien Lespiau241bfc32013-09-25 16:45:37 +01002253 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002254 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002255 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002256 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002257 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002258 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002259 if (planea_wm > (long)wm_info->max_wm)
2260 planea_wm = wm_info->max_wm;
2261 }
2262
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002263 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002264 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002265
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002266 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002267 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002268 if (intel_crtc_active(crtc)) {
2269 const struct drm_display_mode *adjusted_mode =
2270 &crtc->config->base.adjusted_mode;
2271 const struct drm_framebuffer *fb =
2272 crtc->base.primary->state->fb;
2273 int cpp;
2274
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002275 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002276 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002277 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002278 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002279
Damien Lespiau241bfc32013-09-25 16:45:37 +01002280 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002281 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002282 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002283 if (enabled == NULL)
2284 enabled = crtc;
2285 else
2286 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002287 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002288 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002289 if (planeb_wm > (long)wm_info->max_wm)
2290 planeb_wm = wm_info->max_wm;
2291 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002292
2293 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2294
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002295 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002296 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002297
Ville Syrjäläefc26112016-10-31 22:37:04 +02002298 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002299
2300 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002301 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002302 enabled = NULL;
2303 }
2304
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002305 /*
2306 * Overlay gets an aggressive default since video jitter is bad.
2307 */
2308 cwm = 2;
2309
2310 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002311 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002312
2313 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002314 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002315 /* self-refresh has much higher latency */
2316 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002317 const struct drm_display_mode *adjusted_mode =
2318 &enabled->config->base.adjusted_mode;
2319 const struct drm_framebuffer *fb =
2320 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002321 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002322 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002323 int hdisplay = enabled->config->pipe_src_w;
2324 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002325 int entries;
2326
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002327 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002328 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002329 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002330 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002331
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002332 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2333 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002334 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2335 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2336 srwm = wm_info->fifo_size - entries;
2337 if (srwm < 0)
2338 srwm = 1;
2339
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002340 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341 I915_WRITE(FW_BLC_SELF,
2342 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002343 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2345 }
2346
2347 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2348 planea_wm, planeb_wm, cwm, srwm);
2349
2350 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2351 fwater_hi = (cwm & 0x1f);
2352
2353 /* Set request length to 8 cachelines per fetch */
2354 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2355 fwater_hi = fwater_hi | (1 << 8);
2356
2357 I915_WRITE(FW_BLC, fwater_lo);
2358 I915_WRITE(FW_BLC2, fwater_hi);
2359
Imre Deak5209b1f2014-07-01 12:36:17 +03002360 if (enabled)
2361 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002362}
2363
Ville Syrjälä432081b2016-10-31 22:37:03 +02002364static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002365{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002366 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002367 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002368 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002369 uint32_t fwater_lo;
2370 int planea_wm;
2371
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002372 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002373 if (crtc == NULL)
2374 return;
2375
Ville Syrjäläefc26112016-10-31 22:37:04 +02002376 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002377 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002378 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002379 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01002380 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002381 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2382 fwater_lo |= (3<<8) | planea_wm;
2383
2384 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2385
2386 I915_WRITE(FW_BLC, fwater_lo);
2387}
2388
Ville Syrjälä37126462013-08-01 16:18:55 +03002389/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002390static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2391 unsigned int cpp,
2392 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002393{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002394 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002395
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002396 ret = intel_wm_method1(pixel_rate, cpp, latency);
2397 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002398
2399 return ret;
2400}
2401
Ville Syrjälä37126462013-08-01 16:18:55 +03002402/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002403static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2404 unsigned int htotal,
2405 unsigned int width,
2406 unsigned int cpp,
2407 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002408{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002409 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002410
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002411 ret = intel_wm_method2(pixel_rate, htotal,
2412 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002413 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002414
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002415 return ret;
2416}
2417
Ville Syrjälä23297042013-07-05 11:57:17 +03002418static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002419 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002420{
Matt Roper15126882015-12-03 11:37:40 -08002421 /*
2422 * Neither of these should be possible since this function shouldn't be
2423 * called if the CRTC is off or the plane is invisible. But let's be
2424 * extra paranoid to avoid a potential divide-by-zero if we screw up
2425 * elsewhere in the driver.
2426 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002427 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002428 return 0;
2429 if (WARN_ON(!horiz_pixels))
2430 return 0;
2431
Ville Syrjäläac484962016-01-20 21:05:26 +02002432 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002433}
2434
Imre Deak820c1982013-12-17 14:46:36 +02002435struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002436 uint16_t pri;
2437 uint16_t spr;
2438 uint16_t cur;
2439 uint16_t fbc;
2440};
2441
Ville Syrjälä37126462013-08-01 16:18:55 +03002442/*
2443 * For both WM_PIPE and WM_LP.
2444 * mem_value must be in 0.1us units.
2445 */
Matt Roper7221fc32015-09-24 15:53:08 -07002446static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002447 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002448 uint32_t mem_value,
2449 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002450{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002451 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002452 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002453
Ville Syrjälä24304d812017-03-14 17:10:49 +02002454 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002455 return 0;
2456
Ville Syrjälä353c8592016-12-14 23:30:57 +02002457 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002458
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002459 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002460
2461 if (!is_lp)
2462 return method1;
2463
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002464 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002465 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002466 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002467 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002468
2469 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002470}
2471
Ville Syrjälä37126462013-08-01 16:18:55 +03002472/*
2473 * For both WM_PIPE and WM_LP.
2474 * mem_value must be in 0.1us units.
2475 */
Matt Roper7221fc32015-09-24 15:53:08 -07002476static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002477 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002478 uint32_t mem_value)
2479{
2480 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002481 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002482
Ville Syrjälä24304d812017-03-14 17:10:49 +02002483 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002484 return 0;
2485
Ville Syrjälä353c8592016-12-14 23:30:57 +02002486 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002487
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002488 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2489 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002490 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002491 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002492 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002493 return min(method1, method2);
2494}
2495
Ville Syrjälä37126462013-08-01 16:18:55 +03002496/*
2497 * For both WM_PIPE and WM_LP.
2498 * mem_value must be in 0.1us units.
2499 */
Matt Roper7221fc32015-09-24 15:53:08 -07002500static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002501 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002502 uint32_t mem_value)
2503{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002504 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002505
Ville Syrjälä24304d812017-03-14 17:10:49 +02002506 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002507 return 0;
2508
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002509 cpp = pstate->base.fb->format->cpp[0];
2510
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002511 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002512 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002513 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002514}
2515
Paulo Zanonicca32e92013-05-31 11:45:06 -03002516/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002517static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002518 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002519 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002520{
Ville Syrjälä83054942016-11-18 21:53:00 +02002521 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002522
Ville Syrjälä24304d812017-03-14 17:10:49 +02002523 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002524 return 0;
2525
Ville Syrjälä353c8592016-12-14 23:30:57 +02002526 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002527
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002528 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002529}
2530
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002531static unsigned int
2532ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002533{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002534 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002535 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002536 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002537 return 768;
2538 else
2539 return 512;
2540}
2541
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002542static unsigned int
2543ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2544 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002545{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002546 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002547 /* BDW primary/sprite plane watermarks */
2548 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002549 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002550 /* IVB/HSW primary/sprite plane watermarks */
2551 return level == 0 ? 127 : 1023;
2552 else if (!is_sprite)
2553 /* ILK/SNB primary plane watermarks */
2554 return level == 0 ? 127 : 511;
2555 else
2556 /* ILK/SNB sprite plane watermarks */
2557 return level == 0 ? 63 : 255;
2558}
2559
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002560static unsigned int
2561ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002562{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002563 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002564 return level == 0 ? 63 : 255;
2565 else
2566 return level == 0 ? 31 : 63;
2567}
2568
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002569static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002570{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002571 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002572 return 31;
2573 else
2574 return 15;
2575}
2576
Ville Syrjälä158ae642013-08-07 13:28:19 +03002577/* Calculate the maximum primary/sprite plane watermark */
2578static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2579 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002580 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002581 enum intel_ddb_partitioning ddb_partitioning,
2582 bool is_sprite)
2583{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002584 struct drm_i915_private *dev_priv = to_i915(dev);
2585 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002586
2587 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002588 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002589 return 0;
2590
2591 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002592 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002593 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002594
2595 /*
2596 * For some reason the non self refresh
2597 * FIFO size is only half of the self
2598 * refresh FIFO size on ILK/SNB.
2599 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002600 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002601 fifo_size /= 2;
2602 }
2603
Ville Syrjälä240264f2013-08-07 13:29:12 +03002604 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002605 /* level 0 is always calculated with 1:1 split */
2606 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2607 if (is_sprite)
2608 fifo_size *= 5;
2609 fifo_size /= 6;
2610 } else {
2611 fifo_size /= 2;
2612 }
2613 }
2614
2615 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002616 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002617}
2618
2619/* Calculate the maximum cursor plane watermark */
2620static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002621 int level,
2622 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002623{
2624 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002625 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002626 return 64;
2627
2628 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002629 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002630}
2631
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002632static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002633 int level,
2634 const struct intel_wm_config *config,
2635 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002636 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002637{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002638 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2639 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2640 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002641 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002642}
2643
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002644static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002645 int level,
2646 struct ilk_wm_maximums *max)
2647{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002648 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2649 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2650 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2651 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002652}
2653
Ville Syrjäläd9395652013-10-09 19:18:10 +03002654static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002655 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002656 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002657{
2658 bool ret;
2659
2660 /* already determined to be invalid? */
2661 if (!result->enable)
2662 return false;
2663
2664 result->enable = result->pri_val <= max->pri &&
2665 result->spr_val <= max->spr &&
2666 result->cur_val <= max->cur;
2667
2668 ret = result->enable;
2669
2670 /*
2671 * HACK until we can pre-compute everything,
2672 * and thus fail gracefully if LP0 watermarks
2673 * are exceeded...
2674 */
2675 if (level == 0 && !result->enable) {
2676 if (result->pri_val > max->pri)
2677 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2678 level, result->pri_val, max->pri);
2679 if (result->spr_val > max->spr)
2680 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2681 level, result->spr_val, max->spr);
2682 if (result->cur_val > max->cur)
2683 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2684 level, result->cur_val, max->cur);
2685
2686 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2687 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2688 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2689 result->enable = true;
2690 }
2691
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002692 return ret;
2693}
2694
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002695static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002696 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002697 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002698 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002699 struct intel_plane_state *pristate,
2700 struct intel_plane_state *sprstate,
2701 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002702 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002703{
2704 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2705 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2706 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2707
2708 /* WM1+ latency values stored in 0.5us units */
2709 if (level > 0) {
2710 pri_latency *= 5;
2711 spr_latency *= 5;
2712 cur_latency *= 5;
2713 }
2714
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002715 if (pristate) {
2716 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2717 pri_latency, level);
2718 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2719 }
2720
2721 if (sprstate)
2722 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2723
2724 if (curstate)
2725 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2726
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002727 result->enable = true;
2728}
2729
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002730static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002731hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002732{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002733 const struct intel_atomic_state *intel_state =
2734 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002735 const struct drm_display_mode *adjusted_mode =
2736 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002737 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002738
Matt Roperee91a152015-12-03 11:37:39 -08002739 if (!cstate->base.active)
2740 return 0;
2741 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2742 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002743 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002744 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002745
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002746 /* The WM are computed with base on how long it takes to fill a single
2747 * row at the given clock rate, multiplied by 8.
2748 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002749 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2750 adjusted_mode->crtc_clock);
2751 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002752 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002753
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002754 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2755 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002756}
2757
Ville Syrjäläbb726512016-10-31 22:37:24 +02002758static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2759 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002760{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002761 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002762 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002763 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002764 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002765
2766 /* read the first set of memory latencies[0:3] */
2767 val = 0; /* data0 to be programmed to 0 for first set */
2768 mutex_lock(&dev_priv->rps.hw_lock);
2769 ret = sandybridge_pcode_read(dev_priv,
2770 GEN9_PCODE_READ_MEM_LATENCY,
2771 &val);
2772 mutex_unlock(&dev_priv->rps.hw_lock);
2773
2774 if (ret) {
2775 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2776 return;
2777 }
2778
2779 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2780 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2781 GEN9_MEM_LATENCY_LEVEL_MASK;
2782 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2783 GEN9_MEM_LATENCY_LEVEL_MASK;
2784 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2785 GEN9_MEM_LATENCY_LEVEL_MASK;
2786
2787 /* read the second set of memory latencies[4:7] */
2788 val = 1; /* data0 to be programmed to 1 for second set */
2789 mutex_lock(&dev_priv->rps.hw_lock);
2790 ret = sandybridge_pcode_read(dev_priv,
2791 GEN9_PCODE_READ_MEM_LATENCY,
2792 &val);
2793 mutex_unlock(&dev_priv->rps.hw_lock);
2794 if (ret) {
2795 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2796 return;
2797 }
2798
2799 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2800 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2801 GEN9_MEM_LATENCY_LEVEL_MASK;
2802 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2803 GEN9_MEM_LATENCY_LEVEL_MASK;
2804 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2805 GEN9_MEM_LATENCY_LEVEL_MASK;
2806
Vandana Kannan367294b2014-11-04 17:06:46 +00002807 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002808 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2809 * need to be disabled. We make sure to sanitize the values out
2810 * of the punit to satisfy this requirement.
2811 */
2812 for (level = 1; level <= max_level; level++) {
2813 if (wm[level] == 0) {
2814 for (i = level + 1; i <= max_level; i++)
2815 wm[i] = 0;
2816 break;
2817 }
2818 }
2819
2820 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002821 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002822 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002823 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002824 * to add 2us to the various latency levels we retrieve from the
2825 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002826 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002827 if (wm[0] == 0) {
2828 wm[0] += 2;
2829 for (level = 1; level <= max_level; level++) {
2830 if (wm[level] == 0)
2831 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002832 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002833 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002834 }
2835
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002836 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002837 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2838
2839 wm[0] = (sskpd >> 56) & 0xFF;
2840 if (wm[0] == 0)
2841 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002842 wm[1] = (sskpd >> 4) & 0xFF;
2843 wm[2] = (sskpd >> 12) & 0xFF;
2844 wm[3] = (sskpd >> 20) & 0x1FF;
2845 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002846 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002847 uint32_t sskpd = I915_READ(MCH_SSKPD);
2848
2849 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2850 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2851 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2852 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002853 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002854 uint32_t mltr = I915_READ(MLTR_ILK);
2855
2856 /* ILK primary LP0 latency is 700 ns */
2857 wm[0] = 7;
2858 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2859 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002860 }
2861}
2862
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002863static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2864 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002865{
2866 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002867 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002868 wm[0] = 13;
2869}
2870
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002871static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2872 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002873{
2874 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002875 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002876 wm[0] = 13;
2877
2878 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002879 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002880 wm[3] *= 2;
2881}
2882
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002883int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002884{
2885 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002886 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002887 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002888 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002889 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002890 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002891 return 3;
2892 else
2893 return 2;
2894}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002895
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002896static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002897 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002898 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002899{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002900 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002901
2902 for (level = 0; level <= max_level; level++) {
2903 unsigned int latency = wm[level];
2904
2905 if (latency == 0) {
2906 DRM_ERROR("%s WM%d latency not provided\n",
2907 name, level);
2908 continue;
2909 }
2910
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002911 /*
2912 * - latencies are in us on gen9.
2913 * - before then, WM1+ latency values are in 0.5us units
2914 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002915 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002916 latency *= 10;
2917 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002918 latency *= 5;
2919
2920 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2921 name, level, wm[level],
2922 latency / 10, latency % 10);
2923 }
2924}
2925
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002926static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2927 uint16_t wm[5], uint16_t min)
2928{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002929 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002930
2931 if (wm[0] >= min)
2932 return false;
2933
2934 wm[0] = max(wm[0], min);
2935 for (level = 1; level <= max_level; level++)
2936 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2937
2938 return true;
2939}
2940
Ville Syrjäläbb726512016-10-31 22:37:24 +02002941static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002942{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002943 bool changed;
2944
2945 /*
2946 * The BIOS provided WM memory latency values are often
2947 * inadequate for high resolution displays. Adjust them.
2948 */
2949 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2950 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2951 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2952
2953 if (!changed)
2954 return;
2955
2956 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002957 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2958 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2959 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002960}
2961
Ville Syrjäläbb726512016-10-31 22:37:24 +02002962static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002963{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002964 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002965
2966 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2967 sizeof(dev_priv->wm.pri_latency));
2968 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2969 sizeof(dev_priv->wm.pri_latency));
2970
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002971 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002972 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002973
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002974 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2975 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2976 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002977
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002978 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002979 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002980}
2981
Ville Syrjäläbb726512016-10-31 22:37:24 +02002982static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002983{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002984 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002985 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002986}
2987
Matt Ropered4a6a72016-02-23 17:20:13 -08002988static bool ilk_validate_pipe_wm(struct drm_device *dev,
2989 struct intel_pipe_wm *pipe_wm)
2990{
2991 /* LP0 watermark maximums depend on this pipe alone */
2992 const struct intel_wm_config config = {
2993 .num_pipes_active = 1,
2994 .sprites_enabled = pipe_wm->sprites_enabled,
2995 .sprites_scaled = pipe_wm->sprites_scaled,
2996 };
2997 struct ilk_wm_maximums max;
2998
2999 /* LP0 watermarks always use 1/2 DDB partitioning */
3000 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
3001
3002 /* At least LP0 must be valid */
3003 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3004 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3005 return false;
3006 }
3007
3008 return true;
3009}
3010
Matt Roper261a27d2015-10-08 15:28:25 -07003011/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003012static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003013{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003014 struct drm_atomic_state *state = cstate->base.state;
3015 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003016 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003017 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003018 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07003019 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003020 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07003021 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003022 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003023 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003024 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003025
Matt Ropere8f1f022016-05-12 07:05:55 -07003026 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003027
Matt Roper43d59ed2015-09-24 15:53:07 -07003028 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003029 struct intel_plane_state *ps;
3030
3031 ps = intel_atomic_get_existing_plane_state(state,
3032 intel_plane);
3033 if (!ps)
3034 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003035
3036 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003037 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003038 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003039 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003040 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003041 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003042 }
3043
Matt Ropered4a6a72016-02-23 17:20:13 -08003044 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003045 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003046 pipe_wm->sprites_enabled = sprstate->base.visible;
3047 pipe_wm->sprites_scaled = sprstate->base.visible &&
3048 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3049 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003050 }
3051
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003052 usable_level = max_level;
3053
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003054 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003055 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003056 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003057
3058 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003059 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003060 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003061
Matt Roper86c8bbb2015-09-24 15:53:16 -07003062 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003063 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
3064
3065 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3066 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003067
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003068 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003069 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003070
Matt Ropered4a6a72016-02-23 17:20:13 -08003071 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003072 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003073
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003074 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003075
3076 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003077 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003078
Matt Roper86c8bbb2015-09-24 15:53:16 -07003079 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003080 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003081
3082 /*
3083 * Disable any watermark level that exceeds the
3084 * register maximums since such watermarks are
3085 * always invalid.
3086 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003087 if (level > usable_level)
3088 continue;
3089
3090 if (ilk_validate_wm_level(level, &max, wm))
3091 pipe_wm->wm[level] = *wm;
3092 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003093 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003094 }
3095
Matt Roper86c8bbb2015-09-24 15:53:16 -07003096 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003097}
3098
3099/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003100 * Build a set of 'intermediate' watermark values that satisfy both the old
3101 * state and the new state. These can be programmed to the hardware
3102 * immediately.
3103 */
3104static int ilk_compute_intermediate_wm(struct drm_device *dev,
3105 struct intel_crtc *intel_crtc,
3106 struct intel_crtc_state *newstate)
3107{
Matt Ropere8f1f022016-05-12 07:05:55 -07003108 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08003109 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003110 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003111
3112 /*
3113 * Start with the final, target watermarks, then combine with the
3114 * currently active watermarks to get values that are safe both before
3115 * and after the vblank.
3116 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003117 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08003118 a->pipe_enabled |= b->pipe_enabled;
3119 a->sprites_enabled |= b->sprites_enabled;
3120 a->sprites_scaled |= b->sprites_scaled;
3121
3122 for (level = 0; level <= max_level; level++) {
3123 struct intel_wm_level *a_wm = &a->wm[level];
3124 const struct intel_wm_level *b_wm = &b->wm[level];
3125
3126 a_wm->enable &= b_wm->enable;
3127 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3128 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3129 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3130 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3131 }
3132
3133 /*
3134 * We need to make sure that these merged watermark values are
3135 * actually a valid configuration themselves. If they're not,
3136 * there's no safe way to transition from the old state to
3137 * the new state, so we need to fail the atomic transaction.
3138 */
3139 if (!ilk_validate_pipe_wm(dev, a))
3140 return -EINVAL;
3141
3142 /*
3143 * If our intermediate WM are identical to the final WM, then we can
3144 * omit the post-vblank programming; only update if it's different.
3145 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003146 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3147 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003148
3149 return 0;
3150}
3151
3152/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003153 * Merge the watermarks from all active pipes for a specific level.
3154 */
3155static void ilk_merge_wm_level(struct drm_device *dev,
3156 int level,
3157 struct intel_wm_level *ret_wm)
3158{
3159 const struct intel_crtc *intel_crtc;
3160
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003161 ret_wm->enable = true;
3162
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003163 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003164 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003165 const struct intel_wm_level *wm = &active->wm[level];
3166
3167 if (!active->pipe_enabled)
3168 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003169
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003170 /*
3171 * The watermark values may have been used in the past,
3172 * so we must maintain them in the registers for some
3173 * time even if the level is now disabled.
3174 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003175 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003176 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003177
3178 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3179 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3180 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3181 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3182 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003183}
3184
3185/*
3186 * Merge all low power watermarks for all active pipes.
3187 */
3188static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003189 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003190 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003191 struct intel_pipe_wm *merged)
3192{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003193 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003194 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003195 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003196
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003197 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003198 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003199 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003200 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003201
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003202 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003203 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003204
3205 /* merge each WM1+ level */
3206 for (level = 1; level <= max_level; level++) {
3207 struct intel_wm_level *wm = &merged->wm[level];
3208
3209 ilk_merge_wm_level(dev, level, wm);
3210
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003211 if (level > last_enabled_level)
3212 wm->enable = false;
3213 else if (!ilk_validate_wm_level(level, max, wm))
3214 /* make sure all following levels get disabled */
3215 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003216
3217 /*
3218 * The spec says it is preferred to disable
3219 * FBC WMs instead of disabling a WM level.
3220 */
3221 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003222 if (wm->enable)
3223 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003224 wm->fbc_val = 0;
3225 }
3226 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003227
3228 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3229 /*
3230 * FIXME this is racy. FBC might get enabled later.
3231 * What we should check here is whether FBC can be
3232 * enabled sometime later.
3233 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003234 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003235 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003236 for (level = 2; level <= max_level; level++) {
3237 struct intel_wm_level *wm = &merged->wm[level];
3238
3239 wm->enable = false;
3240 }
3241 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003242}
3243
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003244static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3245{
3246 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3247 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3248}
3249
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003250/* The value we need to program into the WM_LPx latency field */
3251static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3252{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003253 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003254
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003255 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003256 return 2 * level;
3257 else
3258 return dev_priv->wm.pri_latency[level];
3259}
3260
Imre Deak820c1982013-12-17 14:46:36 +02003261static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003262 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003263 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003264 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003265{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003266 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003267 struct intel_crtc *intel_crtc;
3268 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003269
Ville Syrjälä0362c782013-10-09 19:17:57 +03003270 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003271 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003272
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003273 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003274 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003275 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003276
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003277 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003278
Ville Syrjälä0362c782013-10-09 19:17:57 +03003279 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003280
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003281 /*
3282 * Maintain the watermark values even if the level is
3283 * disabled. Doing otherwise could cause underruns.
3284 */
3285 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003286 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003287 (r->pri_val << WM1_LP_SR_SHIFT) |
3288 r->cur_val;
3289
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003290 if (r->enable)
3291 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3292
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003293 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003294 results->wm_lp[wm_lp - 1] |=
3295 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3296 else
3297 results->wm_lp[wm_lp - 1] |=
3298 r->fbc_val << WM1_LP_FBC_SHIFT;
3299
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003300 /*
3301 * Always set WM1S_LP_EN when spr_val != 0, even if the
3302 * level is disabled. Doing otherwise could cause underruns.
3303 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003304 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003305 WARN_ON(wm_lp != 1);
3306 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3307 } else
3308 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003309 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003310
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003311 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003312 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003313 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003314 const struct intel_wm_level *r =
3315 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003316
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003317 if (WARN_ON(!r->enable))
3318 continue;
3319
Matt Ropered4a6a72016-02-23 17:20:13 -08003320 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003321
3322 results->wm_pipe[pipe] =
3323 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3324 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3325 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003326 }
3327}
3328
Paulo Zanoni861f3382013-05-31 10:19:21 -03003329/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3330 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003331static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003332 struct intel_pipe_wm *r1,
3333 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003334{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003335 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003336 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003337
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003338 for (level = 1; level <= max_level; level++) {
3339 if (r1->wm[level].enable)
3340 level1 = level;
3341 if (r2->wm[level].enable)
3342 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003343 }
3344
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003345 if (level1 == level2) {
3346 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003347 return r2;
3348 else
3349 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003350 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003351 return r1;
3352 } else {
3353 return r2;
3354 }
3355}
3356
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003357/* dirty bits used to track which watermarks need changes */
3358#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3359#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3360#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3361#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3362#define WM_DIRTY_FBC (1 << 24)
3363#define WM_DIRTY_DDB (1 << 25)
3364
Damien Lespiau055e3932014-08-18 13:49:10 +01003365static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003366 const struct ilk_wm_values *old,
3367 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003368{
3369 unsigned int dirty = 0;
3370 enum pipe pipe;
3371 int wm_lp;
3372
Damien Lespiau055e3932014-08-18 13:49:10 +01003373 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003374 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3375 dirty |= WM_DIRTY_LINETIME(pipe);
3376 /* Must disable LP1+ watermarks too */
3377 dirty |= WM_DIRTY_LP_ALL;
3378 }
3379
3380 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3381 dirty |= WM_DIRTY_PIPE(pipe);
3382 /* Must disable LP1+ watermarks too */
3383 dirty |= WM_DIRTY_LP_ALL;
3384 }
3385 }
3386
3387 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3388 dirty |= WM_DIRTY_FBC;
3389 /* Must disable LP1+ watermarks too */
3390 dirty |= WM_DIRTY_LP_ALL;
3391 }
3392
3393 if (old->partitioning != new->partitioning) {
3394 dirty |= WM_DIRTY_DDB;
3395 /* Must disable LP1+ watermarks too */
3396 dirty |= WM_DIRTY_LP_ALL;
3397 }
3398
3399 /* LP1+ watermarks already deemed dirty, no need to continue */
3400 if (dirty & WM_DIRTY_LP_ALL)
3401 return dirty;
3402
3403 /* Find the lowest numbered LP1+ watermark in need of an update... */
3404 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3405 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3406 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3407 break;
3408 }
3409
3410 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3411 for (; wm_lp <= 3; wm_lp++)
3412 dirty |= WM_DIRTY_LP(wm_lp);
3413
3414 return dirty;
3415}
3416
Ville Syrjälä8553c182013-12-05 15:51:39 +02003417static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3418 unsigned int dirty)
3419{
Imre Deak820c1982013-12-17 14:46:36 +02003420 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003421 bool changed = false;
3422
3423 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3424 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3425 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3426 changed = true;
3427 }
3428 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3429 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3430 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3431 changed = true;
3432 }
3433 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3434 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3435 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3436 changed = true;
3437 }
3438
3439 /*
3440 * Don't touch WM1S_LP_EN here.
3441 * Doing so could cause underruns.
3442 */
3443
3444 return changed;
3445}
3446
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003447/*
3448 * The spec says we shouldn't write when we don't need, because every write
3449 * causes WMs to be re-evaluated, expending some power.
3450 */
Imre Deak820c1982013-12-17 14:46:36 +02003451static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3452 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003453{
Imre Deak820c1982013-12-17 14:46:36 +02003454 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003455 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003456 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003457
Damien Lespiau055e3932014-08-18 13:49:10 +01003458 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003459 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003460 return;
3461
Ville Syrjälä8553c182013-12-05 15:51:39 +02003462 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003463
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003464 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003465 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003466 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003467 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003468 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003469 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3470
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003471 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003472 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003473 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003474 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003475 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003476 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3477
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003478 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003479 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003480 val = I915_READ(WM_MISC);
3481 if (results->partitioning == INTEL_DDB_PART_1_2)
3482 val &= ~WM_MISC_DATA_PARTITION_5_6;
3483 else
3484 val |= WM_MISC_DATA_PARTITION_5_6;
3485 I915_WRITE(WM_MISC, val);
3486 } else {
3487 val = I915_READ(DISP_ARB_CTL2);
3488 if (results->partitioning == INTEL_DDB_PART_1_2)
3489 val &= ~DISP_DATA_PARTITION_5_6;
3490 else
3491 val |= DISP_DATA_PARTITION_5_6;
3492 I915_WRITE(DISP_ARB_CTL2, val);
3493 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003494 }
3495
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003496 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003497 val = I915_READ(DISP_ARB_CTL);
3498 if (results->enable_fbc_wm)
3499 val &= ~DISP_FBC_WM_DIS;
3500 else
3501 val |= DISP_FBC_WM_DIS;
3502 I915_WRITE(DISP_ARB_CTL, val);
3503 }
3504
Imre Deak954911e2013-12-17 14:46:34 +02003505 if (dirty & WM_DIRTY_LP(1) &&
3506 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3507 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3508
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003509 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003510 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3511 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3512 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3513 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3514 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003515
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003516 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003517 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003518 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003519 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003520 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003521 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003522
3523 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003524}
3525
Matt Ropered4a6a72016-02-23 17:20:13 -08003526bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003527{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003528 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003529
3530 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3531}
3532
Lyude656d1b82016-08-17 15:55:54 -04003533#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003534
Matt Roper024c9042015-09-24 15:53:11 -07003535/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003536 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3537 * so assume we'll always need it in order to avoid underruns.
3538 */
3539static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3540{
3541 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3542
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003543 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003544 return true;
3545
3546 return false;
3547}
3548
Paulo Zanoni56feca92016-09-22 18:00:28 -03003549static bool
3550intel_has_sagv(struct drm_i915_private *dev_priv)
3551{
Rodrigo Vivi82525c12017-06-08 08:50:00 -07003552 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003553 return true;
3554
3555 if (IS_SKYLAKE(dev_priv) &&
3556 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3557 return true;
3558
3559 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003560}
3561
Lyude656d1b82016-08-17 15:55:54 -04003562/*
3563 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3564 * depending on power and performance requirements. The display engine access
3565 * to system memory is blocked during the adjustment time. Because of the
3566 * blocking time, having this enabled can cause full system hangs and/or pipe
3567 * underruns if we don't meet all of the following requirements:
3568 *
3569 * - <= 1 pipe enabled
3570 * - All planes can enable watermarks for latencies >= SAGV engine block time
3571 * - We're not using an interlaced display configuration
3572 */
3573int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003574intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003575{
3576 int ret;
3577
Paulo Zanoni56feca92016-09-22 18:00:28 -03003578 if (!intel_has_sagv(dev_priv))
3579 return 0;
3580
3581 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003582 return 0;
3583
3584 DRM_DEBUG_KMS("Enabling the SAGV\n");
3585 mutex_lock(&dev_priv->rps.hw_lock);
3586
3587 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3588 GEN9_SAGV_ENABLE);
3589
3590 /* We don't need to wait for the SAGV when enabling */
3591 mutex_unlock(&dev_priv->rps.hw_lock);
3592
3593 /*
3594 * Some skl systems, pre-release machines in particular,
3595 * don't actually have an SAGV.
3596 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003597 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003598 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003599 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003600 return 0;
3601 } else if (ret < 0) {
3602 DRM_ERROR("Failed to enable the SAGV\n");
3603 return ret;
3604 }
3605
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003606 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003607 return 0;
3608}
3609
Lyude656d1b82016-08-17 15:55:54 -04003610int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003611intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003612{
Imre Deakb3b8e992016-12-05 18:27:38 +02003613 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003614
Paulo Zanoni56feca92016-09-22 18:00:28 -03003615 if (!intel_has_sagv(dev_priv))
3616 return 0;
3617
3618 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003619 return 0;
3620
3621 DRM_DEBUG_KMS("Disabling the SAGV\n");
3622 mutex_lock(&dev_priv->rps.hw_lock);
3623
3624 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003625 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3626 GEN9_SAGV_DISABLE,
3627 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3628 1);
Lyude656d1b82016-08-17 15:55:54 -04003629 mutex_unlock(&dev_priv->rps.hw_lock);
3630
Lyude656d1b82016-08-17 15:55:54 -04003631 /*
3632 * Some skl systems, pre-release machines in particular,
3633 * don't actually have an SAGV.
3634 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003635 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003636 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003637 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003638 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003639 } else if (ret < 0) {
3640 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3641 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003642 }
3643
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003644 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003645 return 0;
3646}
3647
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003648bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003649{
3650 struct drm_device *dev = state->dev;
3651 struct drm_i915_private *dev_priv = to_i915(dev);
3652 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003653 struct intel_crtc *crtc;
3654 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003655 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003656 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003657 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003658
Paulo Zanoni56feca92016-09-22 18:00:28 -03003659 if (!intel_has_sagv(dev_priv))
3660 return false;
3661
Lyude656d1b82016-08-17 15:55:54 -04003662 /*
3663 * SKL workaround: bspec recommends we disable the SAGV when we have
3664 * more then one pipe enabled
3665 *
3666 * If there are no active CRTCs, no additional checks need be performed
3667 */
3668 if (hweight32(intel_state->active_crtcs) == 0)
3669 return true;
3670 else if (hweight32(intel_state->active_crtcs) > 1)
3671 return false;
3672
3673 /* Since we're now guaranteed to only have one active CRTC... */
3674 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003675 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003676 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003677
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003678 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003679 return false;
3680
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003681 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003682 struct skl_plane_wm *wm =
3683 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003684
Lyude656d1b82016-08-17 15:55:54 -04003685 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003686 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003687 continue;
3688
3689 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003690 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003691 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003692 { }
3693
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003694 latency = dev_priv->wm.skl_latency[level];
3695
3696 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003697 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003698 I915_FORMAT_MOD_X_TILED)
3699 latency += 15;
3700
Lyude656d1b82016-08-17 15:55:54 -04003701 /*
3702 * If any of the planes on this pipe don't enable wm levels
3703 * that incur memory latencies higher then 30µs we can't enable
3704 * the SAGV
3705 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003706 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003707 return false;
3708 }
3709
3710 return true;
3711}
3712
Damien Lespiaub9cec072014-11-04 17:06:43 +00003713static void
3714skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003715 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003716 struct skl_ddb_entry *alloc, /* out */
3717 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003718{
Matt Roperc107acf2016-05-12 07:06:01 -07003719 struct drm_atomic_state *state = cstate->base.state;
3720 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3721 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003722 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003723 unsigned int pipe_size, ddb_size;
3724 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003725
Matt Ropera6d3460e2016-05-12 07:06:04 -07003726 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003727 alloc->start = 0;
3728 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003729 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003730 return;
3731 }
3732
Matt Ropera6d3460e2016-05-12 07:06:04 -07003733 if (intel_state->active_pipe_changes)
3734 *num_active = hweight32(intel_state->active_crtcs);
3735 else
3736 *num_active = hweight32(dev_priv->active_crtcs);
3737
Deepak M6f3fff62016-09-15 15:01:10 +05303738 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3739 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003740
3741 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3742
Matt Roperc107acf2016-05-12 07:06:01 -07003743 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003744 * If the state doesn't change the active CRTC's, then there's
3745 * no need to recalculate; the existing pipe allocation limits
3746 * should remain unchanged. Note that we're safe from racing
3747 * commits since any racing commit that changes the active CRTC
3748 * list would need to grab _all_ crtc locks, including the one
3749 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003750 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003751 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003752 /*
3753 * alloc may be cleared by clear_intel_crtc_state,
3754 * copy from old state to be sure
3755 */
3756 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003757 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003758 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003759
3760 nth_active_pipe = hweight32(intel_state->active_crtcs &
3761 (drm_crtc_mask(for_crtc) - 1));
3762 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3763 alloc->start = nth_active_pipe * ddb_size / *num_active;
3764 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003765}
3766
Matt Roperc107acf2016-05-12 07:06:01 -07003767static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003768{
Matt Roperc107acf2016-05-12 07:06:01 -07003769 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003770 return 32;
3771
3772 return 8;
3773}
3774
Damien Lespiaua269c582014-11-04 17:06:49 +00003775static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3776{
3777 entry->start = reg & 0x3ff;
3778 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003779 if (entry->end)
3780 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003781}
3782
Damien Lespiau08db6652014-11-04 17:06:52 +00003783void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3784 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003785{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003786 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003787
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003788 memset(ddb, 0, sizeof(*ddb));
3789
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003790 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003791 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003792 enum plane_id plane_id;
3793 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003794
3795 power_domain = POWER_DOMAIN_PIPE(pipe);
3796 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003797 continue;
3798
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003799 for_each_plane_id_on_crtc(crtc, plane_id) {
3800 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003801
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003802 if (plane_id != PLANE_CURSOR)
3803 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3804 else
3805 val = I915_READ(CUR_BUF_CFG(pipe));
3806
3807 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3808 }
Imre Deak4d800032016-02-17 16:31:29 +02003809
3810 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003811 }
3812}
3813
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003814/*
3815 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3816 * The bspec defines downscale amount as:
3817 *
3818 * """
3819 * Horizontal down scale amount = maximum[1, Horizontal source size /
3820 * Horizontal destination size]
3821 * Vertical down scale amount = maximum[1, Vertical source size /
3822 * Vertical destination size]
3823 * Total down scale amount = Horizontal down scale amount *
3824 * Vertical down scale amount
3825 * """
3826 *
3827 * Return value is provided in 16.16 fixed point form to retain fractional part.
3828 * Caller should take care of dividing & rounding off the value.
3829 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303830static uint_fixed_16_16_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003831skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3832 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003833{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003834 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003835 uint32_t src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303836 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3837 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003838
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003839 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303840 return u32_to_fixed_16_16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003841
3842 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003843 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003844 /*
3845 * Cursors only support 0/180 degree rotation,
3846 * hence no need to account for rotation here.
3847 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303848 src_w = pstate->base.src_w >> 16;
3849 src_h = pstate->base.src_h >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003850 dst_w = pstate->base.crtc_w;
3851 dst_h = pstate->base.crtc_h;
3852 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003853 /*
3854 * Src coordinates are already rotated by 270 degrees for
3855 * the 90/270 degree plane rotation cases (to match the
3856 * GTT mapping), hence no need to account for rotation here.
3857 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303858 src_w = drm_rect_width(&pstate->base.src) >> 16;
3859 src_h = drm_rect_height(&pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003860 dst_w = drm_rect_width(&pstate->base.dst);
3861 dst_h = drm_rect_height(&pstate->base.dst);
3862 }
3863
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303864 fp_w_ratio = fixed_16_16_div(src_w, dst_w);
3865 fp_h_ratio = fixed_16_16_div(src_h, dst_h);
3866 downscale_w = max_fixed_16_16(fp_w_ratio, u32_to_fixed_16_16(1));
3867 downscale_h = max_fixed_16_16(fp_h_ratio, u32_to_fixed_16_16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003868
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303869 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003870}
3871
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303872static uint_fixed_16_16_t
3873skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
3874{
3875 uint_fixed_16_16_t pipe_downscale = u32_to_fixed_16_16(1);
3876
3877 if (!crtc_state->base.enable)
3878 return pipe_downscale;
3879
3880 if (crtc_state->pch_pfit.enabled) {
3881 uint32_t src_w, src_h, dst_w, dst_h;
3882 uint32_t pfit_size = crtc_state->pch_pfit.size;
3883 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3884 uint_fixed_16_16_t downscale_h, downscale_w;
3885
3886 src_w = crtc_state->pipe_src_w;
3887 src_h = crtc_state->pipe_src_h;
3888 dst_w = pfit_size >> 16;
3889 dst_h = pfit_size & 0xffff;
3890
3891 if (!dst_w || !dst_h)
3892 return pipe_downscale;
3893
3894 fp_w_ratio = fixed_16_16_div(src_w, dst_w);
3895 fp_h_ratio = fixed_16_16_div(src_h, dst_h);
3896 downscale_w = max_fixed_16_16(fp_w_ratio, u32_to_fixed_16_16(1));
3897 downscale_h = max_fixed_16_16(fp_h_ratio, u32_to_fixed_16_16(1));
3898
3899 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
3900 }
3901
3902 return pipe_downscale;
3903}
3904
3905int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
3906 struct intel_crtc_state *cstate)
3907{
3908 struct drm_crtc_state *crtc_state = &cstate->base;
3909 struct drm_atomic_state *state = crtc_state->state;
3910 struct drm_plane *plane;
3911 const struct drm_plane_state *pstate;
3912 struct intel_plane_state *intel_pstate;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003913 int crtc_clock, dotclk;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303914 uint32_t pipe_max_pixel_rate;
3915 uint_fixed_16_16_t pipe_downscale;
3916 uint_fixed_16_16_t max_downscale = u32_to_fixed_16_16(1);
3917
3918 if (!cstate->base.enable)
3919 return 0;
3920
3921 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
3922 uint_fixed_16_16_t plane_downscale;
3923 uint_fixed_16_16_t fp_9_div_8 = fixed_16_16_div(9, 8);
3924 int bpp;
3925
3926 if (!intel_wm_plane_visible(cstate,
3927 to_intel_plane_state(pstate)))
3928 continue;
3929
3930 if (WARN_ON(!pstate->fb))
3931 return -EINVAL;
3932
3933 intel_pstate = to_intel_plane_state(pstate);
3934 plane_downscale = skl_plane_downscale_amount(cstate,
3935 intel_pstate);
3936 bpp = pstate->fb->format->cpp[0] * 8;
3937 if (bpp == 64)
3938 plane_downscale = mul_fixed16(plane_downscale,
3939 fp_9_div_8);
3940
3941 max_downscale = max_fixed_16_16(plane_downscale, max_downscale);
3942 }
3943 pipe_downscale = skl_pipe_downscale_amount(cstate);
3944
3945 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
3946
3947 crtc_clock = crtc_state->adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003948 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
3949
3950 if (IS_GEMINILAKE(to_i915(intel_crtc->base.dev)))
3951 dotclk *= 2;
3952
3953 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303954
3955 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003956 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303957 return -EINVAL;
3958 }
3959
3960 return 0;
3961}
3962
Damien Lespiaub9cec072014-11-04 17:06:43 +00003963static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003964skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3965 const struct drm_plane_state *pstate,
3966 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003967{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003968 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003969 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303970 uint32_t data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003971 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003972 struct drm_framebuffer *fb;
3973 u32 format;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303974 uint_fixed_16_16_t down_scale_amount;
Matt Ropera1de91e2016-05-12 07:05:57 -07003975
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003976 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003977 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003978
3979 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003980 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003981
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003982 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07003983 return 0;
3984 if (y && format != DRM_FORMAT_NV12)
3985 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003986
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003987 /*
3988 * Src coordinates are already rotated by 270 degrees for
3989 * the 90/270 degree plane rotation cases (to match the
3990 * GTT mapping), hence no need to account for rotation here.
3991 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003992 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3993 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003994
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003995 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003996 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003997 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003998 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003999 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004000 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004001 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004002 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004003 } else {
4004 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02004005 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004006 }
4007
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004008 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004009
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304010 return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004011}
4012
4013/*
4014 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
4015 * a 8192x4096@32bpp framebuffer:
4016 * 3 * 4096 * 8192 * 4 < 2^32
4017 */
4018static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004019skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
4020 unsigned *plane_data_rate,
4021 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004022{
Matt Roper9c74d822016-05-12 07:05:58 -07004023 struct drm_crtc_state *cstate = &intel_cstate->base;
4024 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004025 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004026 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004027 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004028
4029 if (WARN_ON(!state))
4030 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004031
Matt Ropera1de91e2016-05-12 07:05:57 -07004032 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004033 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004034 enum plane_id plane_id = to_intel_plane(plane)->id;
4035 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07004036
Matt Ropera6d3460e2016-05-12 07:06:04 -07004037 /* packed/uv */
4038 rate = skl_plane_relative_data_rate(intel_cstate,
4039 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004040 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004041
4042 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004043
Matt Ropera6d3460e2016-05-12 07:06:04 -07004044 /* y-plane */
4045 rate = skl_plane_relative_data_rate(intel_cstate,
4046 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004047 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004048
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004049 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004050 }
4051
4052 return total_data_rate;
4053}
4054
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004055static uint16_t
4056skl_ddb_min_alloc(const struct drm_plane_state *pstate,
4057 const int y)
4058{
4059 struct drm_framebuffer *fb = pstate->fb;
4060 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
4061 uint32_t src_w, src_h;
4062 uint32_t min_scanlines = 8;
4063 uint8_t plane_bpp;
4064
4065 if (WARN_ON(!fb))
4066 return 0;
4067
4068 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004069 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004070 return 0;
4071
4072 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02004073 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
4074 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004075 return 8;
4076
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004077 /*
4078 * Src coordinates are already rotated by 270 degrees for
4079 * the 90/270 degree plane rotation cases (to match the
4080 * GTT mapping), hence no need to account for rotation here.
4081 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004082 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
4083 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004084
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004085 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004086 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004087 src_w /= 2;
4088 src_h /= 2;
4089 }
4090
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004091 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02004092 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004093 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02004094 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004095
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004096 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004097 switch (plane_bpp) {
4098 case 1:
4099 min_scanlines = 32;
4100 break;
4101 case 2:
4102 min_scanlines = 16;
4103 break;
4104 case 4:
4105 min_scanlines = 8;
4106 break;
4107 case 8:
4108 min_scanlines = 4;
4109 break;
4110 default:
4111 WARN(1, "Unsupported pixel depth %u for rotation",
4112 plane_bpp);
4113 min_scanlines = 32;
4114 }
4115 }
4116
4117 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4118}
4119
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004120static void
4121skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4122 uint16_t *minimum, uint16_t *y_minimum)
4123{
4124 const struct drm_plane_state *pstate;
4125 struct drm_plane *plane;
4126
4127 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004128 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004129
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004130 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004131 continue;
4132
4133 if (!pstate->visible)
4134 continue;
4135
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004136 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4137 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004138 }
4139
4140 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4141}
4142
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304143static void
4144skl_enable_plane_wm_levels(const struct drm_i915_private *dev_priv,
4145 uint16_t plane_ddb,
4146 uint16_t max_level,
4147 struct skl_plane_wm *wm)
4148{
4149 int level;
4150 /*
4151 * Now enable all levels in WM structure which can be enabled
4152 * using current DDB allocation
4153 */
4154 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
4155 struct skl_wm_level *level_wm = &wm->wm[level];
4156
4157 if (level > max_level || level_wm->plane_res_b == 0
4158 || level_wm->plane_res_l >= 31
4159 || level_wm->plane_res_b >= plane_ddb) {
4160 level_wm->plane_en = false;
4161 level_wm->plane_res_b = 0;
4162 level_wm->plane_res_l = 0;
4163 } else {
4164 level_wm->plane_en = true;
4165 }
4166 }
4167}
4168
Matt Roperc107acf2016-05-12 07:06:01 -07004169static int
Matt Roper024c9042015-09-24 15:53:11 -07004170skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304171 struct skl_pipe_wm *pipe_wm,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004172 struct skl_ddb_allocation *ddb /* out */)
4173{
Matt Roperc107acf2016-05-12 07:06:01 -07004174 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004175 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004176 struct drm_device *dev = crtc->dev;
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304177 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4179 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004180 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004181 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004182 uint16_t minimum[I915_MAX_PLANES] = {};
4183 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004184 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004185 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004186 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004187 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4188 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304189 uint16_t total_min_blocks = 0;
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304190 uint16_t total_level_ddb;
4191 uint16_t plane_blocks = 0;
4192 int max_level, level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004193
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004194 /* Clear the partitioning for disabled planes. */
4195 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4196 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4197
Matt Ropera6d3460e2016-05-12 07:06:04 -07004198 if (WARN_ON(!state))
4199 return 0;
4200
Matt Roperc107acf2016-05-12 07:06:01 -07004201 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004202 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004203 return 0;
4204 }
4205
Matt Ropera6d3460e2016-05-12 07:06:04 -07004206 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004207 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304208 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004209 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004210
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004211 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004212
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004213 /*
4214 * 1. Allocate the mininum required blocks for each active plane
4215 * and allocate the cursor, it doesn't require extra allocation
4216 * proportional to the data rate.
4217 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004218
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004219 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304220 total_min_blocks += minimum[plane_id];
4221 total_min_blocks += y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004222 }
4223
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304224 if (total_min_blocks > alloc_size) {
4225 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4226 DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
4227 alloc_size);
4228 return -EINVAL;
4229 }
4230
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304231 alloc_size -= minimum[PLANE_CURSOR];
4232 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end -
4233 minimum[PLANE_CURSOR];
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004234 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4235
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304236 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
4237 total_level_ddb = 0;
4238 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4239 /*
4240 * TODO: We should calculate watermark values for Y/UV
4241 * plane both in case of NV12 format and use both values
4242 * for ddb calculation. NV12 is disabled as of now, So
4243 * using only single/UV plane value here.
4244 */
4245 struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4246 uint16_t plane_res_b = wm->wm[level].plane_res_b;
4247 uint16_t min = minimum[plane_id] + y_minimum[plane_id];
4248
4249 if (plane_id == PLANE_CURSOR)
4250 continue;
4251
4252 total_level_ddb += max(plane_res_b, min);
4253 }
4254
4255 /*
4256 * If This level can successfully be enabled with the
4257 * pipe's current DDB allocation, then all lower levels are
4258 * guaranteed to succeed as well.
4259 */
4260 if (total_level_ddb <= alloc_size)
4261 break;
4262 }
4263
4264 if ((level < 0) || (total_min_blocks > alloc_size)) {
4265 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4266 DRM_DEBUG_KMS("minimum required %d/%d\n", (level < 0) ?
4267 total_level_ddb : total_min_blocks, alloc_size);
4268 return -EINVAL;
4269 }
4270 max_level = level;
4271 alloc_size -= total_level_ddb;
4272
Damien Lespiaub9cec072014-11-04 17:06:43 +00004273 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004274 * 2. Distribute the remaining space in proportion to the amount of
4275 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004276 *
4277 * FIXME: we may not allocate every single block here.
4278 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004279 total_data_rate = skl_get_total_relative_data_rate(cstate,
4280 plane_data_rate,
4281 plane_y_data_rate);
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304282 /*
4283 * PLANE_CURSOR data rate is not included in total_data_rate.
4284 * If only cursor plane is enabled we have to enable its WM levels
4285 * explicitly before returning. Cursor has fixed ddb allocation,
4286 * So it's ok to always check cursor WM enabling before return.
4287 */
4288 plane_blocks = skl_ddb_entry_size(&ddb->plane[pipe][PLANE_CURSOR]);
4289 skl_enable_plane_wm_levels(dev_priv, plane_blocks, max_level,
4290 &pipe_wm->planes[PLANE_CURSOR]);
Matt Ropera1de91e2016-05-12 07:05:57 -07004291 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004292 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004293
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004294 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004295 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004296 unsigned int data_rate, y_data_rate;
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304297 uint16_t plane_blocks = 0, y_plane_blocks = 0;
4298 struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4299 uint16_t plane_res_b = wm->wm[max_level].plane_res_b;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004300
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004301 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004302 continue;
4303
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004304 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004305
4306 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004307 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004308 * promote the expression to 64 bits to avoid overflowing, the
4309 * result is < available as data_rate / total_data_rate < 1
4310 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004311
Matt Roperc107acf2016-05-12 07:06:01 -07004312 /* Leave disabled planes at (0,0) */
4313 if (data_rate) {
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304314 plane_blocks = max(minimum[plane_id], plane_res_b);
4315 plane_blocks += div_u64((uint64_t)alloc_size *
4316 data_rate, total_data_rate);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004317 ddb->plane[pipe][plane_id].start = start;
4318 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304319 start += plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004320 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004321
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004322 /*
4323 * allocation for y_plane part of planar format:
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304324 * TODO: Once we start calculating watermark values for Y/UV
4325 * plane both consider it for initial allowed wm blocks.
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004326 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004327 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004328
Matt Roperc107acf2016-05-12 07:06:01 -07004329 if (y_data_rate) {
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304330 y_plane_blocks = y_minimum[plane_id];
4331 y_plane_blocks += div_u64((uint64_t)alloc_size *
4332 y_data_rate, total_data_rate);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004333 ddb->y_plane[pipe][plane_id].start = start;
4334 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304335 start += y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004336 }
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304337 skl_enable_plane_wm_levels(dev_priv,
4338 plane_blocks,
4339 max_level,
4340 wm);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004341 }
4342
Matt Roperc107acf2016-05-12 07:06:01 -07004343 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004344}
4345
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004346/*
4347 * 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 +02004348 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004349 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4350 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4351*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304352static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
4353 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004354{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304355 uint32_t wm_intermediate_val;
4356 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004357
4358 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304359 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004360
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304361 wm_intermediate_val = latency * pixel_rate * cpp;
Kumar, Maheshafbc95c2017-05-17 17:28:20 +05304362 ret = fixed_16_16_div_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004363 return ret;
4364}
4365
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304366static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4367 uint32_t pipe_htotal,
4368 uint32_t latency,
4369 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004370{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004371 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304372 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004373
4374 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304375 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004376
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004377 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304378 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4379 pipe_htotal * 1000);
4380 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004381 return ret;
4382}
4383
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304384static uint_fixed_16_16_t
4385intel_get_linetime_us(struct intel_crtc_state *cstate)
4386{
4387 uint32_t pixel_rate;
4388 uint32_t crtc_htotal;
4389 uint_fixed_16_16_t linetime_us;
4390
4391 if (!cstate->base.active)
4392 return u32_to_fixed_16_16(0);
4393
4394 pixel_rate = cstate->pixel_rate;
4395
4396 if (WARN_ON(pixel_rate == 0))
4397 return u32_to_fixed_16_16(0);
4398
4399 crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
4400 linetime_us = fixed_16_16_div_u64(crtc_htotal * 1000, pixel_rate);
4401
4402 return linetime_us;
4403}
4404
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304405static uint32_t
4406skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4407 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004408{
4409 uint64_t adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304410 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004411
4412 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004413 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004414 return 0;
4415
4416 /*
4417 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4418 * with additional adjustments for plane-specific scaling.
4419 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004420 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004421 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004422
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304423 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4424 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004425}
4426
Matt Roper55994c22016-05-12 07:06:08 -07004427static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4428 struct intel_crtc_state *cstate,
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304429 const struct intel_plane_state *intel_pstate,
Matt Roper55994c22016-05-12 07:06:08 -07004430 int level,
4431 uint16_t *out_blocks, /* out */
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304432 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004433{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004434 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304435 const struct drm_plane_state *pstate = &intel_pstate->base;
4436 const struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004437 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304438 uint_fixed_16_16_t method1, method2;
4439 uint_fixed_16_16_t plane_blocks_per_line;
4440 uint_fixed_16_16_t selected_result;
4441 uint32_t interm_pbpl;
4442 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004443 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02004444 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004445 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004446 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304447 uint_fixed_16_16_t y_tile_minimum;
4448 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004449 struct intel_atomic_state *state =
4450 to_intel_atomic_state(cstate->base.state);
4451 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304452 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004453
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004454 if (latency == 0 ||
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304455 !intel_wm_plane_visible(cstate, intel_pstate))
Matt Roper55994c22016-05-12 07:06:08 -07004456 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004457
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304458 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4459 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
4460 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4461
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004462 /* Display WA #1141: kbl,cfl */
4463 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) &&
4464 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304465 latency += 4;
4466
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304467 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004468 latency += 15;
4469
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004470 if (plane->id == PLANE_CURSOR) {
4471 width = intel_pstate->base.crtc_w;
4472 height = intel_pstate->base.crtc_h;
4473 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004474 /*
4475 * Src coordinates are already rotated by 270 degrees for
4476 * the 90/270 degree plane rotation cases (to match the
4477 * GTT mapping), hence no need to account for rotation here.
4478 */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004479 width = drm_rect_width(&intel_pstate->base.src) >> 16;
4480 height = drm_rect_height(&intel_pstate->base.src) >> 16;
4481 }
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004482
Ville Syrjälä353c8592016-12-14 23:30:57 +02004483 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004484 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
4485
Dave Airlie61d0a042016-10-25 16:35:20 +10004486 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004487 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02004488 fb->format->cpp[1] :
4489 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004490
4491 switch (cpp) {
4492 case 1:
4493 y_min_scanlines = 16;
4494 break;
4495 case 2:
4496 y_min_scanlines = 8;
4497 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004498 case 4:
4499 y_min_scanlines = 4;
4500 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03004501 default:
4502 MISSING_CASE(cpp);
4503 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004504 }
4505 } else {
4506 y_min_scanlines = 4;
4507 }
4508
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02004509 if (apply_memory_bw_wa)
4510 y_min_scanlines *= 2;
4511
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004512 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304513 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304514 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
4515 y_min_scanlines, 512);
Kumar, Maheshafbc95c2017-05-17 17:28:20 +05304516 plane_blocks_per_line = fixed_16_16_div(interm_pbpl,
4517 y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304518 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304519 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
4520 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304521 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304522 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
4523 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004524 }
4525
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004526 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
4527 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004528 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004529 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004530 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004531
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304532 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
4533 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004534
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304535 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304536 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004537 } else {
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304538 uint32_t linetime_us;
4539
4540 linetime_us = fixed_16_16_to_u32_round_up(
4541 intel_get_linetime_us(cstate));
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004542 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
4543 (plane_bytes_per_line / 512 < 1))
4544 selected_result = method2;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304545 else if (latency >= linetime_us)
4546 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004547 else
4548 selected_result = method1;
4549 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004550
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304551 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304552 res_lines = div_round_up_fixed16(selected_result,
4553 plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004554
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004555 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304556 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304557 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004558 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004559 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004560 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004561 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004562 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004563
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304564 if (res_lines >= 31 && level == 0) {
4565 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07004566
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304567 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4568 DRM_DEBUG_KMS("[PLANE:%d:%s] lines required = %u/31\n",
4569 plane->base.id, plane->name, res_lines);
4570 return -EINVAL;
Matt Roper55994c22016-05-12 07:06:08 -07004571 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004572
4573 *out_blocks = res_blocks;
4574 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004575
Matt Roper55994c22016-05-12 07:06:08 -07004576 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004577}
4578
Matt Roperf4a96752016-05-12 07:06:06 -07004579static int
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304580skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304581 struct intel_crtc_state *cstate,
4582 const struct intel_plane_state *intel_pstate,
4583 struct skl_plane_wm *wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004584{
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304585 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004586 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004587
Kumar, Mahesh7b751192017-05-17 17:28:24 +05304588 if (WARN_ON(!intel_pstate->base.fb))
4589 return -EINVAL;
Matt Roper024c9042015-09-24 15:53:11 -07004590
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304591 for (level = 0; level <= max_level; level++) {
4592 struct skl_wm_level *result = &wm->wm[level];
4593
4594 ret = skl_compute_plane_wm(dev_priv,
4595 cstate,
4596 intel_pstate,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304597 level,
4598 &result->plane_res_b,
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304599 &result->plane_res_l);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304600 if (ret)
4601 return ret;
4602 }
Matt Roperf4a96752016-05-12 07:06:06 -07004603
4604 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004605}
4606
Damien Lespiau407b50f2014-11-04 17:06:57 +00004607static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004608skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004609{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304610 struct drm_atomic_state *state = cstate->base.state;
4611 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304612 uint_fixed_16_16_t linetime_us;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304613 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004614
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304615 linetime_us = intel_get_linetime_us(cstate);
4616
4617 if (is_fixed16_zero(linetime_us))
Damien Lespiau407b50f2014-11-04 17:06:57 +00004618 return 0;
4619
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304620 linetime_wm = fixed_16_16_to_u32_round_up(mul_u32_fixed_16_16(8,
4621 linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304622
4623 /* Display WA #1135: bxt. */
4624 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
4625 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
4626
4627 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004628}
4629
Matt Roper024c9042015-09-24 15:53:11 -07004630static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00004631 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004632{
Matt Roper024c9042015-09-24 15:53:11 -07004633 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004634 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00004635
4636 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04004637 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004638}
4639
Matt Roper55994c22016-05-12 07:06:08 -07004640static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4641 struct skl_ddb_allocation *ddb,
4642 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004643{
Matt Roper024c9042015-09-24 15:53:11 -07004644 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304645 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004646 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304647 struct drm_plane *plane;
4648 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004649 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004650 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004651
Lyudea62163e2016-10-04 14:28:20 -04004652 /*
4653 * We'll only calculate watermarks for planes that are actually
4654 * enabled, so make sure all other planes are set as disabled.
4655 */
4656 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4657
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304658 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4659 const struct intel_plane_state *intel_pstate =
4660 to_intel_plane_state(pstate);
4661 enum plane_id plane_id = to_intel_plane(plane)->id;
4662
4663 wm = &pipe_wm->planes[plane_id];
Lyudea62163e2016-10-04 14:28:20 -04004664
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304665 ret = skl_compute_wm_levels(dev_priv, cstate, intel_pstate, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304666 if (ret)
4667 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004668 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004669 }
Matt Roper024c9042015-09-24 15:53:11 -07004670 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004671
Matt Roper55994c22016-05-12 07:06:08 -07004672 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004673}
4674
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004675static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4676 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004677 const struct skl_ddb_entry *entry)
4678{
4679 if (entry->end)
4680 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4681 else
4682 I915_WRITE(reg, 0);
4683}
4684
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004685static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4686 i915_reg_t reg,
4687 const struct skl_wm_level *level)
4688{
4689 uint32_t val = 0;
4690
4691 if (level->plane_en) {
4692 val |= PLANE_WM_EN;
4693 val |= level->plane_res_b;
4694 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4695 }
4696
4697 I915_WRITE(reg, val);
4698}
4699
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004700static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4701 const struct skl_plane_wm *wm,
4702 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004703 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004704{
4705 struct drm_crtc *crtc = &intel_crtc->base;
4706 struct drm_device *dev = crtc->dev;
4707 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004708 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004709 enum pipe pipe = intel_crtc->pipe;
4710
4711 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004712 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004713 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004714 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004715 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004716 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004717
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004718 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4719 &ddb->plane[pipe][plane_id]);
4720 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4721 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004722}
4723
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004724static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4725 const struct skl_plane_wm *wm,
4726 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004727{
4728 struct drm_crtc *crtc = &intel_crtc->base;
4729 struct drm_device *dev = crtc->dev;
4730 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004731 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004732 enum pipe pipe = intel_crtc->pipe;
4733
4734 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004735 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4736 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004737 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004738 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004739
4740 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004741 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004742}
4743
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004744bool skl_wm_level_equals(const struct skl_wm_level *l1,
4745 const struct skl_wm_level *l2)
4746{
4747 if (l1->plane_en != l2->plane_en)
4748 return false;
4749
4750 /* If both planes aren't enabled, the rest shouldn't matter */
4751 if (!l1->plane_en)
4752 return true;
4753
4754 return (l1->plane_res_l == l2->plane_res_l &&
4755 l1->plane_res_b == l2->plane_res_b);
4756}
4757
Lyude27082492016-08-24 07:48:10 +02004758static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4759 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004760{
Lyude27082492016-08-24 07:48:10 +02004761 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004762}
4763
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004764bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4765 const struct skl_ddb_entry *ddb,
4766 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004767{
Lyudece0ba282016-09-15 10:46:35 -04004768 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004769
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004770 for (i = 0; i < I915_MAX_PIPES; i++)
4771 if (i != ignore && entries[i] &&
4772 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004773 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004774
Lyude27082492016-08-24 07:48:10 +02004775 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004776}
4777
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304778static int
4779skl_ddb_add_affected_planes(struct intel_crtc_state *cstate,
4780 const struct skl_pipe_wm *old_pipe_wm,
4781 const struct skl_pipe_wm *pipe_wm)
4782{
4783 struct drm_atomic_state *state = cstate->base.state;
4784 struct drm_device *dev = state->dev;
4785 struct drm_crtc *crtc = cstate->base.crtc;
4786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4787 struct drm_i915_private *dev_priv = to_i915(dev);
4788 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4789 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4790 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4791 struct drm_plane_state *plane_state;
4792 struct drm_plane *plane;
4793 enum pipe pipe = intel_crtc->pipe;
4794
4795 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4796
4797 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4798 enum plane_id plane_id = to_intel_plane(plane)->id;
4799 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4800 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane_id];
4801
4802 if ((skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4803 &new_ddb->plane[pipe][plane_id]) &&
4804 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4805 &new_ddb->y_plane[pipe][plane_id])) &&
4806 !memcmp(wm, old_wm, sizeof(struct skl_plane_wm)))
4807 continue;
4808
4809 plane_state = drm_atomic_get_plane_state(state, plane);
4810 if (IS_ERR(plane_state))
4811 return PTR_ERR(plane_state);
4812 }
4813
4814 return 0;
4815}
4816
Matt Roper55994c22016-05-12 07:06:08 -07004817static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004818 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004819 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004820 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004821 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004822{
Matt Roperf4a96752016-05-12 07:06:06 -07004823 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004824 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004825
Matt Roper55994c22016-05-12 07:06:08 -07004826 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4827 if (ret)
4828 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004829
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304830 ret = skl_allocate_pipe_ddb(intel_cstate, pipe_wm, ddb);
4831 if (ret)
4832 return ret;
4833 /*
4834 * TODO: Planes are included in state to arm WM registers.
4835 * Scope to optimize further, by just rewriting plane surf register.
4836 */
4837 ret = skl_ddb_add_affected_planes(intel_cstate, old_pipe_wm, pipe_wm);
4838 if (ret)
4839 return ret;
4840
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004841 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004842 *changed = false;
4843 else
4844 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004845
Matt Roper55994c22016-05-12 07:06:08 -07004846 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004847}
4848
Matt Roper9b613022016-06-27 16:42:44 -07004849static uint32_t
4850pipes_modified(struct drm_atomic_state *state)
4851{
4852 struct drm_crtc *crtc;
4853 struct drm_crtc_state *cstate;
4854 uint32_t i, ret = 0;
4855
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004856 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004857 ret |= drm_crtc_mask(crtc);
4858
4859 return ret;
4860}
4861
Jani Nikulabb7791b2016-10-04 12:29:17 +03004862static int
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05304863skl_include_affected_crtcs(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004864{
4865 struct drm_device *dev = state->dev;
4866 struct drm_i915_private *dev_priv = to_i915(dev);
4867 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4868 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004869 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004870 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004871 int ret;
4872
4873 /*
4874 * If this is our first atomic update following hardware readout,
4875 * we can't trust the DDB that the BIOS programmed for us. Let's
4876 * pretend that all pipes switched active status so that we'll
4877 * ensure a full DDB recompute.
4878 */
Matt Roper1b54a882016-06-17 13:42:18 -07004879 if (dev_priv->wm.distrust_bios_wm) {
4880 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4881 state->acquire_ctx);
4882 if (ret)
4883 return ret;
4884
Matt Roper98d39492016-05-12 07:06:03 -07004885 intel_state->active_pipe_changes = ~0;
4886
Matt Roper1b54a882016-06-17 13:42:18 -07004887 /*
4888 * We usually only initialize intel_state->active_crtcs if we
4889 * we're doing a modeset; make sure this field is always
4890 * initialized during the sanitization process that happens
4891 * on the first commit too.
4892 */
4893 if (!intel_state->modeset)
4894 intel_state->active_crtcs = dev_priv->active_crtcs;
4895 }
4896
Matt Roper98d39492016-05-12 07:06:03 -07004897 /*
4898 * If the modeset changes which CRTC's are active, we need to
4899 * recompute the DDB allocation for *all* active pipes, even
4900 * those that weren't otherwise being modified in any way by this
4901 * atomic commit. Due to the shrinking of the per-pipe allocations
4902 * when new active CRTC's are added, it's possible for a pipe that
4903 * we were already using and aren't changing at all here to suddenly
4904 * become invalid if its DDB needs exceeds its new allocation.
4905 *
4906 * Note that if we wind up doing a full DDB recompute, we can't let
4907 * any other display updates race with this transaction, so we need
4908 * to grab the lock on *all* CRTC's.
4909 */
Matt Roper734fa012016-05-12 15:11:40 -07004910 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004911 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004912 intel_state->wm_results.dirty_pipes = ~0;
4913 }
Matt Roper98d39492016-05-12 07:06:03 -07004914
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004915 /*
4916 * We're not recomputing for the pipes not included in the commit, so
4917 * make sure we start with the current state.
4918 */
4919 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4920
Matt Roper98d39492016-05-12 07:06:03 -07004921 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4922 struct intel_crtc_state *cstate;
4923
4924 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4925 if (IS_ERR(cstate))
4926 return PTR_ERR(cstate);
Matt Roper98d39492016-05-12 07:06:03 -07004927 }
4928
4929 return 0;
4930}
4931
Matt Roper2722efb2016-08-17 15:55:55 -04004932static void
4933skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4934 struct skl_wm_values *src,
4935 enum pipe pipe)
4936{
Matt Roper2722efb2016-08-17 15:55:55 -04004937 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4938 sizeof(dst->ddb.y_plane[pipe]));
4939 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4940 sizeof(dst->ddb.plane[pipe]));
4941}
4942
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004943static void
4944skl_print_wm_changes(const struct drm_atomic_state *state)
4945{
4946 const struct drm_device *dev = state->dev;
4947 const struct drm_i915_private *dev_priv = to_i915(dev);
4948 const struct intel_atomic_state *intel_state =
4949 to_intel_atomic_state(state);
4950 const struct drm_crtc *crtc;
4951 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004952 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004953 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4954 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004955 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004956
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004957 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004958 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4959 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004960
Maarten Lankhorst75704982016-11-01 12:04:10 +01004961 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004962 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004963 const struct skl_ddb_entry *old, *new;
4964
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004965 old = &old_ddb->plane[pipe][plane_id];
4966 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004967
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004968 if (skl_ddb_entry_equal(old, new))
4969 continue;
4970
Maarten Lankhorst75704982016-11-01 12:04:10 +01004971 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4972 intel_plane->base.base.id,
4973 intel_plane->base.name,
4974 old->start, old->end,
4975 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004976 }
4977 }
4978}
4979
Matt Roper98d39492016-05-12 07:06:03 -07004980static int
4981skl_compute_wm(struct drm_atomic_state *state)
4982{
4983 struct drm_crtc *crtc;
4984 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004985 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4986 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004987 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07004988 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004989 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004990 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004991
4992 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004993 * When we distrust bios wm we always need to recompute to set the
4994 * expected DDB allocations for each CRTC.
4995 */
4996 if (to_i915(dev)->wm.distrust_bios_wm)
4997 changed = true;
4998
4999 /*
Matt Roper98d39492016-05-12 07:06:03 -07005000 * If this transaction isn't actually touching any CRTC's, don't
5001 * bother with watermark calculation. Note that if we pass this
5002 * test, we're guaranteed to hold at least one CRTC state mutex,
5003 * which means we can safely use values like dev_priv->active_crtcs
5004 * since any racing commits that want to update them would need to
5005 * hold _all_ CRTC state mutexes.
5006 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005007 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07005008 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005009
Matt Roper98d39492016-05-12 07:06:03 -07005010 if (!changed)
5011 return 0;
5012
Matt Roper734fa012016-05-12 15:11:40 -07005013 /* Clear all dirty flags */
5014 results->dirty_pipes = 0;
5015
Kumar, Maheshbb9d85f2017-06-01 11:29:18 +05305016 ret = skl_include_affected_crtcs(state);
Matt Roper98d39492016-05-12 07:06:03 -07005017 if (ret)
5018 return ret;
5019
Matt Roper734fa012016-05-12 15:11:40 -07005020 /*
5021 * Calculate WM's for all pipes that are part of this transaction.
5022 * Note that the DDB allocation above may have added more CRTC's that
5023 * weren't otherwise being modified (and set bits in dirty_pipes) if
5024 * pipe allocations had to change.
5025 *
5026 * FIXME: Now that we're doing this in the atomic check phase, we
5027 * should allow skl_update_pipe_wm() to return failure in cases where
5028 * no suitable watermark values can be found.
5029 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005030 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07005031 struct intel_crtc_state *intel_cstate =
5032 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005033 const struct skl_pipe_wm *old_pipe_wm =
5034 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07005035
5036 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005037 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
5038 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07005039 if (ret)
5040 return ret;
5041
5042 if (changed)
5043 results->dirty_pipes |= drm_crtc_mask(crtc);
5044
5045 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
5046 /* This pipe's WM's did not change */
5047 continue;
5048
5049 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07005050 }
5051
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005052 skl_print_wm_changes(state);
5053
Matt Roper98d39492016-05-12 07:06:03 -07005054 return 0;
5055}
5056
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005057static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
5058 struct intel_crtc_state *cstate)
5059{
5060 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5061 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5062 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005063 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005064 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005065 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005066
5067 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5068 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005069
5070 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005071
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005072 for_each_plane_id_on_crtc(crtc, plane_id) {
5073 if (plane_id != PLANE_CURSOR)
5074 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5075 ddb, plane_id);
5076 else
5077 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5078 ddb);
5079 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005080}
5081
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005082static void skl_initial_wm(struct intel_atomic_state *state,
5083 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005084{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005085 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005086 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005087 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005088 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005089 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005090 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005091
Ville Syrjälä432081b2016-10-31 22:37:03 +02005092 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005093 return;
5094
Matt Roper734fa012016-05-12 15:11:40 -07005095 mutex_lock(&dev_priv->wm.wm_mutex);
5096
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005097 if (cstate->base.active_changed)
5098 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005099
5100 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005101
5102 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005103}
5104
Ville Syrjäläd8905652016-01-14 14:53:35 +02005105static void ilk_compute_wm_config(struct drm_device *dev,
5106 struct intel_wm_config *config)
5107{
5108 struct intel_crtc *crtc;
5109
5110 /* Compute the currently _active_ config */
5111 for_each_intel_crtc(dev, crtc) {
5112 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5113
5114 if (!wm->pipe_enabled)
5115 continue;
5116
5117 config->sprites_enabled |= wm->sprites_enabled;
5118 config->sprites_scaled |= wm->sprites_scaled;
5119 config->num_pipes_active++;
5120 }
5121}
5122
Matt Ropered4a6a72016-02-23 17:20:13 -08005123static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005124{
Chris Wilson91c8a322016-07-05 10:40:23 +01005125 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005126 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005127 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005128 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005129 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005130 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005131
Ville Syrjäläd8905652016-01-14 14:53:35 +02005132 ilk_compute_wm_config(dev, &config);
5133
5134 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5135 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005136
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005137 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005138 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005139 config.num_pipes_active == 1 && config.sprites_enabled) {
5140 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5141 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005142
Imre Deak820c1982013-12-17 14:46:36 +02005143 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005144 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005145 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005146 }
5147
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005148 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005149 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005150
Imre Deak820c1982013-12-17 14:46:36 +02005151 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005152
Imre Deak820c1982013-12-17 14:46:36 +02005153 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005154}
5155
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005156static void ilk_initial_watermarks(struct intel_atomic_state *state,
5157 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005158{
Matt Ropered4a6a72016-02-23 17:20:13 -08005159 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5160 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005161
Matt Ropered4a6a72016-02-23 17:20:13 -08005162 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005163 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005164 ilk_program_watermarks(dev_priv);
5165 mutex_unlock(&dev_priv->wm.wm_mutex);
5166}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005167
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005168static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5169 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005170{
5171 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5172 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5173
5174 mutex_lock(&dev_priv->wm.wm_mutex);
5175 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005176 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005177 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005178 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005179 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005180}
5181
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005182static inline void skl_wm_level_from_reg_val(uint32_t val,
5183 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005184{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005185 level->plane_en = val & PLANE_WM_EN;
5186 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5187 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5188 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005189}
5190
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005191void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5192 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005193{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005194 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005196 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005197 int level, max_level;
5198 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005199 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005200
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005201 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005202
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005203 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5204 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005205
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005206 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005207 if (plane_id != PLANE_CURSOR)
5208 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005209 else
5210 val = I915_READ(CUR_WM(pipe, level));
5211
5212 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5213 }
5214
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005215 if (plane_id != PLANE_CURSOR)
5216 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005217 else
5218 val = I915_READ(CUR_WM_TRANS(pipe));
5219
5220 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5221 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005222
Matt Roper3ef00282015-03-09 10:19:24 -07005223 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005224 return;
5225
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005226 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005227}
5228
5229void skl_wm_get_hw_state(struct drm_device *dev)
5230{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005231 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005232 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005233 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005234 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005235 struct intel_crtc *intel_crtc;
5236 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005237
Damien Lespiaua269c582014-11-04 17:06:49 +00005238 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005239 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5240 intel_crtc = to_intel_crtc(crtc);
5241 cstate = to_intel_crtc_state(crtc->state);
5242
5243 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5244
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005245 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005246 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005247 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005248
Matt Roper279e99d2016-05-12 07:06:02 -07005249 if (dev_priv->active_crtcs) {
5250 /* Fully recompute DDB on first atomic commit */
5251 dev_priv->wm.distrust_bios_wm = true;
5252 } else {
5253 /* Easy/common case; just sanitize DDB now if everything off */
5254 memset(ddb, 0, sizeof(*ddb));
5255 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005256}
5257
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005258static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5259{
5260 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005261 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005262 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005264 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005265 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005266 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005267 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005268 [PIPE_A] = WM0_PIPEA_ILK,
5269 [PIPE_B] = WM0_PIPEB_ILK,
5270 [PIPE_C] = WM0_PIPEC_IVB,
5271 };
5272
5273 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005274 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005275 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005276
Ville Syrjälä15606532016-05-13 17:55:17 +03005277 memset(active, 0, sizeof(*active));
5278
Matt Roper3ef00282015-03-09 10:19:24 -07005279 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005280
5281 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005282 u32 tmp = hw->wm_pipe[pipe];
5283
5284 /*
5285 * For active pipes LP0 watermark is marked as
5286 * enabled, and LP1+ watermaks as disabled since
5287 * we can't really reverse compute them in case
5288 * multiple pipes are active.
5289 */
5290 active->wm[0].enable = true;
5291 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5292 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5293 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5294 active->linetime = hw->wm_linetime[pipe];
5295 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005296 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005297
5298 /*
5299 * For inactive pipes, all watermark levels
5300 * should be marked as enabled but zeroed,
5301 * which is what we'd compute them to.
5302 */
5303 for (level = 0; level <= max_level; level++)
5304 active->wm[level].enable = true;
5305 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005306
5307 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005308}
5309
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005310#define _FW_WM(value, plane) \
5311 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5312#define _FW_WM_VLV(value, plane) \
5313 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5314
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005315static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5316 struct g4x_wm_values *wm)
5317{
5318 uint32_t tmp;
5319
5320 tmp = I915_READ(DSPFW1);
5321 wm->sr.plane = _FW_WM(tmp, SR);
5322 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5323 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5324 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5325
5326 tmp = I915_READ(DSPFW2);
5327 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5328 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5329 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5330 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5331 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5332 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5333
5334 tmp = I915_READ(DSPFW3);
5335 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5336 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5337 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5338 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5339}
5340
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005341static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5342 struct vlv_wm_values *wm)
5343{
5344 enum pipe pipe;
5345 uint32_t tmp;
5346
5347 for_each_pipe(dev_priv, pipe) {
5348 tmp = I915_READ(VLV_DDL(pipe));
5349
Ville Syrjälä1b313892016-11-28 19:37:08 +02005350 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005351 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005352 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005353 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005354 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005355 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005356 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005357 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5358 }
5359
5360 tmp = I915_READ(DSPFW1);
5361 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005362 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5363 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5364 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005365
5366 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005367 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5368 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5369 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005370
5371 tmp = I915_READ(DSPFW3);
5372 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5373
5374 if (IS_CHERRYVIEW(dev_priv)) {
5375 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005376 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5377 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005378
5379 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005380 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5381 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005382
5383 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005384 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5385 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005386
5387 tmp = I915_READ(DSPHOWM);
5388 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005389 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5390 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5391 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5392 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5393 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5394 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5395 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5396 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5397 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005398 } else {
5399 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005400 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5401 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005402
5403 tmp = I915_READ(DSPHOWM);
5404 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005405 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5406 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5407 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5408 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5409 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5410 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005411 }
5412}
5413
5414#undef _FW_WM
5415#undef _FW_WM_VLV
5416
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005417void g4x_wm_get_hw_state(struct drm_device *dev)
5418{
5419 struct drm_i915_private *dev_priv = to_i915(dev);
5420 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5421 struct intel_crtc *crtc;
5422
5423 g4x_read_wm_values(dev_priv, wm);
5424
5425 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5426
5427 for_each_intel_crtc(dev, crtc) {
5428 struct intel_crtc_state *crtc_state =
5429 to_intel_crtc_state(crtc->base.state);
5430 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5431 struct g4x_pipe_wm *raw;
5432 enum pipe pipe = crtc->pipe;
5433 enum plane_id plane_id;
5434 int level, max_level;
5435
5436 active->cxsr = wm->cxsr;
5437 active->hpll_en = wm->hpll_en;
5438 active->fbc_en = wm->fbc_en;
5439
5440 active->sr = wm->sr;
5441 active->hpll = wm->hpll;
5442
5443 for_each_plane_id_on_crtc(crtc, plane_id) {
5444 active->wm.plane[plane_id] =
5445 wm->pipe[pipe].plane[plane_id];
5446 }
5447
5448 if (wm->cxsr && wm->hpll_en)
5449 max_level = G4X_WM_LEVEL_HPLL;
5450 else if (wm->cxsr)
5451 max_level = G4X_WM_LEVEL_SR;
5452 else
5453 max_level = G4X_WM_LEVEL_NORMAL;
5454
5455 level = G4X_WM_LEVEL_NORMAL;
5456 raw = &crtc_state->wm.g4x.raw[level];
5457 for_each_plane_id_on_crtc(crtc, plane_id)
5458 raw->plane[plane_id] = active->wm.plane[plane_id];
5459
5460 if (++level > max_level)
5461 goto out;
5462
5463 raw = &crtc_state->wm.g4x.raw[level];
5464 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5465 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5466 raw->plane[PLANE_SPRITE0] = 0;
5467 raw->fbc = active->sr.fbc;
5468
5469 if (++level > max_level)
5470 goto out;
5471
5472 raw = &crtc_state->wm.g4x.raw[level];
5473 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5474 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5475 raw->plane[PLANE_SPRITE0] = 0;
5476 raw->fbc = active->hpll.fbc;
5477
5478 out:
5479 for_each_plane_id_on_crtc(crtc, plane_id)
5480 g4x_raw_plane_wm_set(crtc_state, level,
5481 plane_id, USHRT_MAX);
5482 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5483
5484 crtc_state->wm.g4x.optimal = *active;
5485 crtc_state->wm.g4x.intermediate = *active;
5486
5487 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5488 pipe_name(pipe),
5489 wm->pipe[pipe].plane[PLANE_PRIMARY],
5490 wm->pipe[pipe].plane[PLANE_CURSOR],
5491 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5492 }
5493
5494 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5495 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5496 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5497 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5498 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5499 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5500}
5501
5502void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5503{
5504 struct intel_plane *plane;
5505 struct intel_crtc *crtc;
5506
5507 mutex_lock(&dev_priv->wm.wm_mutex);
5508
5509 for_each_intel_plane(&dev_priv->drm, plane) {
5510 struct intel_crtc *crtc =
5511 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5512 struct intel_crtc_state *crtc_state =
5513 to_intel_crtc_state(crtc->base.state);
5514 struct intel_plane_state *plane_state =
5515 to_intel_plane_state(plane->base.state);
5516 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5517 enum plane_id plane_id = plane->id;
5518 int level;
5519
5520 if (plane_state->base.visible)
5521 continue;
5522
5523 for (level = 0; level < 3; level++) {
5524 struct g4x_pipe_wm *raw =
5525 &crtc_state->wm.g4x.raw[level];
5526
5527 raw->plane[plane_id] = 0;
5528 wm_state->wm.plane[plane_id] = 0;
5529 }
5530
5531 if (plane_id == PLANE_PRIMARY) {
5532 for (level = 0; level < 3; level++) {
5533 struct g4x_pipe_wm *raw =
5534 &crtc_state->wm.g4x.raw[level];
5535 raw->fbc = 0;
5536 }
5537
5538 wm_state->sr.fbc = 0;
5539 wm_state->hpll.fbc = 0;
5540 wm_state->fbc_en = false;
5541 }
5542 }
5543
5544 for_each_intel_crtc(&dev_priv->drm, crtc) {
5545 struct intel_crtc_state *crtc_state =
5546 to_intel_crtc_state(crtc->base.state);
5547
5548 crtc_state->wm.g4x.intermediate =
5549 crtc_state->wm.g4x.optimal;
5550 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5551 }
5552
5553 g4x_program_watermarks(dev_priv);
5554
5555 mutex_unlock(&dev_priv->wm.wm_mutex);
5556}
5557
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005558void vlv_wm_get_hw_state(struct drm_device *dev)
5559{
5560 struct drm_i915_private *dev_priv = to_i915(dev);
5561 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005562 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005563 u32 val;
5564
5565 vlv_read_wm_values(dev_priv, wm);
5566
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005567 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5568 wm->level = VLV_WM_LEVEL_PM2;
5569
5570 if (IS_CHERRYVIEW(dev_priv)) {
5571 mutex_lock(&dev_priv->rps.hw_lock);
5572
5573 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5574 if (val & DSP_MAXFIFO_PM5_ENABLE)
5575 wm->level = VLV_WM_LEVEL_PM5;
5576
Ville Syrjälä58590c12015-09-08 21:05:12 +03005577 /*
5578 * If DDR DVFS is disabled in the BIOS, Punit
5579 * will never ack the request. So if that happens
5580 * assume we don't have to enable/disable DDR DVFS
5581 * dynamically. To test that just set the REQ_ACK
5582 * bit to poke the Punit, but don't change the
5583 * HIGH/LOW bits so that we don't actually change
5584 * the current state.
5585 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005586 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005587 val |= FORCE_DDR_FREQ_REQ_ACK;
5588 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5589
5590 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5591 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5592 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5593 "assuming DDR DVFS is disabled\n");
5594 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5595 } else {
5596 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5597 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5598 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5599 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005600
5601 mutex_unlock(&dev_priv->rps.hw_lock);
5602 }
5603
Ville Syrjäläff32c542017-03-02 19:14:57 +02005604 for_each_intel_crtc(dev, crtc) {
5605 struct intel_crtc_state *crtc_state =
5606 to_intel_crtc_state(crtc->base.state);
5607 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5608 const struct vlv_fifo_state *fifo_state =
5609 &crtc_state->wm.vlv.fifo_state;
5610 enum pipe pipe = crtc->pipe;
5611 enum plane_id plane_id;
5612 int level;
5613
5614 vlv_get_fifo_size(crtc_state);
5615
5616 active->num_levels = wm->level + 1;
5617 active->cxsr = wm->cxsr;
5618
Ville Syrjäläff32c542017-03-02 19:14:57 +02005619 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005620 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005621 &crtc_state->wm.vlv.raw[level];
5622
5623 active->sr[level].plane = wm->sr.plane;
5624 active->sr[level].cursor = wm->sr.cursor;
5625
5626 for_each_plane_id_on_crtc(crtc, plane_id) {
5627 active->wm[level].plane[plane_id] =
5628 wm->pipe[pipe].plane[plane_id];
5629
5630 raw->plane[plane_id] =
5631 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5632 fifo_state->plane[plane_id]);
5633 }
5634 }
5635
5636 for_each_plane_id_on_crtc(crtc, plane_id)
5637 vlv_raw_plane_wm_set(crtc_state, level,
5638 plane_id, USHRT_MAX);
5639 vlv_invalidate_wms(crtc, active, level);
5640
5641 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005642 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005643
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005644 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 +02005645 pipe_name(pipe),
5646 wm->pipe[pipe].plane[PLANE_PRIMARY],
5647 wm->pipe[pipe].plane[PLANE_CURSOR],
5648 wm->pipe[pipe].plane[PLANE_SPRITE0],
5649 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005650 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005651
5652 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5653 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5654}
5655
Ville Syrjälä602ae832017-03-02 19:15:02 +02005656void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5657{
5658 struct intel_plane *plane;
5659 struct intel_crtc *crtc;
5660
5661 mutex_lock(&dev_priv->wm.wm_mutex);
5662
5663 for_each_intel_plane(&dev_priv->drm, plane) {
5664 struct intel_crtc *crtc =
5665 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5666 struct intel_crtc_state *crtc_state =
5667 to_intel_crtc_state(crtc->base.state);
5668 struct intel_plane_state *plane_state =
5669 to_intel_plane_state(plane->base.state);
5670 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5671 const struct vlv_fifo_state *fifo_state =
5672 &crtc_state->wm.vlv.fifo_state;
5673 enum plane_id plane_id = plane->id;
5674 int level;
5675
5676 if (plane_state->base.visible)
5677 continue;
5678
5679 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005680 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005681 &crtc_state->wm.vlv.raw[level];
5682
5683 raw->plane[plane_id] = 0;
5684
5685 wm_state->wm[level].plane[plane_id] =
5686 vlv_invert_wm_value(raw->plane[plane_id],
5687 fifo_state->plane[plane_id]);
5688 }
5689 }
5690
5691 for_each_intel_crtc(&dev_priv->drm, crtc) {
5692 struct intel_crtc_state *crtc_state =
5693 to_intel_crtc_state(crtc->base.state);
5694
5695 crtc_state->wm.vlv.intermediate =
5696 crtc_state->wm.vlv.optimal;
5697 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5698 }
5699
5700 vlv_program_watermarks(dev_priv);
5701
5702 mutex_unlock(&dev_priv->wm.wm_mutex);
5703}
5704
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005705void ilk_wm_get_hw_state(struct drm_device *dev)
5706{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005707 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005708 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005709 struct drm_crtc *crtc;
5710
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005711 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005712 ilk_pipe_wm_get_hw_state(crtc);
5713
5714 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5715 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5716 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5717
5718 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005719 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005720 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5721 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5722 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005723
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005724 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005725 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5726 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005727 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005728 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5729 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005730
5731 hw->enable_fbc_wm =
5732 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5733}
5734
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005735/**
5736 * intel_update_watermarks - update FIFO watermark values based on current modes
5737 *
5738 * Calculate watermark values for the various WM regs based on current mode
5739 * and plane configuration.
5740 *
5741 * There are several cases to deal with here:
5742 * - normal (i.e. non-self-refresh)
5743 * - self-refresh (SR) mode
5744 * - lines are large relative to FIFO size (buffer can hold up to 2)
5745 * - lines are small relative to FIFO size (buffer can hold more than 2
5746 * lines), so need to account for TLB latency
5747 *
5748 * The normal calculation is:
5749 * watermark = dotclock * bytes per pixel * latency
5750 * where latency is platform & configuration dependent (we assume pessimal
5751 * values here).
5752 *
5753 * The SR calculation is:
5754 * watermark = (trunc(latency/line time)+1) * surface width *
5755 * bytes per pixel
5756 * where
5757 * line time = htotal / dotclock
5758 * surface width = hdisplay for normal plane and 64 for cursor
5759 * and latency is assumed to be high, as above.
5760 *
5761 * The final value programmed to the register should always be rounded up,
5762 * and include an extra 2 entries to account for clock crossings.
5763 *
5764 * We don't use the sprite, so we can ignore that. And on Crestline we have
5765 * to set the non-SR watermarks to 8.
5766 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005767void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005768{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005769 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005770
5771 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005772 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005773}
5774
Jani Nikulae2828912016-01-18 09:19:47 +02005775/*
Daniel Vetter92703882012-08-09 16:46:01 +02005776 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005777 */
5778DEFINE_SPINLOCK(mchdev_lock);
5779
5780/* Global for IPS driver to get at the current i915 device. Protected by
5781 * mchdev_lock. */
5782static struct drm_i915_private *i915_mch_dev;
5783
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005784bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005785{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005786 u16 rgvswctl;
5787
Chris Wilson67520412017-03-02 13:28:01 +00005788 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005789
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005790 rgvswctl = I915_READ16(MEMSWCTL);
5791 if (rgvswctl & MEMCTL_CMD_STS) {
5792 DRM_DEBUG("gpu busy, RCS change rejected\n");
5793 return false; /* still busy with another command */
5794 }
5795
5796 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5797 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5798 I915_WRITE16(MEMSWCTL, rgvswctl);
5799 POSTING_READ16(MEMSWCTL);
5800
5801 rgvswctl |= MEMCTL_CMD_STS;
5802 I915_WRITE16(MEMSWCTL, rgvswctl);
5803
5804 return true;
5805}
5806
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005807static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005808{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005809 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005810 u8 fmax, fmin, fstart, vstart;
5811
Daniel Vetter92703882012-08-09 16:46:01 +02005812 spin_lock_irq(&mchdev_lock);
5813
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005814 rgvmodectl = I915_READ(MEMMODECTL);
5815
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005816 /* Enable temp reporting */
5817 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5818 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5819
5820 /* 100ms RC evaluation intervals */
5821 I915_WRITE(RCUPEI, 100000);
5822 I915_WRITE(RCDNEI, 100000);
5823
5824 /* Set max/min thresholds to 90ms and 80ms respectively */
5825 I915_WRITE(RCBMAXAVG, 90000);
5826 I915_WRITE(RCBMINAVG, 80000);
5827
5828 I915_WRITE(MEMIHYST, 1);
5829
5830 /* Set up min, max, and cur for interrupt handling */
5831 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5832 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5833 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5834 MEMMODE_FSTART_SHIFT;
5835
Ville Syrjälä616847e2015-09-18 20:03:19 +03005836 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005837 PXVFREQ_PX_SHIFT;
5838
Daniel Vetter20e4d402012-08-08 23:35:39 +02005839 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5840 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005841
Daniel Vetter20e4d402012-08-08 23:35:39 +02005842 dev_priv->ips.max_delay = fstart;
5843 dev_priv->ips.min_delay = fmin;
5844 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005845
5846 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5847 fmax, fmin, fstart);
5848
5849 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5850
5851 /*
5852 * Interrupts will be enabled in ironlake_irq_postinstall
5853 */
5854
5855 I915_WRITE(VIDSTART, vstart);
5856 POSTING_READ(VIDSTART);
5857
5858 rgvmodectl |= MEMMODE_SWMODE_EN;
5859 I915_WRITE(MEMMODECTL, rgvmodectl);
5860
Daniel Vetter92703882012-08-09 16:46:01 +02005861 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005862 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005863 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005864
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005865 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005866
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005867 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5868 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005869 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005870 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005871 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005872
5873 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005874}
5875
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005876static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005877{
Daniel Vetter92703882012-08-09 16:46:01 +02005878 u16 rgvswctl;
5879
5880 spin_lock_irq(&mchdev_lock);
5881
5882 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005883
5884 /* Ack interrupts, disable EFC interrupt */
5885 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5886 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5887 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5888 I915_WRITE(DEIIR, DE_PCU_EVENT);
5889 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5890
5891 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005892 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005893 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005894 rgvswctl |= MEMCTL_CMD_STS;
5895 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005896 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005897
Daniel Vetter92703882012-08-09 16:46:01 +02005898 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005899}
5900
Daniel Vetteracbe9472012-07-26 11:50:05 +02005901/* There's a funny hw issue where the hw returns all 0 when reading from
5902 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5903 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5904 * all limits and the gpu stuck at whatever frequency it is at atm).
5905 */
Akash Goel74ef1172015-03-06 11:07:19 +05305906static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005907{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005908 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005909
Daniel Vetter20b46e52012-07-26 11:16:14 +02005910 /* Only set the down limit when we've reached the lowest level to avoid
5911 * getting more interrupts, otherwise leave this clear. This prevents a
5912 * race in the hw when coming out of rc6: There's a tiny window where
5913 * the hw runs at the minimal clock before selecting the desired
5914 * frequency, if the down threshold expires in that window we will not
5915 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005916 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305917 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5918 if (val <= dev_priv->rps.min_freq_softlimit)
5919 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5920 } else {
5921 limits = dev_priv->rps.max_freq_softlimit << 24;
5922 if (val <= dev_priv->rps.min_freq_softlimit)
5923 limits |= dev_priv->rps.min_freq_softlimit << 16;
5924 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005925
5926 return limits;
5927}
5928
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005929static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5930{
5931 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305932 u32 threshold_up = 0, threshold_down = 0; /* in % */
5933 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005934
5935 new_power = dev_priv->rps.power;
5936 switch (dev_priv->rps.power) {
5937 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005938 if (val > dev_priv->rps.efficient_freq + 1 &&
5939 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005940 new_power = BETWEEN;
5941 break;
5942
5943 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005944 if (val <= dev_priv->rps.efficient_freq &&
5945 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005946 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005947 else if (val >= dev_priv->rps.rp0_freq &&
5948 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005949 new_power = HIGH_POWER;
5950 break;
5951
5952 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005953 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5954 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005955 new_power = BETWEEN;
5956 break;
5957 }
5958 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005959 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005960 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005961 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005962 new_power = HIGH_POWER;
5963 if (new_power == dev_priv->rps.power)
5964 return;
5965
5966 /* Note the units here are not exactly 1us, but 1280ns. */
5967 switch (new_power) {
5968 case LOW_POWER:
5969 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305970 ei_up = 16000;
5971 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005972
5973 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305974 ei_down = 32000;
5975 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005976 break;
5977
5978 case BETWEEN:
5979 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305980 ei_up = 13000;
5981 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005982
5983 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305984 ei_down = 32000;
5985 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005986 break;
5987
5988 case HIGH_POWER:
5989 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305990 ei_up = 10000;
5991 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005992
5993 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305994 ei_down = 32000;
5995 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005996 break;
5997 }
5998
Mika Kuoppala6067a272017-02-15 15:52:59 +02005999 /* When byt can survive without system hang with dynamic
6000 * sw freq adjustments, this restriction can be lifted.
6001 */
6002 if (IS_VALLEYVIEW(dev_priv))
6003 goto skip_hw_write;
6004
Akash Goel8a586432015-03-06 11:07:18 +05306005 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006006 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306007 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006008 GT_INTERVAL_FROM_US(dev_priv,
6009 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306010
6011 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006012 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306013 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006014 GT_INTERVAL_FROM_US(dev_priv,
6015 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306016
Chris Wilsona72b5622016-07-02 15:35:59 +01006017 I915_WRITE(GEN6_RP_CONTROL,
6018 GEN6_RP_MEDIA_TURBO |
6019 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6020 GEN6_RP_MEDIA_IS_GFX |
6021 GEN6_RP_ENABLE |
6022 GEN6_RP_UP_BUSY_AVG |
6023 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306024
Mika Kuoppala6067a272017-02-15 15:52:59 +02006025skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006026 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01006027 dev_priv->rps.up_threshold = threshold_up;
6028 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006029 dev_priv->rps.last_adj = 0;
6030}
6031
Chris Wilson2876ce72014-03-28 08:03:34 +00006032static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6033{
6034 u32 mask = 0;
6035
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006036 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00006037 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006038 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00006039 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006040 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006041
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006042 mask &= dev_priv->pm_rps_events;
6043
Imre Deak59d02a12014-12-19 19:33:26 +02006044 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006045}
6046
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006047/* gen6_set_rps is called to update the frequency request, but should also be
6048 * called when the range (min_delay and max_delay) is modified so that we can
6049 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006050static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006051{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006052 /* min/max delay may still have been modified so be sure to
6053 * write the limits value.
6054 */
6055 if (val != dev_priv->rps.cur_freq) {
6056 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006057
Chris Wilsondc979972016-05-10 14:10:04 +01006058 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05306059 I915_WRITE(GEN6_RPNSWREQ,
6060 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006061 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006062 I915_WRITE(GEN6_RPNSWREQ,
6063 HSW_FREQUENCY(val));
6064 else
6065 I915_WRITE(GEN6_RPNSWREQ,
6066 GEN6_FREQUENCY(val) |
6067 GEN6_OFFSET(0) |
6068 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006069 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006070
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006071 /* Make sure we continue to get interrupts
6072 * until we hit the minimum or maximum frequencies.
6073 */
Akash Goel74ef1172015-03-06 11:07:19 +05306074 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006075 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006076
Ben Widawskyb39fb292014-03-19 18:31:11 -07006077 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006078 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006079
6080 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006081}
6082
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006083static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006084{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006085 int err;
6086
Chris Wilsondc979972016-05-10 14:10:04 +01006087 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006088 "Odd GPU freq value\n"))
6089 val &= ~1;
6090
Deepak Scd25dd52015-07-10 18:31:40 +05306091 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6092
Chris Wilson8fb55192015-04-07 16:20:28 +01006093 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006094 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6095 if (err)
6096 return err;
6097
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006098 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006099 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006100
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006101 dev_priv->rps.cur_freq = val;
6102 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006103
6104 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006105}
6106
Deepak Sa7f6e232015-05-09 18:04:44 +05306107/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306108 *
6109 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306110 * 1. Forcewake Media well.
6111 * 2. Request idle freq.
6112 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306113*/
6114static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6115{
Chris Wilsonaed242f2015-03-18 09:48:21 +00006116 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006117 int err;
Deepak S5549d252014-06-28 11:26:11 +05306118
Chris Wilsonaed242f2015-03-18 09:48:21 +00006119 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306120 return;
6121
Chris Wilsonc9efef72017-01-02 15:28:45 +00006122 /* The punit delays the write of the frequency and voltage until it
6123 * determines the GPU is awake. During normal usage we don't want to
6124 * waste power changing the frequency if the GPU is sleeping (rc6).
6125 * However, the GPU and driver is now idle and we do not want to delay
6126 * switching to minimum voltage (reducing power whilst idle) as we do
6127 * not expect to be woken in the near future and so must flush the
6128 * change by waking the device.
6129 *
6130 * We choose to take the media powerwell (either would do to trick the
6131 * punit into committing the voltage change) as that takes a lot less
6132 * power than the render powerwell.
6133 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306134 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006135 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306136 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006137
6138 if (err)
6139 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306140}
6141
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006142void gen6_rps_busy(struct drm_i915_private *dev_priv)
6143{
6144 mutex_lock(&dev_priv->rps.hw_lock);
6145 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006146 u8 freq;
6147
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006148 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006149 gen6_rps_reset_ei(dev_priv);
6150 I915_WRITE(GEN6_PMINTRMSK,
6151 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006152
Chris Wilsonc33d2472016-07-04 08:08:36 +01006153 gen6_enable_rps_interrupts(dev_priv);
6154
Chris Wilsonbd648182017-02-10 15:03:48 +00006155 /* Use the user's desired frequency as a guide, but for better
6156 * performance, jump directly to RPe as our starting frequency.
6157 */
6158 freq = max(dev_priv->rps.cur_freq,
6159 dev_priv->rps.efficient_freq);
6160
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006161 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006162 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006163 dev_priv->rps.min_freq_softlimit,
6164 dev_priv->rps.max_freq_softlimit)))
6165 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006166 }
6167 mutex_unlock(&dev_priv->rps.hw_lock);
6168}
6169
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006170void gen6_rps_idle(struct drm_i915_private *dev_priv)
6171{
Chris Wilsonc33d2472016-07-04 08:08:36 +01006172 /* Flush our bottom-half so that it does not race with us
6173 * setting the idle frequency and so that it is bounded by
6174 * our rpm wakeref. And then disable the interrupts to stop any
6175 * futher RPS reclocking whilst we are asleep.
6176 */
6177 gen6_disable_rps_interrupts(dev_priv);
6178
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006179 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006180 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006181 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306182 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006183 else
Chris Wilsondc979972016-05-10 14:10:04 +01006184 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006185 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006186 I915_WRITE(GEN6_PMINTRMSK,
6187 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006188 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006189 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01006190
Chris Wilson8d3afd72015-05-21 21:01:47 +01006191 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01006192 while (!list_empty(&dev_priv->rps.clients))
6193 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01006194 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006195}
6196
Chris Wilson1854d5c2015-04-07 16:20:32 +01006197void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01006198 struct intel_rps_client *rps,
6199 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006200{
Chris Wilson8d3afd72015-05-21 21:01:47 +01006201 /* This is intentionally racy! We peek at the state here, then
6202 * validate inside the RPS worker.
6203 */
Chris Wilson67d97da2016-07-04 08:08:31 +01006204 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01006205 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006206 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01006207 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006208
Chris Wilsone61b9952015-04-27 13:41:24 +01006209 /* Force a RPS boost (and don't count it against the client) if
6210 * the GPU is severely congested.
6211 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01006212 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01006213 rps = NULL;
6214
Chris Wilson8d3afd72015-05-21 21:01:47 +01006215 spin_lock(&dev_priv->rps.client_lock);
6216 if (rps == NULL || list_empty(&rps->link)) {
6217 spin_lock_irq(&dev_priv->irq_lock);
6218 if (dev_priv->rps.interrupts_enabled) {
6219 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01006220 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01006221 }
6222 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01006223
Chris Wilson2e1b8732015-04-27 13:41:22 +01006224 if (rps != NULL) {
6225 list_add(&rps->link, &dev_priv->rps.clients);
6226 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01006227 } else
6228 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006229 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006230 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006231}
6232
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006233int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006234{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006235 int err;
6236
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006237 lockdep_assert_held(&dev_priv->rps.hw_lock);
6238 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6239 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6240
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006241 if (!dev_priv->rps.enabled) {
6242 dev_priv->rps.cur_freq = val;
6243 return 0;
6244 }
6245
Chris Wilsondc979972016-05-10 14:10:04 +01006246 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006247 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006248 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006249 err = gen6_set_rps(dev_priv, val);
6250
6251 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006252}
6253
Chris Wilsondc979972016-05-10 14:10:04 +01006254static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006255{
Zhe Wang20e49362014-11-04 17:07:05 +00006256 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006257 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006258}
6259
Chris Wilsondc979972016-05-10 14:10:04 +01006260static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306261{
Akash Goel2030d682016-04-23 00:05:45 +05306262 I915_WRITE(GEN6_RP_CONTROL, 0);
6263}
6264
Chris Wilsondc979972016-05-10 14:10:04 +01006265static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006266{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006267 I915_WRITE(GEN6_RC_CONTROL, 0);
6268 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306269 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006270}
6271
Chris Wilsondc979972016-05-10 14:10:04 +01006272static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306273{
Deepak S38807742014-05-23 21:00:15 +05306274 I915_WRITE(GEN6_RC_CONTROL, 0);
6275}
6276
Chris Wilsondc979972016-05-10 14:10:04 +01006277static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006278{
Deepak S98a2e5f2014-08-18 10:35:27 -07006279 /* we're doing forcewake before Disabling RC6,
6280 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006281 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006282
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006283 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006284
Mika Kuoppala59bad942015-01-16 11:34:40 +02006285 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006286}
6287
Chris Wilsondc979972016-05-10 14:10:04 +01006288static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006289{
Chris Wilsondc979972016-05-10 14:10:04 +01006290 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006291 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6292 mode = GEN6_RC_CTL_RC6_ENABLE;
6293 else
6294 mode = 0;
6295 }
Chris Wilsondc979972016-05-10 14:10:04 +01006296 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006297 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6298 "RC6 %s RC6p %s RC6pp %s\n",
6299 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6300 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6301 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006302
6303 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006304 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6305 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006306}
6307
Chris Wilsondc979972016-05-10 14:10:04 +01006308static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306309{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006310 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306311 bool enable_rc6 = true;
6312 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006313 u32 rc_ctl;
6314 int rc_sw_target;
6315
6316 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6317 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6318 RC_SW_TARGET_STATE_SHIFT;
6319 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6320 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6321 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6322 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6323 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306324
6325 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006326 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306327 enable_rc6 = false;
6328 }
6329
6330 /*
6331 * The exact context size is not known for BXT, so assume a page size
6332 * for this check.
6333 */
6334 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006335 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6336 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6337 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006338 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306339 enable_rc6 = false;
6340 }
6341
6342 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6343 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6344 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6345 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006346 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306347 enable_rc6 = false;
6348 }
6349
Imre Deakfc619842016-06-29 19:13:55 +03006350 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6351 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6352 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6353 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6354 enable_rc6 = false;
6355 }
6356
6357 if (!I915_READ(GEN6_GFXPAUSE)) {
6358 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6359 enable_rc6 = false;
6360 }
6361
6362 if (!I915_READ(GEN8_MISC_CTRL0)) {
6363 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306364 enable_rc6 = false;
6365 }
6366
6367 return enable_rc6;
6368}
6369
Chris Wilsondc979972016-05-10 14:10:04 +01006370int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006371{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006372 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006373 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006374 return 0;
6375
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306376 if (!enable_rc6)
6377 return 0;
6378
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006379 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306380 DRM_INFO("RC6 disabled by BIOS\n");
6381 return 0;
6382 }
6383
Daniel Vetter456470e2012-08-08 23:35:40 +02006384 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006385 if (enable_rc6 >= 0) {
6386 int mask;
6387
Chris Wilsondc979972016-05-10 14:10:04 +01006388 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006389 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6390 INTEL_RC6pp_ENABLE;
6391 else
6392 mask = INTEL_RC6_ENABLE;
6393
6394 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006395 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6396 "(requested %d, valid %d)\n",
6397 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006398
6399 return enable_rc6 & mask;
6400 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006401
Chris Wilsondc979972016-05-10 14:10:04 +01006402 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006403 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006404
6405 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006406}
6407
Chris Wilsondc979972016-05-10 14:10:04 +01006408static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006409{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006410 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006411
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006412 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006413 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006414 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006415 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6416 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6417 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6418 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006419 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006420 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6421 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6422 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6423 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006424 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006425 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006426
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006427 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006428 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006429 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006430 u32 ddcc_status = 0;
6431
6432 if (sandybridge_pcode_read(dev_priv,
6433 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6434 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006435 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006436 clamp_t(u8,
6437 ((ddcc_status >> 8) & 0xff),
6438 dev_priv->rps.min_freq,
6439 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006440 }
6441
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006442 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306443 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006444 * the natural hardware unit for SKL
6445 */
Akash Goelc5e06882015-06-29 14:50:19 +05306446 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6447 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6448 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6449 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6450 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6451 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006452}
6453
Chris Wilson3a45b052016-07-13 09:10:32 +01006454static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006455 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006456{
6457 u8 freq = dev_priv->rps.cur_freq;
6458
6459 /* force a reset */
6460 dev_priv->rps.power = -1;
6461 dev_priv->rps.cur_freq = -1;
6462
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006463 if (set(dev_priv, freq))
6464 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006465}
6466
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006467/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006468static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006469{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006470 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6471
Akash Goel0beb0592015-03-06 11:07:20 +05306472 /* Program defaults and thresholds for RPS*/
6473 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6474 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006475
Akash Goel0beb0592015-03-06 11:07:20 +05306476 /* 1 second timeout*/
6477 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6478 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6479
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006480 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006481
Akash Goel0beb0592015-03-06 11:07:20 +05306482 /* Leaning on the below call to gen6_set_rps to program/setup the
6483 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6484 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006485 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006486
6487 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6488}
6489
Chris Wilsondc979972016-05-10 14:10:04 +01006490static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006491{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006492 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306493 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006494 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006495
6496 /* 1a: Software RC state - RC0 */
6497 I915_WRITE(GEN6_RC_STATE, 0);
6498
6499 /* 1b: Get forcewake during program sequence. Although the driver
6500 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006501 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006502
6503 /* 2a: Disable RC states. */
6504 I915_WRITE(GEN6_RC_CONTROL, 0);
6505
6506 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306507
6508 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006509 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306510 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6511 else
6512 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006513 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6514 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306515 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006516 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306517
Dave Gordon1a3d1892016-05-13 15:36:30 +01006518 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306519 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6520
Zhe Wang20e49362014-11-04 17:07:05 +00006521 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006522
Zhe Wang38c23522015-01-20 12:23:04 +00006523 /* 2c: Program Coarse Power Gating Policies. */
6524 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6525 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6526
Zhe Wang20e49362014-11-04 17:07:05 +00006527 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006528 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006529 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006530 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006531 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6532 I915_WRITE(GEN6_RC_CONTROL,
6533 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006534
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306535 /*
6536 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306537 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306538 */
Chris Wilsondc979972016-05-10 14:10:04 +01006539 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306540 I915_WRITE(GEN9_PG_ENABLE, 0);
6541 else
6542 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6543 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006544
Mika Kuoppala59bad942015-01-16 11:34:40 +02006545 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006546}
6547
Chris Wilsondc979972016-05-10 14:10:04 +01006548static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006549{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006550 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306551 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006552 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006553
6554 /* 1a: Software RC state - RC0 */
6555 I915_WRITE(GEN6_RC_STATE, 0);
6556
6557 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6558 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006559 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006560
6561 /* 2a: Disable RC states. */
6562 I915_WRITE(GEN6_RC_CONTROL, 0);
6563
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006564 /* 2b: Program RC6 thresholds.*/
6565 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6566 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6567 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306568 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006569 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006570 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006571 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006572 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6573 else
6574 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006575
6576 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006577 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006578 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006579 intel_print_rc6_info(dev_priv, rc6_mask);
6580 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006581 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6582 GEN7_RC_CTL_TO_MODE |
6583 rc6_mask);
6584 else
6585 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6586 GEN6_RC_CTL_EI_MODE(1) |
6587 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006588
6589 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006590 I915_WRITE(GEN6_RPNSWREQ,
6591 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6592 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6593 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006594 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6595 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006596
Daniel Vetter7526ed72014-09-29 15:07:19 +02006597 /* Docs recommend 900MHz, and 300 MHz respectively */
6598 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6599 dev_priv->rps.max_freq_softlimit << 24 |
6600 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006601
Daniel Vetter7526ed72014-09-29 15:07:19 +02006602 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6603 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6604 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6605 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006606
Daniel Vetter7526ed72014-09-29 15:07:19 +02006607 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006608
6609 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006610 I915_WRITE(GEN6_RP_CONTROL,
6611 GEN6_RP_MEDIA_TURBO |
6612 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6613 GEN6_RP_MEDIA_IS_GFX |
6614 GEN6_RP_ENABLE |
6615 GEN6_RP_UP_BUSY_AVG |
6616 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006617
Daniel Vetter7526ed72014-09-29 15:07:19 +02006618 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006619
Chris Wilson3a45b052016-07-13 09:10:32 +01006620 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006621
Mika Kuoppala59bad942015-01-16 11:34:40 +02006622 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006623}
6624
Chris Wilsondc979972016-05-10 14:10:04 +01006625static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006626{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006627 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306628 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006629 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006630 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006631 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006632 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006633
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006634 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006635
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006636 /* Here begins a magic sequence of register writes to enable
6637 * auto-downclocking.
6638 *
6639 * Perhaps there might be some value in exposing these to
6640 * userspace...
6641 */
6642 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006643
6644 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006645 gtfifodbg = I915_READ(GTFIFODBG);
6646 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006647 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6648 I915_WRITE(GTFIFODBG, gtfifodbg);
6649 }
6650
Mika Kuoppala59bad942015-01-16 11:34:40 +02006651 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006652
6653 /* disable the counters and set deterministic thresholds */
6654 I915_WRITE(GEN6_RC_CONTROL, 0);
6655
6656 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6657 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6658 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6659 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6660 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6661
Akash Goel3b3f1652016-10-13 22:44:48 +05306662 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006663 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006664
6665 I915_WRITE(GEN6_RC_SLEEP, 0);
6666 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006667 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006668 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6669 else
6670 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006671 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006672 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6673
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006674 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006675 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006676 if (rc6_mode & INTEL_RC6_ENABLE)
6677 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6678
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006679 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006680 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006681 if (rc6_mode & INTEL_RC6p_ENABLE)
6682 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006683
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006684 if (rc6_mode & INTEL_RC6pp_ENABLE)
6685 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6686 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006687
Chris Wilsondc979972016-05-10 14:10:04 +01006688 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006689
6690 I915_WRITE(GEN6_RC_CONTROL,
6691 rc6_mask |
6692 GEN6_RC_CTL_EI_MODE(1) |
6693 GEN6_RC_CTL_HW_ENABLE);
6694
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006695 /* Power down if completely idle for over 50ms */
6696 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006697 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006698
Chris Wilson3a45b052016-07-13 09:10:32 +01006699 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006700
Ben Widawsky31643d52012-09-26 10:34:01 -07006701 rc6vids = 0;
6702 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006703 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006704 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006705 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006706 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6707 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6708 rc6vids &= 0xffff00;
6709 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6710 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6711 if (ret)
6712 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6713 }
6714
Mika Kuoppala59bad942015-01-16 11:34:40 +02006715 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006716}
6717
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006718static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006719{
6720 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006721 unsigned int gpu_freq;
6722 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306723 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006724 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006725 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006726
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006727 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006728
Ben Widawskyeda79642013-10-07 17:15:48 -03006729 policy = cpufreq_cpu_get(0);
6730 if (policy) {
6731 max_ia_freq = policy->cpuinfo.max_freq;
6732 cpufreq_cpu_put(policy);
6733 } else {
6734 /*
6735 * Default to measured freq if none found, PCU will ensure we
6736 * don't go over
6737 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006738 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006739 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006740
6741 /* Convert from kHz to MHz */
6742 max_ia_freq /= 1000;
6743
Ben Widawsky153b4b952013-10-22 22:05:09 -07006744 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006745 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6746 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006747
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006748 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306749 /* Convert GT frequency to 50 HZ units */
6750 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6751 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6752 } else {
6753 min_gpu_freq = dev_priv->rps.min_freq;
6754 max_gpu_freq = dev_priv->rps.max_freq;
6755 }
6756
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006757 /*
6758 * For each potential GPU frequency, load a ring frequency we'd like
6759 * to use for memory access. We do this by specifying the IA frequency
6760 * the PCU should use as a reference to determine the ring frequency.
6761 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306762 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6763 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006764 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006765
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006766 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306767 /*
6768 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6769 * No floor required for ring frequency on SKL.
6770 */
6771 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006772 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006773 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6774 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006775 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006776 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006777 ring_freq = max(min_ring_freq, ring_freq);
6778 /* leave ia_freq as the default, chosen by cpufreq */
6779 } else {
6780 /* On older processors, there is no separate ring
6781 * clock domain, so in order to boost the bandwidth
6782 * of the ring, we need to upclock the CPU (ia_freq).
6783 *
6784 * For GPU frequencies less than 750MHz,
6785 * just use the lowest ring freq.
6786 */
6787 if (gpu_freq < min_freq)
6788 ia_freq = 800;
6789 else
6790 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6791 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6792 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006793
Ben Widawsky42c05262012-09-26 10:34:00 -07006794 sandybridge_pcode_write(dev_priv,
6795 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006796 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6797 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6798 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006799 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006800}
6801
Ville Syrjälä03af2042014-06-28 02:03:53 +03006802static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306803{
6804 u32 val, rp0;
6805
Jani Nikula5b5929c2015-10-07 11:17:46 +03006806 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306807
Imre Deak43b67992016-08-31 19:13:02 +03006808 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006809 case 8:
6810 /* (2 * 4) config */
6811 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6812 break;
6813 case 12:
6814 /* (2 * 6) config */
6815 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6816 break;
6817 case 16:
6818 /* (2 * 8) config */
6819 default:
6820 /* Setting (2 * 8) Min RP0 for any other combination */
6821 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6822 break;
Deepak S095acd52015-01-17 11:05:59 +05306823 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006824
6825 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6826
Deepak S2b6b3a02014-05-27 15:59:30 +05306827 return rp0;
6828}
6829
6830static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6831{
6832 u32 val, rpe;
6833
6834 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6835 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6836
6837 return rpe;
6838}
6839
Deepak S7707df42014-07-12 18:46:14 +05306840static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6841{
6842 u32 val, rp1;
6843
Jani Nikula5b5929c2015-10-07 11:17:46 +03006844 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6845 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6846
Deepak S7707df42014-07-12 18:46:14 +05306847 return rp1;
6848}
6849
Deepak S96676fe2016-08-12 18:46:41 +05306850static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6851{
6852 u32 val, rpn;
6853
6854 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6855 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6856 FB_GFX_FREQ_FUSE_MASK);
6857
6858 return rpn;
6859}
6860
Deepak Sf8f2b002014-07-10 13:16:21 +05306861static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6862{
6863 u32 val, rp1;
6864
6865 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6866
6867 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6868
6869 return rp1;
6870}
6871
Ville Syrjälä03af2042014-06-28 02:03:53 +03006872static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006873{
6874 u32 val, rp0;
6875
Jani Nikula64936252013-05-22 15:36:20 +03006876 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006877
6878 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6879 /* Clamp to max */
6880 rp0 = min_t(u32, rp0, 0xea);
6881
6882 return rp0;
6883}
6884
6885static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6886{
6887 u32 val, rpe;
6888
Jani Nikula64936252013-05-22 15:36:20 +03006889 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006890 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006891 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006892 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6893
6894 return rpe;
6895}
6896
Ville Syrjälä03af2042014-06-28 02:03:53 +03006897static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006898{
Imre Deak36146032014-12-04 18:39:35 +02006899 u32 val;
6900
6901 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6902 /*
6903 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6904 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6905 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6906 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6907 * to make sure it matches what Punit accepts.
6908 */
6909 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006910}
6911
Imre Deakae484342014-03-31 15:10:44 +03006912/* Check that the pctx buffer wasn't move under us. */
6913static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6914{
6915 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6916
6917 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6918 dev_priv->vlv_pctx->stolen->start);
6919}
6920
Deepak S38807742014-05-23 21:00:15 +05306921
6922/* Check that the pcbr address is not empty. */
6923static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6924{
6925 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6926
6927 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6928}
6929
Chris Wilsondc979972016-05-10 14:10:04 +01006930static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306931{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006932 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006933 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306934 u32 pcbr;
6935 int pctx_size = 32*1024;
6936
Deepak S38807742014-05-23 21:00:15 +05306937 pcbr = I915_READ(VLV_PCBR);
6938 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006939 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306940 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006941 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306942
6943 pctx_paddr = (paddr & (~4095));
6944 I915_WRITE(VLV_PCBR, pctx_paddr);
6945 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006946
6947 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306948}
6949
Chris Wilsondc979972016-05-10 14:10:04 +01006950static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006951{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006952 struct drm_i915_gem_object *pctx;
6953 unsigned long pctx_paddr;
6954 u32 pcbr;
6955 int pctx_size = 24*1024;
6956
6957 pcbr = I915_READ(VLV_PCBR);
6958 if (pcbr) {
6959 /* BIOS set it up already, grab the pre-alloc'd space */
6960 int pcbr_offset;
6961
6962 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006963 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006964 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006965 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006966 pctx_size);
6967 goto out;
6968 }
6969
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006970 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6971
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006972 /*
6973 * From the Gunit register HAS:
6974 * The Gfx driver is expected to program this register and ensure
6975 * proper allocation within Gfx stolen memory. For example, this
6976 * register should be programmed such than the PCBR range does not
6977 * overlap with other ranges, such as the frame buffer, protected
6978 * memory, or any other relevant ranges.
6979 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006980 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006981 if (!pctx) {
6982 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006983 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006984 }
6985
6986 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6987 I915_WRITE(VLV_PCBR, pctx_paddr);
6988
6989out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006990 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006991 dev_priv->vlv_pctx = pctx;
6992}
6993
Chris Wilsondc979972016-05-10 14:10:04 +01006994static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006995{
Imre Deakae484342014-03-31 15:10:44 +03006996 if (WARN_ON(!dev_priv->vlv_pctx))
6997 return;
6998
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006999 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03007000 dev_priv->vlv_pctx = NULL;
7001}
7002
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007003static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7004{
7005 dev_priv->rps.gpll_ref_freq =
7006 vlv_get_cck_clock(dev_priv, "GPLL ref",
7007 CCK_GPLL_CLOCK_CONTROL,
7008 dev_priv->czclk_freq);
7009
7010 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
7011 dev_priv->rps.gpll_ref_freq);
7012}
7013
Chris Wilsondc979972016-05-10 14:10:04 +01007014static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007015{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007016 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007017
Chris Wilsondc979972016-05-10 14:10:04 +01007018 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007019
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007020 vlv_init_gpll_ref_freq(dev_priv);
7021
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007022 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7023 switch ((val >> 6) & 3) {
7024 case 0:
7025 case 1:
7026 dev_priv->mem_freq = 800;
7027 break;
7028 case 2:
7029 dev_priv->mem_freq = 1066;
7030 break;
7031 case 3:
7032 dev_priv->mem_freq = 1333;
7033 break;
7034 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007035 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007036
Imre Deak4e805192014-04-14 20:24:41 +03007037 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
7038 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7039 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007040 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007041 dev_priv->rps.max_freq);
7042
7043 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
7044 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007045 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007046 dev_priv->rps.efficient_freq);
7047
Deepak Sf8f2b002014-07-10 13:16:21 +05307048 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
7049 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007050 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05307051 dev_priv->rps.rp1_freq);
7052
Imre Deak4e805192014-04-14 20:24:41 +03007053 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
7054 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007055 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007056 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007057}
7058
Chris Wilsondc979972016-05-10 14:10:04 +01007059static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307060{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007061 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307062
Chris Wilsondc979972016-05-10 14:10:04 +01007063 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307064
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007065 vlv_init_gpll_ref_freq(dev_priv);
7066
Ville Syrjäläa5805162015-05-26 20:42:30 +03007067 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007068 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007069 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007070
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007071 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007072 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007073 dev_priv->mem_freq = 2000;
7074 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007075 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007076 dev_priv->mem_freq = 1600;
7077 break;
7078 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007079 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007080
Deepak S2b6b3a02014-05-27 15:59:30 +05307081 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
7082 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7083 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007084 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307085 dev_priv->rps.max_freq);
7086
7087 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
7088 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007089 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307090 dev_priv->rps.efficient_freq);
7091
Deepak S7707df42014-07-12 18:46:14 +05307092 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
7093 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007094 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05307095 dev_priv->rps.rp1_freq);
7096
Deepak S96676fe2016-08-12 18:46:41 +05307097 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307098 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007099 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307100 dev_priv->rps.min_freq);
7101
Ville Syrjälä1c147622014-08-18 14:42:43 +03007102 WARN_ONCE((dev_priv->rps.max_freq |
7103 dev_priv->rps.efficient_freq |
7104 dev_priv->rps.rp1_freq |
7105 dev_priv->rps.min_freq) & 1,
7106 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307107}
7108
Chris Wilsondc979972016-05-10 14:10:04 +01007109static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007110{
Chris Wilsondc979972016-05-10 14:10:04 +01007111 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007112}
7113
Chris Wilsondc979972016-05-10 14:10:04 +01007114static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307115{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007116 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307117 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05307118 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307119
7120 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7121
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007122 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7123 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307124 if (gtfifodbg) {
7125 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7126 gtfifodbg);
7127 I915_WRITE(GTFIFODBG, gtfifodbg);
7128 }
7129
7130 cherryview_check_pctx(dev_priv);
7131
7132 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7133 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007134 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307135
Ville Syrjälä160614a2015-01-19 13:50:47 +02007136 /* Disable RC states. */
7137 I915_WRITE(GEN6_RC_CONTROL, 0);
7138
Deepak S38807742014-05-23 21:00:15 +05307139 /* 2a: Program RC6 thresholds.*/
7140 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7141 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7142 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7143
Akash Goel3b3f1652016-10-13 22:44:48 +05307144 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007145 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307146 I915_WRITE(GEN6_RC_SLEEP, 0);
7147
Deepak Sf4f71c72015-03-28 15:23:35 +05307148 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7149 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307150
7151 /* allows RC6 residency counter to work */
7152 I915_WRITE(VLV_COUNTER_CONTROL,
7153 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7154 VLV_MEDIA_RC6_COUNT_EN |
7155 VLV_RENDER_RC6_COUNT_EN));
7156
7157 /* For now we assume BIOS is allocating and populating the PCBR */
7158 pcbr = I915_READ(VLV_PCBR);
7159
Deepak S38807742014-05-23 21:00:15 +05307160 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01007161 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
7162 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007163 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307164
7165 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7166
Deepak S2b6b3a02014-05-27 15:59:30 +05307167 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007168 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307169 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7170 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7171 I915_WRITE(GEN6_RP_UP_EI, 66000);
7172 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7173
7174 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7175
7176 /* 5: Enable RPS */
7177 I915_WRITE(GEN6_RP_CONTROL,
7178 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007179 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307180 GEN6_RP_ENABLE |
7181 GEN6_RP_UP_BUSY_AVG |
7182 GEN6_RP_DOWN_IDLE_AVG);
7183
Deepak S3ef62342015-04-29 08:36:24 +05307184 /* Setting Fixed Bias */
7185 val = VLV_OVERRIDE_EN |
7186 VLV_SOC_TDP_EN |
7187 CHV_BIAS_CPU_50_SOC_50;
7188 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7189
Deepak S2b6b3a02014-05-27 15:59:30 +05307190 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7191
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007192 /* RPS code assumes GPLL is used */
7193 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7194
Jani Nikula742f4912015-09-03 11:16:09 +03007195 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307196 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7197
Chris Wilson3a45b052016-07-13 09:10:32 +01007198 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307199
Mika Kuoppala59bad942015-01-16 11:34:40 +02007200 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307201}
7202
Chris Wilsondc979972016-05-10 14:10:04 +01007203static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007204{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007205 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307206 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007207 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007208
7209 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7210
Imre Deakae484342014-03-31 15:10:44 +03007211 valleyview_check_pctx(dev_priv);
7212
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007213 gtfifodbg = I915_READ(GTFIFODBG);
7214 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007215 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7216 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007217 I915_WRITE(GTFIFODBG, gtfifodbg);
7218 }
7219
Deepak Sc8d9a592013-11-23 14:55:42 +05307220 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007221 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007222
Ville Syrjälä160614a2015-01-19 13:50:47 +02007223 /* Disable RC states. */
7224 I915_WRITE(GEN6_RC_CONTROL, 0);
7225
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007226 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007227 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7228 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7229 I915_WRITE(GEN6_RP_UP_EI, 66000);
7230 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7231
7232 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7233
7234 I915_WRITE(GEN6_RP_CONTROL,
7235 GEN6_RP_MEDIA_TURBO |
7236 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7237 GEN6_RP_MEDIA_IS_GFX |
7238 GEN6_RP_ENABLE |
7239 GEN6_RP_UP_BUSY_AVG |
7240 GEN6_RP_DOWN_IDLE_CONT);
7241
7242 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7243 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7244 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7245
Akash Goel3b3f1652016-10-13 22:44:48 +05307246 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007247 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007248
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08007249 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007250
7251 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007252 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007253 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7254 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007255 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007256 VLV_MEDIA_RC6_COUNT_EN |
7257 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007258
Chris Wilsondc979972016-05-10 14:10:04 +01007259 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007260 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007261
Chris Wilsondc979972016-05-10 14:10:04 +01007262 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007263
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007264 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007265
Deepak S3ef62342015-04-29 08:36:24 +05307266 /* Setting Fixed Bias */
7267 val = VLV_OVERRIDE_EN |
7268 VLV_SOC_TDP_EN |
7269 VLV_BIAS_CPU_125_SOC_875;
7270 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7271
Jani Nikula64936252013-05-22 15:36:20 +03007272 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007273
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007274 /* RPS code assumes GPLL is used */
7275 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7276
Jani Nikula742f4912015-09-03 11:16:09 +03007277 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007278 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7279
Chris Wilson3a45b052016-07-13 09:10:32 +01007280 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007281
Mika Kuoppala59bad942015-01-16 11:34:40 +02007282 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007283}
7284
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007285static unsigned long intel_pxfreq(u32 vidfreq)
7286{
7287 unsigned long freq;
7288 int div = (vidfreq & 0x3f0000) >> 16;
7289 int post = (vidfreq & 0x3000) >> 12;
7290 int pre = (vidfreq & 0x7);
7291
7292 if (!pre)
7293 return 0;
7294
7295 freq = ((div * 133333) / ((1<<post) * pre));
7296
7297 return freq;
7298}
7299
Daniel Vettereb48eb02012-04-26 23:28:12 +02007300static const struct cparams {
7301 u16 i;
7302 u16 t;
7303 u16 m;
7304 u16 c;
7305} cparams[] = {
7306 { 1, 1333, 301, 28664 },
7307 { 1, 1066, 294, 24460 },
7308 { 1, 800, 294, 25192 },
7309 { 0, 1333, 276, 27605 },
7310 { 0, 1066, 276, 27605 },
7311 { 0, 800, 231, 23784 },
7312};
7313
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007314static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007315{
7316 u64 total_count, diff, ret;
7317 u32 count1, count2, count3, m = 0, c = 0;
7318 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7319 int i;
7320
Chris Wilson67520412017-03-02 13:28:01 +00007321 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007322
Daniel Vetter20e4d402012-08-08 23:35:39 +02007323 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007324
7325 /* Prevent division-by-zero if we are asking too fast.
7326 * Also, we don't get interesting results if we are polling
7327 * faster than once in 10ms, so just return the saved value
7328 * in such cases.
7329 */
7330 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007331 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007332
7333 count1 = I915_READ(DMIEC);
7334 count2 = I915_READ(DDREC);
7335 count3 = I915_READ(CSIEC);
7336
7337 total_count = count1 + count2 + count3;
7338
7339 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007340 if (total_count < dev_priv->ips.last_count1) {
7341 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007342 diff += total_count;
7343 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007344 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007345 }
7346
7347 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007348 if (cparams[i].i == dev_priv->ips.c_m &&
7349 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007350 m = cparams[i].m;
7351 c = cparams[i].c;
7352 break;
7353 }
7354 }
7355
7356 diff = div_u64(diff, diff1);
7357 ret = ((m * diff) + c);
7358 ret = div_u64(ret, 10);
7359
Daniel Vetter20e4d402012-08-08 23:35:39 +02007360 dev_priv->ips.last_count1 = total_count;
7361 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007362
Daniel Vetter20e4d402012-08-08 23:35:39 +02007363 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007364
7365 return ret;
7366}
7367
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007368unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7369{
7370 unsigned long val;
7371
Chris Wilsondc979972016-05-10 14:10:04 +01007372 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007373 return 0;
7374
7375 spin_lock_irq(&mchdev_lock);
7376
7377 val = __i915_chipset_val(dev_priv);
7378
7379 spin_unlock_irq(&mchdev_lock);
7380
7381 return val;
7382}
7383
Daniel Vettereb48eb02012-04-26 23:28:12 +02007384unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7385{
7386 unsigned long m, x, b;
7387 u32 tsfs;
7388
7389 tsfs = I915_READ(TSFS);
7390
7391 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7392 x = I915_READ8(TR1);
7393
7394 b = tsfs & TSFS_INTR_MASK;
7395
7396 return ((m * x) / 127) - b;
7397}
7398
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007399static int _pxvid_to_vd(u8 pxvid)
7400{
7401 if (pxvid == 0)
7402 return 0;
7403
7404 if (pxvid >= 8 && pxvid < 31)
7405 pxvid = 31;
7406
7407 return (pxvid + 2) * 125;
7408}
7409
7410static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007411{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007412 const int vd = _pxvid_to_vd(pxvid);
7413 const int vm = vd - 1125;
7414
Chris Wilsondc979972016-05-10 14:10:04 +01007415 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007416 return vm > 0 ? vm : 0;
7417
7418 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007419}
7420
Daniel Vetter02d71952012-08-09 16:44:54 +02007421static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007422{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007423 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007424 u32 count;
7425
Chris Wilson67520412017-03-02 13:28:01 +00007426 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007427
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007428 now = ktime_get_raw_ns();
7429 diffms = now - dev_priv->ips.last_time2;
7430 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007431
7432 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007433 if (!diffms)
7434 return;
7435
7436 count = I915_READ(GFXEC);
7437
Daniel Vetter20e4d402012-08-08 23:35:39 +02007438 if (count < dev_priv->ips.last_count2) {
7439 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007440 diff += count;
7441 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007442 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007443 }
7444
Daniel Vetter20e4d402012-08-08 23:35:39 +02007445 dev_priv->ips.last_count2 = count;
7446 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007447
7448 /* More magic constants... */
7449 diff = diff * 1181;
7450 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007451 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007452}
7453
Daniel Vetter02d71952012-08-09 16:44:54 +02007454void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7455{
Chris Wilsondc979972016-05-10 14:10:04 +01007456 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007457 return;
7458
Daniel Vetter92703882012-08-09 16:46:01 +02007459 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007460
7461 __i915_update_gfx_val(dev_priv);
7462
Daniel Vetter92703882012-08-09 16:46:01 +02007463 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007464}
7465
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007466static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007467{
7468 unsigned long t, corr, state1, corr2, state2;
7469 u32 pxvid, ext_v;
7470
Chris Wilson67520412017-03-02 13:28:01 +00007471 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007472
Ville Syrjälä616847e2015-09-18 20:03:19 +03007473 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007474 pxvid = (pxvid >> 24) & 0x7f;
7475 ext_v = pvid_to_extvid(dev_priv, pxvid);
7476
7477 state1 = ext_v;
7478
7479 t = i915_mch_val(dev_priv);
7480
7481 /* Revel in the empirically derived constants */
7482
7483 /* Correction factor in 1/100000 units */
7484 if (t > 80)
7485 corr = ((t * 2349) + 135940);
7486 else if (t >= 50)
7487 corr = ((t * 964) + 29317);
7488 else /* < 50 */
7489 corr = ((t * 301) + 1004);
7490
7491 corr = corr * ((150142 * state1) / 10000 - 78642);
7492 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007493 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007494
7495 state2 = (corr2 * state1) / 10000;
7496 state2 /= 100; /* convert to mW */
7497
Daniel Vetter02d71952012-08-09 16:44:54 +02007498 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007499
Daniel Vetter20e4d402012-08-08 23:35:39 +02007500 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007501}
7502
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007503unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7504{
7505 unsigned long val;
7506
Chris Wilsondc979972016-05-10 14:10:04 +01007507 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007508 return 0;
7509
7510 spin_lock_irq(&mchdev_lock);
7511
7512 val = __i915_gfx_val(dev_priv);
7513
7514 spin_unlock_irq(&mchdev_lock);
7515
7516 return val;
7517}
7518
Daniel Vettereb48eb02012-04-26 23:28:12 +02007519/**
7520 * i915_read_mch_val - return value for IPS use
7521 *
7522 * Calculate and return a value for the IPS driver to use when deciding whether
7523 * we have thermal and power headroom to increase CPU or GPU power budget.
7524 */
7525unsigned long i915_read_mch_val(void)
7526{
7527 struct drm_i915_private *dev_priv;
7528 unsigned long chipset_val, graphics_val, ret = 0;
7529
Daniel Vetter92703882012-08-09 16:46:01 +02007530 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007531 if (!i915_mch_dev)
7532 goto out_unlock;
7533 dev_priv = i915_mch_dev;
7534
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007535 chipset_val = __i915_chipset_val(dev_priv);
7536 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007537
7538 ret = chipset_val + graphics_val;
7539
7540out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007541 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007542
7543 return ret;
7544}
7545EXPORT_SYMBOL_GPL(i915_read_mch_val);
7546
7547/**
7548 * i915_gpu_raise - raise GPU frequency limit
7549 *
7550 * Raise the limit; IPS indicates we have thermal headroom.
7551 */
7552bool i915_gpu_raise(void)
7553{
7554 struct drm_i915_private *dev_priv;
7555 bool ret = true;
7556
Daniel Vetter92703882012-08-09 16:46:01 +02007557 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007558 if (!i915_mch_dev) {
7559 ret = false;
7560 goto out_unlock;
7561 }
7562 dev_priv = i915_mch_dev;
7563
Daniel Vetter20e4d402012-08-08 23:35:39 +02007564 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7565 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007566
7567out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007568 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007569
7570 return ret;
7571}
7572EXPORT_SYMBOL_GPL(i915_gpu_raise);
7573
7574/**
7575 * i915_gpu_lower - lower GPU frequency limit
7576 *
7577 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7578 * frequency maximum.
7579 */
7580bool i915_gpu_lower(void)
7581{
7582 struct drm_i915_private *dev_priv;
7583 bool ret = true;
7584
Daniel Vetter92703882012-08-09 16:46:01 +02007585 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007586 if (!i915_mch_dev) {
7587 ret = false;
7588 goto out_unlock;
7589 }
7590 dev_priv = i915_mch_dev;
7591
Daniel Vetter20e4d402012-08-08 23:35:39 +02007592 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7593 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007594
7595out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007596 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007597
7598 return ret;
7599}
7600EXPORT_SYMBOL_GPL(i915_gpu_lower);
7601
7602/**
7603 * i915_gpu_busy - indicate GPU business to IPS
7604 *
7605 * Tell the IPS driver whether or not the GPU is busy.
7606 */
7607bool i915_gpu_busy(void)
7608{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007609 bool ret = false;
7610
Daniel Vetter92703882012-08-09 16:46:01 +02007611 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007612 if (i915_mch_dev)
7613 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007614 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007615
7616 return ret;
7617}
7618EXPORT_SYMBOL_GPL(i915_gpu_busy);
7619
7620/**
7621 * i915_gpu_turbo_disable - disable graphics turbo
7622 *
7623 * Disable graphics turbo by resetting the max frequency and setting the
7624 * current frequency to the default.
7625 */
7626bool i915_gpu_turbo_disable(void)
7627{
7628 struct drm_i915_private *dev_priv;
7629 bool ret = true;
7630
Daniel Vetter92703882012-08-09 16:46:01 +02007631 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007632 if (!i915_mch_dev) {
7633 ret = false;
7634 goto out_unlock;
7635 }
7636 dev_priv = i915_mch_dev;
7637
Daniel Vetter20e4d402012-08-08 23:35:39 +02007638 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007639
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007640 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007641 ret = false;
7642
7643out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007644 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007645
7646 return ret;
7647}
7648EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7649
7650/**
7651 * Tells the intel_ips driver that the i915 driver is now loaded, if
7652 * IPS got loaded first.
7653 *
7654 * This awkward dance is so that neither module has to depend on the
7655 * other in order for IPS to do the appropriate communication of
7656 * GPU turbo limits to i915.
7657 */
7658static void
7659ips_ping_for_i915_load(void)
7660{
7661 void (*link)(void);
7662
7663 link = symbol_get(ips_link_to_i915_driver);
7664 if (link) {
7665 link();
7666 symbol_put(ips_link_to_i915_driver);
7667 }
7668}
7669
7670void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7671{
Daniel Vetter02d71952012-08-09 16:44:54 +02007672 /* We only register the i915 ips part with intel-ips once everything is
7673 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007674 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007675 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007676 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007677
7678 ips_ping_for_i915_load();
7679}
7680
7681void intel_gpu_ips_teardown(void)
7682{
Daniel Vetter92703882012-08-09 16:46:01 +02007683 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007684 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007685 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007686}
Deepak S76c3552f2014-01-30 23:08:16 +05307687
Chris Wilsondc979972016-05-10 14:10:04 +01007688static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007689{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007690 u32 lcfuse;
7691 u8 pxw[16];
7692 int i;
7693
7694 /* Disable to program */
7695 I915_WRITE(ECR, 0);
7696 POSTING_READ(ECR);
7697
7698 /* Program energy weights for various events */
7699 I915_WRITE(SDEW, 0x15040d00);
7700 I915_WRITE(CSIEW0, 0x007f0000);
7701 I915_WRITE(CSIEW1, 0x1e220004);
7702 I915_WRITE(CSIEW2, 0x04000004);
7703
7704 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007705 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007706 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007707 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007708
7709 /* Program P-state weights to account for frequency power adjustment */
7710 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007711 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007712 unsigned long freq = intel_pxfreq(pxvidfreq);
7713 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7714 PXVFREQ_PX_SHIFT;
7715 unsigned long val;
7716
7717 val = vid * vid;
7718 val *= (freq / 1000);
7719 val *= 255;
7720 val /= (127*127*900);
7721 if (val > 0xff)
7722 DRM_ERROR("bad pxval: %ld\n", val);
7723 pxw[i] = val;
7724 }
7725 /* Render standby states get 0 weight */
7726 pxw[14] = 0;
7727 pxw[15] = 0;
7728
7729 for (i = 0; i < 4; i++) {
7730 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7731 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007732 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007733 }
7734
7735 /* Adjust magic regs to magic values (more experimental results) */
7736 I915_WRITE(OGW0, 0);
7737 I915_WRITE(OGW1, 0);
7738 I915_WRITE(EG0, 0x00007f00);
7739 I915_WRITE(EG1, 0x0000000e);
7740 I915_WRITE(EG2, 0x000e0000);
7741 I915_WRITE(EG3, 0x68000300);
7742 I915_WRITE(EG4, 0x42000000);
7743 I915_WRITE(EG5, 0x00140031);
7744 I915_WRITE(EG6, 0);
7745 I915_WRITE(EG7, 0);
7746
7747 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007748 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007749
7750 /* Enable PMON + select events */
7751 I915_WRITE(ECR, 0x80000019);
7752
7753 lcfuse = I915_READ(LCFUSE02);
7754
Daniel Vetter20e4d402012-08-08 23:35:39 +02007755 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007756}
7757
Chris Wilsondc979972016-05-10 14:10:04 +01007758void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007759{
Imre Deakb268c692015-12-15 20:10:31 +02007760 /*
7761 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7762 * requirement.
7763 */
7764 if (!i915.enable_rc6) {
7765 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7766 intel_runtime_pm_get(dev_priv);
7767 }
Imre Deake6069ca2014-04-18 16:01:02 +03007768
Chris Wilsonb5163db2016-08-10 13:58:24 +01007769 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007770 mutex_lock(&dev_priv->rps.hw_lock);
7771
7772 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007773 if (IS_CHERRYVIEW(dev_priv))
7774 cherryview_init_gt_powersave(dev_priv);
7775 else if (IS_VALLEYVIEW(dev_priv))
7776 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007777 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007778 gen6_init_rps_frequencies(dev_priv);
7779
7780 /* Derive initial user preferences/limits from the hardware limits */
7781 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7782 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7783
7784 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7785 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7786
7787 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7788 dev_priv->rps.min_freq_softlimit =
7789 max_t(int,
7790 dev_priv->rps.efficient_freq,
7791 intel_freq_opcode(dev_priv, 450));
7792
Chris Wilson99ac9612016-07-13 09:10:34 +01007793 /* After setting max-softlimit, find the overclock max freq */
7794 if (IS_GEN6(dev_priv) ||
7795 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7796 u32 params = 0;
7797
7798 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7799 if (params & BIT(31)) { /* OC supported */
7800 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7801 (dev_priv->rps.max_freq & 0xff) * 50,
7802 (params & 0xff) * 50);
7803 dev_priv->rps.max_freq = params & 0xff;
7804 }
7805 }
7806
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007807 /* Finally allow us to boost to max by default */
7808 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7809
Chris Wilson773ea9a2016-07-13 09:10:33 +01007810 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007811 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007812
7813 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007814}
7815
Chris Wilsondc979972016-05-10 14:10:04 +01007816void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007817{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007818 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007819 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007820
7821 if (!i915.enable_rc6)
7822 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007823}
7824
Chris Wilson54b4f682016-07-21 21:16:19 +01007825/**
7826 * intel_suspend_gt_powersave - suspend PM work and helper threads
7827 * @dev_priv: i915 device
7828 *
7829 * We don't want to disable RC6 or other features here, we just want
7830 * to make sure any work we've queued has finished and won't bother
7831 * us while we're suspended.
7832 */
7833void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7834{
7835 if (INTEL_GEN(dev_priv) < 6)
7836 return;
7837
7838 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7839 intel_runtime_pm_put(dev_priv);
7840
7841 /* gen6_rps_idle() will be called later to disable interrupts */
7842}
7843
Chris Wilsonb7137e02016-07-13 09:10:37 +01007844void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7845{
7846 dev_priv->rps.enabled = true; /* force disabling */
7847 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007848
7849 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007850}
7851
Chris Wilsondc979972016-05-10 14:10:04 +01007852void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007853{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007854 if (!READ_ONCE(dev_priv->rps.enabled))
7855 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007856
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007857 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007858
Chris Wilsonb7137e02016-07-13 09:10:37 +01007859 if (INTEL_GEN(dev_priv) >= 9) {
7860 gen9_disable_rc6(dev_priv);
7861 gen9_disable_rps(dev_priv);
7862 } else if (IS_CHERRYVIEW(dev_priv)) {
7863 cherryview_disable_rps(dev_priv);
7864 } else if (IS_VALLEYVIEW(dev_priv)) {
7865 valleyview_disable_rps(dev_priv);
7866 } else if (INTEL_GEN(dev_priv) >= 6) {
7867 gen6_disable_rps(dev_priv);
7868 } else if (IS_IRONLAKE_M(dev_priv)) {
7869 ironlake_disable_drps(dev_priv);
7870 }
7871
7872 dev_priv->rps.enabled = false;
7873 mutex_unlock(&dev_priv->rps.hw_lock);
7874}
7875
7876void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7877{
Chris Wilson54b4f682016-07-21 21:16:19 +01007878 /* We shouldn't be disabling as we submit, so this should be less
7879 * racy than it appears!
7880 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007881 if (READ_ONCE(dev_priv->rps.enabled))
7882 return;
7883
7884 /* Powersaving is controlled by the host when inside a VM */
7885 if (intel_vgpu_active(dev_priv))
7886 return;
7887
7888 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007889
Chris Wilsondc979972016-05-10 14:10:04 +01007890 if (IS_CHERRYVIEW(dev_priv)) {
7891 cherryview_enable_rps(dev_priv);
7892 } else if (IS_VALLEYVIEW(dev_priv)) {
7893 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007894 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007895 gen9_enable_rc6(dev_priv);
7896 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007897 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007898 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007899 } else if (IS_BROADWELL(dev_priv)) {
7900 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007901 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007902 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007903 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007904 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007905 } else if (IS_IRONLAKE_M(dev_priv)) {
7906 ironlake_enable_drps(dev_priv);
7907 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007908 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007909
7910 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7911 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7912
7913 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7914 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7915
Chris Wilson54b4f682016-07-21 21:16:19 +01007916 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007917 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007918}
Imre Deakc6df39b2014-04-14 20:24:29 +03007919
Chris Wilson54b4f682016-07-21 21:16:19 +01007920static void __intel_autoenable_gt_powersave(struct work_struct *work)
7921{
7922 struct drm_i915_private *dev_priv =
7923 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7924 struct intel_engine_cs *rcs;
7925 struct drm_i915_gem_request *req;
7926
7927 if (READ_ONCE(dev_priv->rps.enabled))
7928 goto out;
7929
Akash Goel3b3f1652016-10-13 22:44:48 +05307930 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007931 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007932 goto out;
7933
7934 if (!rcs->init_context)
7935 goto out;
7936
7937 mutex_lock(&dev_priv->drm.struct_mutex);
7938
7939 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7940 if (IS_ERR(req))
7941 goto unlock;
7942
7943 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7944 rcs->init_context(req);
7945
7946 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007947 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007948
7949unlock:
7950 mutex_unlock(&dev_priv->drm.struct_mutex);
7951out:
7952 intel_runtime_pm_put(dev_priv);
7953}
7954
7955void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7956{
7957 if (READ_ONCE(dev_priv->rps.enabled))
7958 return;
7959
7960 if (IS_IRONLAKE_M(dev_priv)) {
7961 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007962 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007963 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7964 /*
7965 * PCU communication is slow and this doesn't need to be
7966 * done at any specific time, so do this out of our fast path
7967 * to make resume and init faster.
7968 *
7969 * We depend on the HW RC6 power context save/restore
7970 * mechanism when entering D3 through runtime PM suspend. So
7971 * disable RPM until RPS/RC6 is properly setup. We can only
7972 * get here via the driver load/system resume/runtime resume
7973 * paths, so the _noresume version is enough (and in case of
7974 * runtime resume it's necessary).
7975 */
7976 if (queue_delayed_work(dev_priv->wq,
7977 &dev_priv->rps.autoenable_work,
7978 round_jiffies_up_relative(HZ)))
7979 intel_runtime_pm_get_noresume(dev_priv);
7980 }
7981}
7982
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007983static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007984{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007985 /*
7986 * On Ibex Peak and Cougar Point, we need to disable clock
7987 * gating for the panel power sequencer or it will fail to
7988 * start up when no ports are active.
7989 */
7990 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7991}
7992
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007993static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007994{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007995 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007996
Damien Lespiau055e3932014-08-18 13:49:10 +01007997 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007998 I915_WRITE(DSPCNTR(pipe),
7999 I915_READ(DSPCNTR(pipe)) |
8000 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008001
8002 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8003 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008004 }
8005}
8006
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008007static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02008008{
Ville Syrjälä017636c2013-12-05 15:51:37 +02008009 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
8010 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
8011 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
8012
8013 /*
8014 * Don't touch WM1S_LP_EN here.
8015 * Doing so could cause underruns.
8016 */
8017}
8018
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008019static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008020{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008021 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008022
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008023 /*
8024 * Required for FBC
8025 * WaFbcDisableDpfcClockGating:ilk
8026 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008027 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8028 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8029 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008030
8031 I915_WRITE(PCH_3DCGDIS0,
8032 MARIUNIT_CLOCK_GATE_DISABLE |
8033 SVSMUNIT_CLOCK_GATE_DISABLE);
8034 I915_WRITE(PCH_3DCGDIS1,
8035 VFMUNIT_CLOCK_GATE_DISABLE);
8036
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008037 /*
8038 * According to the spec the following bits should be set in
8039 * order to enable memory self-refresh
8040 * The bit 22/21 of 0x42004
8041 * The bit 5 of 0x42020
8042 * The bit 15 of 0x45000
8043 */
8044 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8045 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8046 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008047 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008048 I915_WRITE(DISP_ARB_CTL,
8049 (I915_READ(DISP_ARB_CTL) |
8050 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008051
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008052 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008053
8054 /*
8055 * Based on the document from hardware guys the following bits
8056 * should be set unconditionally in order to enable FBC.
8057 * The bit 22 of 0x42000
8058 * The bit 22 of 0x42004
8059 * The bit 7,8,9 of 0x42020.
8060 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008061 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008062 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008063 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8064 I915_READ(ILK_DISPLAY_CHICKEN1) |
8065 ILK_FBCQ_DIS);
8066 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8067 I915_READ(ILK_DISPLAY_CHICKEN2) |
8068 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008069 }
8070
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008071 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8072
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008073 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8074 I915_READ(ILK_DISPLAY_CHICKEN2) |
8075 ILK_ELPIN_409_SELECT);
8076 I915_WRITE(_3D_CHICKEN2,
8077 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8078 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008079
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008080 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008081 I915_WRITE(CACHE_MODE_0,
8082 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008083
Akash Goel4e046322014-04-04 17:14:38 +05308084 /* WaDisable_RenderCache_OperationalFlush:ilk */
8085 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8086
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008087 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008088
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008089 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008090}
8091
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008092static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008093{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008094 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008095 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008096
8097 /*
8098 * On Ibex Peak and Cougar Point, we need to disable clock
8099 * gating for the panel power sequencer or it will fail to
8100 * start up when no ports are active.
8101 */
Jesse Barnescd664072013-10-02 10:34:19 -07008102 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8103 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8104 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008105 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8106 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008107 /* The below fixes the weird display corruption, a few pixels shifted
8108 * downward, on (only) LVDS of some HP laptops with IVY.
8109 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008110 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008111 val = I915_READ(TRANS_CHICKEN2(pipe));
8112 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8113 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008114 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008115 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008116 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8117 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8118 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008119 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8120 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008121 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008122 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008123 I915_WRITE(TRANS_CHICKEN1(pipe),
8124 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8125 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008126}
8127
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008128static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008129{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008130 uint32_t tmp;
8131
8132 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008133 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8134 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8135 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008136}
8137
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008138static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008139{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008140 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008141
Damien Lespiau231e54f2012-10-19 17:55:41 +01008142 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008143
8144 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8145 I915_READ(ILK_DISPLAY_CHICKEN2) |
8146 ILK_ELPIN_409_SELECT);
8147
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008148 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008149 I915_WRITE(_3D_CHICKEN,
8150 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8151
Akash Goel4e046322014-04-04 17:14:38 +05308152 /* WaDisable_RenderCache_OperationalFlush:snb */
8153 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8154
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008155 /*
8156 * BSpec recoomends 8x4 when MSAA is used,
8157 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008158 *
8159 * Note that PS/WM thread counts depend on the WIZ hashing
8160 * disable bit, which we don't touch here, but it's good
8161 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008162 */
8163 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008164 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008165
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008166 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008167
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008168 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008169 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008170
8171 I915_WRITE(GEN6_UCGCTL1,
8172 I915_READ(GEN6_UCGCTL1) |
8173 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8174 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8175
8176 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8177 * gating disable must be set. Failure to set it results in
8178 * flickering pixels due to Z write ordering failures after
8179 * some amount of runtime in the Mesa "fire" demo, and Unigine
8180 * Sanctuary and Tropics, and apparently anything else with
8181 * alpha test or pixel discard.
8182 *
8183 * According to the spec, bit 11 (RCCUNIT) must also be set,
8184 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008185 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008186 * WaDisableRCCUnitClockGating:snb
8187 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008188 */
8189 I915_WRITE(GEN6_UCGCTL2,
8190 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8191 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8192
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008193 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008194 I915_WRITE(_3D_CHICKEN3,
8195 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008196
8197 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008198 * Bspec says:
8199 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8200 * 3DSTATE_SF number of SF output attributes is more than 16."
8201 */
8202 I915_WRITE(_3D_CHICKEN3,
8203 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8204
8205 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008206 * According to the spec the following bits should be
8207 * set in order to enable memory self-refresh and fbc:
8208 * The bit21 and bit22 of 0x42000
8209 * The bit21 and bit22 of 0x42004
8210 * The bit5 and bit7 of 0x42020
8211 * The bit14 of 0x70180
8212 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008213 *
8214 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008215 */
8216 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8217 I915_READ(ILK_DISPLAY_CHICKEN1) |
8218 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8219 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8220 I915_READ(ILK_DISPLAY_CHICKEN2) |
8221 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008222 I915_WRITE(ILK_DSPCLK_GATE_D,
8223 I915_READ(ILK_DSPCLK_GATE_D) |
8224 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8225 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008226
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008227 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008228
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008229 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008230
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008231 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008232}
8233
8234static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8235{
8236 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8237
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008238 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008239 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008240 *
8241 * This actually overrides the dispatch
8242 * mode for all thread types.
8243 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008244 reg &= ~GEN7_FF_SCHED_MASK;
8245 reg |= GEN7_FF_TS_SCHED_HW;
8246 reg |= GEN7_FF_VS_SCHED_HW;
8247 reg |= GEN7_FF_DS_SCHED_HW;
8248
8249 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8250}
8251
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008252static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008253{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008254 /*
8255 * TODO: this bit should only be enabled when really needed, then
8256 * disabled when not needed anymore in order to save power.
8257 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008258 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008259 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8260 I915_READ(SOUTH_DSPCLK_GATE_D) |
8261 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008262
8263 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008264 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8265 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008266 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008267}
8268
Ville Syrjälä712bf362016-10-31 22:37:23 +02008269static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008270{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008271 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008272 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8273
8274 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8275 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8276 }
8277}
8278
Imre Deak450174f2016-05-03 15:54:21 +03008279static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8280 int general_prio_credits,
8281 int high_prio_credits)
8282{
8283 u32 misccpctl;
8284
8285 /* WaTempDisableDOPClkGating:bdw */
8286 misccpctl = I915_READ(GEN7_MISCCPCTL);
8287 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8288
8289 I915_WRITE(GEN8_L3SQCREG1,
8290 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8291 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8292
8293 /*
8294 * Wait at least 100 clocks before re-enabling clock gating.
8295 * See the definition of L3SQCREG1 in BSpec.
8296 */
8297 POSTING_READ(GEN8_L3SQCREG1);
8298 udelay(1);
8299 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8300}
8301
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008302static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008303{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008304 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008305
8306 /* WaDisableSDEUnitClockGating:kbl */
8307 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8308 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8309 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008310
8311 /* WaDisableGamClockGating:kbl */
8312 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8313 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8314 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008315
Rodrigo Vivi82525c12017-06-08 08:50:00 -07008316 /* WaFbcNukeOnHostModify:kbl,cfl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008317 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8318 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008319}
8320
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008321static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008322{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008323 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008324
8325 /* WAC6entrylatency:skl */
8326 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8327 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008328
8329 /* WaFbcNukeOnHostModify:skl */
8330 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8331 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008332}
8333
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008334static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008335{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008336 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008337
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008338 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008339
Ben Widawskyab57fff2013-12-12 15:28:04 -08008340 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008341 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008342
Ben Widawskyab57fff2013-12-12 15:28:04 -08008343 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008344 I915_WRITE(CHICKEN_PAR1_1,
8345 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8346
Ben Widawskyab57fff2013-12-12 15:28:04 -08008347 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008348 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008349 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008350 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008351 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008352 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008353
Ben Widawskyab57fff2013-12-12 15:28:04 -08008354 /* WaVSRefCountFullforceMissDisable:bdw */
8355 /* WaDSRefCountFullforceMissDisable:bdw */
8356 I915_WRITE(GEN7_FF_THREAD_MODE,
8357 I915_READ(GEN7_FF_THREAD_MODE) &
8358 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008359
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008360 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8361 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008362
8363 /* WaDisableSDEUnitClockGating:bdw */
8364 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8365 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008366
Imre Deak450174f2016-05-03 15:54:21 +03008367 /* WaProgramL3SqcReg1Default:bdw */
8368 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008369
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008370 /*
8371 * WaGttCachingOffByDefault:bdw
8372 * GTT cache may not work with big pages, so if those
8373 * are ever enabled GTT cache may need to be disabled.
8374 */
8375 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8376
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008377 /* WaKVMNotificationOnConfigChange:bdw */
8378 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8379 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8380
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008381 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008382
8383 /* WaDisableDopClockGating:bdw
8384 *
8385 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8386 * clock gating.
8387 */
8388 I915_WRITE(GEN6_UCGCTL1,
8389 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008390}
8391
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008392static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008393{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008394 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008395
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008396 /* L3 caching of data atomics doesn't work -- disable it. */
8397 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8398 I915_WRITE(HSW_ROW_CHICKEN3,
8399 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8400
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008401 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008402 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8403 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8404 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8405
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008406 /* WaVSRefCountFullforceMissDisable:hsw */
8407 I915_WRITE(GEN7_FF_THREAD_MODE,
8408 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008409
Akash Goel4e046322014-04-04 17:14:38 +05308410 /* WaDisable_RenderCache_OperationalFlush:hsw */
8411 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8412
Chia-I Wufe27c602014-01-28 13:29:33 +08008413 /* enable HiZ Raw Stall Optimization */
8414 I915_WRITE(CACHE_MODE_0_GEN7,
8415 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8416
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008417 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008418 I915_WRITE(CACHE_MODE_1,
8419 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008420
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008421 /*
8422 * BSpec recommends 8x4 when MSAA is used,
8423 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008424 *
8425 * Note that PS/WM thread counts depend on the WIZ hashing
8426 * disable bit, which we don't touch here, but it's good
8427 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008428 */
8429 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008430 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008431
Kenneth Graunke94411592014-12-31 16:23:00 -08008432 /* WaSampleCChickenBitEnable:hsw */
8433 I915_WRITE(HALF_SLICE_CHICKEN3,
8434 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8435
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008436 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008437 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8438
Paulo Zanoni90a88642013-05-03 17:23:45 -03008439 /* WaRsPkgCStateDisplayPMReq:hsw */
8440 I915_WRITE(CHICKEN_PAR1_1,
8441 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008442
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008443 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008444}
8445
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008446static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008447{
Ben Widawsky20848222012-05-04 18:58:59 -07008448 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008449
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008450 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008451
Damien Lespiau231e54f2012-10-19 17:55:41 +01008452 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008453
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008454 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008455 I915_WRITE(_3D_CHICKEN3,
8456 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8457
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008458 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008459 I915_WRITE(IVB_CHICKEN3,
8460 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8461 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8462
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008463 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008464 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008465 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8466 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008467
Akash Goel4e046322014-04-04 17:14:38 +05308468 /* WaDisable_RenderCache_OperationalFlush:ivb */
8469 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8470
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008471 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008472 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8473 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8474
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008475 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008476 I915_WRITE(GEN7_L3CNTLREG1,
8477 GEN7_WA_FOR_GEN7_L3_CONTROL);
8478 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008479 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008480 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008481 I915_WRITE(GEN7_ROW_CHICKEN2,
8482 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008483 else {
8484 /* must write both registers */
8485 I915_WRITE(GEN7_ROW_CHICKEN2,
8486 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008487 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8488 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008489 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008490
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008491 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008492 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8493 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8494
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008495 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008496 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008497 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008498 */
8499 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008500 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008501
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008502 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008503 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8504 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8505 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8506
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008507 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008508
8509 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008510
Chris Wilson22721342014-03-04 09:41:43 +00008511 if (0) { /* causes HiZ corruption on ivb:gt1 */
8512 /* enable HiZ Raw Stall Optimization */
8513 I915_WRITE(CACHE_MODE_0_GEN7,
8514 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8515 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008516
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008517 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008518 I915_WRITE(CACHE_MODE_1,
8519 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008520
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008521 /*
8522 * BSpec recommends 8x4 when MSAA is used,
8523 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008524 *
8525 * Note that PS/WM thread counts depend on the WIZ hashing
8526 * disable bit, which we don't touch here, but it's good
8527 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008528 */
8529 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008530 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008531
Ben Widawsky20848222012-05-04 18:58:59 -07008532 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8533 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8534 snpcr |= GEN6_MBC_SNPCR_MED;
8535 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008536
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008537 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008538 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008539
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008540 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008541}
8542
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008543static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008544{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008545 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008546 I915_WRITE(_3D_CHICKEN3,
8547 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8548
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008549 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008550 I915_WRITE(IVB_CHICKEN3,
8551 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8552 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8553
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008554 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008555 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008556 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008557 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8558 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008559
Akash Goel4e046322014-04-04 17:14:38 +05308560 /* WaDisable_RenderCache_OperationalFlush:vlv */
8561 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8562
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008563 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008564 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8565 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8566
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008567 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008568 I915_WRITE(GEN7_ROW_CHICKEN2,
8569 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8570
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008571 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008572 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8573 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8574 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8575
Ville Syrjälä46680e02014-01-22 21:33:01 +02008576 gen7_setup_fixed_func_scheduler(dev_priv);
8577
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008578 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008579 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008580 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008581 */
8582 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008583 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008584
Akash Goelc98f5062014-03-24 23:00:07 +05308585 /* WaDisableL3Bank2xClockGate:vlv
8586 * Disabling L3 clock gating- MMIO 940c[25] = 1
8587 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8588 I915_WRITE(GEN7_UCGCTL4,
8589 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008590
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008591 /*
8592 * BSpec says this must be set, even though
8593 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8594 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008595 I915_WRITE(CACHE_MODE_1,
8596 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008597
8598 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008599 * BSpec recommends 8x4 when MSAA is used,
8600 * however in practice 16x4 seems fastest.
8601 *
8602 * Note that PS/WM thread counts depend on the WIZ hashing
8603 * disable bit, which we don't touch here, but it's good
8604 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8605 */
8606 I915_WRITE(GEN7_GT_MODE,
8607 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8608
8609 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008610 * WaIncreaseL3CreditsForVLVB0:vlv
8611 * This is the hardware default actually.
8612 */
8613 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8614
8615 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008616 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008617 * Disable clock gating on th GCFG unit to prevent a delay
8618 * in the reporting of vblank events.
8619 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008620 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008621}
8622
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008623static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008624{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008625 /* WaVSRefCountFullforceMissDisable:chv */
8626 /* WaDSRefCountFullforceMissDisable:chv */
8627 I915_WRITE(GEN7_FF_THREAD_MODE,
8628 I915_READ(GEN7_FF_THREAD_MODE) &
8629 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008630
8631 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8632 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8633 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008634
8635 /* WaDisableCSUnitClockGating:chv */
8636 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8637 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008638
8639 /* WaDisableSDEUnitClockGating:chv */
8640 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8641 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008642
8643 /*
Imre Deak450174f2016-05-03 15:54:21 +03008644 * WaProgramL3SqcReg1Default:chv
8645 * See gfxspecs/Related Documents/Performance Guide/
8646 * LSQC Setting Recommendations.
8647 */
8648 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8649
8650 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008651 * GTT cache may not work with big pages, so if those
8652 * are ever enabled GTT cache may need to be disabled.
8653 */
8654 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008655}
8656
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008657static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008658{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008659 uint32_t dspclk_gate;
8660
8661 I915_WRITE(RENCLK_GATE_D1, 0);
8662 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8663 GS_UNIT_CLOCK_GATE_DISABLE |
8664 CL_UNIT_CLOCK_GATE_DISABLE);
8665 I915_WRITE(RAMCLK_GATE_D, 0);
8666 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8667 OVRUNIT_CLOCK_GATE_DISABLE |
8668 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008669 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008670 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8671 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008672
8673 /* WaDisableRenderCachePipelinedFlush */
8674 I915_WRITE(CACHE_MODE_0,
8675 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008676
Akash Goel4e046322014-04-04 17:14:38 +05308677 /* WaDisable_RenderCache_OperationalFlush:g4x */
8678 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8679
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008680 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008681}
8682
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008683static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008684{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008685 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8686 I915_WRITE(RENCLK_GATE_D2, 0);
8687 I915_WRITE(DSPCLK_GATE_D, 0);
8688 I915_WRITE(RAMCLK_GATE_D, 0);
8689 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008690 I915_WRITE(MI_ARB_STATE,
8691 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308692
8693 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8694 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008695}
8696
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008697static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008698{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008699 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8700 I965_RCC_CLOCK_GATE_DISABLE |
8701 I965_RCPB_CLOCK_GATE_DISABLE |
8702 I965_ISC_CLOCK_GATE_DISABLE |
8703 I965_FBC_CLOCK_GATE_DISABLE);
8704 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008705 I915_WRITE(MI_ARB_STATE,
8706 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308707
8708 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8709 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008710}
8711
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008712static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008713{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008714 u32 dstate = I915_READ(D_STATE);
8715
8716 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8717 DSTATE_DOT_CLOCK_GATING;
8718 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008719
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008720 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008721 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008722
8723 /* IIR "flip pending" means done if this bit is set */
8724 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008725
8726 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008727 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008728
8729 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8730 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008731
8732 I915_WRITE(MI_ARB_STATE,
8733 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008734}
8735
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008736static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008737{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008738 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008739
8740 /* interrupts should cause a wake up from C3 */
8741 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8742 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008743
8744 I915_WRITE(MEM_MODE,
8745 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008746}
8747
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008748static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008749{
Ville Syrjälä10383922014-08-15 01:21:54 +03008750 I915_WRITE(MEM_MODE,
8751 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8752 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008753}
8754
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008755void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008756{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008757 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008758}
8759
Ville Syrjälä712bf362016-10-31 22:37:23 +02008760void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008761{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008762 if (HAS_PCH_LPT(dev_priv))
8763 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008764}
8765
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008766static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008767{
8768 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8769}
8770
8771/**
8772 * intel_init_clock_gating_hooks - setup the clock gating hooks
8773 * @dev_priv: device private
8774 *
8775 * Setup the hooks that configure which clocks of a given platform can be
8776 * gated and also apply various GT and display specific workarounds for these
8777 * platforms. Note that some GT specific workarounds are applied separately
8778 * when GPU contexts or batchbuffers start their execution.
8779 */
8780void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8781{
8782 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008783 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Rodrigo Vivi82525c12017-06-08 08:50:00 -07008784 else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008785 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008786 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008787 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008788 else if (IS_GEMINILAKE(dev_priv))
8789 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008790 else if (IS_BROADWELL(dev_priv))
8791 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
8792 else if (IS_CHERRYVIEW(dev_priv))
8793 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
8794 else if (IS_HASWELL(dev_priv))
8795 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
8796 else if (IS_IVYBRIDGE(dev_priv))
8797 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
8798 else if (IS_VALLEYVIEW(dev_priv))
8799 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
8800 else if (IS_GEN6(dev_priv))
8801 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8802 else if (IS_GEN5(dev_priv))
8803 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
8804 else if (IS_G4X(dev_priv))
8805 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008806 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008807 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008808 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008809 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
8810 else if (IS_GEN3(dev_priv))
8811 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8812 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8813 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8814 else if (IS_GEN2(dev_priv))
8815 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8816 else {
8817 MISSING_CASE(INTEL_DEVID(dev_priv));
8818 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8819 }
8820}
8821
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008822/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008823void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008824{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008825 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008826
Daniel Vetterc921aba2012-04-26 23:28:17 +02008827 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008828 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008829 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008830 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008831 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008832
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008833 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008834 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008835 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008836 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008837 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008838 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008839 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008840 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008841
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008842 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008843 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008844 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008845 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008846 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008847 dev_priv->display.compute_intermediate_wm =
8848 ilk_compute_intermediate_wm;
8849 dev_priv->display.initial_watermarks =
8850 ilk_initial_watermarks;
8851 dev_priv->display.optimize_watermarks =
8852 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008853 } else {
8854 DRM_DEBUG_KMS("Failed to read display plane latency. "
8855 "Disable CxSR\n");
8856 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008857 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008858 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008859 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008860 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008861 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008862 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008863 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008864 } else if (IS_G4X(dev_priv)) {
8865 g4x_setup_wm_latency(dev_priv);
8866 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8867 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8868 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8869 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008870 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008871 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008872 dev_priv->is_ddr3,
8873 dev_priv->fsb_freq,
8874 dev_priv->mem_freq)) {
8875 DRM_INFO("failed to find known CxSR latency "
8876 "(found ddr%s fsb freq %d, mem freq %d), "
8877 "disabling CxSR\n",
8878 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8879 dev_priv->fsb_freq, dev_priv->mem_freq);
8880 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008881 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008882 dev_priv->display.update_wm = NULL;
8883 } else
8884 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008885 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008886 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008887 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008888 dev_priv->display.update_wm = i9xx_update_wm;
8889 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008890 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008891 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008892 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008893 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008894 } else {
8895 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008896 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008897 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008898 } else {
8899 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008900 }
8901}
8902
Lyude87660502016-08-17 15:55:53 -04008903static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8904{
8905 uint32_t flags =
8906 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8907
8908 switch (flags) {
8909 case GEN6_PCODE_SUCCESS:
8910 return 0;
8911 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8912 case GEN6_PCODE_ILLEGAL_CMD:
8913 return -ENXIO;
8914 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008915 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008916 return -EOVERFLOW;
8917 case GEN6_PCODE_TIMEOUT:
8918 return -ETIMEDOUT;
8919 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00008920 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04008921 return 0;
8922 }
8923}
8924
8925static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8926{
8927 uint32_t flags =
8928 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8929
8930 switch (flags) {
8931 case GEN6_PCODE_SUCCESS:
8932 return 0;
8933 case GEN6_PCODE_ILLEGAL_CMD:
8934 return -ENXIO;
8935 case GEN7_PCODE_TIMEOUT:
8936 return -ETIMEDOUT;
8937 case GEN7_PCODE_ILLEGAL_DATA:
8938 return -EINVAL;
8939 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8940 return -EOVERFLOW;
8941 default:
8942 MISSING_CASE(flags);
8943 return 0;
8944 }
8945}
8946
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008947int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008948{
Lyude87660502016-08-17 15:55:53 -04008949 int status;
8950
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008951 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008952
Chris Wilson3f5582d2016-06-30 15:32:45 +01008953 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8954 * use te fw I915_READ variants to reduce the amount of work
8955 * required when reading/writing.
8956 */
8957
8958 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008959 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8960 return -EAGAIN;
8961 }
8962
Chris Wilson3f5582d2016-06-30 15:32:45 +01008963 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8964 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8965 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008966
Chris Wilsone09a3032017-04-11 11:13:39 +01008967 if (__intel_wait_for_register_fw(dev_priv,
8968 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8969 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008970 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8971 return -ETIMEDOUT;
8972 }
8973
Chris Wilson3f5582d2016-06-30 15:32:45 +01008974 *val = I915_READ_FW(GEN6_PCODE_DATA);
8975 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008976
Lyude87660502016-08-17 15:55:53 -04008977 if (INTEL_GEN(dev_priv) > 6)
8978 status = gen7_check_mailbox_status(dev_priv);
8979 else
8980 status = gen6_check_mailbox_status(dev_priv);
8981
8982 if (status) {
8983 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8984 status);
8985 return status;
8986 }
8987
Ben Widawsky42c05262012-09-26 10:34:00 -07008988 return 0;
8989}
8990
Chris Wilson3f5582d2016-06-30 15:32:45 +01008991int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008992 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008993{
Lyude87660502016-08-17 15:55:53 -04008994 int status;
8995
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008996 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008997
Chris Wilson3f5582d2016-06-30 15:32:45 +01008998 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8999 * use te fw I915_READ variants to reduce the amount of work
9000 * required when reading/writing.
9001 */
9002
9003 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07009004 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
9005 return -EAGAIN;
9006 }
9007
Chris Wilson3f5582d2016-06-30 15:32:45 +01009008 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02009009 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01009010 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009011
Chris Wilsone09a3032017-04-11 11:13:39 +01009012 if (__intel_wait_for_register_fw(dev_priv,
9013 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9014 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07009015 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
9016 return -ETIMEDOUT;
9017 }
9018
Chris Wilson3f5582d2016-06-30 15:32:45 +01009019 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009020
Lyude87660502016-08-17 15:55:53 -04009021 if (INTEL_GEN(dev_priv) > 6)
9022 status = gen7_check_mailbox_status(dev_priv);
9023 else
9024 status = gen6_check_mailbox_status(dev_priv);
9025
9026 if (status) {
9027 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
9028 status);
9029 return status;
9030 }
9031
Ben Widawsky42c05262012-09-26 10:34:00 -07009032 return 0;
9033}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07009034
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009035static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
9036 u32 request, u32 reply_mask, u32 reply,
9037 u32 *status)
9038{
9039 u32 val = request;
9040
9041 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
9042
9043 return *status || ((val & reply_mask) == reply);
9044}
9045
9046/**
9047 * skl_pcode_request - send PCODE request until acknowledgment
9048 * @dev_priv: device private
9049 * @mbox: PCODE mailbox ID the request is targeted for
9050 * @request: request ID
9051 * @reply_mask: mask used to check for request acknowledgment
9052 * @reply: value used to check for request acknowledgment
9053 * @timeout_base_ms: timeout for polling with preemption enabled
9054 *
9055 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02009056 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009057 * The request is acknowledged once the PCODE reply dword equals @reply after
9058 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02009059 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009060 * preemption disabled.
9061 *
9062 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
9063 * other error as reported by PCODE.
9064 */
9065int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
9066 u32 reply_mask, u32 reply, int timeout_base_ms)
9067{
9068 u32 status;
9069 int ret;
9070
9071 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
9072
9073#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9074 &status)
9075
9076 /*
9077 * Prime the PCODE by doing a request first. Normally it guarantees
9078 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9079 * _wait_for() doesn't guarantee when its passed condition is evaluated
9080 * first, so send the first request explicitly.
9081 */
9082 if (COND) {
9083 ret = 0;
9084 goto out;
9085 }
9086 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
9087 if (!ret)
9088 goto out;
9089
9090 /*
9091 * The above can time out if the number of requests was low (2 in the
9092 * worst case) _and_ PCODE was busy for some reason even after a
9093 * (queued) request and @timeout_base_ms delay. As a workaround retry
9094 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009095 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009096 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009097 * requests, and for any quirks of the PCODE firmware that delays
9098 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009099 */
9100 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9101 WARN_ON_ONCE(timeout_base_ms > 3);
9102 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009103 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009104 preempt_enable();
9105
9106out:
9107 return ret ? ret : status;
9108#undef COND
9109}
9110
Ville Syrjälädd06f882014-11-10 22:55:12 +02009111static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9112{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009113 /*
9114 * N = val - 0xb7
9115 * Slow = Fast = GPLL ref * N
9116 */
9117 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009118}
9119
Fengguang Wub55dd642014-07-12 11:21:39 +02009120static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009121{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009122 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009123}
9124
Fengguang Wub55dd642014-07-12 11:21:39 +02009125static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309126{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009127 /*
9128 * N = val / 2
9129 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9130 */
9131 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309132}
9133
Fengguang Wub55dd642014-07-12 11:21:39 +02009134static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309135{
Ville Syrjälä1c147622014-08-18 14:42:43 +03009136 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009137 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309138}
9139
Ville Syrjälä616bc822015-01-23 21:04:25 +02009140int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9141{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009142 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009143 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9144 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009145 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009146 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009147 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009148 return byt_gpu_freq(dev_priv, val);
9149 else
9150 return val * GT_FREQUENCY_MULTIPLIER;
9151}
9152
Ville Syrjälä616bc822015-01-23 21:04:25 +02009153int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9154{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009155 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009156 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9157 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009158 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009159 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009160 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009161 return byt_freq_opcode(dev_priv, val);
9162 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009163 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309164}
9165
Chris Wilson6ad790c2015-04-07 16:20:31 +01009166struct request_boost {
9167 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02009168 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01009169};
9170
9171static void __intel_rps_boost_work(struct work_struct *work)
9172{
9173 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01009174 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01009175
Chris Wilsonf69a02c2016-07-01 17:23:16 +01009176 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01009177 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009178
Chris Wilsone8a261e2016-07-20 13:31:49 +01009179 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009180 kfree(boost);
9181}
9182
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009183void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01009184{
9185 struct request_boost *boost;
9186
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009187 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01009188 return;
9189
Chris Wilsonf69a02c2016-07-01 17:23:16 +01009190 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01009191 return;
9192
Chris Wilson6ad790c2015-04-07 16:20:31 +01009193 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
9194 if (boost == NULL)
9195 return;
9196
Chris Wilsone8a261e2016-07-20 13:31:49 +01009197 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009198
9199 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009200 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009201}
9202
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009203void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009204{
Daniel Vetterf742a552013-12-06 10:17:53 +01009205 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01009206 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01009207
Chris Wilson54b4f682016-07-21 21:16:19 +01009208 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9209 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01009210 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009211
Paulo Zanoni33688d92014-03-07 20:08:19 -03009212 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009213 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009214}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009215
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009216static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9217 const i915_reg_t reg)
9218{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009219 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009220 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009221
9222 /* The register accessed do not need forcewake. We borrow
9223 * uncore lock to prevent concurrent access to range reg.
9224 */
9225 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009226
9227 /* vlv and chv residency counters are 40 bits in width.
9228 * With a control bit, we can choose between upper or lower
9229 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009230 *
9231 * Although we always use the counter in high-range mode elsewhere,
9232 * userspace may attempt to read the value before rc6 is initialised,
9233 * before we have set the default VLV_COUNTER_CONTROL value. So always
9234 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009235 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009236 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9237 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009238 upper = I915_READ_FW(reg);
9239 do {
9240 tmp = upper;
9241
9242 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9243 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9244 lower = I915_READ_FW(reg);
9245
9246 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9247 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9248 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009249 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009250
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009251 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9252 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9253 * now.
9254 */
9255
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009256 spin_unlock_irq(&dev_priv->uncore.lock);
9257
9258 return lower | (u64)upper << 8;
9259}
9260
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009261u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9262 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009263{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009264 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009265
9266 if (!intel_enable_rc6())
9267 return 0;
9268
9269 intel_runtime_pm_get(dev_priv);
9270
9271 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9272 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009273 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009274 div = dev_priv->czclk_freq;
9275
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009276 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009277 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009278 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009279 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009280
9281 time_hw = I915_READ(reg);
9282 } else {
9283 units = 128000; /* 1.28us */
9284 div = 100000;
9285
9286 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009287 }
9288
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009289 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009290 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009291}