blob: 6bed2ed1457493175ef09498eb9bbc39f6204bc6 [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>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010029#include <linux/module.h>
Chris Wilson08ea70a2018-08-12 23:36:31 +010030#include <linux/pm_runtime.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010031
32#include <drm/drm_atomic_helper.h>
33#include <drm/drm_fourcc.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070034#include <drm/drm_plane_helper.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010035
Jani Nikuladf0566a2019-06-13 11:44:16 +030036#include "display/intel_atomic.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030037#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030038#include "display/intel_fbc.h"
39#include "display/intel_sprite.h"
40
Eugeni Dodonov85208be2012-04-16 22:20:34 -030041#include "i915_drv.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030042#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030043#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030044#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010045#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020046#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030047
Ben Widawskydc39fff2013-10-18 12:32:07 -070048/**
Jani Nikula18afd442016-01-18 09:19:48 +020049 * DOC: RC6
50 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070051 * RC6 is a special power stage which allows the GPU to enter an very
52 * low-voltage mode when idle, using down to 0V while at this stage. This
53 * stage is entered automatically when the GPU is idle when RC6 support is
54 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
55 *
56 * There are different RC6 modes available in Intel GPU, which differentiate
57 * among each other with the latency required to enter and leave RC6 and
58 * voltage consumed by the GPU in different states.
59 *
60 * The combination of the following flags define which states GPU is allowed
61 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
62 * RC6pp is deepest RC6. Their support by hardware varies according to the
63 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
64 * which brings the most power savings; deeper states save more power, but
65 * require higher latency to switch to and wake up.
66 */
Ben Widawskydc39fff2013-10-18 12:32:07 -070067
Ville Syrjälä46f16e62016-10-31 22:37:22 +020068static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030069{
Ville Syrjälä93564042017-08-24 22:10:51 +030070 if (HAS_LLC(dev_priv)) {
71 /*
72 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080073 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030074 *
75 * Must match Sampler, Pixel Back End, and Media. See
76 * WaCompressedResourceSamplerPbeMediaNewHashMode.
77 */
78 I915_WRITE(CHICKEN_PAR1_1,
79 I915_READ(CHICKEN_PAR1_1) |
80 SKL_DE_COMPRESSED_HASH_MODE);
81 }
82
Rodrigo Vivi82525c12017-06-08 08:50:00 -070083 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030084 I915_WRITE(CHICKEN_PAR1_1,
85 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
86
Rodrigo Vivi82525c12017-06-08 08:50:00 -070087 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030088 I915_WRITE(GEN8_CHICKEN_DCPR_1,
89 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030090
Rodrigo Vivi82525c12017-06-08 08:50:00 -070091 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
92 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030093 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
94 DISP_FBC_WM_DIS |
95 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030096
Rodrigo Vivi82525c12017-06-08 08:50:00 -070097 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030098 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
99 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +0530100
101 if (IS_SKYLAKE(dev_priv)) {
102 /* WaDisableDopClockGating */
103 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
104 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
105 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300106}
107
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200108static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200109{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200110 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200111
Nick Hoatha7546152015-06-29 14:07:32 +0100112 /* WaDisableSDEUnitClockGating:bxt */
113 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
114 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
115
Imre Deak32608ca2015-03-11 11:10:27 +0200116 /*
117 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200118 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200119 */
Imre Deak32608ca2015-03-11 11:10:27 +0200120 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200121 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200122
123 /*
124 * Wa: Backlight PWM may stop in the asserted state, causing backlight
125 * to stay fully on.
126 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200127 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
128 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200129}
130
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200131static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
132{
133 gen9_init_clock_gating(dev_priv);
134
135 /*
136 * WaDisablePWMClockGating:glk
137 * Backlight PWM may stop in the asserted state, causing backlight
138 * to stay fully on.
139 */
140 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
141 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200142
143 /* WaDDIIOTimeout:glk */
144 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
145 u32 val = I915_READ(CHICKEN_MISC_2);
146 val &= ~(GLK_CL0_PWR_DOWN |
147 GLK_CL1_PWR_DOWN |
148 GLK_CL2_PWR_DOWN);
149 I915_WRITE(CHICKEN_MISC_2, val);
150 }
151
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200152}
153
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200154static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200155{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200156 u32 tmp;
157
158 tmp = I915_READ(CLKCFG);
159
160 switch (tmp & CLKCFG_FSB_MASK) {
161 case CLKCFG_FSB_533:
162 dev_priv->fsb_freq = 533; /* 133*4 */
163 break;
164 case CLKCFG_FSB_800:
165 dev_priv->fsb_freq = 800; /* 200*4 */
166 break;
167 case CLKCFG_FSB_667:
168 dev_priv->fsb_freq = 667; /* 167*4 */
169 break;
170 case CLKCFG_FSB_400:
171 dev_priv->fsb_freq = 400; /* 100*4 */
172 break;
173 }
174
175 switch (tmp & CLKCFG_MEM_MASK) {
176 case CLKCFG_MEM_533:
177 dev_priv->mem_freq = 533;
178 break;
179 case CLKCFG_MEM_667:
180 dev_priv->mem_freq = 667;
181 break;
182 case CLKCFG_MEM_800:
183 dev_priv->mem_freq = 800;
184 break;
185 }
186
187 /* detect pineview DDR3 setting */
188 tmp = I915_READ(CSHRDDR3CTL);
189 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
190}
191
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200192static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200193{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200194 u16 ddrpll, csipll;
195
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100196 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
197 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200198
199 switch (ddrpll & 0xff) {
200 case 0xc:
201 dev_priv->mem_freq = 800;
202 break;
203 case 0x10:
204 dev_priv->mem_freq = 1066;
205 break;
206 case 0x14:
207 dev_priv->mem_freq = 1333;
208 break;
209 case 0x18:
210 dev_priv->mem_freq = 1600;
211 break;
212 default:
213 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
214 ddrpll & 0xff);
215 dev_priv->mem_freq = 0;
216 break;
217 }
218
Daniel Vetter20e4d402012-08-08 23:35:39 +0200219 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200220
221 switch (csipll & 0x3ff) {
222 case 0x00c:
223 dev_priv->fsb_freq = 3200;
224 break;
225 case 0x00e:
226 dev_priv->fsb_freq = 3733;
227 break;
228 case 0x010:
229 dev_priv->fsb_freq = 4266;
230 break;
231 case 0x012:
232 dev_priv->fsb_freq = 4800;
233 break;
234 case 0x014:
235 dev_priv->fsb_freq = 5333;
236 break;
237 case 0x016:
238 dev_priv->fsb_freq = 5866;
239 break;
240 case 0x018:
241 dev_priv->fsb_freq = 6400;
242 break;
243 default:
244 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
245 csipll & 0x3ff);
246 dev_priv->fsb_freq = 0;
247 break;
248 }
249
250 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200251 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200252 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200253 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200254 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200255 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200256 }
257}
258
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300259static const struct cxsr_latency cxsr_latency_table[] = {
260 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
261 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
262 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
263 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
264 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
265
266 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
267 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
268 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
269 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
270 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
271
272 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
273 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
274 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
275 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
276 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
277
278 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
279 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
280 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
281 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
282 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
283
284 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
285 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
286 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
287 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
288 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
289
290 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
291 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
292 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
293 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
294 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
295};
296
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100297static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
298 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300299 int fsb,
300 int mem)
301{
302 const struct cxsr_latency *latency;
303 int i;
304
305 if (fsb == 0 || mem == 0)
306 return NULL;
307
308 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
309 latency = &cxsr_latency_table[i];
310 if (is_desktop == latency->is_desktop &&
311 is_ddr3 == latency->is_ddr3 &&
312 fsb == latency->fsb_freq && mem == latency->mem_freq)
313 return latency;
314 }
315
316 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
317
318 return NULL;
319}
320
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200321static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
322{
323 u32 val;
324
Chris Wilson337fa6e2019-04-26 09:17:20 +0100325 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200326
327 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
328 if (enable)
329 val &= ~FORCE_DDR_HIGH_FREQ;
330 else
331 val |= FORCE_DDR_HIGH_FREQ;
332 val &= ~FORCE_DDR_LOW_FREQ;
333 val |= FORCE_DDR_FREQ_REQ_ACK;
334 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
335
336 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
337 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
338 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
339
Chris Wilson337fa6e2019-04-26 09:17:20 +0100340 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200341}
342
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200343static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
344{
345 u32 val;
346
Chris Wilson337fa6e2019-04-26 09:17:20 +0100347 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200348
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200349 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200350 if (enable)
351 val |= DSP_MAXFIFO_PM5_ENABLE;
352 else
353 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200354 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200355
Chris Wilson337fa6e2019-04-26 09:17:20 +0100356 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200357}
358
Ville Syrjäläf4998962015-03-10 17:02:21 +0200359#define FW_WM(value, plane) \
360 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
361
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200362static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300363{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200364 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300365 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300366
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100367 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200368 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300369 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300370 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200371 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200372 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300373 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300374 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200375 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200376 val = I915_READ(DSPFW3);
377 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
378 if (enable)
379 val |= PINEVIEW_SELF_REFRESH_EN;
380 else
381 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300382 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300383 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100384 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200385 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300386 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
387 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
388 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300389 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100390 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300391 /*
392 * FIXME can't find a bit like this for 915G, and
393 * and yet it does have the related watermark in
394 * FW_BLC_SELF. What's going on?
395 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200396 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300397 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
398 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
399 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300400 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300401 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200402 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300403 }
404
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200405 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
406
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200407 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
408 enableddisabled(enable),
409 enableddisabled(was_enabled));
410
411 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300412}
413
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300414/**
415 * intel_set_memory_cxsr - Configure CxSR state
416 * @dev_priv: i915 device
417 * @enable: Allow vs. disallow CxSR
418 *
419 * Allow or disallow the system to enter a special CxSR
420 * (C-state self refresh) state. What typically happens in CxSR mode
421 * is that several display FIFOs may get combined into a single larger
422 * FIFO for a particular plane (so called max FIFO mode) to allow the
423 * system to defer memory fetches longer, and the memory will enter
424 * self refresh.
425 *
426 * Note that enabling CxSR does not guarantee that the system enter
427 * this special mode, nor does it guarantee that the system stays
428 * in that mode once entered. So this just allows/disallows the system
429 * to autonomously utilize the CxSR mode. Other factors such as core
430 * C-states will affect when/if the system actually enters/exits the
431 * CxSR mode.
432 *
433 * Note that on VLV/CHV this actually only controls the max FIFO mode,
434 * and the system is free to enter/exit memory self refresh at any time
435 * even when the use of CxSR has been disallowed.
436 *
437 * While the system is actually in the CxSR/max FIFO mode, some plane
438 * control registers will not get latched on vblank. Thus in order to
439 * guarantee the system will respond to changes in the plane registers
440 * we must always disallow CxSR prior to making changes to those registers.
441 * Unfortunately the system will re-evaluate the CxSR conditions at
442 * frame start which happens after vblank start (which is when the plane
443 * registers would get latched), so we can't proceed with the plane update
444 * during the same frame where we disallowed CxSR.
445 *
446 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
447 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
448 * the hardware w.r.t. HPLL SR when writing to plane registers.
449 * Disallowing just CxSR is sufficient.
450 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200451bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200452{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200453 bool ret;
454
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200455 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200456 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300457 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
458 dev_priv->wm.vlv.cxsr = enable;
459 else if (IS_G4X(dev_priv))
460 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200461 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200462
463 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200464}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200465
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300466/*
467 * Latency for FIFO fetches is dependent on several factors:
468 * - memory configuration (speed, channels)
469 * - chipset
470 * - current MCH state
471 * It can be fairly high in some situations, so here we assume a fairly
472 * pessimal value. It's a tradeoff between extra memory fetches (if we
473 * set this value too high, the FIFO will fetch frequently to stay full)
474 * and power consumption (set it too low to save power and we might see
475 * FIFO underruns and display "flicker").
476 *
477 * A value of 5us seems to be a good balance; safe for very low end
478 * platforms but not overly aggressive on lower latency configs.
479 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100480static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300481
Ville Syrjäläb5004722015-03-05 21:19:47 +0200482#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
483 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
484
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200485static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200486{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200487 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200488 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200489 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200490 enum pipe pipe = crtc->pipe;
491 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200492
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200493 switch (pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200494 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200495 case PIPE_A:
496 dsparb = I915_READ(DSPARB);
497 dsparb2 = I915_READ(DSPARB2);
498 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
499 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
500 break;
501 case PIPE_B:
502 dsparb = I915_READ(DSPARB);
503 dsparb2 = I915_READ(DSPARB2);
504 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
505 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
506 break;
507 case PIPE_C:
508 dsparb2 = I915_READ(DSPARB2);
509 dsparb3 = I915_READ(DSPARB3);
510 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
511 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
512 break;
513 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200514 MISSING_CASE(pipe);
515 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200516 }
517
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200518 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
519 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
520 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
521 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200522}
523
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200524static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
525 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200527 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300528 int size;
529
530 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200531 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300532 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
533
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200534 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
535 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300536
537 return size;
538}
539
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200540static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
541 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200543 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300544 int size;
545
546 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200547 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300548 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
549 size >>= 1; /* Convert to cachelines */
550
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200551 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
552 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300553
554 return size;
555}
556
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200557static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
558 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200560 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300561 int size;
562
563 size = dsparb & 0x7f;
564 size >>= 2; /* Convert to cachelines */
565
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200566 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
567 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300568
569 return size;
570}
571
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300572/* Pineview has different values for various configs */
573static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300574 .fifo_size = PINEVIEW_DISPLAY_FIFO,
575 .max_wm = PINEVIEW_MAX_WM,
576 .default_wm = PINEVIEW_DFT_WM,
577 .guard_size = PINEVIEW_GUARD_WM,
578 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300579};
580static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300581 .fifo_size = PINEVIEW_DISPLAY_FIFO,
582 .max_wm = PINEVIEW_MAX_WM,
583 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
584 .guard_size = PINEVIEW_GUARD_WM,
585 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300586};
587static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300588 .fifo_size = PINEVIEW_CURSOR_FIFO,
589 .max_wm = PINEVIEW_CURSOR_MAX_WM,
590 .default_wm = PINEVIEW_CURSOR_DFT_WM,
591 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
592 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300593};
594static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300595 .fifo_size = PINEVIEW_CURSOR_FIFO,
596 .max_wm = PINEVIEW_CURSOR_MAX_WM,
597 .default_wm = PINEVIEW_CURSOR_DFT_WM,
598 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
599 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300602 .fifo_size = I965_CURSOR_FIFO,
603 .max_wm = I965_CURSOR_MAX_WM,
604 .default_wm = I965_CURSOR_DFT_WM,
605 .guard_size = 2,
606 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300607};
608static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300609 .fifo_size = I945_FIFO_SIZE,
610 .max_wm = I915_MAX_WM,
611 .default_wm = 1,
612 .guard_size = 2,
613 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300614};
615static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300616 .fifo_size = I915_FIFO_SIZE,
617 .max_wm = I915_MAX_WM,
618 .default_wm = 1,
619 .guard_size = 2,
620 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300621};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300622static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300623 .fifo_size = I855GM_FIFO_SIZE,
624 .max_wm = I915_MAX_WM,
625 .default_wm = 1,
626 .guard_size = 2,
627 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300628};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300629static const struct intel_watermark_params i830_bc_wm_info = {
630 .fifo_size = I855GM_FIFO_SIZE,
631 .max_wm = I915_MAX_WM/2,
632 .default_wm = 1,
633 .guard_size = 2,
634 .cacheline_size = I830_FIFO_LINE_SIZE,
635};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200636static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300637 .fifo_size = I830_FIFO_SIZE,
638 .max_wm = I915_MAX_WM,
639 .default_wm = 1,
640 .guard_size = 2,
641 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300642};
643
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300644/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300645 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
646 * @pixel_rate: Pipe pixel rate in kHz
647 * @cpp: Plane bytes per pixel
648 * @latency: Memory wakeup latency in 0.1us units
649 *
650 * Compute the watermark using the method 1 or "small buffer"
651 * formula. The caller may additonally add extra cachelines
652 * to account for TLB misses and clock crossings.
653 *
654 * This method is concerned with the short term drain rate
655 * of the FIFO, ie. it does not account for blanking periods
656 * which would effectively reduce the average drain rate across
657 * a longer period. The name "small" refers to the fact the
658 * FIFO is relatively small compared to the amount of data
659 * fetched.
660 *
661 * The FIFO level vs. time graph might look something like:
662 *
663 * |\ |\
664 * | \ | \
665 * __---__---__ (- plane active, _ blanking)
666 * -> time
667 *
668 * or perhaps like this:
669 *
670 * |\|\ |\|\
671 * __----__----__ (- plane active, _ blanking)
672 * -> time
673 *
674 * Returns:
675 * The watermark in bytes
676 */
677static unsigned int intel_wm_method1(unsigned int pixel_rate,
678 unsigned int cpp,
679 unsigned int latency)
680{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200681 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300682
Ville Syrjäläd492a292019-04-08 18:27:01 +0300683 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300684 ret = DIV_ROUND_UP_ULL(ret, 10000);
685
686 return ret;
687}
688
689/**
690 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
691 * @pixel_rate: Pipe pixel rate in kHz
692 * @htotal: Pipe horizontal total
693 * @width: Plane width in pixels
694 * @cpp: Plane bytes per pixel
695 * @latency: Memory wakeup latency in 0.1us units
696 *
697 * Compute the watermark using the method 2 or "large buffer"
698 * formula. The caller may additonally add extra cachelines
699 * to account for TLB misses and clock crossings.
700 *
701 * This method is concerned with the long term drain rate
702 * of the FIFO, ie. it does account for blanking periods
703 * which effectively reduce the average drain rate across
704 * a longer period. The name "large" refers to the fact the
705 * FIFO is relatively large compared to the amount of data
706 * fetched.
707 *
708 * The FIFO level vs. time graph might look something like:
709 *
710 * |\___ |\___
711 * | \___ | \___
712 * | \ | \
713 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
714 * -> time
715 *
716 * Returns:
717 * The watermark in bytes
718 */
719static unsigned int intel_wm_method2(unsigned int pixel_rate,
720 unsigned int htotal,
721 unsigned int width,
722 unsigned int cpp,
723 unsigned int latency)
724{
725 unsigned int ret;
726
727 /*
728 * FIXME remove once all users are computing
729 * watermarks in the correct place.
730 */
731 if (WARN_ON_ONCE(htotal == 0))
732 htotal = 1;
733
734 ret = (latency * pixel_rate) / (htotal * 10000);
735 ret = (ret + 1) * width * cpp;
736
737 return ret;
738}
739
740/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300741 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300742 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300743 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000744 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200745 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746 * @latency_ns: memory latency for the platform
747 *
748 * Calculate the watermark level (the level at which the display plane will
749 * start fetching from memory again). Each chip has a different display
750 * FIFO size and allocation, so the caller needs to figure that out and pass
751 * in the correct intel_watermark_params structure.
752 *
753 * As the pixel clock runs, the FIFO will be drained at a rate that depends
754 * on the pixel size. When it reaches the watermark level, it'll start
755 * fetching FIFO line sized based chunks from memory until the FIFO fills
756 * past the watermark point. If the FIFO drains completely, a FIFO underrun
757 * will occur, and a display engine hang could result.
758 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300759static unsigned int intel_calculate_wm(int pixel_rate,
760 const struct intel_watermark_params *wm,
761 int fifo_size, int cpp,
762 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300763{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300764 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300765
766 /*
767 * Note: we need to make sure we don't overflow for various clock &
768 * latency values.
769 * clocks go from a few thousand to several hundred thousand.
770 * latency is usually a few thousand
771 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300772 entries = intel_wm_method1(pixel_rate, cpp,
773 latency_ns / 100);
774 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
775 wm->guard_size;
776 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300777
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300778 wm_size = fifo_size - entries;
779 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300780
781 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300782 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300783 wm_size = wm->max_wm;
784 if (wm_size <= 0)
785 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300786
787 /*
788 * Bspec seems to indicate that the value shouldn't be lower than
789 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
790 * Lets go for 8 which is the burst size since certain platforms
791 * already use a hardcoded 8 (which is what the spec says should be
792 * done).
793 */
794 if (wm_size <= 8)
795 wm_size = 8;
796
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300797 return wm_size;
798}
799
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300800static bool is_disabling(int old, int new, int threshold)
801{
802 return old >= threshold && new < threshold;
803}
804
805static bool is_enabling(int old, int new, int threshold)
806{
807 return old < threshold && new >= threshold;
808}
809
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300810static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
811{
812 return dev_priv->wm.max_level + 1;
813}
814
Ville Syrjälä24304d812017-03-14 17:10:49 +0200815static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
816 const struct intel_plane_state *plane_state)
817{
818 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
819
820 /* FIXME check the 'enable' instead */
821 if (!crtc_state->base.active)
822 return false;
823
824 /*
825 * Treat cursor with fb as always visible since cursor updates
826 * can happen faster than the vrefresh rate, and the current
827 * watermark code doesn't handle that correctly. Cursor updates
828 * which set/clear the fb or change the cursor size are going
829 * to get throttled by intel_legacy_cursor_update() to work
830 * around this problem with the watermark code.
831 */
832 if (plane->id == PLANE_CURSOR)
833 return plane_state->base.fb != NULL;
834 else
835 return plane_state->base.visible;
836}
837
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200838static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200840 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300841
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200842 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200843 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300844 if (enabled)
845 return NULL;
846 enabled = crtc;
847 }
848 }
849
850 return enabled;
851}
852
Ville Syrjälä432081b2016-10-31 22:37:03 +0200853static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300854{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200855 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200856 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300857 const struct cxsr_latency *latency;
858 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300859 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300860
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000861 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100862 dev_priv->is_ddr3,
863 dev_priv->fsb_freq,
864 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300865 if (!latency) {
866 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300867 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868 return;
869 }
870
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200871 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300872 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200873 const struct drm_display_mode *adjusted_mode =
874 &crtc->config->base.adjusted_mode;
875 const struct drm_framebuffer *fb =
876 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200877 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300878 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300879
880 /* Display SR */
881 wm = intel_calculate_wm(clock, &pineview_display_wm,
882 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200883 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884 reg = I915_READ(DSPFW1);
885 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200886 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887 I915_WRITE(DSPFW1, reg);
888 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
889
890 /* cursor SR */
891 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
892 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300893 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300894 reg = I915_READ(DSPFW3);
895 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200896 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300897 I915_WRITE(DSPFW3, reg);
898
899 /* Display HPLL off SR */
900 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
901 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200902 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300903 reg = I915_READ(DSPFW3);
904 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200905 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300906 I915_WRITE(DSPFW3, reg);
907
908 /* cursor HPLL off SR */
909 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
910 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300911 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300912 reg = I915_READ(DSPFW3);
913 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200914 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300915 I915_WRITE(DSPFW3, reg);
916 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
917
Imre Deak5209b1f2014-07-01 12:36:17 +0300918 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300919 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300920 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300921 }
922}
923
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300924/*
925 * Documentation says:
926 * "If the line size is small, the TLB fetches can get in the way of the
927 * data fetches, causing some lag in the pixel data return which is not
928 * accounted for in the above formulas. The following adjustment only
929 * needs to be applied if eight whole lines fit in the buffer at once.
930 * The WM is adjusted upwards by the difference between the FIFO size
931 * and the size of 8 whole lines. This adjustment is always performed
932 * in the actual pixel depth regardless of whether FBC is enabled or not."
933 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000934static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300935{
936 int tlb_miss = fifo_size * 64 - width * cpp * 8;
937
938 return max(0, tlb_miss);
939}
940
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300941static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
942 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300943{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300944 enum pipe pipe;
945
946 for_each_pipe(dev_priv, pipe)
947 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
948
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300949 I915_WRITE(DSPFW1,
950 FW_WM(wm->sr.plane, SR) |
951 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
952 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
953 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
954 I915_WRITE(DSPFW2,
955 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
956 FW_WM(wm->sr.fbc, FBC_SR) |
957 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
958 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
959 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
960 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
961 I915_WRITE(DSPFW3,
962 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
963 FW_WM(wm->sr.cursor, CURSOR_SR) |
964 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
965 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300966
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300967 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300968}
969
Ville Syrjälä15665972015-03-10 16:16:28 +0200970#define FW_WM_VLV(value, plane) \
971 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
972
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200973static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200974 const struct vlv_wm_values *wm)
975{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200976 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200977
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200978 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200979 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
980
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200981 I915_WRITE(VLV_DDL(pipe),
982 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
983 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
984 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
985 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
986 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200987
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200988 /*
989 * Zero the (unused) WM1 watermarks, and also clear all the
990 * high order bits so that there are no out of bounds values
991 * present in the registers during the reprogramming.
992 */
993 I915_WRITE(DSPHOWM, 0);
994 I915_WRITE(DSPHOWM1, 0);
995 I915_WRITE(DSPFW4, 0);
996 I915_WRITE(DSPFW5, 0);
997 I915_WRITE(DSPFW6, 0);
998
Ville Syrjäläae801522015-03-05 21:19:49 +0200999 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001000 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001001 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1002 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1003 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001004 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001005 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1006 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1007 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001008 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001009 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001010
1011 if (IS_CHERRYVIEW(dev_priv)) {
1012 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001013 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1014 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001015 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001016 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1017 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001018 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001019 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1020 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001021 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001022 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001023 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1024 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1025 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1026 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1027 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1028 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1029 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1030 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1031 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001032 } else {
1033 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001034 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1035 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001036 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001037 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001038 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1039 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1040 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1041 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1042 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1043 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001044 }
1045
1046 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001047}
1048
Ville Syrjälä15665972015-03-10 16:16:28 +02001049#undef FW_WM_VLV
1050
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001051static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1052{
1053 /* all latencies in usec */
1054 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1055 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001056 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001057
Ville Syrjälä79d94302017-04-21 21:14:30 +03001058 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001059}
1060
1061static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1062{
1063 /*
1064 * DSPCNTR[13] supposedly controls whether the
1065 * primary plane can use the FIFO space otherwise
1066 * reserved for the sprite plane. It's not 100% clear
1067 * what the actual FIFO size is, but it looks like we
1068 * can happily set both primary and sprite watermarks
1069 * up to 127 cachelines. So that would seem to mean
1070 * that either DSPCNTR[13] doesn't do anything, or that
1071 * the total FIFO is >= 256 cachelines in size. Either
1072 * way, we don't seem to have to worry about this
1073 * repartitioning as the maximum watermark value the
1074 * register can hold for each plane is lower than the
1075 * minimum FIFO size.
1076 */
1077 switch (plane_id) {
1078 case PLANE_CURSOR:
1079 return 63;
1080 case PLANE_PRIMARY:
1081 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1082 case PLANE_SPRITE0:
1083 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1084 default:
1085 MISSING_CASE(plane_id);
1086 return 0;
1087 }
1088}
1089
1090static int g4x_fbc_fifo_size(int level)
1091{
1092 switch (level) {
1093 case G4X_WM_LEVEL_SR:
1094 return 7;
1095 case G4X_WM_LEVEL_HPLL:
1096 return 15;
1097 default:
1098 MISSING_CASE(level);
1099 return 0;
1100 }
1101}
1102
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001103static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1104 const struct intel_plane_state *plane_state,
1105 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001106{
1107 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1108 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1109 const struct drm_display_mode *adjusted_mode =
1110 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001111 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1112 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001113
1114 if (latency == 0)
1115 return USHRT_MAX;
1116
1117 if (!intel_wm_plane_visible(crtc_state, plane_state))
1118 return 0;
1119
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001120 cpp = plane_state->base.fb->format->cpp[0];
1121
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001122 /*
1123 * Not 100% sure which way ELK should go here as the
1124 * spec only says CL/CTG should assume 32bpp and BW
1125 * doesn't need to. But as these things followed the
1126 * mobile vs. desktop lines on gen3 as well, let's
1127 * assume ELK doesn't need this.
1128 *
1129 * The spec also fails to list such a restriction for
1130 * the HPLL watermark, which seems a little strange.
1131 * Let's use 32bpp for the HPLL watermark as well.
1132 */
1133 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1134 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001135 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001136
1137 clock = adjusted_mode->crtc_clock;
1138 htotal = adjusted_mode->crtc_htotal;
1139
1140 if (plane->id == PLANE_CURSOR)
1141 width = plane_state->base.crtc_w;
1142 else
1143 width = drm_rect_width(&plane_state->base.dst);
1144
1145 if (plane->id == PLANE_CURSOR) {
1146 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1147 } else if (plane->id == PLANE_PRIMARY &&
1148 level == G4X_WM_LEVEL_NORMAL) {
1149 wm = intel_wm_method1(clock, cpp, latency);
1150 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001151 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001152
1153 small = intel_wm_method1(clock, cpp, latency);
1154 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1155
1156 wm = min(small, large);
1157 }
1158
1159 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1160 width, cpp);
1161
1162 wm = DIV_ROUND_UP(wm, 64) + 2;
1163
Chris Wilson1a1f1282017-11-07 14:03:38 +00001164 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001165}
1166
1167static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1168 int level, enum plane_id plane_id, u16 value)
1169{
1170 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1171 bool dirty = false;
1172
1173 for (; level < intel_wm_num_levels(dev_priv); level++) {
1174 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1175
1176 dirty |= raw->plane[plane_id] != value;
1177 raw->plane[plane_id] = value;
1178 }
1179
1180 return dirty;
1181}
1182
1183static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1184 int level, u16 value)
1185{
1186 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1187 bool dirty = false;
1188
1189 /* NORMAL level doesn't have an FBC watermark */
1190 level = max(level, G4X_WM_LEVEL_SR);
1191
1192 for (; level < intel_wm_num_levels(dev_priv); level++) {
1193 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1194
1195 dirty |= raw->fbc != value;
1196 raw->fbc = value;
1197 }
1198
1199 return dirty;
1200}
1201
Maarten Lankhorstec193642019-06-28 10:55:17 +02001202static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1203 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001204 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001205
1206static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1207 const struct intel_plane_state *plane_state)
1208{
1209 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1210 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1211 enum plane_id plane_id = plane->id;
1212 bool dirty = false;
1213 int level;
1214
1215 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1216 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1217 if (plane_id == PLANE_PRIMARY)
1218 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1219 goto out;
1220 }
1221
1222 for (level = 0; level < num_levels; level++) {
1223 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1224 int wm, max_wm;
1225
1226 wm = g4x_compute_wm(crtc_state, plane_state, level);
1227 max_wm = g4x_plane_fifo_size(plane_id, level);
1228
1229 if (wm > max_wm)
1230 break;
1231
1232 dirty |= raw->plane[plane_id] != wm;
1233 raw->plane[plane_id] = wm;
1234
1235 if (plane_id != PLANE_PRIMARY ||
1236 level == G4X_WM_LEVEL_NORMAL)
1237 continue;
1238
1239 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1240 raw->plane[plane_id]);
1241 max_wm = g4x_fbc_fifo_size(level);
1242
1243 /*
1244 * FBC wm is not mandatory as we
1245 * can always just disable its use.
1246 */
1247 if (wm > max_wm)
1248 wm = USHRT_MAX;
1249
1250 dirty |= raw->fbc != wm;
1251 raw->fbc = wm;
1252 }
1253
1254 /* mark watermarks as invalid */
1255 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1256
1257 if (plane_id == PLANE_PRIMARY)
1258 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1259
1260 out:
1261 if (dirty) {
1262 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1263 plane->base.name,
1264 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1265 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1266 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1267
1268 if (plane_id == PLANE_PRIMARY)
1269 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1270 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1271 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1272 }
1273
1274 return dirty;
1275}
1276
1277static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1278 enum plane_id plane_id, int level)
1279{
1280 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1281
1282 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1283}
1284
1285static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1286 int level)
1287{
1288 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1289
1290 if (level > dev_priv->wm.max_level)
1291 return false;
1292
1293 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1294 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1295 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1296}
1297
1298/* mark all levels starting from 'level' as invalid */
1299static void g4x_invalidate_wms(struct intel_crtc *crtc,
1300 struct g4x_wm_state *wm_state, int level)
1301{
1302 if (level <= G4X_WM_LEVEL_NORMAL) {
1303 enum plane_id plane_id;
1304
1305 for_each_plane_id_on_crtc(crtc, plane_id)
1306 wm_state->wm.plane[plane_id] = USHRT_MAX;
1307 }
1308
1309 if (level <= G4X_WM_LEVEL_SR) {
1310 wm_state->cxsr = false;
1311 wm_state->sr.cursor = USHRT_MAX;
1312 wm_state->sr.plane = USHRT_MAX;
1313 wm_state->sr.fbc = USHRT_MAX;
1314 }
1315
1316 if (level <= G4X_WM_LEVEL_HPLL) {
1317 wm_state->hpll_en = false;
1318 wm_state->hpll.cursor = USHRT_MAX;
1319 wm_state->hpll.plane = USHRT_MAX;
1320 wm_state->hpll.fbc = USHRT_MAX;
1321 }
1322}
1323
1324static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1325{
1326 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1327 struct intel_atomic_state *state =
1328 to_intel_atomic_state(crtc_state->base.state);
1329 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001330 int num_active_planes = hweight8(crtc_state->active_planes &
1331 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001332 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001333 const struct intel_plane_state *old_plane_state;
1334 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001335 struct intel_plane *plane;
1336 enum plane_id plane_id;
1337 int i, level;
1338 unsigned int dirty = 0;
1339
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001340 for_each_oldnew_intel_plane_in_state(state, plane,
1341 old_plane_state,
1342 new_plane_state, i) {
1343 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001344 old_plane_state->base.crtc != &crtc->base)
1345 continue;
1346
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001347 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001348 dirty |= BIT(plane->id);
1349 }
1350
1351 if (!dirty)
1352 return 0;
1353
1354 level = G4X_WM_LEVEL_NORMAL;
1355 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1356 goto out;
1357
1358 raw = &crtc_state->wm.g4x.raw[level];
1359 for_each_plane_id_on_crtc(crtc, plane_id)
1360 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1361
1362 level = G4X_WM_LEVEL_SR;
1363
1364 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1365 goto out;
1366
1367 raw = &crtc_state->wm.g4x.raw[level];
1368 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1369 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1370 wm_state->sr.fbc = raw->fbc;
1371
1372 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1373
1374 level = G4X_WM_LEVEL_HPLL;
1375
1376 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1377 goto out;
1378
1379 raw = &crtc_state->wm.g4x.raw[level];
1380 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1381 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1382 wm_state->hpll.fbc = raw->fbc;
1383
1384 wm_state->hpll_en = wm_state->cxsr;
1385
1386 level++;
1387
1388 out:
1389 if (level == G4X_WM_LEVEL_NORMAL)
1390 return -EINVAL;
1391
1392 /* invalidate the higher levels */
1393 g4x_invalidate_wms(crtc, wm_state, level);
1394
1395 /*
1396 * Determine if the FBC watermark(s) can be used. IF
1397 * this isn't the case we prefer to disable the FBC
1398 ( watermark(s) rather than disable the SR/HPLL
1399 * level(s) entirely.
1400 */
1401 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1402
1403 if (level >= G4X_WM_LEVEL_SR &&
1404 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1405 wm_state->fbc_en = false;
1406 else if (level >= G4X_WM_LEVEL_HPLL &&
1407 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1408 wm_state->fbc_en = false;
1409
1410 return 0;
1411}
1412
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001413static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001414{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001415 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001416 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1417 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1418 struct intel_atomic_state *intel_state =
1419 to_intel_atomic_state(new_crtc_state->base.state);
1420 const struct intel_crtc_state *old_crtc_state =
1421 intel_atomic_get_old_crtc_state(intel_state, crtc);
1422 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001423 enum plane_id plane_id;
1424
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001425 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
1426 *intermediate = *optimal;
1427
1428 intermediate->cxsr = false;
1429 intermediate->hpll_en = false;
1430 goto out;
1431 }
1432
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001433 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001434 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001435 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001436 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001437 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1438
1439 for_each_plane_id_on_crtc(crtc, plane_id) {
1440 intermediate->wm.plane[plane_id] =
1441 max(optimal->wm.plane[plane_id],
1442 active->wm.plane[plane_id]);
1443
1444 WARN_ON(intermediate->wm.plane[plane_id] >
1445 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1446 }
1447
1448 intermediate->sr.plane = max(optimal->sr.plane,
1449 active->sr.plane);
1450 intermediate->sr.cursor = max(optimal->sr.cursor,
1451 active->sr.cursor);
1452 intermediate->sr.fbc = max(optimal->sr.fbc,
1453 active->sr.fbc);
1454
1455 intermediate->hpll.plane = max(optimal->hpll.plane,
1456 active->hpll.plane);
1457 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1458 active->hpll.cursor);
1459 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1460 active->hpll.fbc);
1461
1462 WARN_ON((intermediate->sr.plane >
1463 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1464 intermediate->sr.cursor >
1465 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1466 intermediate->cxsr);
1467 WARN_ON((intermediate->sr.plane >
1468 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1469 intermediate->sr.cursor >
1470 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1471 intermediate->hpll_en);
1472
1473 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1474 intermediate->fbc_en && intermediate->cxsr);
1475 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1476 intermediate->fbc_en && intermediate->hpll_en);
1477
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001478out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001479 /*
1480 * If our intermediate WM are identical to the final WM, then we can
1481 * omit the post-vblank programming; only update if it's different.
1482 */
1483 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001484 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001485
1486 return 0;
1487}
1488
1489static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1490 struct g4x_wm_values *wm)
1491{
1492 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001493 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001494
1495 wm->cxsr = true;
1496 wm->hpll_en = true;
1497 wm->fbc_en = true;
1498
1499 for_each_intel_crtc(&dev_priv->drm, crtc) {
1500 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1501
1502 if (!crtc->active)
1503 continue;
1504
1505 if (!wm_state->cxsr)
1506 wm->cxsr = false;
1507 if (!wm_state->hpll_en)
1508 wm->hpll_en = false;
1509 if (!wm_state->fbc_en)
1510 wm->fbc_en = false;
1511
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001512 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001513 }
1514
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001515 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001516 wm->cxsr = false;
1517 wm->hpll_en = false;
1518 wm->fbc_en = false;
1519 }
1520
1521 for_each_intel_crtc(&dev_priv->drm, crtc) {
1522 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1523 enum pipe pipe = crtc->pipe;
1524
1525 wm->pipe[pipe] = wm_state->wm;
1526 if (crtc->active && wm->cxsr)
1527 wm->sr = wm_state->sr;
1528 if (crtc->active && wm->hpll_en)
1529 wm->hpll = wm_state->hpll;
1530 }
1531}
1532
1533static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1534{
1535 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1536 struct g4x_wm_values new_wm = {};
1537
1538 g4x_merge_wm(dev_priv, &new_wm);
1539
1540 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1541 return;
1542
1543 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1544 _intel_set_memory_cxsr(dev_priv, false);
1545
1546 g4x_write_wm_values(dev_priv, &new_wm);
1547
1548 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1549 _intel_set_memory_cxsr(dev_priv, true);
1550
1551 *old_wm = new_wm;
1552}
1553
1554static void g4x_initial_watermarks(struct intel_atomic_state *state,
1555 struct intel_crtc_state *crtc_state)
1556{
1557 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1558 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1559
1560 mutex_lock(&dev_priv->wm.wm_mutex);
1561 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1562 g4x_program_watermarks(dev_priv);
1563 mutex_unlock(&dev_priv->wm.wm_mutex);
1564}
1565
1566static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1567 struct intel_crtc_state *crtc_state)
1568{
1569 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001570 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001571
1572 if (!crtc_state->wm.need_postvbl_update)
1573 return;
1574
1575 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001576 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001577 g4x_program_watermarks(dev_priv);
1578 mutex_unlock(&dev_priv->wm.wm_mutex);
1579}
1580
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001581/* latency must be in 0.1us units. */
1582static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001583 unsigned int htotal,
1584 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001585 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001586 unsigned int latency)
1587{
1588 unsigned int ret;
1589
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001590 ret = intel_wm_method2(pixel_rate, htotal,
1591 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001592 ret = DIV_ROUND_UP(ret, 64);
1593
1594 return ret;
1595}
1596
Ville Syrjäläbb726512016-10-31 22:37:24 +02001597static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001598{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001599 /* all latencies in usec */
1600 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1601
Ville Syrjälä58590c12015-09-08 21:05:12 +03001602 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1603
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001604 if (IS_CHERRYVIEW(dev_priv)) {
1605 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1606 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001607
1608 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001609 }
1610}
1611
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001612static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1613 const struct intel_plane_state *plane_state,
1614 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001615{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001616 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001617 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001618 const struct drm_display_mode *adjusted_mode =
1619 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001620 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001621
1622 if (dev_priv->wm.pri_latency[level] == 0)
1623 return USHRT_MAX;
1624
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001625 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001626 return 0;
1627
Daniel Vetteref426c12017-01-04 11:41:10 +01001628 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001629 clock = adjusted_mode->crtc_clock;
1630 htotal = adjusted_mode->crtc_htotal;
1631 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001632
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001633 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001634 /*
1635 * FIXME the formula gives values that are
1636 * too big for the cursor FIFO, and hence we
1637 * would never be able to use cursors. For
1638 * now just hardcode the watermark.
1639 */
1640 wm = 63;
1641 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001642 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001643 dev_priv->wm.pri_latency[level] * 10);
1644 }
1645
Chris Wilson1a1f1282017-11-07 14:03:38 +00001646 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001647}
1648
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001649static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1650{
1651 return (active_planes & (BIT(PLANE_SPRITE0) |
1652 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1653}
1654
Ville Syrjälä5012e602017-03-02 19:14:56 +02001655static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001656{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001657 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001658 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001659 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001660 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001661 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001662 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001663 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001664 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001665 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001666 unsigned int total_rate;
1667 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001668
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001669 /*
1670 * When enabling sprite0 after sprite1 has already been enabled
1671 * we tend to get an underrun unless sprite0 already has some
1672 * FIFO space allcoated. Hence we always allocate at least one
1673 * cacheline for sprite0 whenever sprite1 is enabled.
1674 *
1675 * All other plane enable sequences appear immune to this problem.
1676 */
1677 if (vlv_need_sprite0_fifo_workaround(active_planes))
1678 sprite0_fifo_extra = 1;
1679
Ville Syrjälä5012e602017-03-02 19:14:56 +02001680 total_rate = raw->plane[PLANE_PRIMARY] +
1681 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001682 raw->plane[PLANE_SPRITE1] +
1683 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001684
Ville Syrjälä5012e602017-03-02 19:14:56 +02001685 if (total_rate > fifo_size)
1686 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001687
Ville Syrjälä5012e602017-03-02 19:14:56 +02001688 if (total_rate == 0)
1689 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001690
Ville Syrjälä5012e602017-03-02 19:14:56 +02001691 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001692 unsigned int rate;
1693
Ville Syrjälä5012e602017-03-02 19:14:56 +02001694 if ((active_planes & BIT(plane_id)) == 0) {
1695 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001696 continue;
1697 }
1698
Ville Syrjälä5012e602017-03-02 19:14:56 +02001699 rate = raw->plane[plane_id];
1700 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1701 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001702 }
1703
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001704 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1705 fifo_left -= sprite0_fifo_extra;
1706
Ville Syrjälä5012e602017-03-02 19:14:56 +02001707 fifo_state->plane[PLANE_CURSOR] = 63;
1708
1709 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001710
1711 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001712 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001713 int plane_extra;
1714
1715 if (fifo_left == 0)
1716 break;
1717
Ville Syrjälä5012e602017-03-02 19:14:56 +02001718 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001719 continue;
1720
1721 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001722 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001723 fifo_left -= plane_extra;
1724 }
1725
Ville Syrjälä5012e602017-03-02 19:14:56 +02001726 WARN_ON(active_planes != 0 && fifo_left != 0);
1727
1728 /* give it all to the first plane if none are active */
1729 if (active_planes == 0) {
1730 WARN_ON(fifo_left != fifo_size);
1731 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1732 }
1733
1734 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001735}
1736
Ville Syrjäläff32c542017-03-02 19:14:57 +02001737/* mark all levels starting from 'level' as invalid */
1738static void vlv_invalidate_wms(struct intel_crtc *crtc,
1739 struct vlv_wm_state *wm_state, int level)
1740{
1741 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1742
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001743 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001744 enum plane_id plane_id;
1745
1746 for_each_plane_id_on_crtc(crtc, plane_id)
1747 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1748
1749 wm_state->sr[level].cursor = USHRT_MAX;
1750 wm_state->sr[level].plane = USHRT_MAX;
1751 }
1752}
1753
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001754static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1755{
1756 if (wm > fifo_size)
1757 return USHRT_MAX;
1758 else
1759 return fifo_size - wm;
1760}
1761
Ville Syrjäläff32c542017-03-02 19:14:57 +02001762/*
1763 * Starting from 'level' set all higher
1764 * levels to 'value' in the "raw" watermarks.
1765 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001766static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001767 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001768{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001769 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001770 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001771 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001772
Ville Syrjäläff32c542017-03-02 19:14:57 +02001773 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001774 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001775
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001776 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001777 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001778 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001779
1780 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001781}
1782
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001783static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1784 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001785{
1786 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1787 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001788 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001789 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001790 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001791
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001792 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001793 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1794 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001795 }
1796
1797 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001798 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001799 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1800 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1801
Ville Syrjäläff32c542017-03-02 19:14:57 +02001802 if (wm > max_wm)
1803 break;
1804
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001805 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001806 raw->plane[plane_id] = wm;
1807 }
1808
1809 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001810 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001811
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001812out:
1813 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001814 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001815 plane->base.name,
1816 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1817 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1818 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1819
1820 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001821}
1822
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001823static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1824 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001825{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001826 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001827 &crtc_state->wm.vlv.raw[level];
1828 const struct vlv_fifo_state *fifo_state =
1829 &crtc_state->wm.vlv.fifo_state;
1830
1831 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1832}
1833
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001834static 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 +02001835{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001836 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1837 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1838 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1839 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001840}
1841
1842static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001843{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001844 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001845 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001846 struct intel_atomic_state *state =
1847 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001848 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001849 const struct vlv_fifo_state *fifo_state =
1850 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001851 int num_active_planes = hweight8(crtc_state->active_planes &
1852 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001853 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001854 const struct intel_plane_state *old_plane_state;
1855 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001856 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001857 enum plane_id plane_id;
1858 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001859 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001860
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001861 for_each_oldnew_intel_plane_in_state(state, plane,
1862 old_plane_state,
1863 new_plane_state, i) {
1864 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001865 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001866 continue;
1867
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001868 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001869 dirty |= BIT(plane->id);
1870 }
1871
1872 /*
1873 * DSPARB registers may have been reset due to the
1874 * power well being turned off. Make sure we restore
1875 * them to a consistent state even if no primary/sprite
1876 * planes are initially active.
1877 */
1878 if (needs_modeset)
1879 crtc_state->fifo_changed = true;
1880
1881 if (!dirty)
1882 return 0;
1883
1884 /* cursor changes don't warrant a FIFO recompute */
1885 if (dirty & ~BIT(PLANE_CURSOR)) {
1886 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001887 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001888 const struct vlv_fifo_state *old_fifo_state =
1889 &old_crtc_state->wm.vlv.fifo_state;
1890
1891 ret = vlv_compute_fifo(crtc_state);
1892 if (ret)
1893 return ret;
1894
1895 if (needs_modeset ||
1896 memcmp(old_fifo_state, fifo_state,
1897 sizeof(*fifo_state)) != 0)
1898 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001899 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001900
Ville Syrjäläff32c542017-03-02 19:14:57 +02001901 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001902 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903 /*
1904 * Note that enabling cxsr with no primary/sprite planes
1905 * enabled can wedge the pipe. Hence we only allow cxsr
1906 * with exactly one enabled primary/sprite plane.
1907 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001908 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001909
Ville Syrjälä5012e602017-03-02 19:14:56 +02001910 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001911 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001912 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001913
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001914 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001915 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001916
Ville Syrjäläff32c542017-03-02 19:14:57 +02001917 for_each_plane_id_on_crtc(crtc, plane_id) {
1918 wm_state->wm[level].plane[plane_id] =
1919 vlv_invert_wm_value(raw->plane[plane_id],
1920 fifo_state->plane[plane_id]);
1921 }
1922
1923 wm_state->sr[level].plane =
1924 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001925 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001926 raw->plane[PLANE_SPRITE1]),
1927 sr_fifo_size);
1928
1929 wm_state->sr[level].cursor =
1930 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1931 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001932 }
1933
Ville Syrjäläff32c542017-03-02 19:14:57 +02001934 if (level == 0)
1935 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001936
Ville Syrjäläff32c542017-03-02 19:14:57 +02001937 /* limit to only levels we can actually handle */
1938 wm_state->num_levels = level;
1939
1940 /* invalidate the higher levels */
1941 vlv_invalidate_wms(crtc, wm_state, level);
1942
1943 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001944}
1945
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001946#define VLV_FIFO(plane, value) \
1947 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1948
Ville Syrjäläff32c542017-03-02 19:14:57 +02001949static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1950 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001951{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001952 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001953 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001954 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001955 const struct vlv_fifo_state *fifo_state =
1956 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001957 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001958
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001959 if (!crtc_state->fifo_changed)
1960 return;
1961
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001962 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1963 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1964 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001965
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001966 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1967 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001968
Ville Syrjäläc137d662017-03-02 19:15:06 +02001969 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1970
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001971 /*
1972 * uncore.lock serves a double purpose here. It allows us to
1973 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1974 * it protects the DSPARB registers from getting clobbered by
1975 * parallel updates from multiple pipes.
1976 *
1977 * intel_pipe_update_start() has already disabled interrupts
1978 * for us, so a plain spin_lock() is sufficient here.
1979 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001980 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001981
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001982 switch (crtc->pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001983 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001984 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001985 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1986 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001987
1988 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1989 VLV_FIFO(SPRITEB, 0xff));
1990 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1991 VLV_FIFO(SPRITEB, sprite1_start));
1992
1993 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1994 VLV_FIFO(SPRITEB_HI, 0x1));
1995 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1996 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1997
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001998 intel_uncore_write_fw(uncore, DSPARB, dsparb);
1999 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002000 break;
2001 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002002 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2003 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002004
2005 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2006 VLV_FIFO(SPRITED, 0xff));
2007 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2008 VLV_FIFO(SPRITED, sprite1_start));
2009
2010 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2011 VLV_FIFO(SPRITED_HI, 0xff));
2012 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2013 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2014
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002015 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2016 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002017 break;
2018 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002019 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2020 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002021
2022 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2023 VLV_FIFO(SPRITEF, 0xff));
2024 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2025 VLV_FIFO(SPRITEF, sprite1_start));
2026
2027 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2028 VLV_FIFO(SPRITEF_HI, 0xff));
2029 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2030 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2031
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002032 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2033 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002034 break;
2035 default:
2036 break;
2037 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002038
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002039 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002040
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002041 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002042}
2043
2044#undef VLV_FIFO
2045
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002046static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002047{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002048 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002049 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2050 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2051 struct intel_atomic_state *intel_state =
2052 to_intel_atomic_state(new_crtc_state->base.state);
2053 const struct intel_crtc_state *old_crtc_state =
2054 intel_atomic_get_old_crtc_state(intel_state, crtc);
2055 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002056 int level;
2057
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002058 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
2059 *intermediate = *optimal;
2060
2061 intermediate->cxsr = false;
2062 goto out;
2063 }
2064
Ville Syrjälä4841da52017-03-02 19:14:59 +02002065 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002066 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002067 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002068
2069 for (level = 0; level < intermediate->num_levels; level++) {
2070 enum plane_id plane_id;
2071
2072 for_each_plane_id_on_crtc(crtc, plane_id) {
2073 intermediate->wm[level].plane[plane_id] =
2074 min(optimal->wm[level].plane[plane_id],
2075 active->wm[level].plane[plane_id]);
2076 }
2077
2078 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2079 active->sr[level].plane);
2080 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2081 active->sr[level].cursor);
2082 }
2083
2084 vlv_invalidate_wms(crtc, intermediate, level);
2085
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002086out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002087 /*
2088 * If our intermediate WM are identical to the final WM, then we can
2089 * omit the post-vblank programming; only update if it's different.
2090 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002091 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002092 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002093
2094 return 0;
2095}
2096
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002097static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002098 struct vlv_wm_values *wm)
2099{
2100 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002101 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002102
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002103 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002104 wm->cxsr = true;
2105
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002106 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002107 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002108
2109 if (!crtc->active)
2110 continue;
2111
2112 if (!wm_state->cxsr)
2113 wm->cxsr = false;
2114
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002115 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002116 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2117 }
2118
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002119 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002120 wm->cxsr = false;
2121
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002122 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002123 wm->level = VLV_WM_LEVEL_PM2;
2124
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002125 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002126 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002127 enum pipe pipe = crtc->pipe;
2128
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002129 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002130 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002131 wm->sr = wm_state->sr[wm->level];
2132
Ville Syrjälä1b313892016-11-28 19:37:08 +02002133 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2134 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2135 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2136 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002137 }
2138}
2139
Ville Syrjäläff32c542017-03-02 19:14:57 +02002140static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002141{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002142 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2143 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002144
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002145 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002146
Ville Syrjäläff32c542017-03-02 19:14:57 +02002147 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002148 return;
2149
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002150 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002151 chv_set_memory_dvfs(dev_priv, false);
2152
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002153 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002154 chv_set_memory_pm5(dev_priv, false);
2155
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002156 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002157 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002158
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002159 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002160
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002161 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002162 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002163
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002164 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002165 chv_set_memory_pm5(dev_priv, true);
2166
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002167 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002168 chv_set_memory_dvfs(dev_priv, true);
2169
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002170 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002171}
2172
Ville Syrjäläff32c542017-03-02 19:14:57 +02002173static void vlv_initial_watermarks(struct intel_atomic_state *state,
2174 struct intel_crtc_state *crtc_state)
2175{
2176 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2177 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2178
2179 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002180 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2181 vlv_program_watermarks(dev_priv);
2182 mutex_unlock(&dev_priv->wm.wm_mutex);
2183}
2184
2185static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2186 struct intel_crtc_state *crtc_state)
2187{
2188 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002189 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002190
2191 if (!crtc_state->wm.need_postvbl_update)
2192 return;
2193
2194 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002195 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002196 vlv_program_watermarks(dev_priv);
2197 mutex_unlock(&dev_priv->wm.wm_mutex);
2198}
2199
Ville Syrjälä432081b2016-10-31 22:37:03 +02002200static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002201{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002202 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002203 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002204 int srwm = 1;
2205 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002206 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002207
2208 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002209 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002210 if (crtc) {
2211 /* self-refresh has much higher latency */
2212 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002213 const struct drm_display_mode *adjusted_mode =
2214 &crtc->config->base.adjusted_mode;
2215 const struct drm_framebuffer *fb =
2216 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002217 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002218 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002219 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002220 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002221 int entries;
2222
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002223 entries = intel_wm_method2(clock, htotal,
2224 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002225 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2226 srwm = I965_FIFO_SIZE - entries;
2227 if (srwm < 0)
2228 srwm = 1;
2229 srwm &= 0x1ff;
2230 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2231 entries, srwm);
2232
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002233 entries = intel_wm_method2(clock, htotal,
2234 crtc->base.cursor->state->crtc_w, 4,
2235 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002236 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002237 i965_cursor_wm_info.cacheline_size) +
2238 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002239
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002240 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002241 if (cursor_sr > i965_cursor_wm_info.max_wm)
2242 cursor_sr = i965_cursor_wm_info.max_wm;
2243
2244 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2245 "cursor %d\n", srwm, cursor_sr);
2246
Imre Deak98584252014-06-13 14:54:20 +03002247 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002248 } else {
Imre Deak98584252014-06-13 14:54:20 +03002249 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002250 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002251 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002252 }
2253
2254 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2255 srwm);
2256
2257 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002258 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2259 FW_WM(8, CURSORB) |
2260 FW_WM(8, PLANEB) |
2261 FW_WM(8, PLANEA));
2262 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2263 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002264 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002265 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002266
2267 if (cxsr_enabled)
2268 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002269}
2270
Ville Syrjäläf4998962015-03-10 17:02:21 +02002271#undef FW_WM
2272
Ville Syrjälä432081b2016-10-31 22:37:03 +02002273static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002274{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002275 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002276 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002277 u32 fwater_lo;
2278 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002279 int cwm, srwm = 1;
2280 int fifo_size;
2281 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002282 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002283
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002284 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002285 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002286 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002287 wm_info = &i915_wm_info;
2288 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002289 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002290
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002291 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2292 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002293 if (intel_crtc_active(crtc)) {
2294 const struct drm_display_mode *adjusted_mode =
2295 &crtc->config->base.adjusted_mode;
2296 const struct drm_framebuffer *fb =
2297 crtc->base.primary->state->fb;
2298 int cpp;
2299
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002300 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002301 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002302 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002303 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002304
Damien Lespiau241bfc32013-09-25 16:45:37 +01002305 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002306 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002307 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002308 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002309 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002310 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002311 if (planea_wm > (long)wm_info->max_wm)
2312 planea_wm = wm_info->max_wm;
2313 }
2314
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002315 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002316 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002317
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002318 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2319 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002320 if (intel_crtc_active(crtc)) {
2321 const struct drm_display_mode *adjusted_mode =
2322 &crtc->config->base.adjusted_mode;
2323 const struct drm_framebuffer *fb =
2324 crtc->base.primary->state->fb;
2325 int cpp;
2326
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002327 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002328 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];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002331
Damien Lespiau241bfc32013-09-25 16:45:37 +01002332 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002333 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002334 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002335 if (enabled == NULL)
2336 enabled = crtc;
2337 else
2338 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002339 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002340 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002341 if (planeb_wm > (long)wm_info->max_wm)
2342 planeb_wm = wm_info->max_wm;
2343 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344
2345 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2346
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002347 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002348 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002349
Ville Syrjäläefc26112016-10-31 22:37:04 +02002350 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002351
2352 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002353 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002354 enabled = NULL;
2355 }
2356
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002357 /*
2358 * Overlay gets an aggressive default since video jitter is bad.
2359 */
2360 cwm = 2;
2361
2362 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002363 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002364
2365 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002366 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002367 /* self-refresh has much higher latency */
2368 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002369 const struct drm_display_mode *adjusted_mode =
2370 &enabled->config->base.adjusted_mode;
2371 const struct drm_framebuffer *fb =
2372 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002373 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002374 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002375 int hdisplay = enabled->config->pipe_src_w;
2376 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002377 int entries;
2378
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002379 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002380 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002381 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002382 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002383
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002384 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2385 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002386 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2387 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2388 srwm = wm_info->fifo_size - entries;
2389 if (srwm < 0)
2390 srwm = 1;
2391
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002392 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002393 I915_WRITE(FW_BLC_SELF,
2394 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002395 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002396 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2397 }
2398
2399 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2400 planea_wm, planeb_wm, cwm, srwm);
2401
2402 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2403 fwater_hi = (cwm & 0x1f);
2404
2405 /* Set request length to 8 cachelines per fetch */
2406 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2407 fwater_hi = fwater_hi | (1 << 8);
2408
2409 I915_WRITE(FW_BLC, fwater_lo);
2410 I915_WRITE(FW_BLC2, fwater_hi);
2411
Imre Deak5209b1f2014-07-01 12:36:17 +03002412 if (enabled)
2413 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002414}
2415
Ville Syrjälä432081b2016-10-31 22:37:03 +02002416static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002417{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002418 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002419 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002420 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002421 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002422 int planea_wm;
2423
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002424 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002425 if (crtc == NULL)
2426 return;
2427
Ville Syrjäläefc26112016-10-31 22:37:04 +02002428 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002429 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002430 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002431 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002432 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002433 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2434 fwater_lo |= (3<<8) | planea_wm;
2435
2436 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2437
2438 I915_WRITE(FW_BLC, fwater_lo);
2439}
2440
Ville Syrjälä37126462013-08-01 16:18:55 +03002441/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002442static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2443 unsigned int cpp,
2444 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002445{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002446 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002447
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002448 ret = intel_wm_method1(pixel_rate, cpp, latency);
2449 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002450
2451 return ret;
2452}
2453
Ville Syrjälä37126462013-08-01 16:18:55 +03002454/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002455static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2456 unsigned int htotal,
2457 unsigned int width,
2458 unsigned int cpp,
2459 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002460{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002461 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002462
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002463 ret = intel_wm_method2(pixel_rate, htotal,
2464 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002465 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002466
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002467 return ret;
2468}
2469
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002470static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002471{
Matt Roper15126882015-12-03 11:37:40 -08002472 /*
2473 * Neither of these should be possible since this function shouldn't be
2474 * called if the CRTC is off or the plane is invisible. But let's be
2475 * extra paranoid to avoid a potential divide-by-zero if we screw up
2476 * elsewhere in the driver.
2477 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002478 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002479 return 0;
2480 if (WARN_ON(!horiz_pixels))
2481 return 0;
2482
Ville Syrjäläac484962016-01-20 21:05:26 +02002483 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002484}
2485
Imre Deak820c1982013-12-17 14:46:36 +02002486struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002487 u16 pri;
2488 u16 spr;
2489 u16 cur;
2490 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002491};
2492
Ville Syrjälä37126462013-08-01 16:18:55 +03002493/*
2494 * For both WM_PIPE and WM_LP.
2495 * mem_value must be in 0.1us units.
2496 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002497static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2498 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002499 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002500{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002501 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002502 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002503
Ville Syrjälä03981c62018-11-14 19:34:40 +02002504 if (mem_value == 0)
2505 return U32_MAX;
2506
Maarten Lankhorstec193642019-06-28 10:55:17 +02002507 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508 return 0;
2509
Maarten Lankhorstec193642019-06-28 10:55:17 +02002510 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002511
Maarten Lankhorstec193642019-06-28 10:55:17 +02002512 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002513
2514 if (!is_lp)
2515 return method1;
2516
Maarten Lankhorstec193642019-06-28 10:55:17 +02002517 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2518 crtc_state->base.adjusted_mode.crtc_htotal,
2519 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002520 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002521
2522 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002523}
2524
Ville Syrjälä37126462013-08-01 16:18:55 +03002525/*
2526 * For both WM_PIPE and WM_LP.
2527 * mem_value must be in 0.1us units.
2528 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002529static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2530 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002531 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002532{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002533 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002534 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002535
Ville Syrjälä03981c62018-11-14 19:34:40 +02002536 if (mem_value == 0)
2537 return U32_MAX;
2538
Maarten Lankhorstec193642019-06-28 10:55:17 +02002539 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002540 return 0;
2541
Maarten Lankhorstec193642019-06-28 10:55:17 +02002542 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002543
Maarten Lankhorstec193642019-06-28 10:55:17 +02002544 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2545 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2546 crtc_state->base.adjusted_mode.crtc_htotal,
2547 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002548 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002549 return min(method1, method2);
2550}
2551
Ville Syrjälä37126462013-08-01 16:18:55 +03002552/*
2553 * For both WM_PIPE and WM_LP.
2554 * mem_value must be in 0.1us units.
2555 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002556static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2557 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002558 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002559{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002560 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002561
Ville Syrjälä03981c62018-11-14 19:34:40 +02002562 if (mem_value == 0)
2563 return U32_MAX;
2564
Maarten Lankhorstec193642019-06-28 10:55:17 +02002565 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002566 return 0;
2567
Maarten Lankhorstec193642019-06-28 10:55:17 +02002568 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002569
Maarten Lankhorstec193642019-06-28 10:55:17 +02002570 return ilk_wm_method2(crtc_state->pixel_rate,
2571 crtc_state->base.adjusted_mode.crtc_htotal,
2572 plane_state->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002573}
2574
Paulo Zanonicca32e92013-05-31 11:45:06 -03002575/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002576static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2577 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002578 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002579{
Ville Syrjälä83054942016-11-18 21:53:00 +02002580 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002581
Maarten Lankhorstec193642019-06-28 10:55:17 +02002582 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002583 return 0;
2584
Maarten Lankhorstec193642019-06-28 10:55:17 +02002585 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002586
Maarten Lankhorstec193642019-06-28 10:55:17 +02002587 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002588}
2589
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002590static unsigned int
2591ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002592{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002593 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002594 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002595 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002596 return 768;
2597 else
2598 return 512;
2599}
2600
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002601static unsigned int
2602ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2603 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002604{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002605 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002606 /* BDW primary/sprite plane watermarks */
2607 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002608 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002609 /* IVB/HSW primary/sprite plane watermarks */
2610 return level == 0 ? 127 : 1023;
2611 else if (!is_sprite)
2612 /* ILK/SNB primary plane watermarks */
2613 return level == 0 ? 127 : 511;
2614 else
2615 /* ILK/SNB sprite plane watermarks */
2616 return level == 0 ? 63 : 255;
2617}
2618
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002619static unsigned int
2620ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002621{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002622 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002623 return level == 0 ? 63 : 255;
2624 else
2625 return level == 0 ? 31 : 63;
2626}
2627
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002628static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002629{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002630 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002631 return 31;
2632 else
2633 return 15;
2634}
2635
Ville Syrjälä158ae642013-08-07 13:28:19 +03002636/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002637static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002638 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002639 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002640 enum intel_ddb_partitioning ddb_partitioning,
2641 bool is_sprite)
2642{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002643 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002644
2645 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002646 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002647 return 0;
2648
2649 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002650 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002651 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002652
2653 /*
2654 * For some reason the non self refresh
2655 * FIFO size is only half of the self
2656 * refresh FIFO size on ILK/SNB.
2657 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002658 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002659 fifo_size /= 2;
2660 }
2661
Ville Syrjälä240264f2013-08-07 13:29:12 +03002662 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002663 /* level 0 is always calculated with 1:1 split */
2664 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2665 if (is_sprite)
2666 fifo_size *= 5;
2667 fifo_size /= 6;
2668 } else {
2669 fifo_size /= 2;
2670 }
2671 }
2672
2673 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002674 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002675}
2676
2677/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002678static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002679 int level,
2680 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002681{
2682 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002683 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002684 return 64;
2685
2686 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002687 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002688}
2689
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002690static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002691 int level,
2692 const struct intel_wm_config *config,
2693 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002694 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002695{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002696 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2697 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2698 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2699 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002700}
2701
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002702static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002703 int level,
2704 struct ilk_wm_maximums *max)
2705{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002706 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2707 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2708 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2709 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002710}
2711
Ville Syrjäläd9395652013-10-09 19:18:10 +03002712static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002713 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002714 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002715{
2716 bool ret;
2717
2718 /* already determined to be invalid? */
2719 if (!result->enable)
2720 return false;
2721
2722 result->enable = result->pri_val <= max->pri &&
2723 result->spr_val <= max->spr &&
2724 result->cur_val <= max->cur;
2725
2726 ret = result->enable;
2727
2728 /*
2729 * HACK until we can pre-compute everything,
2730 * and thus fail gracefully if LP0 watermarks
2731 * are exceeded...
2732 */
2733 if (level == 0 && !result->enable) {
2734 if (result->pri_val > max->pri)
2735 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2736 level, result->pri_val, max->pri);
2737 if (result->spr_val > max->spr)
2738 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2739 level, result->spr_val, max->spr);
2740 if (result->cur_val > max->cur)
2741 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2742 level, result->cur_val, max->cur);
2743
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002744 result->pri_val = min_t(u32, result->pri_val, max->pri);
2745 result->spr_val = min_t(u32, result->spr_val, max->spr);
2746 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002747 result->enable = true;
2748 }
2749
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002750 return ret;
2751}
2752
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002753static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002754 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002755 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002756 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002757 const struct intel_plane_state *pristate,
2758 const struct intel_plane_state *sprstate,
2759 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002760 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002761{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002762 u16 pri_latency = dev_priv->wm.pri_latency[level];
2763 u16 spr_latency = dev_priv->wm.spr_latency[level];
2764 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002765
2766 /* WM1+ latency values stored in 0.5us units */
2767 if (level > 0) {
2768 pri_latency *= 5;
2769 spr_latency *= 5;
2770 cur_latency *= 5;
2771 }
2772
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002773 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002774 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002775 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002776 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002777 }
2778
2779 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002780 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002781
2782 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002783 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002784
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002785 result->enable = true;
2786}
2787
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002788static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02002789hsw_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002790{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002791 const struct intel_atomic_state *intel_state =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002792 to_intel_atomic_state(crtc_state->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002793 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002794 &crtc_state->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002795 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002796
Maarten Lankhorstec193642019-06-28 10:55:17 +02002797 if (!crtc_state->base.active)
Matt Roperee91a152015-12-03 11:37:39 -08002798 return 0;
2799 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2800 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002801 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002802 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002803
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002804 /* The WM are computed with base on how long it takes to fill a single
2805 * row at the given clock rate, multiplied by 8.
2806 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002807 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2808 adjusted_mode->crtc_clock);
2809 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002810 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002811
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002812 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2813 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002814}
2815
Ville Syrjäläbb726512016-10-31 22:37:24 +02002816static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002817 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002818{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002819 struct intel_uncore *uncore = &dev_priv->uncore;
2820
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002821 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002822 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002823 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002824 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002825
2826 /* read the first set of memory latencies[0:3] */
2827 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002828 ret = sandybridge_pcode_read(dev_priv,
2829 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002830 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002831
2832 if (ret) {
2833 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2834 return;
2835 }
2836
2837 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2838 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2839 GEN9_MEM_LATENCY_LEVEL_MASK;
2840 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2841 GEN9_MEM_LATENCY_LEVEL_MASK;
2842 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2843 GEN9_MEM_LATENCY_LEVEL_MASK;
2844
2845 /* read the second set of memory latencies[4:7] */
2846 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002847 ret = sandybridge_pcode_read(dev_priv,
2848 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002849 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002850 if (ret) {
2851 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2852 return;
2853 }
2854
2855 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2856 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2857 GEN9_MEM_LATENCY_LEVEL_MASK;
2858 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2859 GEN9_MEM_LATENCY_LEVEL_MASK;
2860 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2861 GEN9_MEM_LATENCY_LEVEL_MASK;
2862
Vandana Kannan367294b2014-11-04 17:06:46 +00002863 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002864 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2865 * need to be disabled. We make sure to sanitize the values out
2866 * of the punit to satisfy this requirement.
2867 */
2868 for (level = 1; level <= max_level; level++) {
2869 if (wm[level] == 0) {
2870 for (i = level + 1; i <= max_level; i++)
2871 wm[i] = 0;
2872 break;
2873 }
2874 }
2875
2876 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002877 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002878 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002879 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002880 * to add 2us to the various latency levels we retrieve from the
2881 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002882 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002883 if (wm[0] == 0) {
2884 wm[0] += 2;
2885 for (level = 1; level <= max_level; level++) {
2886 if (wm[level] == 0)
2887 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002888 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002889 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002890 }
2891
Mahesh Kumar86b59282018-08-31 16:39:42 +05302892 /*
2893 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2894 * If we could not get dimm info enable this WA to prevent from
2895 * any underrun. If not able to get Dimm info assume 16GB dimm
2896 * to avoid any underrun.
2897 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002898 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302899 wm[0] += 1;
2900
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002901 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002902 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002903
2904 wm[0] = (sskpd >> 56) & 0xFF;
2905 if (wm[0] == 0)
2906 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002907 wm[1] = (sskpd >> 4) & 0xFF;
2908 wm[2] = (sskpd >> 12) & 0xFF;
2909 wm[3] = (sskpd >> 20) & 0x1FF;
2910 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002911 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002912 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002913
2914 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2915 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2916 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2917 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002918 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002919 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002920
2921 /* ILK primary LP0 latency is 700 ns */
2922 wm[0] = 7;
2923 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2924 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002925 } else {
2926 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002927 }
2928}
2929
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002930static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002931 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002932{
2933 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002934 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002935 wm[0] = 13;
2936}
2937
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002938static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002939 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002940{
2941 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002942 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002943 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002944}
2945
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002946int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002947{
2948 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002949 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002950 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002951 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002952 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002953 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002954 return 3;
2955 else
2956 return 2;
2957}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002958
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002959static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002960 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002961 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002962{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002963 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002964
2965 for (level = 0; level <= max_level; level++) {
2966 unsigned int latency = wm[level];
2967
2968 if (latency == 0) {
Chris Wilson86c1c872018-07-26 17:15:27 +01002969 DRM_DEBUG_KMS("%s WM%d latency not provided\n",
2970 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002971 continue;
2972 }
2973
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002974 /*
2975 * - latencies are in us on gen9.
2976 * - before then, WM1+ latency values are in 0.5us units
2977 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002978 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002979 latency *= 10;
2980 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002981 latency *= 5;
2982
2983 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2984 name, level, wm[level],
2985 latency / 10, latency % 10);
2986 }
2987}
2988
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002989static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002990 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002991{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002992 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002993
2994 if (wm[0] >= min)
2995 return false;
2996
2997 wm[0] = max(wm[0], min);
2998 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002999 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003000
3001 return true;
3002}
3003
Ville Syrjäläbb726512016-10-31 22:37:24 +02003004static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003005{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003006 bool changed;
3007
3008 /*
3009 * The BIOS provided WM memory latency values are often
3010 * inadequate for high resolution displays. Adjust them.
3011 */
3012 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3013 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3014 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3015
3016 if (!changed)
3017 return;
3018
3019 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003020 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3021 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3022 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003023}
3024
Ville Syrjälä03981c62018-11-14 19:34:40 +02003025static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3026{
3027 /*
3028 * On some SNB machines (Thinkpad X220 Tablet at least)
3029 * LP3 usage can cause vblank interrupts to be lost.
3030 * The DEIIR bit will go high but it looks like the CPU
3031 * never gets interrupted.
3032 *
3033 * It's not clear whether other interrupt source could
3034 * be affected or if this is somehow limited to vblank
3035 * interrupts only. To play it safe we disable LP3
3036 * watermarks entirely.
3037 */
3038 if (dev_priv->wm.pri_latency[3] == 0 &&
3039 dev_priv->wm.spr_latency[3] == 0 &&
3040 dev_priv->wm.cur_latency[3] == 0)
3041 return;
3042
3043 dev_priv->wm.pri_latency[3] = 0;
3044 dev_priv->wm.spr_latency[3] = 0;
3045 dev_priv->wm.cur_latency[3] = 0;
3046
3047 DRM_DEBUG_KMS("LP3 watermarks disabled due to potential for lost interrupts\n");
3048 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3049 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3050 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3051}
3052
Ville Syrjäläbb726512016-10-31 22:37:24 +02003053static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003054{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003055 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003056
3057 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3058 sizeof(dev_priv->wm.pri_latency));
3059 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3060 sizeof(dev_priv->wm.pri_latency));
3061
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003062 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003063 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003064
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003065 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3066 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3067 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003068
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003069 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003070 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003071 snb_wm_lp3_irq_quirk(dev_priv);
3072 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003073}
3074
Ville Syrjäläbb726512016-10-31 22:37:24 +02003075static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003076{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003077 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003078 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003079}
3080
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003081static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003082 struct intel_pipe_wm *pipe_wm)
3083{
3084 /* LP0 watermark maximums depend on this pipe alone */
3085 const struct intel_wm_config config = {
3086 .num_pipes_active = 1,
3087 .sprites_enabled = pipe_wm->sprites_enabled,
3088 .sprites_scaled = pipe_wm->sprites_scaled,
3089 };
3090 struct ilk_wm_maximums max;
3091
3092 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003093 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003094
3095 /* At least LP0 must be valid */
3096 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3097 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3098 return false;
3099 }
3100
3101 return true;
3102}
3103
Matt Roper261a27d2015-10-08 15:28:25 -07003104/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003105static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003106{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003107 struct drm_atomic_state *state = crtc_state->base.state;
3108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003109 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003110 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003111 const struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003112 struct drm_plane *plane;
3113 const struct drm_plane_state *plane_state;
3114 const struct intel_plane_state *pristate = NULL;
3115 const struct intel_plane_state *sprstate = NULL;
3116 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003117 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003118 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003119
Maarten Lankhorstec193642019-06-28 10:55:17 +02003120 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003121
Maarten Lankhorstec193642019-06-28 10:55:17 +02003122 drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, &crtc_state->base) {
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003123 const struct intel_plane_state *ps = to_intel_plane_state(plane_state);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003124
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003125 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003126 pristate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003127 else if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003128 sprstate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003129 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003130 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003131 }
3132
Maarten Lankhorstec193642019-06-28 10:55:17 +02003133 pipe_wm->pipe_enabled = crtc_state->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003134 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003135 pipe_wm->sprites_enabled = sprstate->base.visible;
3136 pipe_wm->sprites_scaled = sprstate->base.visible &&
3137 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3138 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003139 }
3140
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003141 usable_level = max_level;
3142
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003143 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003144 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003145 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003146
3147 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003148 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003149 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003150
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003151 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorstec193642019-06-28 10:55:17 +02003152 ilk_compute_wm_level(dev_priv, intel_crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003153 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003154
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003155 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Maarten Lankhorstec193642019-06-28 10:55:17 +02003156 pipe_wm->linetime = hsw_compute_linetime_wm(crtc_state);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003157
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003158 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003159 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003160
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003161 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003162
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003163 for (level = 1; level <= usable_level; level++) {
3164 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003165
Maarten Lankhorstec193642019-06-28 10:55:17 +02003166 ilk_compute_wm_level(dev_priv, intel_crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003167 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003168
3169 /*
3170 * Disable any watermark level that exceeds the
3171 * register maximums since such watermarks are
3172 * always invalid.
3173 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003174 if (!ilk_validate_wm_level(level, &max, wm)) {
3175 memset(wm, 0, sizeof(*wm));
3176 break;
3177 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003178 }
3179
Matt Roper86c8bbb2015-09-24 15:53:16 -07003180 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003181}
3182
3183/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003184 * Build a set of 'intermediate' watermark values that satisfy both the old
3185 * state and the new state. These can be programmed to the hardware
3186 * immediately.
3187 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003188static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003189{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003190 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->base.crtc);
3191 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003192 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003193 struct intel_atomic_state *intel_state =
3194 to_intel_atomic_state(newstate->base.state);
3195 const struct intel_crtc_state *oldstate =
3196 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3197 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003198 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003199
3200 /*
3201 * Start with the final, target watermarks, then combine with the
3202 * currently active watermarks to get values that are safe both before
3203 * and after the vblank.
3204 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003205 *a = newstate->wm.ilk.optimal;
Ville Syrjäläf255c622018-11-08 17:10:13 +02003206 if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base) ||
3207 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003208 return 0;
3209
Matt Ropered4a6a72016-02-23 17:20:13 -08003210 a->pipe_enabled |= b->pipe_enabled;
3211 a->sprites_enabled |= b->sprites_enabled;
3212 a->sprites_scaled |= b->sprites_scaled;
3213
3214 for (level = 0; level <= max_level; level++) {
3215 struct intel_wm_level *a_wm = &a->wm[level];
3216 const struct intel_wm_level *b_wm = &b->wm[level];
3217
3218 a_wm->enable &= b_wm->enable;
3219 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3220 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3221 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3222 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3223 }
3224
3225 /*
3226 * We need to make sure that these merged watermark values are
3227 * actually a valid configuration themselves. If they're not,
3228 * there's no safe way to transition from the old state to
3229 * the new state, so we need to fail the atomic transaction.
3230 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003231 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003232 return -EINVAL;
3233
3234 /*
3235 * If our intermediate WM are identical to the final WM, then we can
3236 * omit the post-vblank programming; only update if it's different.
3237 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003238 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3239 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003240
3241 return 0;
3242}
3243
3244/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003245 * Merge the watermarks from all active pipes for a specific level.
3246 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003247static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003248 int level,
3249 struct intel_wm_level *ret_wm)
3250{
3251 const struct intel_crtc *intel_crtc;
3252
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003253 ret_wm->enable = true;
3254
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003255 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003256 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003257 const struct intel_wm_level *wm = &active->wm[level];
3258
3259 if (!active->pipe_enabled)
3260 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003261
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003262 /*
3263 * The watermark values may have been used in the past,
3264 * so we must maintain them in the registers for some
3265 * time even if the level is now disabled.
3266 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003267 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003268 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003269
3270 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3271 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3272 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3273 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3274 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003275}
3276
3277/*
3278 * Merge all low power watermarks for all active pipes.
3279 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003280static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003281 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003282 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003283 struct intel_pipe_wm *merged)
3284{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003285 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003286 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003287
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003288 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003289 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003290 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003291 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003292
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003293 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003294 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003295
3296 /* merge each WM1+ level */
3297 for (level = 1; level <= max_level; level++) {
3298 struct intel_wm_level *wm = &merged->wm[level];
3299
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003300 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003301
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003302 if (level > last_enabled_level)
3303 wm->enable = false;
3304 else if (!ilk_validate_wm_level(level, max, wm))
3305 /* make sure all following levels get disabled */
3306 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003307
3308 /*
3309 * The spec says it is preferred to disable
3310 * FBC WMs instead of disabling a WM level.
3311 */
3312 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003313 if (wm->enable)
3314 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003315 wm->fbc_val = 0;
3316 }
3317 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003318
3319 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3320 /*
3321 * FIXME this is racy. FBC might get enabled later.
3322 * What we should check here is whether FBC can be
3323 * enabled sometime later.
3324 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003325 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003326 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003327 for (level = 2; level <= max_level; level++) {
3328 struct intel_wm_level *wm = &merged->wm[level];
3329
3330 wm->enable = false;
3331 }
3332 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003333}
3334
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003335static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3336{
3337 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3338 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3339}
3340
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003341/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003342static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3343 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003344{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003345 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003346 return 2 * level;
3347 else
3348 return dev_priv->wm.pri_latency[level];
3349}
3350
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003351static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003352 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003353 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003354 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003355{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003356 struct intel_crtc *intel_crtc;
3357 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003358
Ville Syrjälä0362c782013-10-09 19:17:57 +03003359 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003360 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003361
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003362 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003363 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003364 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003365
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003366 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003367
Ville Syrjälä0362c782013-10-09 19:17:57 +03003368 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003369
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003370 /*
3371 * Maintain the watermark values even if the level is
3372 * disabled. Doing otherwise could cause underruns.
3373 */
3374 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003375 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003376 (r->pri_val << WM1_LP_SR_SHIFT) |
3377 r->cur_val;
3378
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003379 if (r->enable)
3380 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3381
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003382 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003383 results->wm_lp[wm_lp - 1] |=
3384 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3385 else
3386 results->wm_lp[wm_lp - 1] |=
3387 r->fbc_val << WM1_LP_FBC_SHIFT;
3388
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003389 /*
3390 * Always set WM1S_LP_EN when spr_val != 0, even if the
3391 * level is disabled. Doing otherwise could cause underruns.
3392 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003393 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003394 WARN_ON(wm_lp != 1);
3395 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3396 } else
3397 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003398 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003399
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003400 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003401 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003402 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003403 const struct intel_wm_level *r =
3404 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003405
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003406 if (WARN_ON(!r->enable))
3407 continue;
3408
Matt Ropered4a6a72016-02-23 17:20:13 -08003409 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003410
3411 results->wm_pipe[pipe] =
3412 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3413 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3414 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003415 }
3416}
3417
Paulo Zanoni861f3382013-05-31 10:19:21 -03003418/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3419 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003420static struct intel_pipe_wm *
3421ilk_find_best_result(struct drm_i915_private *dev_priv,
3422 struct intel_pipe_wm *r1,
3423 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003424{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003425 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003426 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003427
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003428 for (level = 1; level <= max_level; level++) {
3429 if (r1->wm[level].enable)
3430 level1 = level;
3431 if (r2->wm[level].enable)
3432 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003433 }
3434
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003435 if (level1 == level2) {
3436 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003437 return r2;
3438 else
3439 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003440 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003441 return r1;
3442 } else {
3443 return r2;
3444 }
3445}
3446
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003447/* dirty bits used to track which watermarks need changes */
3448#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3449#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3450#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3451#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3452#define WM_DIRTY_FBC (1 << 24)
3453#define WM_DIRTY_DDB (1 << 25)
3454
Damien Lespiau055e3932014-08-18 13:49:10 +01003455static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003456 const struct ilk_wm_values *old,
3457 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003458{
3459 unsigned int dirty = 0;
3460 enum pipe pipe;
3461 int wm_lp;
3462
Damien Lespiau055e3932014-08-18 13:49:10 +01003463 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003464 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3465 dirty |= WM_DIRTY_LINETIME(pipe);
3466 /* Must disable LP1+ watermarks too */
3467 dirty |= WM_DIRTY_LP_ALL;
3468 }
3469
3470 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3471 dirty |= WM_DIRTY_PIPE(pipe);
3472 /* Must disable LP1+ watermarks too */
3473 dirty |= WM_DIRTY_LP_ALL;
3474 }
3475 }
3476
3477 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3478 dirty |= WM_DIRTY_FBC;
3479 /* Must disable LP1+ watermarks too */
3480 dirty |= WM_DIRTY_LP_ALL;
3481 }
3482
3483 if (old->partitioning != new->partitioning) {
3484 dirty |= WM_DIRTY_DDB;
3485 /* Must disable LP1+ watermarks too */
3486 dirty |= WM_DIRTY_LP_ALL;
3487 }
3488
3489 /* LP1+ watermarks already deemed dirty, no need to continue */
3490 if (dirty & WM_DIRTY_LP_ALL)
3491 return dirty;
3492
3493 /* Find the lowest numbered LP1+ watermark in need of an update... */
3494 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3495 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3496 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3497 break;
3498 }
3499
3500 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3501 for (; wm_lp <= 3; wm_lp++)
3502 dirty |= WM_DIRTY_LP(wm_lp);
3503
3504 return dirty;
3505}
3506
Ville Syrjälä8553c182013-12-05 15:51:39 +02003507static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3508 unsigned int dirty)
3509{
Imre Deak820c1982013-12-17 14:46:36 +02003510 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003511 bool changed = false;
3512
3513 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3514 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3515 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3516 changed = true;
3517 }
3518 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3519 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3520 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3521 changed = true;
3522 }
3523 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3524 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3525 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3526 changed = true;
3527 }
3528
3529 /*
3530 * Don't touch WM1S_LP_EN here.
3531 * Doing so could cause underruns.
3532 */
3533
3534 return changed;
3535}
3536
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003537/*
3538 * The spec says we shouldn't write when we don't need, because every write
3539 * causes WMs to be re-evaluated, expending some power.
3540 */
Imre Deak820c1982013-12-17 14:46:36 +02003541static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3542 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003543{
Imre Deak820c1982013-12-17 14:46:36 +02003544 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003545 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003546 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003547
Damien Lespiau055e3932014-08-18 13:49:10 +01003548 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003549 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003550 return;
3551
Ville Syrjälä8553c182013-12-05 15:51:39 +02003552 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003553
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003554 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003555 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003556 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003557 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003558 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003559 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3560
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003561 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003562 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003563 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003564 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003565 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003566 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3567
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003568 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003569 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003570 val = I915_READ(WM_MISC);
3571 if (results->partitioning == INTEL_DDB_PART_1_2)
3572 val &= ~WM_MISC_DATA_PARTITION_5_6;
3573 else
3574 val |= WM_MISC_DATA_PARTITION_5_6;
3575 I915_WRITE(WM_MISC, val);
3576 } else {
3577 val = I915_READ(DISP_ARB_CTL2);
3578 if (results->partitioning == INTEL_DDB_PART_1_2)
3579 val &= ~DISP_DATA_PARTITION_5_6;
3580 else
3581 val |= DISP_DATA_PARTITION_5_6;
3582 I915_WRITE(DISP_ARB_CTL2, val);
3583 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003584 }
3585
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003586 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003587 val = I915_READ(DISP_ARB_CTL);
3588 if (results->enable_fbc_wm)
3589 val &= ~DISP_FBC_WM_DIS;
3590 else
3591 val |= DISP_FBC_WM_DIS;
3592 I915_WRITE(DISP_ARB_CTL, val);
3593 }
3594
Imre Deak954911e2013-12-17 14:46:34 +02003595 if (dirty & WM_DIRTY_LP(1) &&
3596 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3597 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3598
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003599 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003600 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3601 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3602 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3603 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3604 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003605
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003606 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003607 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003608 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003609 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003610 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003611 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003612
3613 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003614}
3615
Matt Ropered4a6a72016-02-23 17:20:13 -08003616bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003617{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003618 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003619
3620 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3621}
3622
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303623static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
3624{
3625 u8 enabled_slices;
3626
3627 /* Slice 1 will always be enabled */
3628 enabled_slices = 1;
3629
3630 /* Gen prior to GEN11 have only one DBuf slice */
3631 if (INTEL_GEN(dev_priv) < 11)
3632 return enabled_slices;
3633
Imre Deak209d7352019-03-07 12:32:35 +02003634 /*
3635 * FIXME: for now we'll only ever use 1 slice; pretend that we have
3636 * only that 1 slice enabled until we have a proper way for on-demand
3637 * toggling of the second slice.
3638 */
3639 if (0 && I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303640 enabled_slices++;
3641
3642 return enabled_slices;
3643}
3644
Matt Roper024c9042015-09-24 15:53:11 -07003645/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003646 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3647 * so assume we'll always need it in order to avoid underruns.
3648 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003649static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003650{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003651 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003652}
3653
Paulo Zanoni56feca92016-09-22 18:00:28 -03003654static bool
3655intel_has_sagv(struct drm_i915_private *dev_priv)
3656{
Lucas De Marchi8ffa4392019-09-04 14:34:18 -07003657 /* HACK! */
3658 if (IS_GEN(dev_priv, 12))
3659 return false;
3660
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003661 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3662 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003663}
3664
Lyude656d1b82016-08-17 15:55:54 -04003665/*
3666 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3667 * depending on power and performance requirements. The display engine access
3668 * to system memory is blocked during the adjustment time. Because of the
3669 * blocking time, having this enabled can cause full system hangs and/or pipe
3670 * underruns if we don't meet all of the following requirements:
3671 *
3672 * - <= 1 pipe enabled
3673 * - All planes can enable watermarks for latencies >= SAGV engine block time
3674 * - We're not using an interlaced display configuration
3675 */
3676int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003677intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003678{
3679 int ret;
3680
Paulo Zanoni56feca92016-09-22 18:00:28 -03003681 if (!intel_has_sagv(dev_priv))
3682 return 0;
3683
3684 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003685 return 0;
3686
Ville Syrjäläff61a972018-12-21 19:14:34 +02003687 DRM_DEBUG_KMS("Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003688 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3689 GEN9_SAGV_ENABLE);
3690
Ville Syrjäläff61a972018-12-21 19:14:34 +02003691 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003692
3693 /*
3694 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003695 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003696 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003697 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003698 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003699 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003700 return 0;
3701 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003702 DRM_ERROR("Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003703 return ret;
3704 }
3705
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003706 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003707 return 0;
3708}
3709
Lyude656d1b82016-08-17 15:55:54 -04003710int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003711intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003712{
Imre Deakb3b8e992016-12-05 18:27:38 +02003713 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003714
Paulo Zanoni56feca92016-09-22 18:00:28 -03003715 if (!intel_has_sagv(dev_priv))
3716 return 0;
3717
3718 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003719 return 0;
3720
Ville Syrjäläff61a972018-12-21 19:14:34 +02003721 DRM_DEBUG_KMS("Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003722 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003723 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3724 GEN9_SAGV_DISABLE,
3725 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3726 1);
Lyude656d1b82016-08-17 15:55:54 -04003727 /*
3728 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003729 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003730 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003731 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003732 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003733 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003734 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003735 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003736 DRM_ERROR("Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003737 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003738 }
3739
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003740 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003741 return 0;
3742}
3743
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003744bool intel_can_enable_sagv(struct intel_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003745{
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003746 struct drm_device *dev = state->base.dev;
Lyude656d1b82016-08-17 15:55:54 -04003747 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003748 struct intel_crtc *crtc;
3749 struct intel_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003750 struct intel_crtc_state *crtc_state;
Lyude656d1b82016-08-17 15:55:54 -04003751 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003752 int level, latency;
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003753 int sagv_block_time_us;
Lyude656d1b82016-08-17 15:55:54 -04003754
Paulo Zanoni56feca92016-09-22 18:00:28 -03003755 if (!intel_has_sagv(dev_priv))
3756 return false;
3757
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003758 if (IS_GEN(dev_priv, 9))
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003759 sagv_block_time_us = 30;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003760 else if (IS_GEN(dev_priv, 10))
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003761 sagv_block_time_us = 20;
3762 else
3763 sagv_block_time_us = 10;
3764
Lyude656d1b82016-08-17 15:55:54 -04003765 /*
Lyude656d1b82016-08-17 15:55:54 -04003766 * If there are no active CRTCs, no additional checks need be performed
3767 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003768 if (hweight8(state->active_pipes) == 0)
Lyude656d1b82016-08-17 15:55:54 -04003769 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003770
3771 /*
3772 * SKL+ workaround: bspec recommends we disable SAGV when we have
3773 * more then one pipe enabled
3774 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003775 if (hweight8(state->active_pipes) > 1)
Lyude656d1b82016-08-17 15:55:54 -04003776 return false;
3777
3778 /* Since we're now guaranteed to only have one active CRTC... */
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03003779 pipe = ffs(state->active_pipes) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003780 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003781 crtc_state = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003782
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003783 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003784 return false;
3785
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003786 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003787 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003788 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003789
Lyude656d1b82016-08-17 15:55:54 -04003790 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003791 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003792 continue;
3793
3794 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003795 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003796 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003797 { }
3798
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003799 latency = dev_priv->wm.skl_latency[level];
3800
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003801 if (skl_needs_memory_bw_wa(dev_priv) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003802 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003803 I915_FORMAT_MOD_X_TILED)
3804 latency += 15;
3805
Lyude656d1b82016-08-17 15:55:54 -04003806 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003807 * If any of the planes on this pipe don't enable wm levels that
3808 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003809 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003810 */
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003811 if (latency < sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003812 return false;
3813 }
3814
3815 return true;
3816}
3817
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303818static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003819 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003820 const u64 total_data_rate,
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303821 const int num_active,
3822 struct skl_ddb_allocation *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303823{
3824 const struct drm_display_mode *adjusted_mode;
3825 u64 total_data_bw;
3826 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3827
3828 WARN_ON(ddb_size == 0);
3829
3830 if (INTEL_GEN(dev_priv) < 11)
3831 return ddb_size - 4; /* 4 blocks for bypass path allocation */
3832
Maarten Lankhorstec193642019-06-28 10:55:17 +02003833 adjusted_mode = &crtc_state->base.adjusted_mode;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003834 total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303835
3836 /*
3837 * 12GB/s is maximum BW supported by single DBuf slice.
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003838 *
3839 * FIXME dbuf slice code is broken:
3840 * - must wait for planes to stop using the slice before powering it off
3841 * - plane straddling both slices is illegal in multi-pipe scenarios
3842 * - should validate we stay within the hw bandwidth limits
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303843 */
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003844 if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303845 ddb->enabled_slices = 2;
3846 } else {
3847 ddb->enabled_slices = 1;
3848 ddb_size /= 2;
3849 }
3850
3851 return ddb_size;
3852}
3853
Damien Lespiaub9cec072014-11-04 17:06:43 +00003854static void
Maarten Lankhorstb048a002018-10-18 13:51:30 +02003855skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003856 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003857 const u64 total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303858 struct skl_ddb_allocation *ddb,
Matt Roperc107acf2016-05-12 07:06:01 -07003859 struct skl_ddb_entry *alloc, /* out */
3860 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003861{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003862 struct drm_atomic_state *state = crtc_state->base.state;
Matt Roperc107acf2016-05-12 07:06:01 -07003863 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003864 struct drm_crtc *for_crtc = crtc_state->base.crtc;
3865 const struct intel_crtc *crtc;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303866 u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
3867 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
3868 u16 ddb_size;
3869 u32 i;
Matt Roperc107acf2016-05-12 07:06:01 -07003870
Maarten Lankhorstec193642019-06-28 10:55:17 +02003871 if (WARN_ON(!state) || !crtc_state->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003872 alloc->start = 0;
3873 alloc->end = 0;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003874 *num_active = hweight8(dev_priv->active_pipes);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003875 return;
3876 }
3877
Matt Ropera6d3460e2016-05-12 07:06:04 -07003878 if (intel_state->active_pipe_changes)
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003879 *num_active = hweight8(intel_state->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003880 else
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003881 *num_active = hweight8(dev_priv->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003882
Maarten Lankhorstec193642019-06-28 10:55:17 +02003883 ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303884 *num_active, ddb);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003885
Matt Roperc107acf2016-05-12 07:06:01 -07003886 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303887 * If the state doesn't change the active CRTC's or there is no
3888 * modeset request, then there's no need to recalculate;
3889 * the existing pipe allocation limits should remain unchanged.
3890 * Note that we're safe from racing commits since any racing commit
3891 * that changes the active CRTC list or do modeset would need to
3892 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003893 */
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303894 if (!intel_state->active_pipe_changes && !intel_state->modeset) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003895 /*
3896 * alloc may be cleared by clear_intel_crtc_state,
3897 * copy from old state to be sure
3898 */
3899 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003900 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003901 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003902
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303903 /*
3904 * Watermark/ddb requirement highly depends upon width of the
3905 * framebuffer, So instead of allocating DDB equally among pipes
3906 * distribute DDB based on resolution/width of the display.
3907 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003908 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3909 const struct drm_display_mode *adjusted_mode =
3910 &crtc_state->base.adjusted_mode;
3911 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303912 int hdisplay, vdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303913
Maarten Lankhorstec193642019-06-28 10:55:17 +02003914 if (!crtc_state->base.enable)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303915 continue;
3916
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303917 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
3918 total_width += hdisplay;
3919
3920 if (pipe < for_pipe)
3921 width_before_pipe += hdisplay;
3922 else if (pipe == for_pipe)
3923 pipe_width = hdisplay;
3924 }
3925
3926 alloc->start = ddb_size * width_before_pipe / total_width;
3927 alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003928}
3929
Ville Syrjälädf331de2019-03-19 18:03:11 +02003930static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
3931 int width, const struct drm_format_info *format,
3932 u64 modifier, unsigned int rotation,
3933 u32 plane_pixel_rate, struct skl_wm_params *wp,
3934 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003935static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02003936 int level,
3937 const struct skl_wm_params *wp,
3938 const struct skl_wm_level *result_prev,
3939 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003940
Ville Syrjälädf331de2019-03-19 18:03:11 +02003941static unsigned int
3942skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
3943 int num_active)
3944{
3945 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3946 int level, max_level = ilk_wm_max_level(dev_priv);
3947 struct skl_wm_level wm = {};
3948 int ret, min_ddb_alloc = 0;
3949 struct skl_wm_params wp;
3950
3951 ret = skl_compute_wm_params(crtc_state, 256,
3952 drm_format_info(DRM_FORMAT_ARGB8888),
3953 DRM_FORMAT_MOD_LINEAR,
3954 DRM_MODE_ROTATE_0,
3955 crtc_state->pixel_rate, &wp, 0);
3956 WARN_ON(ret);
3957
3958 for (level = 0; level <= max_level; level++) {
Ville Syrjälä6086e472019-03-21 19:51:28 +02003959 skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003960 if (wm.min_ddb_alloc == U16_MAX)
3961 break;
3962
3963 min_ddb_alloc = wm.min_ddb_alloc;
3964 }
3965
3966 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003967}
3968
Mahesh Kumar37cde112018-04-26 19:55:17 +05303969static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
3970 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00003971{
Mahesh Kumar37cde112018-04-26 19:55:17 +05303972
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02003973 entry->start = reg & DDB_ENTRY_MASK;
3974 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05303975
Damien Lespiau16160e32014-11-04 17:06:53 +00003976 if (entry->end)
3977 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003978}
3979
Mahesh Kumarddf34312018-04-09 09:11:03 +05303980static void
3981skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
3982 const enum pipe pipe,
3983 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003984 struct skl_ddb_entry *ddb_y,
3985 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05303986{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003987 u32 val, val2;
3988 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05303989
3990 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
3991 if (plane_id == PLANE_CURSOR) {
3992 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003993 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303994 return;
3995 }
3996
3997 val = I915_READ(PLANE_CTL(pipe, plane_id));
3998
3999 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004000 if (val & PLANE_CTL_ENABLE)
4001 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4002 val & PLANE_CTL_ORDER_RGBX,
4003 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304004
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004005 if (INTEL_GEN(dev_priv) >= 11) {
4006 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
4007 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4008 } else {
4009 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07004010 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304011
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004012 if (fourcc &&
4013 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004014 swap(val, val2);
4015
4016 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4017 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304018 }
4019}
4020
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004021void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4022 struct skl_ddb_entry *ddb_y,
4023 struct skl_ddb_entry *ddb_uv)
4024{
4025 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4026 enum intel_display_power_domain power_domain;
4027 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004028 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004029 enum plane_id plane_id;
4030
4031 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004032 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4033 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004034 return;
4035
4036 for_each_plane_id_on_crtc(crtc, plane_id)
4037 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4038 plane_id,
4039 &ddb_y[plane_id],
4040 &ddb_uv[plane_id]);
4041
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004042 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004043}
4044
Damien Lespiau08db6652014-11-04 17:06:52 +00004045void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
4046 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00004047{
Mahesh Kumar74bd8002018-04-26 19:55:15 +05304048 ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
Damien Lespiaua269c582014-11-04 17:06:49 +00004049}
4050
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004051/*
4052 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4053 * The bspec defines downscale amount as:
4054 *
4055 * """
4056 * Horizontal down scale amount = maximum[1, Horizontal source size /
4057 * Horizontal destination size]
4058 * Vertical down scale amount = maximum[1, Vertical source size /
4059 * Vertical destination size]
4060 * Total down scale amount = Horizontal down scale amount *
4061 * Vertical down scale amount
4062 * """
4063 *
4064 * Return value is provided in 16.16 fixed point form to retain fractional part.
4065 * Caller should take care of dividing & rounding off the value.
4066 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304067static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004068skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4069 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004070{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004071 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004072 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304073 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4074 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004075
Maarten Lankhorstec193642019-06-28 10:55:17 +02004076 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304077 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004078
4079 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004080 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004081 /*
4082 * Cursors only support 0/180 degree rotation,
4083 * hence no need to account for rotation here.
4084 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004085 src_w = plane_state->base.src_w >> 16;
4086 src_h = plane_state->base.src_h >> 16;
4087 dst_w = plane_state->base.crtc_w;
4088 dst_h = plane_state->base.crtc_h;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004089 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004090 /*
4091 * Src coordinates are already rotated by 270 degrees for
4092 * the 90/270 degree plane rotation cases (to match the
4093 * GTT mapping), hence no need to account for rotation here.
4094 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004095 src_w = drm_rect_width(&plane_state->base.src) >> 16;
4096 src_h = drm_rect_height(&plane_state->base.src) >> 16;
4097 dst_w = drm_rect_width(&plane_state->base.dst);
4098 dst_h = drm_rect_height(&plane_state->base.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004099 }
4100
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304101 fp_w_ratio = div_fixed16(src_w, dst_w);
4102 fp_h_ratio = div_fixed16(src_h, dst_h);
4103 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4104 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004105
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304106 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004107}
4108
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304109static uint_fixed_16_16_t
4110skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
4111{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304112 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304113
4114 if (!crtc_state->base.enable)
4115 return pipe_downscale;
4116
4117 if (crtc_state->pch_pfit.enabled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004118 u32 src_w, src_h, dst_w, dst_h;
4119 u32 pfit_size = crtc_state->pch_pfit.size;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304120 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4121 uint_fixed_16_16_t downscale_h, downscale_w;
4122
4123 src_w = crtc_state->pipe_src_w;
4124 src_h = crtc_state->pipe_src_h;
4125 dst_w = pfit_size >> 16;
4126 dst_h = pfit_size & 0xffff;
4127
4128 if (!dst_w || !dst_h)
4129 return pipe_downscale;
4130
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304131 fp_w_ratio = div_fixed16(src_w, dst_w);
4132 fp_h_ratio = div_fixed16(src_h, dst_h);
4133 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4134 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304135
4136 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
4137 }
4138
4139 return pipe_downscale;
4140}
4141
4142int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004143 struct intel_crtc_state *crtc_state)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304144{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004145 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004146 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304147 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004148 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004149 int crtc_clock, dotclk;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004150 u32 pipe_max_pixel_rate;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304151 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304152 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304153
Maarten Lankhorstec193642019-06-28 10:55:17 +02004154 if (!crtc_state->base.enable)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304155 return 0;
4156
Maarten Lankhorstec193642019-06-28 10:55:17 +02004157 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304158 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304159 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304160 int bpp;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004161 const struct intel_plane_state *plane_state =
4162 to_intel_plane_state(drm_plane_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304163
Maarten Lankhorstec193642019-06-28 10:55:17 +02004164 if (!intel_wm_plane_visible(crtc_state, plane_state))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304165 continue;
4166
Maarten Lankhorstec193642019-06-28 10:55:17 +02004167 if (WARN_ON(!plane_state->base.fb))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304168 return -EINVAL;
4169
Maarten Lankhorstec193642019-06-28 10:55:17 +02004170 plane_downscale = skl_plane_downscale_amount(crtc_state, plane_state);
4171 bpp = plane_state->base.fb->format->cpp[0] * 8;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304172 if (bpp == 64)
4173 plane_downscale = mul_fixed16(plane_downscale,
4174 fp_9_div_8);
4175
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304176 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304177 }
Maarten Lankhorstec193642019-06-28 10:55:17 +02004178 pipe_downscale = skl_pipe_downscale_amount(crtc_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304179
4180 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
4181
Maarten Lankhorstec193642019-06-28 10:55:17 +02004182 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004183 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
4184
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004185 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004186 dotclk *= 2;
4187
4188 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304189
4190 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004191 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304192 return -EINVAL;
4193 }
4194
4195 return 0;
4196}
4197
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004198static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004199skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4200 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004201 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004202{
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004203 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4204 const struct drm_framebuffer *fb = plane_state->base.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004205 u32 data_rate;
4206 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304207 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004208 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004209
Maarten Lankhorstec193642019-06-28 10:55:17 +02004210 if (!plane_state->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004211 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004212
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004213 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004214 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004215
4216 if (color_plane == 1 &&
4217 !drm_format_info_is_yuv_semiplanar(fb->format))
Matt Ropera1de91e2016-05-12 07:05:57 -07004218 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004219
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004220 /*
4221 * Src coordinates are already rotated by 270 degrees for
4222 * the 90/270 degree plane rotation cases (to match the
4223 * GTT mapping), hence no need to account for rotation here.
4224 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004225 width = drm_rect_width(&plane_state->base.src) >> 16;
4226 height = drm_rect_height(&plane_state->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004227
Mahesh Kumarb879d582018-04-09 09:11:01 +05304228 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004229 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304230 width /= 2;
4231 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004232 }
4233
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004234 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304235
Maarten Lankhorstec193642019-06-28 10:55:17 +02004236 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004237
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004238 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4239
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004240 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004241 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004242}
4243
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004244static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004245skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004246 u64 *plane_data_rate,
4247 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004248{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004249 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004250 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004251 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004252 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004253
4254 if (WARN_ON(!state))
4255 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004256
Matt Ropera1de91e2016-05-12 07:05:57 -07004257 /* Calculate and cache data rate for each plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004258 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004259 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004260 const struct intel_plane_state *plane_state =
4261 to_intel_plane_state(drm_plane_state);
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004262 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004263
Mahesh Kumarb879d582018-04-09 09:11:01 +05304264 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004265 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004266 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004267 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004268
Mahesh Kumarb879d582018-04-09 09:11:01 +05304269 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004270 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304271 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004272 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004273 }
4274
4275 return total_data_rate;
4276}
4277
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004278static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004279icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004280 u64 *plane_data_rate)
4281{
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004282 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004283 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004284 u64 total_data_rate = 0;
4285
Maarten Lankhorstec193642019-06-28 10:55:17 +02004286 if (WARN_ON(!crtc_state->base.state))
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004287 return 0;
4288
4289 /* Calculate and cache data rate for each plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004290 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
4291 const struct intel_plane_state *plane_state =
4292 to_intel_plane_state(drm_plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004293 enum plane_id plane_id = to_intel_plane(plane)->id;
4294 u64 rate;
4295
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004296 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004297 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004298 plane_data_rate[plane_id] = rate;
4299 total_data_rate += rate;
4300 } else {
4301 enum plane_id y_plane_id;
4302
4303 /*
4304 * The slave plane might not iterate in
4305 * drm_atomic_crtc_state_for_each_plane_state(),
4306 * and needs the master plane state which may be
4307 * NULL if we try get_new_plane_state(), so we
4308 * always calculate from the master.
4309 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004310 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004311 continue;
4312
4313 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004314 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004315 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004316 plane_data_rate[y_plane_id] = rate;
4317 total_data_rate += rate;
4318
Maarten Lankhorstec193642019-06-28 10:55:17 +02004319 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004320 plane_data_rate[plane_id] = rate;
4321 total_data_rate += rate;
4322 }
4323 }
4324
4325 return total_data_rate;
4326}
4327
Matt Roperc107acf2016-05-12 07:06:01 -07004328static int
Maarten Lankhorstec193642019-06-28 10:55:17 +02004329skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004330 struct skl_ddb_allocation *ddb /* out */)
4331{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004332 struct drm_atomic_state *state = crtc_state->base.state;
4333 struct drm_crtc *crtc = crtc_state->base.crtc;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004334 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004335 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004336 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004337 u16 alloc_size, start = 0;
4338 u16 total[I915_MAX_PLANES] = {};
4339 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004340 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004341 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004342 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004343 u64 plane_data_rate[I915_MAX_PLANES] = {};
4344 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004345 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004346 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004347
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004348 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004349 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4350 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004351
Matt Ropera6d3460e2016-05-12 07:06:04 -07004352 if (WARN_ON(!state))
4353 return 0;
4354
Maarten Lankhorstec193642019-06-28 10:55:17 +02004355 if (!crtc_state->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004356 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004357 return 0;
4358 }
4359
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004360 if (INTEL_GEN(dev_priv) >= 11)
4361 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004362 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004363 plane_data_rate);
4364 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004365 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004366 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004367 plane_data_rate,
4368 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004369
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004370
Maarten Lankhorstec193642019-06-28 10:55:17 +02004371 skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004372 ddb, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004373 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304374 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004375 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004376
Matt Roperd8e87492018-12-11 09:31:07 -08004377 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004378 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004379 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004380 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004381 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004382 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004383
Matt Ropera1de91e2016-05-12 07:05:57 -07004384 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004385 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004386
Matt Roperd8e87492018-12-11 09:31:07 -08004387 /*
4388 * Find the highest watermark level for which we can satisfy the block
4389 * requirement of active planes.
4390 */
4391 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004392 blocks = 0;
Matt Roperd8e87492018-12-11 09:31:07 -08004393 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004394 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004395 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004396
4397 if (plane_id == PLANE_CURSOR) {
4398 if (WARN_ON(wm->wm[level].min_ddb_alloc >
4399 total[PLANE_CURSOR])) {
4400 blocks = U32_MAX;
4401 break;
4402 }
4403 continue;
4404 }
4405
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004406 blocks += wm->wm[level].min_ddb_alloc;
4407 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004408 }
4409
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004410 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004411 alloc_size -= blocks;
4412 break;
4413 }
4414 }
4415
4416 if (level < 0) {
4417 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4418 DRM_DEBUG_KMS("minimum required %d/%d\n", blocks,
4419 alloc_size);
4420 return -EINVAL;
4421 }
4422
4423 /*
4424 * Grant each plane the blocks it requires at the highest achievable
4425 * watermark level, plus an extra share of the leftover blocks
4426 * proportional to its relative data rate.
4427 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004428 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004429 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004430 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004431 u64 rate;
4432 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004433
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004434 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004435 continue;
4436
Damien Lespiaub9cec072014-11-04 17:06:43 +00004437 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004438 * We've accounted for all active planes; remaining planes are
4439 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004440 */
Matt Roperd8e87492018-12-11 09:31:07 -08004441 if (total_data_rate == 0)
4442 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004443
Matt Roperd8e87492018-12-11 09:31:07 -08004444 rate = plane_data_rate[plane_id];
4445 extra = min_t(u16, alloc_size,
4446 DIV64_U64_ROUND_UP(alloc_size * rate,
4447 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004448 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004449 alloc_size -= extra;
4450 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004451
Matt Roperd8e87492018-12-11 09:31:07 -08004452 if (total_data_rate == 0)
4453 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004454
Matt Roperd8e87492018-12-11 09:31:07 -08004455 rate = uv_plane_data_rate[plane_id];
4456 extra = min_t(u16, alloc_size,
4457 DIV64_U64_ROUND_UP(alloc_size * rate,
4458 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004459 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004460 alloc_size -= extra;
4461 total_data_rate -= rate;
4462 }
4463 WARN_ON(alloc_size != 0 || total_data_rate != 0);
4464
4465 /* Set the actual DDB start/end points for each plane */
4466 start = alloc->start;
4467 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004468 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004469 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004470 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004471 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004472
4473 if (plane_id == PLANE_CURSOR)
4474 continue;
4475
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004476 /* Gen11+ uses a separate plane for UV watermarks */
Matt Roperd8e87492018-12-11 09:31:07 -08004477 WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004478
Matt Roperd8e87492018-12-11 09:31:07 -08004479 /* Leave disabled planes at (0,0) */
4480 if (total[plane_id]) {
4481 plane_alloc->start = start;
4482 start += total[plane_id];
4483 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004484 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004485
Matt Roperd8e87492018-12-11 09:31:07 -08004486 if (uv_total[plane_id]) {
4487 uv_plane_alloc->start = start;
4488 start += uv_total[plane_id];
4489 uv_plane_alloc->end = start;
4490 }
4491 }
4492
4493 /*
4494 * When we calculated watermark values we didn't know how high
4495 * of a level we'd actually be able to hit, so we just marked
4496 * all levels as "enabled." Go back now and disable the ones
4497 * that aren't actually possible.
4498 */
4499 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
4500 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004501 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004502 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004503
4504 /*
4505 * We only disable the watermarks for each plane if
4506 * they exceed the ddb allocation of said plane. This
4507 * is done so that we don't end up touching cursor
4508 * watermarks needlessly when some other plane reduces
4509 * our max possible watermark level.
4510 *
4511 * Bspec has this to say about the PLANE_WM enable bit:
4512 * "All the watermarks at this level for all enabled
4513 * planes must be enabled before the level will be used."
4514 * So this is actually safe to do.
4515 */
4516 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
4517 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
4518 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02004519
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004520 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004521 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004522 * Underruns with WM1+ disabled
4523 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07004524 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02004525 level == 1 && wm->wm[0].plane_en) {
4526 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004527 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
4528 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004529 }
Matt Roperd8e87492018-12-11 09:31:07 -08004530 }
4531 }
4532
4533 /*
4534 * Go back and disable the transition watermark if it turns out we
4535 * don't have enough DDB blocks for it.
4536 */
4537 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004538 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004539 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004540
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02004541 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08004542 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00004543 }
4544
Matt Roperc107acf2016-05-12 07:06:01 -07004545 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004546}
4547
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004548/*
4549 * 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 +02004550 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004551 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4552 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4553*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004554static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004555skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
4556 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004557{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004558 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304559 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004560
4561 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304562 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004563
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304564 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004565 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004566
4567 if (INTEL_GEN(dev_priv) >= 10)
4568 ret = add_fixed16_u32(ret, 1);
4569
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004570 return ret;
4571}
4572
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004573static uint_fixed_16_16_t
4574skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
4575 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004576{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004577 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304578 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004579
4580 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304581 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004582
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004583 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304584 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4585 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304586 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004587 return ret;
4588}
4589
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304590static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004591intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304592{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004593 u32 pixel_rate;
4594 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304595 uint_fixed_16_16_t linetime_us;
4596
Maarten Lankhorstec193642019-06-28 10:55:17 +02004597 if (!crtc_state->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304598 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304599
Maarten Lankhorstec193642019-06-28 10:55:17 +02004600 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304601
4602 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304603 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304604
Maarten Lankhorstec193642019-06-28 10:55:17 +02004605 crtc_htotal = crtc_state->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304606 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304607
4608 return linetime_us;
4609}
4610
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004611static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004612skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
4613 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004614{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004615 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304616 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004617
4618 /* Shouldn't reach here on disabled planes... */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004619 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004620 return 0;
4621
4622 /*
4623 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4624 * with additional adjustments for plane-specific scaling.
4625 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004626 adjusted_pixel_rate = crtc_state->pixel_rate;
4627 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004628
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304629 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4630 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004631}
4632
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304633static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004634skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4635 int width, const struct drm_format_info *format,
4636 u64 modifier, unsigned int rotation,
4637 u32 plane_pixel_rate, struct skl_wm_params *wp,
4638 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304639{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004640 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4641 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004642 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304643
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304644 /* only planar format has two planes */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004645 if (color_plane == 1 && !drm_format_info_is_yuv_semiplanar(format)) {
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304646 DRM_DEBUG_KMS("Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304647 return -EINVAL;
4648 }
4649
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004650 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
4651 modifier == I915_FORMAT_MOD_Yf_TILED ||
4652 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4653 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4654 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
4655 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4656 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004657 wp->is_planar = drm_format_info_is_yuv_semiplanar(format);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304658
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004659 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02004660 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304661 wp->width /= 2;
4662
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004663 wp->cpp = format->cpp[color_plane];
4664 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304665
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004666 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004667 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004668 wp->dbuf_block_size = 256;
4669 else
4670 wp->dbuf_block_size = 512;
4671
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004672 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304673 switch (wp->cpp) {
4674 case 1:
4675 wp->y_min_scanlines = 16;
4676 break;
4677 case 2:
4678 wp->y_min_scanlines = 8;
4679 break;
4680 case 4:
4681 wp->y_min_scanlines = 4;
4682 break;
4683 default:
4684 MISSING_CASE(wp->cpp);
4685 return -EINVAL;
4686 }
4687 } else {
4688 wp->y_min_scanlines = 4;
4689 }
4690
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004691 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304692 wp->y_min_scanlines *= 2;
4693
4694 wp->plane_bytes_per_line = wp->width * wp->cpp;
4695 if (wp->y_tiled) {
4696 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004697 wp->y_min_scanlines,
4698 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304699
4700 if (INTEL_GEN(dev_priv) >= 10)
4701 interm_pbpl++;
4702
4703 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4704 wp->y_min_scanlines);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004705 } else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004706 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4707 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304708 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4709 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004710 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4711 wp->dbuf_block_size) + 1;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304712 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4713 }
4714
4715 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4716 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004717
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304718 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004719 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304720
4721 return 0;
4722}
4723
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004724static int
4725skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
4726 const struct intel_plane_state *plane_state,
4727 struct skl_wm_params *wp, int color_plane)
4728{
4729 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4730 const struct drm_framebuffer *fb = plane_state->base.fb;
4731 int width;
4732
4733 if (plane->id == PLANE_CURSOR) {
4734 width = plane_state->base.crtc_w;
4735 } else {
4736 /*
4737 * Src coordinates are already rotated by 270 degrees for
4738 * the 90/270 degree plane rotation cases (to match the
4739 * GTT mapping), hence no need to account for rotation here.
4740 */
4741 width = drm_rect_width(&plane_state->base.src) >> 16;
4742 }
4743
4744 return skl_compute_wm_params(crtc_state, width,
4745 fb->format, fb->modifier,
4746 plane_state->base.rotation,
4747 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
4748 wp, color_plane);
4749}
4750
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004751static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
4752{
4753 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4754 return true;
4755
4756 /* The number of lines are ignored for the level 0 watermark. */
4757 return level > 0;
4758}
4759
Maarten Lankhorstec193642019-06-28 10:55:17 +02004760static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08004761 int level,
4762 const struct skl_wm_params *wp,
4763 const struct skl_wm_level *result_prev,
4764 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004765{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004766 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004767 u32 latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304768 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304769 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004770 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02004771
Ville Syrjälä0aded172019-02-05 17:50:53 +02004772 if (latency == 0) {
4773 /* reject it */
4774 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02004775 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004776 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02004777
Ville Syrjälä25312ef2019-05-03 20:38:05 +03004778 /*
4779 * WaIncreaseLatencyIPCEnabled: kbl,cfl
4780 * Display WA #1141: kbl,cfl
4781 */
Ville Syrjälä5a7d2022019-05-03 20:38:06 +03004782 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004783 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304784 latency += 4;
4785
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004786 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004787 latency += 15;
4788
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304789 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004790 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304791 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004792 crtc_state->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004793 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304794 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004795
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304796 if (wp->y_tiled) {
4797 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004798 } else {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004799 if ((wp->cpp * crtc_state->base.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004800 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004801 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004802 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004803 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004804 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004805 !IS_GEMINILAKE(dev_priv))
4806 selected_result = min_fixed16(method1, method2);
4807 else
4808 selected_result = method2;
4809 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004810 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004811 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004812 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004813
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304814 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304815 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304816 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004817
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004818 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
4819 /* Display WA #1125: skl,bxt,kbl */
4820 if (level == 0 && wp->rc_surface)
4821 res_blocks +=
4822 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004823
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004824 /* Display WA #1126: skl,bxt,kbl */
4825 if (level >= 1 && level <= 7) {
4826 if (wp->y_tiled) {
4827 res_blocks +=
4828 fixed16_to_u32_round_up(wp->y_tile_minimum);
4829 res_lines += wp->y_min_scanlines;
4830 } else {
4831 res_blocks++;
4832 }
4833
4834 /*
4835 * Make sure result blocks for higher latency levels are
4836 * atleast as high as level below the current level.
4837 * Assumption in DDB algorithm optimization for special
4838 * cases. Also covers Display WA #1125 for RC.
4839 */
4840 if (result_prev->plane_res_b > res_blocks)
4841 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004842 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004843 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004844
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004845 if (INTEL_GEN(dev_priv) >= 11) {
4846 if (wp->y_tiled) {
4847 int extra_lines;
4848
4849 if (res_lines % wp->y_min_scanlines == 0)
4850 extra_lines = wp->y_min_scanlines;
4851 else
4852 extra_lines = wp->y_min_scanlines * 2 -
4853 res_lines % wp->y_min_scanlines;
4854
4855 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
4856 wp->plane_blocks_per_line);
4857 } else {
4858 min_ddb_alloc = res_blocks +
4859 DIV_ROUND_UP(res_blocks, 10);
4860 }
4861 }
4862
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004863 if (!skl_wm_has_lines(dev_priv, level))
4864 res_lines = 0;
4865
Ville Syrjälä0aded172019-02-05 17:50:53 +02004866 if (res_lines > 31) {
4867 /* reject it */
4868 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08004869 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004870 }
Matt Roperd8e87492018-12-11 09:31:07 -08004871
4872 /*
4873 * If res_lines is valid, assume we can use this watermark level
4874 * for now. We'll come back and disable it after we calculate the
4875 * DDB allocation if it turns out we don't actually have enough
4876 * blocks to satisfy it.
4877 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05304878 result->plane_res_b = res_blocks;
4879 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004880 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
4881 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05304882 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004883}
4884
Matt Roperd8e87492018-12-11 09:31:07 -08004885static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02004886skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304887 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004888 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004889{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004890 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304891 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004892 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04004893
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304894 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004895 struct skl_wm_level *result = &levels[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304896
Maarten Lankhorstec193642019-06-28 10:55:17 +02004897 skl_compute_plane_wm(crtc_state, level, wm_params,
Matt Roperd8e87492018-12-11 09:31:07 -08004898 result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004899
4900 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304901 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004902}
4903
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004904static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004905skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004906{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004907 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304908 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304909 uint_fixed_16_16_t linetime_us;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004910 u32 linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004911
Maarten Lankhorstec193642019-06-28 10:55:17 +02004912 linetime_us = intel_get_linetime_us(crtc_state);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304913 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304914
Ville Syrjälä717671c2018-12-21 19:14:36 +02004915 /* Display WA #1135: BXT:ALL GLK:ALL */
4916 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304917 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304918
4919 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004920}
4921
Maarten Lankhorstec193642019-06-28 10:55:17 +02004922static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004923 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08004924 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004925{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004926 struct drm_device *dev = crtc_state->base.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05304927 const struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004928 u16 trans_min, trans_y_tile_min;
4929 const u16 trans_amount = 10; /* This is configurable amount */
4930 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004931
Kumar, Maheshca476672017-08-17 19:15:24 +05304932 /* Transition WM are not recommended by HW team for GEN9 */
4933 if (INTEL_GEN(dev_priv) <= 9)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004934 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304935
4936 /* Transition WM don't make any sense if ipc is disabled */
4937 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004938 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304939
Paulo Zanoni91961a82018-10-04 16:15:56 -07004940 trans_min = 14;
4941 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05304942 trans_min = 4;
4943
4944 trans_offset_b = trans_min + trans_amount;
4945
Paulo Zanonicbacc792018-10-04 16:15:58 -07004946 /*
4947 * The spec asks for Selected Result Blocks for wm0 (the real value),
4948 * not Result Blocks (the integer value). Pay attention to the capital
4949 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
4950 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
4951 * and since we later will have to get the ceiling of the sum in the
4952 * transition watermarks calculation, we can just pretend Selected
4953 * Result Blocks is Result Blocks minus 1 and it should work for the
4954 * current platforms.
4955 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004956 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07004957
Kumar, Maheshca476672017-08-17 19:15:24 +05304958 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004959 trans_y_tile_min =
4960 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07004961 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05304962 trans_offset_b;
4963 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07004964 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05304965
4966 /* WA BUG:1938466 add one block for non y-tile planes */
4967 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4968 res_blocks += 1;
4969
4970 }
4971
Matt Roperd8e87492018-12-11 09:31:07 -08004972 /*
4973 * Just assume we can enable the transition watermark. After
4974 * computing the DDB we'll come back and disable it if that
4975 * assumption turns out to be false.
4976 */
4977 wm->trans_wm.plane_res_b = res_blocks + 1;
4978 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004979}
4980
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004981static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004982 const struct intel_plane_state *plane_state,
4983 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004984{
Ville Syrjälä83158472018-11-27 18:57:26 +02004985 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004986 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004987 int ret;
4988
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004989 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004990 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004991 if (ret)
4992 return ret;
4993
Ville Syrjälä67155a62019-03-12 22:58:37 +02004994 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Matt Roperd8e87492018-12-11 09:31:07 -08004995 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004996
4997 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004998}
4999
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005000static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005001 const struct intel_plane_state *plane_state,
5002 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005003{
Ville Syrjälä83158472018-11-27 18:57:26 +02005004 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
5005 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005006 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005007
Ville Syrjälä83158472018-11-27 18:57:26 +02005008 wm->is_planar = true;
5009
5010 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005011 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005012 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005013 if (ret)
5014 return ret;
5015
Ville Syrjälä67155a62019-03-12 22:58:37 +02005016 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005017
5018 return 0;
5019}
5020
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005021static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005022 const struct intel_plane_state *plane_state)
5023{
5024 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
5025 const struct drm_framebuffer *fb = plane_state->base.fb;
5026 enum plane_id plane_id = plane->id;
5027 int ret;
5028
5029 if (!intel_wm_plane_visible(crtc_state, plane_state))
5030 return 0;
5031
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005032 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005033 plane_id, 0);
5034 if (ret)
5035 return ret;
5036
5037 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005038 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005039 plane_id);
5040 if (ret)
5041 return ret;
5042 }
5043
5044 return 0;
5045}
5046
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005047static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005048 const struct intel_plane_state *plane_state)
5049{
5050 enum plane_id plane_id = to_intel_plane(plane_state->base.plane)->id;
5051 int ret;
5052
5053 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005054 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005055 return 0;
5056
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005057 if (plane_state->planar_linked_plane) {
Ville Syrjälä83158472018-11-27 18:57:26 +02005058 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005059 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005060
5061 WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
5062 WARN_ON(!fb->format->is_yuv ||
5063 fb->format->num_planes == 1);
5064
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005065 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005066 y_plane_id, 0);
5067 if (ret)
5068 return ret;
5069
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005070 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005071 plane_id, 1);
5072 if (ret)
5073 return ret;
5074 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005075 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005076 plane_id, 0);
5077 if (ret)
5078 return ret;
5079 }
5080
5081 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005082}
5083
Maarten Lankhorstec193642019-06-28 10:55:17 +02005084static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005085{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005086 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
5087 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305088 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005089 const struct drm_plane_state *drm_plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005090 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005091
Lyudea62163e2016-10-04 14:28:20 -04005092 /*
5093 * We'll only calculate watermarks for planes that are actually
5094 * enabled, so make sure all other planes are set as disabled.
5095 */
5096 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5097
Maarten Lankhorstec193642019-06-28 10:55:17 +02005098 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state,
5099 &crtc_state->base) {
5100 const struct intel_plane_state *plane_state =
5101 to_intel_plane_state(drm_plane_state);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305102
Ville Syrjälä83158472018-11-27 18:57:26 +02005103 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005104 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005105 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005106 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305107 if (ret)
5108 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005109 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305110
Maarten Lankhorstec193642019-06-28 10:55:17 +02005111 pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005112
Matt Roper55994c22016-05-12 07:06:08 -07005113 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005114}
5115
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005116static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5117 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005118 const struct skl_ddb_entry *entry)
5119{
5120 if (entry->end)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005121 I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005122 else
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005123 I915_WRITE_FW(reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005124}
5125
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005126static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5127 i915_reg_t reg,
5128 const struct skl_wm_level *level)
5129{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005130 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005131
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005132 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005133 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005134 if (level->ignore_lines)
5135 val |= PLANE_WM_IGNORE_LINES;
5136 val |= level->plane_res_b;
5137 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005138
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005139 I915_WRITE_FW(reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005140}
5141
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005142void skl_write_plane_wm(struct intel_plane *plane,
5143 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005144{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005145 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005146 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005147 enum plane_id plane_id = plane->id;
5148 enum pipe pipe = plane->pipe;
5149 const struct skl_plane_wm *wm =
5150 &crtc_state->wm.skl.optimal.planes[plane_id];
5151 const struct skl_ddb_entry *ddb_y =
5152 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5153 const struct skl_ddb_entry *ddb_uv =
5154 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005155
5156 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005157 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005158 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005159 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005160 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005161 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005162
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005163 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005164 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005165 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5166 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305167 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005168
5169 if (wm->is_planar)
5170 swap(ddb_y, ddb_uv);
5171
5172 skl_ddb_entry_write(dev_priv,
5173 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5174 skl_ddb_entry_write(dev_priv,
5175 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005176}
5177
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005178void skl_write_cursor_wm(struct intel_plane *plane,
5179 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005180{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005181 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005182 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005183 enum plane_id plane_id = plane->id;
5184 enum pipe pipe = plane->pipe;
5185 const struct skl_plane_wm *wm =
5186 &crtc_state->wm.skl.optimal.planes[plane_id];
5187 const struct skl_ddb_entry *ddb =
5188 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005189
5190 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005191 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5192 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005193 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005194 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005195
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005196 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005197}
5198
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005199bool skl_wm_level_equals(const struct skl_wm_level *l1,
5200 const struct skl_wm_level *l2)
5201{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005202 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005203 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005204 l1->plane_res_l == l2->plane_res_l &&
5205 l1->plane_res_b == l2->plane_res_b;
5206}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005207
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005208static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5209 const struct skl_plane_wm *wm1,
5210 const struct skl_plane_wm *wm2)
5211{
5212 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005213
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005214 for (level = 0; level <= max_level; level++) {
5215 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
5216 !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
5217 return false;
5218 }
5219
5220 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005221}
5222
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005223static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
5224 const struct skl_pipe_wm *wm1,
5225 const struct skl_pipe_wm *wm2)
5226{
5227 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5228 enum plane_id plane_id;
5229
5230 for_each_plane_id_on_crtc(crtc, plane_id) {
5231 if (!skl_plane_wm_equals(dev_priv,
5232 &wm1->planes[plane_id],
5233 &wm2->planes[plane_id]))
5234 return false;
5235 }
5236
5237 return wm1->linetime == wm2->linetime;
5238}
5239
Lyude27082492016-08-24 07:48:10 +02005240static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5241 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005242{
Lyude27082492016-08-24 07:48:10 +02005243 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005244}
5245
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005246bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005247 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005248 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005249{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005250 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005251
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005252 for (i = 0; i < num_entries; i++) {
5253 if (i != ignore_idx &&
5254 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005255 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005256 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005257
Lyude27082492016-08-24 07:48:10 +02005258 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005259}
5260
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005261static u32
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005262pipes_modified(struct intel_atomic_state *state)
Matt Roper9b613022016-06-27 16:42:44 -07005263{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005264 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005265 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005266 u32 i, ret = 0;
Matt Roper9b613022016-06-27 16:42:44 -07005267
Maarten Lankhorstec193642019-06-28 10:55:17 +02005268 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005269 ret |= drm_crtc_mask(&crtc->base);
Matt Roper9b613022016-06-27 16:42:44 -07005270
5271 return ret;
5272}
5273
Jani Nikulabb7791b2016-10-04 12:29:17 +03005274static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005275skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5276 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005277{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005278 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->base.state);
5279 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
5280 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5281 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005282
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005283 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5284 struct intel_plane_state *plane_state;
5285 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005286
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005287 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5288 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5289 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5290 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005291 continue;
5292
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005293 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005294 if (IS_ERR(plane_state))
5295 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005296
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005297 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005298 }
5299
5300 return 0;
5301}
5302
5303static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005304skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005305{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005306 const struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5307 struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005308 struct intel_crtc_state *old_crtc_state;
5309 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305310 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305311 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005312
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005313 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
5314
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005315 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005316 new_crtc_state, i) {
5317 ret = skl_allocate_pipe_ddb(new_crtc_state, ddb);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005318 if (ret)
5319 return ret;
5320
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005321 ret = skl_ddb_add_affected_planes(old_crtc_state,
5322 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005323 if (ret)
5324 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005325 }
5326
5327 return 0;
5328}
5329
Ville Syrjäläab98e942019-02-08 22:05:27 +02005330static char enast(bool enable)
5331{
5332 return enable ? '*' : ' ';
5333}
5334
Matt Roper2722efb2016-08-17 15:55:55 -04005335static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005336skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005337{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005338 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5339 const struct intel_crtc_state *old_crtc_state;
5340 const struct intel_crtc_state *new_crtc_state;
5341 struct intel_plane *plane;
5342 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005343 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005344
Ville Syrjäläab98e942019-02-08 22:05:27 +02005345 if ((drm_debug & DRM_UT_KMS) == 0)
5346 return;
5347
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005348 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5349 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005350 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5351
5352 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5353 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5354
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005355 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5356 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005357 const struct skl_ddb_entry *old, *new;
5358
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005359 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5360 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005361
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005362 if (skl_ddb_entry_equal(old, new))
5363 continue;
5364
Ville Syrjäläab98e942019-02-08 22:05:27 +02005365 DRM_DEBUG_KMS("[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005366 plane->base.base.id, plane->base.name,
Ville Syrjäläab98e942019-02-08 22:05:27 +02005367 old->start, old->end, new->start, new->end,
5368 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
5369 }
5370
5371 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5372 enum plane_id plane_id = plane->id;
5373 const struct skl_plane_wm *old_wm, *new_wm;
5374
5375 old_wm = &old_pipe_wm->planes[plane_id];
5376 new_wm = &new_pipe_wm->planes[plane_id];
5377
5378 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5379 continue;
5380
5381 DRM_DEBUG_KMS("[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm"
5382 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm\n",
5383 plane->base.base.id, plane->base.name,
5384 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5385 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5386 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5387 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5388 enast(old_wm->trans_wm.plane_en),
5389 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5390 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5391 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5392 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
5393 enast(new_wm->trans_wm.plane_en));
5394
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005395 DRM_DEBUG_KMS("[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d"
5396 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d\n",
Ville Syrjäläab98e942019-02-08 22:05:27 +02005397 plane->base.base.id, plane->base.name,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005398 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5399 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5400 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5401 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5402 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5403 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5404 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5405 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5406 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
5407
5408 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5409 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5410 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5411 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5412 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5413 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5414 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5415 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
5416 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005417
5418 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5419 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5420 plane->base.base.id, plane->base.name,
5421 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5422 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5423 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5424 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5425 old_wm->trans_wm.plane_res_b,
5426 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5427 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5428 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5429 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
5430 new_wm->trans_wm.plane_res_b);
5431
5432 DRM_DEBUG_KMS("[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5433 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5434 plane->base.base.id, plane->base.name,
5435 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5436 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5437 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5438 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5439 old_wm->trans_wm.min_ddb_alloc,
5440 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5441 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5442 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5443 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
5444 new_wm->trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005445 }
5446 }
5447}
5448
Matt Roper98d39492016-05-12 07:06:03 -07005449static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005450skl_ddb_add_affected_pipes(struct intel_atomic_state *state, bool *changed)
Matt Roper98d39492016-05-12 07:06:03 -07005451{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005452 struct drm_device *dev = state->base.dev;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305453 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005454 struct intel_crtc *crtc;
5455 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005456 u32 realloc_pipes = pipes_modified(state);
Matt Roper734fa012016-05-12 15:11:40 -07005457 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005458
5459 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005460 * When we distrust bios wm we always need to recompute to set the
5461 * expected DDB allocations for each CRTC.
5462 */
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305463 if (dev_priv->wm.distrust_bios_wm)
5464 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005465
5466 /*
Matt Roper98d39492016-05-12 07:06:03 -07005467 * If this transaction isn't actually touching any CRTC's, don't
5468 * bother with watermark calculation. Note that if we pass this
5469 * test, we're guaranteed to hold at least one CRTC state mutex,
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005470 * which means we can safely use values like dev_priv->active_pipes
Matt Roper98d39492016-05-12 07:06:03 -07005471 * since any racing commits that want to update them would need to
5472 * hold _all_ CRTC state mutexes.
5473 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005474 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305475 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005476
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305477 if (!*changed)
Matt Roper98d39492016-05-12 07:06:03 -07005478 return 0;
5479
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305480 /*
5481 * If this is our first atomic update following hardware readout,
5482 * we can't trust the DDB that the BIOS programmed for us. Let's
5483 * pretend that all pipes switched active status so that we'll
5484 * ensure a full DDB recompute.
5485 */
5486 if (dev_priv->wm.distrust_bios_wm) {
5487 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005488 state->base.acquire_ctx);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305489 if (ret)
5490 return ret;
5491
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005492 state->active_pipe_changes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305493
5494 /*
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005495 * We usually only initialize state->active_pipes if we
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305496 * we're doing a modeset; make sure this field is always
5497 * initialized during the sanitization process that happens
5498 * on the first commit too.
5499 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005500 if (!state->modeset)
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005501 state->active_pipes = dev_priv->active_pipes;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305502 }
5503
5504 /*
5505 * If the modeset changes which CRTC's are active, we need to
5506 * recompute the DDB allocation for *all* active pipes, even
5507 * those that weren't otherwise being modified in any way by this
5508 * atomic commit. Due to the shrinking of the per-pipe allocations
5509 * when new active CRTC's are added, it's possible for a pipe that
5510 * we were already using and aren't changing at all here to suddenly
5511 * become invalid if its DDB needs exceeds its new allocation.
5512 *
5513 * Note that if we wind up doing a full DDB recompute, we can't let
5514 * any other display updates race with this transaction, so we need
5515 * to grab the lock on *all* CRTC's.
5516 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005517 if (state->active_pipe_changes || state->modeset) {
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305518 realloc_pipes = ~0;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005519 state->wm_results.dirty_pipes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305520 }
5521
5522 /*
5523 * We're not recomputing for the pipes not included in the commit, so
5524 * make sure we start with the current state.
5525 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005526 for_each_intel_crtc_mask(dev, crtc, realloc_pipes) {
5527 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5528 if (IS_ERR(crtc_state))
5529 return PTR_ERR(crtc_state);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305530 }
5531
5532 return 0;
5533}
5534
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005535/*
5536 * To make sure the cursor watermark registers are always consistent
5537 * with our computed state the following scenario needs special
5538 * treatment:
5539 *
5540 * 1. enable cursor
5541 * 2. move cursor entirely offscreen
5542 * 3. disable cursor
5543 *
5544 * Step 2. does call .disable_plane() but does not zero the watermarks
5545 * (since we consider an offscreen cursor still active for the purposes
5546 * of watermarks). Step 3. would not normally call .disable_plane()
5547 * because the actual plane visibility isn't changing, and we don't
5548 * deallocate the cursor ddb until the pipe gets disabled. So we must
5549 * force step 3. to call .disable_plane() to update the watermark
5550 * registers properly.
5551 *
5552 * Other planes do not suffer from this issues as their watermarks are
5553 * calculated based on the actual plane visibility. The only time this
5554 * can trigger for the other planes is during the initial readout as the
5555 * default value of the watermarks registers is not zero.
5556 */
5557static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
5558 struct intel_crtc *crtc)
5559{
5560 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5561 const struct intel_crtc_state *old_crtc_state =
5562 intel_atomic_get_old_crtc_state(state, crtc);
5563 struct intel_crtc_state *new_crtc_state =
5564 intel_atomic_get_new_crtc_state(state, crtc);
5565 struct intel_plane *plane;
5566
5567 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5568 struct intel_plane_state *plane_state;
5569 enum plane_id plane_id = plane->id;
5570
5571 /*
5572 * Force a full wm update for every plane on modeset.
5573 * Required because the reset value of the wm registers
5574 * is non-zero, whereas we want all disabled planes to
5575 * have zero watermarks. So if we turn off the relevant
5576 * power well the hardware state will go out of sync
5577 * with the software state.
5578 */
5579 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) &&
5580 skl_plane_wm_equals(dev_priv,
5581 &old_crtc_state->wm.skl.optimal.planes[plane_id],
5582 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
5583 continue;
5584
5585 plane_state = intel_atomic_get_plane_state(state, plane);
5586 if (IS_ERR(plane_state))
5587 return PTR_ERR(plane_state);
5588
5589 new_crtc_state->update_planes |= BIT(plane_id);
5590 }
5591
5592 return 0;
5593}
5594
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305595static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005596skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305597{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005598 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005599 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005600 struct intel_crtc_state *old_crtc_state;
5601 struct skl_ddb_values *results = &state->wm_results;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305602 bool changed = false;
5603 int ret, i;
5604
Matt Roper734fa012016-05-12 15:11:40 -07005605 /* Clear all dirty flags */
5606 results->dirty_pipes = 0;
5607
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305608 ret = skl_ddb_add_affected_pipes(state, &changed);
5609 if (ret || !changed)
5610 return ret;
5611
Matt Roper734fa012016-05-12 15:11:40 -07005612 /*
5613 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08005614 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Matt Roper734fa012016-05-12 15:11:40 -07005615 * weren't otherwise being modified (and set bits in dirty_pipes) if
5616 * pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07005617 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005618 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005619 new_crtc_state, i) {
5620 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005621 if (ret)
5622 return ret;
5623
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005624 ret = skl_wm_add_affected_planes(state, crtc);
Matt Roper734fa012016-05-12 15:11:40 -07005625 if (ret)
5626 return ret;
5627
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005628 if (!skl_pipe_wm_equals(crtc,
5629 &old_crtc_state->wm.skl.optimal,
5630 &new_crtc_state->wm.skl.optimal))
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005631 results->dirty_pipes |= drm_crtc_mask(&crtc->base);
Matt Roper734fa012016-05-12 15:11:40 -07005632 }
5633
Matt Roperd8e87492018-12-11 09:31:07 -08005634 ret = skl_compute_ddb(state);
5635 if (ret)
5636 return ret;
5637
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005638 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005639
Matt Roper98d39492016-05-12 07:06:03 -07005640 return 0;
5641}
5642
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005643static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005644 struct intel_crtc_state *crtc_state)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005645{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005646 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005647 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005648 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005649 enum pipe pipe = crtc->pipe;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005650
5651 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5652 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005653
5654 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
5655}
5656
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005657static void skl_initial_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005658 struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005659{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005661 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005662 struct drm_i915_private *dev_priv = to_i915(dev);
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305663 struct skl_ddb_values *results = &state->wm_results;
Bob Paauweadda50b2015-07-21 10:42:53 -07005664
Ville Syrjälä432081b2016-10-31 22:37:03 +02005665 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005666 return;
5667
Matt Roper734fa012016-05-12 15:11:40 -07005668 mutex_lock(&dev_priv->wm.wm_mutex);
5669
Maarten Lankhorstec193642019-06-28 10:55:17 +02005670 if (crtc_state->base.active_changed)
5671 skl_atomic_update_crtc_wm(state, crtc_state);
Lyude27082492016-08-24 07:48:10 +02005672
Matt Roper734fa012016-05-12 15:11:40 -07005673 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005674}
5675
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005676static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02005677 struct intel_wm_config *config)
5678{
5679 struct intel_crtc *crtc;
5680
5681 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005682 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02005683 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5684
5685 if (!wm->pipe_enabled)
5686 continue;
5687
5688 config->sprites_enabled |= wm->sprites_enabled;
5689 config->sprites_scaled |= wm->sprites_scaled;
5690 config->num_pipes_active++;
5691 }
5692}
5693
Matt Ropered4a6a72016-02-23 17:20:13 -08005694static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005695{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005696 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005697 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005698 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005699 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005700 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005701
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005702 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02005703
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005704 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
5705 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005706
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005707 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005708 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005709 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005710 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
5711 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005712
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005713 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005714 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005715 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005716 }
5717
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005718 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005719 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005720
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005721 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005722
Imre Deak820c1982013-12-17 14:46:36 +02005723 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005724}
5725
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005726static void ilk_initial_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005727 struct intel_crtc_state *crtc_state)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005728{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005729 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005730 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005731
Matt Ropered4a6a72016-02-23 17:20:13 -08005732 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005733 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005734 ilk_program_watermarks(dev_priv);
5735 mutex_unlock(&dev_priv->wm.wm_mutex);
5736}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005737
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005738static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005739 struct intel_crtc_state *crtc_state)
Matt Ropered4a6a72016-02-23 17:20:13 -08005740{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005741 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005742 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5743
5744 if (!crtc_state->wm.need_postvbl_update)
5745 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08005746
5747 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005748 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
5749 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08005750 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005751}
5752
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005753static inline void skl_wm_level_from_reg_val(u32 val,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005754 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005755{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005756 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005757 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005758 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5759 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5760 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005761}
5762
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005763void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005764 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005765{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005766 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5767 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005768 int level, max_level;
5769 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005770 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005771
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005772 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005773
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005774 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005775 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005776
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005777 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005778 if (plane_id != PLANE_CURSOR)
5779 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005780 else
5781 val = I915_READ(CUR_WM(pipe, level));
5782
5783 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5784 }
5785
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005786 if (plane_id != PLANE_CURSOR)
5787 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005788 else
5789 val = I915_READ(CUR_WM_TRANS(pipe));
5790
5791 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5792 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005793
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005794 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005795 return;
5796
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005797 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005798}
5799
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005800void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00005801{
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305802 struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005803 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005804 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005805 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00005806
Damien Lespiaua269c582014-11-04 17:06:49 +00005807 skl_ddb_get_hw_state(dev_priv, ddb);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005808 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02005809 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005810
Maarten Lankhorstec193642019-06-28 10:55:17 +02005811 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005812
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005813 if (crtc->active)
5814 hw->dirty_pipes |= drm_crtc_mask(&crtc->base);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005815 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005816
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005817 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07005818 /* Fully recompute DDB on first atomic commit */
5819 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07005820 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005821}
5822
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005823static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005824{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005825 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005826 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005827 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005828 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
5829 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005830 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005831 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005832 [PIPE_A] = WM0_PIPEA_ILK,
5833 [PIPE_B] = WM0_PIPEB_ILK,
5834 [PIPE_C] = WM0_PIPEC_IVB,
5835 };
5836
5837 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005838 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005839 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005840
Ville Syrjälä15606532016-05-13 17:55:17 +03005841 memset(active, 0, sizeof(*active));
5842
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005843 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005844
5845 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005846 u32 tmp = hw->wm_pipe[pipe];
5847
5848 /*
5849 * For active pipes LP0 watermark is marked as
5850 * enabled, and LP1+ watermaks as disabled since
5851 * we can't really reverse compute them in case
5852 * multiple pipes are active.
5853 */
5854 active->wm[0].enable = true;
5855 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5856 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5857 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5858 active->linetime = hw->wm_linetime[pipe];
5859 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005860 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005861
5862 /*
5863 * For inactive pipes, all watermark levels
5864 * should be marked as enabled but zeroed,
5865 * which is what we'd compute them to.
5866 */
5867 for (level = 0; level <= max_level; level++)
5868 active->wm[level].enable = true;
5869 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005870
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005871 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005872}
5873
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005874#define _FW_WM(value, plane) \
5875 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5876#define _FW_WM_VLV(value, plane) \
5877 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5878
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005879static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5880 struct g4x_wm_values *wm)
5881{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005882 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005883
5884 tmp = I915_READ(DSPFW1);
5885 wm->sr.plane = _FW_WM(tmp, SR);
5886 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5887 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5888 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5889
5890 tmp = I915_READ(DSPFW2);
5891 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5892 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5893 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5894 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5895 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5896 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5897
5898 tmp = I915_READ(DSPFW3);
5899 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5900 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5901 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5902 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5903}
5904
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005905static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5906 struct vlv_wm_values *wm)
5907{
5908 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005909 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005910
5911 for_each_pipe(dev_priv, pipe) {
5912 tmp = I915_READ(VLV_DDL(pipe));
5913
Ville Syrjälä1b313892016-11-28 19:37:08 +02005914 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005915 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005916 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005917 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005918 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005919 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005920 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005921 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5922 }
5923
5924 tmp = I915_READ(DSPFW1);
5925 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005926 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5927 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5928 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005929
5930 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005931 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5932 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5933 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005934
5935 tmp = I915_READ(DSPFW3);
5936 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5937
5938 if (IS_CHERRYVIEW(dev_priv)) {
5939 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005940 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5941 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005942
5943 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005944 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5945 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005946
5947 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005948 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5949 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005950
5951 tmp = I915_READ(DSPHOWM);
5952 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005953 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5954 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5955 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5956 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5957 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5958 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5959 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5960 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5961 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005962 } else {
5963 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005964 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5965 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005966
5967 tmp = I915_READ(DSPHOWM);
5968 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005969 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5970 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5971 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5972 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5973 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5974 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005975 }
5976}
5977
5978#undef _FW_WM
5979#undef _FW_WM_VLV
5980
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005981void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005982{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005983 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5984 struct intel_crtc *crtc;
5985
5986 g4x_read_wm_values(dev_priv, wm);
5987
5988 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5989
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005990 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005991 struct intel_crtc_state *crtc_state =
5992 to_intel_crtc_state(crtc->base.state);
5993 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5994 struct g4x_pipe_wm *raw;
5995 enum pipe pipe = crtc->pipe;
5996 enum plane_id plane_id;
5997 int level, max_level;
5998
5999 active->cxsr = wm->cxsr;
6000 active->hpll_en = wm->hpll_en;
6001 active->fbc_en = wm->fbc_en;
6002
6003 active->sr = wm->sr;
6004 active->hpll = wm->hpll;
6005
6006 for_each_plane_id_on_crtc(crtc, plane_id) {
6007 active->wm.plane[plane_id] =
6008 wm->pipe[pipe].plane[plane_id];
6009 }
6010
6011 if (wm->cxsr && wm->hpll_en)
6012 max_level = G4X_WM_LEVEL_HPLL;
6013 else if (wm->cxsr)
6014 max_level = G4X_WM_LEVEL_SR;
6015 else
6016 max_level = G4X_WM_LEVEL_NORMAL;
6017
6018 level = G4X_WM_LEVEL_NORMAL;
6019 raw = &crtc_state->wm.g4x.raw[level];
6020 for_each_plane_id_on_crtc(crtc, plane_id)
6021 raw->plane[plane_id] = active->wm.plane[plane_id];
6022
6023 if (++level > max_level)
6024 goto out;
6025
6026 raw = &crtc_state->wm.g4x.raw[level];
6027 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6028 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6029 raw->plane[PLANE_SPRITE0] = 0;
6030 raw->fbc = active->sr.fbc;
6031
6032 if (++level > max_level)
6033 goto out;
6034
6035 raw = &crtc_state->wm.g4x.raw[level];
6036 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6037 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6038 raw->plane[PLANE_SPRITE0] = 0;
6039 raw->fbc = active->hpll.fbc;
6040
6041 out:
6042 for_each_plane_id_on_crtc(crtc, plane_id)
6043 g4x_raw_plane_wm_set(crtc_state, level,
6044 plane_id, USHRT_MAX);
6045 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6046
6047 crtc_state->wm.g4x.optimal = *active;
6048 crtc_state->wm.g4x.intermediate = *active;
6049
6050 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6051 pipe_name(pipe),
6052 wm->pipe[pipe].plane[PLANE_PRIMARY],
6053 wm->pipe[pipe].plane[PLANE_CURSOR],
6054 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6055 }
6056
6057 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6058 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6059 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6060 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6061 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
6062 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
6063}
6064
6065void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6066{
6067 struct intel_plane *plane;
6068 struct intel_crtc *crtc;
6069
6070 mutex_lock(&dev_priv->wm.wm_mutex);
6071
6072 for_each_intel_plane(&dev_priv->drm, plane) {
6073 struct intel_crtc *crtc =
6074 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6075 struct intel_crtc_state *crtc_state =
6076 to_intel_crtc_state(crtc->base.state);
6077 struct intel_plane_state *plane_state =
6078 to_intel_plane_state(plane->base.state);
6079 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6080 enum plane_id plane_id = plane->id;
6081 int level;
6082
6083 if (plane_state->base.visible)
6084 continue;
6085
6086 for (level = 0; level < 3; level++) {
6087 struct g4x_pipe_wm *raw =
6088 &crtc_state->wm.g4x.raw[level];
6089
6090 raw->plane[plane_id] = 0;
6091 wm_state->wm.plane[plane_id] = 0;
6092 }
6093
6094 if (plane_id == PLANE_PRIMARY) {
6095 for (level = 0; level < 3; level++) {
6096 struct g4x_pipe_wm *raw =
6097 &crtc_state->wm.g4x.raw[level];
6098 raw->fbc = 0;
6099 }
6100
6101 wm_state->sr.fbc = 0;
6102 wm_state->hpll.fbc = 0;
6103 wm_state->fbc_en = false;
6104 }
6105 }
6106
6107 for_each_intel_crtc(&dev_priv->drm, crtc) {
6108 struct intel_crtc_state *crtc_state =
6109 to_intel_crtc_state(crtc->base.state);
6110
6111 crtc_state->wm.g4x.intermediate =
6112 crtc_state->wm.g4x.optimal;
6113 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6114 }
6115
6116 g4x_program_watermarks(dev_priv);
6117
6118 mutex_unlock(&dev_priv->wm.wm_mutex);
6119}
6120
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006121void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006122{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006123 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006124 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006125 u32 val;
6126
6127 vlv_read_wm_values(dev_priv, wm);
6128
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006129 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6130 wm->level = VLV_WM_LEVEL_PM2;
6131
6132 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006133 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006134
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006135 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006136 if (val & DSP_MAXFIFO_PM5_ENABLE)
6137 wm->level = VLV_WM_LEVEL_PM5;
6138
Ville Syrjälä58590c12015-09-08 21:05:12 +03006139 /*
6140 * If DDR DVFS is disabled in the BIOS, Punit
6141 * will never ack the request. So if that happens
6142 * assume we don't have to enable/disable DDR DVFS
6143 * dynamically. To test that just set the REQ_ACK
6144 * bit to poke the Punit, but don't change the
6145 * HIGH/LOW bits so that we don't actually change
6146 * the current state.
6147 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006148 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006149 val |= FORCE_DDR_FREQ_REQ_ACK;
6150 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6151
6152 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6153 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6154 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
6155 "assuming DDR DVFS is disabled\n");
6156 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6157 } else {
6158 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6159 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6160 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6161 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006162
Chris Wilson337fa6e2019-04-26 09:17:20 +01006163 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006164 }
6165
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006166 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006167 struct intel_crtc_state *crtc_state =
6168 to_intel_crtc_state(crtc->base.state);
6169 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6170 const struct vlv_fifo_state *fifo_state =
6171 &crtc_state->wm.vlv.fifo_state;
6172 enum pipe pipe = crtc->pipe;
6173 enum plane_id plane_id;
6174 int level;
6175
6176 vlv_get_fifo_size(crtc_state);
6177
6178 active->num_levels = wm->level + 1;
6179 active->cxsr = wm->cxsr;
6180
Ville Syrjäläff32c542017-03-02 19:14:57 +02006181 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006182 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006183 &crtc_state->wm.vlv.raw[level];
6184
6185 active->sr[level].plane = wm->sr.plane;
6186 active->sr[level].cursor = wm->sr.cursor;
6187
6188 for_each_plane_id_on_crtc(crtc, plane_id) {
6189 active->wm[level].plane[plane_id] =
6190 wm->pipe[pipe].plane[plane_id];
6191
6192 raw->plane[plane_id] =
6193 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6194 fifo_state->plane[plane_id]);
6195 }
6196 }
6197
6198 for_each_plane_id_on_crtc(crtc, plane_id)
6199 vlv_raw_plane_wm_set(crtc_state, level,
6200 plane_id, USHRT_MAX);
6201 vlv_invalidate_wms(crtc, active, level);
6202
6203 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006204 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006205
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006206 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 +02006207 pipe_name(pipe),
6208 wm->pipe[pipe].plane[PLANE_PRIMARY],
6209 wm->pipe[pipe].plane[PLANE_CURSOR],
6210 wm->pipe[pipe].plane[PLANE_SPRITE0],
6211 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006212 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006213
6214 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6215 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6216}
6217
Ville Syrjälä602ae832017-03-02 19:15:02 +02006218void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6219{
6220 struct intel_plane *plane;
6221 struct intel_crtc *crtc;
6222
6223 mutex_lock(&dev_priv->wm.wm_mutex);
6224
6225 for_each_intel_plane(&dev_priv->drm, plane) {
6226 struct intel_crtc *crtc =
6227 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6228 struct intel_crtc_state *crtc_state =
6229 to_intel_crtc_state(crtc->base.state);
6230 struct intel_plane_state *plane_state =
6231 to_intel_plane_state(plane->base.state);
6232 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6233 const struct vlv_fifo_state *fifo_state =
6234 &crtc_state->wm.vlv.fifo_state;
6235 enum plane_id plane_id = plane->id;
6236 int level;
6237
6238 if (plane_state->base.visible)
6239 continue;
6240
6241 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006242 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006243 &crtc_state->wm.vlv.raw[level];
6244
6245 raw->plane[plane_id] = 0;
6246
6247 wm_state->wm[level].plane[plane_id] =
6248 vlv_invert_wm_value(raw->plane[plane_id],
6249 fifo_state->plane[plane_id]);
6250 }
6251 }
6252
6253 for_each_intel_crtc(&dev_priv->drm, crtc) {
6254 struct intel_crtc_state *crtc_state =
6255 to_intel_crtc_state(crtc->base.state);
6256
6257 crtc_state->wm.vlv.intermediate =
6258 crtc_state->wm.vlv.optimal;
6259 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6260 }
6261
6262 vlv_program_watermarks(dev_priv);
6263
6264 mutex_unlock(&dev_priv->wm.wm_mutex);
6265}
6266
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006267/*
6268 * FIXME should probably kill this and improve
6269 * the real watermark readout/sanitation instead
6270 */
6271static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6272{
6273 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6274 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6275 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6276
6277 /*
6278 * Don't touch WM1S_LP_EN here.
6279 * Doing so could cause underruns.
6280 */
6281}
6282
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006283void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006284{
Imre Deak820c1982013-12-17 14:46:36 +02006285 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006286 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006287
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006288 ilk_init_lp_watermarks(dev_priv);
6289
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006290 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006291 ilk_pipe_wm_get_hw_state(crtc);
6292
6293 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6294 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6295 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6296
6297 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006298 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006299 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6300 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6301 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006302
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006303 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006304 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6305 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006306 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006307 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6308 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006309
6310 hw->enable_fbc_wm =
6311 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6312}
6313
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006314/**
6315 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006316 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006317 *
6318 * Calculate watermark values for the various WM regs based on current mode
6319 * and plane configuration.
6320 *
6321 * There are several cases to deal with here:
6322 * - normal (i.e. non-self-refresh)
6323 * - self-refresh (SR) mode
6324 * - lines are large relative to FIFO size (buffer can hold up to 2)
6325 * - lines are small relative to FIFO size (buffer can hold more than 2
6326 * lines), so need to account for TLB latency
6327 *
6328 * The normal calculation is:
6329 * watermark = dotclock * bytes per pixel * latency
6330 * where latency is platform & configuration dependent (we assume pessimal
6331 * values here).
6332 *
6333 * The SR calculation is:
6334 * watermark = (trunc(latency/line time)+1) * surface width *
6335 * bytes per pixel
6336 * where
6337 * line time = htotal / dotclock
6338 * surface width = hdisplay for normal plane and 64 for cursor
6339 * and latency is assumed to be high, as above.
6340 *
6341 * The final value programmed to the register should always be rounded up,
6342 * and include an extra 2 entries to account for clock crossings.
6343 *
6344 * We don't use the sprite, so we can ignore that. And on Crestline we have
6345 * to set the non-SR watermarks to 8.
6346 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006347void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006348{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006349 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006350
6351 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006352 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006353}
6354
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306355void intel_enable_ipc(struct drm_i915_private *dev_priv)
6356{
6357 u32 val;
6358
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006359 if (!HAS_IPC(dev_priv))
6360 return;
6361
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306362 val = I915_READ(DISP_ARB_CTL2);
6363
6364 if (dev_priv->ipc_enabled)
6365 val |= DISP_IPC_ENABLE;
6366 else
6367 val &= ~DISP_IPC_ENABLE;
6368
6369 I915_WRITE(DISP_ARB_CTL2, val);
6370}
6371
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006372static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6373{
6374 /* Display WA #0477 WaDisableIPC: skl */
6375 if (IS_SKYLAKE(dev_priv))
6376 return false;
6377
6378 /* Display WA #1141: SKL:all KBL:all CFL */
6379 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
6380 return dev_priv->dram_info.symmetric_memory;
6381
6382 return true;
6383}
6384
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306385void intel_init_ipc(struct drm_i915_private *dev_priv)
6386{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306387 if (!HAS_IPC(dev_priv))
6388 return;
6389
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006390 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006391
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306392 intel_enable_ipc(dev_priv);
6393}
6394
Jani Nikulae2828912016-01-18 09:19:47 +02006395/*
Daniel Vetter92703882012-08-09 16:46:01 +02006396 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02006397 */
6398DEFINE_SPINLOCK(mchdev_lock);
6399
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006400bool ironlake_set_drps(struct drm_i915_private *i915, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006401{
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006402 struct intel_uncore *uncore = &i915->uncore;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006403 u16 rgvswctl;
6404
Chris Wilson67520412017-03-02 13:28:01 +00006405 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02006406
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006407 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006408 if (rgvswctl & MEMCTL_CMD_STS) {
6409 DRM_DEBUG("gpu busy, RCS change rejected\n");
6410 return false; /* still busy with another command */
6411 }
6412
6413 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6414 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006415 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
6416 intel_uncore_posting_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006417
6418 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006419 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006420
6421 return true;
6422}
6423
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006424static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006425{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006426 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006427 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006428 u8 fmax, fmin, fstart, vstart;
6429
Daniel Vetter92703882012-08-09 16:46:01 +02006430 spin_lock_irq(&mchdev_lock);
6431
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006432 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006433
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006434 /* Enable temp reporting */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006435 intel_uncore_write16(uncore, PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6436 intel_uncore_write16(uncore, TSC1, I915_READ(TSC1) | TSE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006437
6438 /* 100ms RC evaluation intervals */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006439 intel_uncore_write(uncore, RCUPEI, 100000);
6440 intel_uncore_write(uncore, RCDNEI, 100000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006441
6442 /* Set max/min thresholds to 90ms and 80ms respectively */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006443 intel_uncore_write(uncore, RCBMAXAVG, 90000);
6444 intel_uncore_write(uncore, RCBMINAVG, 80000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006445
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006446 intel_uncore_write(uncore, MEMIHYST, 1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006447
6448 /* Set up min, max, and cur for interrupt handling */
6449 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6450 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6451 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6452 MEMMODE_FSTART_SHIFT;
6453
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006454 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
6455 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006456
Daniel Vetter20e4d402012-08-08 23:35:39 +02006457 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
6458 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006459
Daniel Vetter20e4d402012-08-08 23:35:39 +02006460 dev_priv->ips.max_delay = fstart;
6461 dev_priv->ips.min_delay = fmin;
6462 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006463
6464 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6465 fmax, fmin, fstart);
6466
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006467 intel_uncore_write(uncore,
6468 MEMINTREN,
6469 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006470
6471 /*
6472 * Interrupts will be enabled in ironlake_irq_postinstall
6473 */
6474
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006475 intel_uncore_write(uncore, VIDSTART, vstart);
6476 intel_uncore_posting_read(uncore, VIDSTART);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006477
6478 rgvmodectl |= MEMMODE_SWMODE_EN;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006479 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006480
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006481 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
6482 MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006483 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006484 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006485
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006486 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006487
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006488 dev_priv->ips.last_count1 =
6489 intel_uncore_read(uncore, DMIEC) +
6490 intel_uncore_read(uncore, DDREC) +
6491 intel_uncore_read(uncore, CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006492 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006493 dev_priv->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006494 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02006495
6496 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006497}
6498
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006499static void ironlake_disable_drps(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006500{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006501 struct intel_uncore *uncore = &i915->uncore;
Daniel Vetter92703882012-08-09 16:46:01 +02006502 u16 rgvswctl;
6503
6504 spin_lock_irq(&mchdev_lock);
6505
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006506 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006507
6508 /* Ack interrupts, disable EFC interrupt */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006509 intel_uncore_write(uncore,
6510 MEMINTREN,
6511 intel_uncore_read(uncore, MEMINTREN) &
6512 ~MEMINT_EVAL_CHG_EN);
6513 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
6514 intel_uncore_write(uncore,
6515 DEIER,
6516 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
6517 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
6518 intel_uncore_write(uncore,
6519 DEIMR,
6520 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006521
6522 /* Go back to the starting frequency */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006523 ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006524 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006525 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006526 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006527 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006528
Daniel Vetter92703882012-08-09 16:46:01 +02006529 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006530}
6531
Daniel Vetteracbe9472012-07-26 11:50:05 +02006532/* There's a funny hw issue where the hw returns all 0 when reading from
6533 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6534 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6535 * all limits and the gpu stuck at whatever frequency it is at atm).
6536 */
Akash Goel74ef1172015-03-06 11:07:19 +05306537static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006538{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006539 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006540 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006541
Daniel Vetter20b46e52012-07-26 11:16:14 +02006542 /* Only set the down limit when we've reached the lowest level to avoid
6543 * getting more interrupts, otherwise leave this clear. This prevents a
6544 * race in the hw when coming out of rc6: There's a tiny window where
6545 * the hw runs at the minimal clock before selecting the desired
6546 * frequency, if the down threshold expires in that window we will not
6547 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006548 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006549 limits = (rps->max_freq_softlimit) << 23;
6550 if (val <= rps->min_freq_softlimit)
6551 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306552 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006553 limits = rps->max_freq_softlimit << 24;
6554 if (val <= rps->min_freq_softlimit)
6555 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306556 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006557
6558 return limits;
6559}
6560
Chris Wilson60548c52018-07-31 14:26:29 +01006561static void rps_set_power(struct drm_i915_private *dev_priv, int new_power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006562{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006563 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Akash Goel8a586432015-03-06 11:07:18 +05306564 u32 threshold_up = 0, threshold_down = 0; /* in % */
6565 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006566
Chris Wilson60548c52018-07-31 14:26:29 +01006567 lockdep_assert_held(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006568
Chris Wilson60548c52018-07-31 14:26:29 +01006569 if (new_power == rps->power.mode)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006570 return;
6571
6572 /* Note the units here are not exactly 1us, but 1280ns. */
6573 switch (new_power) {
6574 case LOW_POWER:
6575 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306576 ei_up = 16000;
6577 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006578
6579 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306580 ei_down = 32000;
6581 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006582 break;
6583
6584 case BETWEEN:
6585 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306586 ei_up = 13000;
6587 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006588
6589 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306590 ei_down = 32000;
6591 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006592 break;
6593
6594 case HIGH_POWER:
6595 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306596 ei_up = 10000;
6597 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006598
6599 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306600 ei_down = 32000;
6601 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006602 break;
6603 }
6604
Mika Kuoppala6067a272017-02-15 15:52:59 +02006605 /* When byt can survive without system hang with dynamic
6606 * sw freq adjustments, this restriction can be lifted.
6607 */
6608 if (IS_VALLEYVIEW(dev_priv))
6609 goto skip_hw_write;
6610
Akash Goel8a586432015-03-06 11:07:18 +05306611 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006612 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306613 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006614 GT_INTERVAL_FROM_US(dev_priv,
6615 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306616
6617 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006618 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306619 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006620 GT_INTERVAL_FROM_US(dev_priv,
6621 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306622
Chris Wilsona72b5622016-07-02 15:35:59 +01006623 I915_WRITE(GEN6_RP_CONTROL,
Mika Kuoppala1071d0f2019-04-10 16:24:36 +03006624 (INTEL_GEN(dev_priv) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
Chris Wilsona72b5622016-07-02 15:35:59 +01006625 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6626 GEN6_RP_MEDIA_IS_GFX |
6627 GEN6_RP_ENABLE |
6628 GEN6_RP_UP_BUSY_AVG |
6629 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306630
Mika Kuoppala6067a272017-02-15 15:52:59 +02006631skip_hw_write:
Chris Wilson60548c52018-07-31 14:26:29 +01006632 rps->power.mode = new_power;
6633 rps->power.up_threshold = threshold_up;
6634 rps->power.down_threshold = threshold_down;
6635}
6636
6637static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6638{
6639 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6640 int new_power;
6641
6642 new_power = rps->power.mode;
6643 switch (rps->power.mode) {
6644 case LOW_POWER:
6645 if (val > rps->efficient_freq + 1 &&
6646 val > rps->cur_freq)
6647 new_power = BETWEEN;
6648 break;
6649
6650 case BETWEEN:
6651 if (val <= rps->efficient_freq &&
6652 val < rps->cur_freq)
6653 new_power = LOW_POWER;
6654 else if (val >= rps->rp0_freq &&
6655 val > rps->cur_freq)
6656 new_power = HIGH_POWER;
6657 break;
6658
6659 case HIGH_POWER:
6660 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6661 val < rps->cur_freq)
6662 new_power = BETWEEN;
6663 break;
6664 }
6665 /* Max/min bins are special */
6666 if (val <= rps->min_freq_softlimit)
6667 new_power = LOW_POWER;
6668 if (val >= rps->max_freq_softlimit)
6669 new_power = HIGH_POWER;
6670
6671 mutex_lock(&rps->power.mutex);
6672 if (rps->power.interactive)
6673 new_power = HIGH_POWER;
6674 rps_set_power(dev_priv, new_power);
6675 mutex_unlock(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006676}
6677
Chris Wilson60548c52018-07-31 14:26:29 +01006678void intel_rps_mark_interactive(struct drm_i915_private *i915, bool interactive)
6679{
6680 struct intel_rps *rps = &i915->gt_pm.rps;
6681
6682 if (INTEL_GEN(i915) < 6)
6683 return;
6684
6685 mutex_lock(&rps->power.mutex);
6686 if (interactive) {
6687 if (!rps->power.interactive++ && READ_ONCE(i915->gt.awake))
6688 rps_set_power(i915, HIGH_POWER);
6689 } else {
6690 GEM_BUG_ON(!rps->power.interactive);
6691 rps->power.interactive--;
6692 }
6693 mutex_unlock(&rps->power.mutex);
6694}
6695
Chris Wilson2876ce72014-03-28 08:03:34 +00006696static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6697{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006698 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006699 u32 mask = 0;
6700
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006701 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006702 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006703 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006704 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006705 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006706
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006707 mask &= dev_priv->pm_rps_events;
6708
Imre Deak59d02a12014-12-19 19:33:26 +02006709 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006710}
6711
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006712/* gen6_set_rps is called to update the frequency request, but should also be
6713 * called when the range (min_delay and max_delay) is modified so that we can
6714 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006715static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006716{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006717 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6718
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006719 /* min/max delay may still have been modified so be sure to
6720 * write the limits value.
6721 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006722 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006723 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006724
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006725 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306726 I915_WRITE(GEN6_RPNSWREQ,
6727 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006728 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006729 I915_WRITE(GEN6_RPNSWREQ,
6730 HSW_FREQUENCY(val));
6731 else
6732 I915_WRITE(GEN6_RPNSWREQ,
6733 GEN6_FREQUENCY(val) |
6734 GEN6_OFFSET(0) |
6735 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006736 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006737
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006738 /* Make sure we continue to get interrupts
6739 * until we hit the minimum or maximum frequencies.
6740 */
Akash Goel74ef1172015-03-06 11:07:19 +05306741 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006742 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006743
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006744 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006745 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006746
6747 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006748}
6749
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006750static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006751{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006752 int err;
6753
Chris Wilsondc979972016-05-10 14:10:04 +01006754 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006755 "Odd GPU freq value\n"))
6756 val &= ~1;
6757
Deepak Scd25dd52015-07-10 18:31:40 +05306758 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6759
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006760 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006761 vlv_punit_get(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006762 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson337fa6e2019-04-26 09:17:20 +01006763 vlv_punit_put(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006764 if (err)
6765 return err;
6766
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006767 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006768 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006769
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006770 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006771 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006772
6773 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006774}
6775
Deepak Sa7f6e232015-05-09 18:04:44 +05306776/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306777 *
6778 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306779 * 1. Forcewake Media well.
6780 * 2. Request idle freq.
6781 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306782*/
6783static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6784{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006785 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6786 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006787 int err;
Deepak S5549d252014-06-28 11:26:11 +05306788
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006789 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306790 return;
6791
Chris Wilsonc9efef72017-01-02 15:28:45 +00006792 /* The punit delays the write of the frequency and voltage until it
6793 * determines the GPU is awake. During normal usage we don't want to
6794 * waste power changing the frequency if the GPU is sleeping (rc6).
6795 * However, the GPU and driver is now idle and we do not want to delay
6796 * switching to minimum voltage (reducing power whilst idle) as we do
6797 * not expect to be woken in the near future and so must flush the
6798 * change by waking the device.
6799 *
6800 * We choose to take the media powerwell (either would do to trick the
6801 * punit into committing the voltage change) as that takes a lot less
6802 * power than the render powerwell.
6803 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006804 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006805 err = valleyview_set_rps(dev_priv, val);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006806 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006807
6808 if (err)
6809 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306810}
6811
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006812void gen6_rps_busy(struct drm_i915_private *dev_priv)
6813{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006814 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6815
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006816 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006817 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006818 u8 freq;
6819
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006820 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006821 gen6_rps_reset_ei(dev_priv);
6822 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006823 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006824
Chris Wilsonc33d2472016-07-04 08:08:36 +01006825 gen6_enable_rps_interrupts(dev_priv);
6826
Chris Wilsonbd648182017-02-10 15:03:48 +00006827 /* Use the user's desired frequency as a guide, but for better
6828 * performance, jump directly to RPe as our starting frequency.
6829 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006830 freq = max(rps->cur_freq,
6831 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006832
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006833 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006834 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006835 rps->min_freq_softlimit,
6836 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006837 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006838 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006839 mutex_unlock(&rps->lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006840}
6841
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006842void gen6_rps_idle(struct drm_i915_private *dev_priv)
6843{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006844 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6845
Chris Wilsonc33d2472016-07-04 08:08:36 +01006846 /* Flush our bottom-half so that it does not race with us
6847 * setting the idle frequency and so that it is bounded by
6848 * our rpm wakeref. And then disable the interrupts to stop any
6849 * futher RPS reclocking whilst we are asleep.
6850 */
6851 gen6_disable_rps_interrupts(dev_priv);
6852
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006853 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006854 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006855 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306856 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006857 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006858 gen6_set_rps(dev_priv, rps->idle_freq);
6859 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006860 I915_WRITE(GEN6_PMINTRMSK,
6861 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006862 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006863 mutex_unlock(&rps->lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006864}
6865
Chris Wilson62eb3c22019-02-13 09:25:04 +00006866void gen6_rps_boost(struct i915_request *rq)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006867{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006868 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006869 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006870 bool boost;
6871
Chris Wilson8d3afd72015-05-21 21:01:47 +01006872 /* This is intentionally racy! We peek at the state here, then
6873 * validate inside the RPS worker.
6874 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006875 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006876 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006877
Chris Wilson0e218342019-01-21 22:21:02 +00006878 if (i915_request_signaled(rq))
Chris Wilson253a2812018-02-06 14:31:37 +00006879 return;
6880
Chris Wilsone61e0f52018-02-21 09:56:36 +00006881 /* Serializes with i915_request_retire() */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006882 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006883 spin_lock_irqsave(&rq->lock, flags);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006884 if (!i915_request_has_waitboost(rq) &&
6885 !dma_fence_is_signaled_locked(&rq->fence)) {
Chris Wilson253a2812018-02-06 14:31:37 +00006886 boost = !atomic_fetch_inc(&rps->num_waiters);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006887 rq->flags |= I915_REQUEST_WAITBOOST;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006888 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006889 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006890 if (!boost)
6891 return;
6892
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006893 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6894 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006895
Chris Wilson62eb3c22019-02-13 09:25:04 +00006896 atomic_inc(&rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006897}
6898
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006899int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006900{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006901 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006902 int err;
6903
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006904 lockdep_assert_held(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006905 GEM_BUG_ON(val > rps->max_freq);
6906 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006907
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006908 if (!rps->enabled) {
6909 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006910 return 0;
6911 }
6912
Chris Wilsondc979972016-05-10 14:10:04 +01006913 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006914 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006915 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006916 err = gen6_set_rps(dev_priv, val);
6917
6918 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006919}
6920
Chris Wilsondc979972016-05-10 14:10:04 +01006921static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006922{
Zhe Wang20e49362014-11-04 17:07:05 +00006923 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006924 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006925}
6926
Chris Wilsondc979972016-05-10 14:10:04 +01006927static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306928{
Akash Goel2030d682016-04-23 00:05:45 +05306929 I915_WRITE(GEN6_RP_CONTROL, 0);
6930}
6931
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006932static void gen6_disable_rc6(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006933{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006934 I915_WRITE(GEN6_RC_CONTROL, 0);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006935}
6936
6937static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6938{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006939 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306940 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006941}
6942
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006943static void cherryview_disable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306944{
Deepak S38807742014-05-23 21:00:15 +05306945 I915_WRITE(GEN6_RC_CONTROL, 0);
6946}
6947
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006948static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6949{
6950 I915_WRITE(GEN6_RP_CONTROL, 0);
6951}
6952
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006953static void valleyview_disable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006954{
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006955 /* We're doing forcewake before Disabling RC6,
Deepak S98a2e5f2014-08-18 10:35:27 -07006956 * This what the BIOS expects when going into suspend */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006957 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006958
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006959 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006960
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006961 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006962}
6963
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006964static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6965{
6966 I915_WRITE(GEN6_RP_CONTROL, 0);
6967}
6968
Chris Wilsondc979972016-05-10 14:10:04 +01006969static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306970{
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306971 bool enable_rc6 = true;
6972 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006973 u32 rc_ctl;
6974 int rc_sw_target;
6975
6976 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6977 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6978 RC_SW_TARGET_STATE_SHIFT;
6979 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6980 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6981 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6982 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6983 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306984
6985 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006986 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306987 enable_rc6 = false;
6988 }
6989
6990 /*
6991 * The exact context size is not known for BXT, so assume a page size
6992 * for this check.
6993 */
6994 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Matthew Auld17a05342017-12-11 15:18:19 +00006995 if (!((rc6_ctx_base >= dev_priv->dsm_reserved.start) &&
6996 (rc6_ctx_base + PAGE_SIZE < dev_priv->dsm_reserved.end))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006997 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306998 enable_rc6 = false;
6999 }
7000
7001 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
7002 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
7003 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
7004 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03007005 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307006 enable_rc6 = false;
7007 }
7008
Imre Deakfc619842016-06-29 19:13:55 +03007009 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
7010 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
7011 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
7012 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
7013 enable_rc6 = false;
7014 }
7015
7016 if (!I915_READ(GEN6_GFXPAUSE)) {
7017 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
7018 enable_rc6 = false;
7019 }
7020
7021 if (!I915_READ(GEN8_MISC_CTRL0)) {
7022 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307023 enable_rc6 = false;
7024 }
7025
7026 return enable_rc6;
7027}
7028
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007029static bool sanitize_rc6(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007030{
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007031 struct intel_device_info *info = mkwrite_device_info(i915);
Imre Deake6069ca2014-04-18 16:01:02 +03007032
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007033 /* Powersaving is controlled by the host when inside a VM */
Chris Wilson91cbdb82019-04-19 14:48:36 +01007034 if (intel_vgpu_active(i915)) {
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007035 info->has_rc6 = 0;
Chris Wilson91cbdb82019-04-19 14:48:36 +01007036 info->has_rps = false;
7037 }
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307038
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007039 if (info->has_rc6 &&
7040 IS_GEN9_LP(i915) && !bxt_check_bios_rc6_setup(i915)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307041 DRM_INFO("RC6 disabled by BIOS\n");
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007042 info->has_rc6 = 0;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307043 }
7044
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007045 /*
7046 * We assume that we do not have any deep rc6 levels if we don't have
7047 * have the previous rc6 level supported, i.e. we use HAS_RC6()
7048 * as the initial coarse check for rc6 in general, moving on to
7049 * progressively finer/deeper levels.
7050 */
7051 if (!info->has_rc6 && info->has_rc6p)
7052 info->has_rc6p = 0;
Imre Deake6069ca2014-04-18 16:01:02 +03007053
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007054 return info->has_rc6;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007055}
7056
Chris Wilsondc979972016-05-10 14:10:04 +01007057static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03007058{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007059 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7060
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007061 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01007062
Tom O'Rourke93ee2922014-11-19 14:21:52 -08007063 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02007064 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01007065 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007066 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
7067 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
7068 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07007069 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01007070 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007071 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
7072 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
7073 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07007074 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007075 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007076 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007077
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007078 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01007079 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007080 IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01007081 u32 ddcc_status = 0;
7082
7083 if (sandybridge_pcode_read(dev_priv,
7084 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
Ville Syrjäläd284d512019-05-21 19:40:24 +03007085 &ddcc_status, NULL) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007086 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08007087 clamp_t(u8,
7088 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007089 rps->min_freq,
7090 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08007091 }
7092
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007093 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goelc5e06882015-06-29 14:50:19 +05307094 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01007095 * the natural hardware unit for SKL
7096 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007097 rps->rp0_freq *= GEN9_FREQ_SCALER;
7098 rps->rp1_freq *= GEN9_FREQ_SCALER;
7099 rps->min_freq *= GEN9_FREQ_SCALER;
7100 rps->max_freq *= GEN9_FREQ_SCALER;
7101 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05307102 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007103}
7104
Chris Wilson3a45b052016-07-13 09:10:32 +01007105static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00007106 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01007107{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007108 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7109 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01007110
7111 /* force a reset */
Chris Wilson60548c52018-07-31 14:26:29 +01007112 rps->power.mode = -1;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007113 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01007114
Chris Wilson9fcee2f2017-01-26 10:19:19 +00007115 if (set(dev_priv, freq))
7116 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01007117}
7118
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007119/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01007120static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00007121{
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007122 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007123
David Weinehall36fe7782017-11-17 10:01:46 +02007124 /* Program defaults and thresholds for RPS */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007125 if (IS_GEN(dev_priv, 9))
David Weinehall36fe7782017-11-17 10:01:46 +02007126 I915_WRITE(GEN6_RC_VIDEO_FREQ,
7127 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007128
Akash Goel0beb0592015-03-06 11:07:20 +05307129 /* 1 second timeout*/
7130 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
7131 GT_INTERVAL_FROM_US(dev_priv, 1000000));
7132
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007133 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007134
Akash Goel0beb0592015-03-06 11:07:20 +05307135 /* Leaning on the below call to gen6_set_rps to program/setup the
7136 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
7137 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01007138 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007139
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007140 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007141}
7142
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007143static void gen11_enable_rc6(struct drm_i915_private *dev_priv)
7144{
7145 struct intel_engine_cs *engine;
7146 enum intel_engine_id id;
7147
7148 /* 1a: Software RC state - RC0 */
7149 I915_WRITE(GEN6_RC_STATE, 0);
7150
7151 /*
7152 * 1b: Get forcewake during program sequence. Although the driver
7153 * hasn't enabled a state yet where we need forcewake, BIOS may have.
7154 */
7155 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
7156
7157 /* 2a: Disable RC states. */
7158 I915_WRITE(GEN6_RC_CONTROL, 0);
7159
7160 /* 2b: Program RC6 thresholds.*/
7161 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
7162 I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
7163
7164 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7165 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7166 for_each_engine(engine, dev_priv, id)
7167 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
7168
Daniele Ceraolo Spurio702668e2019-07-24 17:18:06 -07007169 if (HAS_GT_UC(dev_priv))
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007170 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
7171
7172 I915_WRITE(GEN6_RC_SLEEP, 0);
7173
Mika Kuoppalad105e9a2019-04-10 13:59:18 +03007174 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
7175
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007176 /*
7177 * 2c: Program Coarse Power Gating Policies.
7178 *
7179 * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
7180 * use instead is a more conservative estimate for the maximum time
7181 * it takes us to service a CS interrupt and submit a new ELSP - that
7182 * is the time which the GPU is idle waiting for the CPU to select the
7183 * next request to execute. If the idle hysteresis is less than that
7184 * interrupt service latency, the hardware will automatically gate
7185 * the power well and we will then incur the wake up cost on top of
Maarten Lankhorstec193642019-06-28 10:55:17 +02007186 * the service latency. A similar guide from plane_state is that we
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007187 * do not want the enable hysteresis to less than the wakeup latency.
7188 *
7189 * igt/gem_exec_nop/sequential provides a rough estimate for the
7190 * service latency, and puts it around 10us for Broadwell (and other
7191 * big core) and around 40us for Broxton (and other low power cores).
7192 * [Note that for legacy ringbuffer submission, this is less than 1us!]
7193 * However, the wakeup latency on Broxton is closer to 100us. To be
7194 * conservative, we have to factor in a context switch on top (due
7195 * to ksoftirqd).
7196 */
7197 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 250);
7198 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 250);
7199
7200 /* 3a: Enable RC6 */
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007201 I915_WRITE(GEN6_RC_CONTROL,
7202 GEN6_RC_CTL_HW_ENABLE |
7203 GEN6_RC_CTL_RC6_ENABLE |
7204 GEN6_RC_CTL_EI_MODE(1));
7205
7206 /* 3b: Enable Coarse Power Gating only when RC6 is enabled. */
7207 I915_WRITE(GEN9_PG_ENABLE,
Mika Kuoppala2ea74142019-04-10 13:59:19 +03007208 GEN9_RENDER_PG_ENABLE |
7209 GEN9_MEDIA_PG_ENABLE |
7210 GEN11_MEDIA_SAMPLER_PG_ENABLE);
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007211
7212 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
7213}
7214
Chris Wilsondc979972016-05-10 14:10:04 +01007215static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007216{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007217 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307218 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007219 u32 rc6_mode;
Zhe Wang20e49362014-11-04 17:07:05 +00007220
7221 /* 1a: Software RC state - RC0 */
7222 I915_WRITE(GEN6_RC_STATE, 0);
7223
7224 /* 1b: Get forcewake during program sequence. Although the driver
7225 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007226 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00007227
7228 /* 2a: Disable RC states. */
7229 I915_WRITE(GEN6_RC_CONTROL, 0);
7230
7231 /* 2b: Program RC6 thresholds.*/
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07007232 if (INTEL_GEN(dev_priv) >= 10) {
7233 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
7234 I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
7235 } else if (IS_SKYLAKE(dev_priv)) {
7236 /*
7237 * WaRsDoubleRc6WrlWithCoarsePowerGating:skl Doubling WRL only
7238 * when CPG is enabled
7239 */
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05307240 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07007241 } else {
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05307242 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07007243 }
7244
Zhe Wang20e49362014-11-04 17:07:05 +00007245 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7246 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05307247 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007248 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05307249
Daniele Ceraolo Spurio702668e2019-07-24 17:18:06 -07007250 if (HAS_GT_UC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05307251 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
7252
Zhe Wang20e49362014-11-04 17:07:05 +00007253 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00007254
Chris Wilsonc1beabc2018-01-22 13:55:41 +00007255 /*
7256 * 2c: Program Coarse Power Gating Policies.
7257 *
7258 * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
7259 * use instead is a more conservative estimate for the maximum time
7260 * it takes us to service a CS interrupt and submit a new ELSP - that
7261 * is the time which the GPU is idle waiting for the CPU to select the
7262 * next request to execute. If the idle hysteresis is less than that
7263 * interrupt service latency, the hardware will automatically gate
7264 * the power well and we will then incur the wake up cost on top of
Maarten Lankhorstec193642019-06-28 10:55:17 +02007265 * the service latency. A similar guide from plane_state is that we
Chris Wilsonc1beabc2018-01-22 13:55:41 +00007266 * do not want the enable hysteresis to less than the wakeup latency.
7267 *
7268 * igt/gem_exec_nop/sequential provides a rough estimate for the
7269 * service latency, and puts it around 10us for Broadwell (and other
7270 * big core) and around 40us for Broxton (and other low power cores).
7271 * [Note that for legacy ringbuffer submission, this is less than 1us!]
7272 * However, the wakeup latency on Broxton is closer to 100us. To be
7273 * conservative, we have to factor in a context switch on top (due
7274 * to ksoftirqd).
7275 */
7276 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 250);
7277 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 250);
Zhe Wang38c23522015-01-20 12:23:04 +00007278
Zhe Wang20e49362014-11-04 17:07:05 +00007279 /* 3a: Enable RC6 */
Chris Wilson1c044f92017-01-25 17:26:01 +00007280 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07007281
7282 /* WaRsUseTimeoutMode:cnl (pre-prod) */
7283 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_C0))
7284 rc6_mode = GEN7_RC_CTL_TO_MODE;
7285 else
7286 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
7287
Chris Wilson1c044f92017-01-25 17:26:01 +00007288 I915_WRITE(GEN6_RC_CONTROL,
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007289 GEN6_RC_CTL_HW_ENABLE |
7290 GEN6_RC_CTL_RC6_ENABLE |
7291 rc6_mode);
Zhe Wang20e49362014-11-04 17:07:05 +00007292
Sagar Kamblecb07bae2015-04-12 11:28:14 +05307293 /*
7294 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Rodrigo Vivid66047e42018-02-22 12:05:35 -08007295 * WaRsDisableCoarsePowerGating:skl,cnl - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05307296 */
Chris Wilsondc979972016-05-10 14:10:04 +01007297 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05307298 I915_WRITE(GEN9_PG_ENABLE, 0);
7299 else
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007300 I915_WRITE(GEN9_PG_ENABLE,
7301 GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE);
Zhe Wang38c23522015-01-20 12:23:04 +00007302
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007303 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00007304}
7305
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007306static void gen8_enable_rc6(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007307{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007308 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307309 enum intel_engine_id id;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007310
7311 /* 1a: Software RC state - RC0 */
7312 I915_WRITE(GEN6_RC_STATE, 0);
7313
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007314 /* 1b: Get forcewake during program sequence. Although the driver
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007315 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007316 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007317
7318 /* 2a: Disable RC states. */
7319 I915_WRITE(GEN6_RC_CONTROL, 0);
7320
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007321 /* 2b: Program RC6 thresholds.*/
7322 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7323 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7324 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05307325 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007326 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007327 I915_WRITE(GEN6_RC_SLEEP, 0);
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01007328 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007329
7330 /* 3: Enable RC6 */
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01007331
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007332 I915_WRITE(GEN6_RC_CONTROL,
7333 GEN6_RC_CTL_HW_ENABLE |
7334 GEN7_RC_CTL_TO_MODE |
7335 GEN6_RC_CTL_RC6_ENABLE);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007336
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007337 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007338}
7339
7340static void gen8_enable_rps(struct drm_i915_private *dev_priv)
7341{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007342 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7343
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007344 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007345
7346 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07007347 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007348 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07007349 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007350 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02007351 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
7352 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007353
Daniel Vetter7526ed72014-09-29 15:07:19 +02007354 /* Docs recommend 900MHz, and 300 MHz respectively */
7355 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007356 rps->max_freq_softlimit << 24 |
7357 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007358
Daniel Vetter7526ed72014-09-29 15:07:19 +02007359 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
7360 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
7361 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
7362 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007363
Daniel Vetter7526ed72014-09-29 15:07:19 +02007364 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007365
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007366 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02007367 I915_WRITE(GEN6_RP_CONTROL,
7368 GEN6_RP_MEDIA_TURBO |
7369 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7370 GEN6_RP_MEDIA_IS_GFX |
7371 GEN6_RP_ENABLE |
7372 GEN6_RP_UP_BUSY_AVG |
7373 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007374
Chris Wilson3a45b052016-07-13 09:10:32 +01007375 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02007376
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007377 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007378}
7379
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007380static void gen6_enable_rc6(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007381{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007382 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307383 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007384 u32 rc6vids, rc6_mask;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007385 u32 gtfifodbg;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00007386 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007387
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007388 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007389
7390 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007391 gtfifodbg = I915_READ(GTFIFODBG);
7392 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007393 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
7394 I915_WRITE(GTFIFODBG, gtfifodbg);
7395 }
7396
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007397 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007398
7399 /* disable the counters and set deterministic thresholds */
7400 I915_WRITE(GEN6_RC_CONTROL, 0);
7401
7402 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
7403 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
7404 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
7405 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7406 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7407
Akash Goel3b3f1652016-10-13 22:44:48 +05307408 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007409 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007410
7411 I915_WRITE(GEN6_RC_SLEEP, 0);
7412 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01007413 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07007414 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
7415 else
7416 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08007417 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007418 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
7419
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03007420 /* We don't use those on Haswell */
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007421 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
7422 if (HAS_RC6p(dev_priv))
7423 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
7424 if (HAS_RC6pp(dev_priv))
7425 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007426 I915_WRITE(GEN6_RC_CONTROL,
7427 rc6_mask |
7428 GEN6_RC_CTL_EI_MODE(1) |
7429 GEN6_RC_CTL_HW_ENABLE);
7430
Ben Widawsky31643d52012-09-26 10:34:01 -07007431 rc6vids = 0;
Ville Syrjäläd284d512019-05-21 19:40:24 +03007432 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS,
7433 &rc6vids, NULL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007434 if (IS_GEN(dev_priv, 6) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07007435 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007436 } else if (IS_GEN(dev_priv, 6) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07007437 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
7438 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
7439 rc6vids &= 0xffff00;
7440 rc6vids |= GEN6_ENCODE_RC6_VID(450);
7441 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
7442 if (ret)
7443 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
7444 }
7445
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007446 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007447}
7448
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007449static void gen6_enable_rps(struct drm_i915_private *dev_priv)
7450{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007451 /* Here begins a magic sequence of register writes to enable
7452 * auto-downclocking.
7453 *
7454 * Perhaps there might be some value in exposing these to
7455 * userspace...
7456 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007457 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007458
7459 /* Power down if completely idle for over 50ms */
7460 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
7461 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7462
7463 reset_rps(dev_priv, gen6_set_rps);
7464
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007465 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007466}
7467
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007468static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007469{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007470 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007471 const int min_freq = 15;
7472 const int scaling_factor = 180;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007473 unsigned int gpu_freq;
7474 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05307475 unsigned int max_gpu_freq, min_gpu_freq;
Ben Widawskyeda79642013-10-07 17:15:48 -03007476 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007477
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007478 lockdep_assert_held(&rps->lock);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02007479
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007480 if (rps->max_freq <= rps->min_freq)
7481 return;
7482
Ben Widawskyeda79642013-10-07 17:15:48 -03007483 policy = cpufreq_cpu_get(0);
7484 if (policy) {
7485 max_ia_freq = policy->cpuinfo.max_freq;
7486 cpufreq_cpu_put(policy);
7487 } else {
7488 /*
7489 * Default to measured freq if none found, PCU will ensure we
7490 * don't go over
7491 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007492 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03007493 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007494
7495 /* Convert from kHz to MHz */
7496 max_ia_freq /= 1000;
7497
Ben Widawsky153b4b952013-10-22 22:05:09 -07007498 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07007499 /* convert DDR frequency from units of 266.6MHz to bandwidth */
7500 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007501
Chris Wilsond586b5f2018-03-08 14:26:48 +00007502 min_gpu_freq = rps->min_freq;
7503 max_gpu_freq = rps->max_freq;
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007504 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307505 /* Convert GT frequency to 50 HZ units */
Chris Wilsond586b5f2018-03-08 14:26:48 +00007506 min_gpu_freq /= GEN9_FREQ_SCALER;
7507 max_gpu_freq /= GEN9_FREQ_SCALER;
Akash Goel4c8c7742015-06-29 14:50:20 +05307508 }
7509
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007510 /*
7511 * For each potential GPU frequency, load a ring frequency we'd like
7512 * to use for memory access. We do this by specifying the IA frequency
7513 * the PCU should use as a reference to determine the ring frequency.
7514 */
Akash Goel4c8c7742015-06-29 14:50:20 +05307515 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007516 const int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007517 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007518
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007519 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307520 /*
7521 * ring_freq = 2 * GT. ring_freq is in 100MHz units
7522 * No floor required for ring frequency on SKL.
7523 */
7524 ring_freq = gpu_freq;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00007525 } else if (INTEL_GEN(dev_priv) >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07007526 /* max(2 * GT, DDR). NB: GT is 50MHz units */
7527 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01007528 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07007529 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007530 ring_freq = max(min_ring_freq, ring_freq);
7531 /* leave ia_freq as the default, chosen by cpufreq */
7532 } else {
7533 /* On older processors, there is no separate ring
7534 * clock domain, so in order to boost the bandwidth
7535 * of the ring, we need to upclock the CPU (ia_freq).
7536 *
7537 * For GPU frequencies less than 750MHz,
7538 * just use the lowest ring freq.
7539 */
7540 if (gpu_freq < min_freq)
7541 ia_freq = 800;
7542 else
7543 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
7544 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
7545 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007546
Ben Widawsky42c05262012-09-26 10:34:00 -07007547 sandybridge_pcode_write(dev_priv,
7548 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01007549 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
7550 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
7551 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007552 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007553}
7554
Ville Syrjälä03af2042014-06-28 02:03:53 +03007555static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05307556{
7557 u32 val, rp0;
7558
Jani Nikula5b5929c2015-10-07 11:17:46 +03007559 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05307560
Jani Nikula02584042018-12-31 16:56:41 +02007561 switch (RUNTIME_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03007562 case 8:
7563 /* (2 * 4) config */
7564 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
7565 break;
7566 case 12:
7567 /* (2 * 6) config */
7568 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
7569 break;
7570 case 16:
7571 /* (2 * 8) config */
7572 default:
7573 /* Setting (2 * 8) Min RP0 for any other combination */
7574 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
7575 break;
Deepak S095acd52015-01-17 11:05:59 +05307576 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03007577
7578 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
7579
Deepak S2b6b3a02014-05-27 15:59:30 +05307580 return rp0;
7581}
7582
7583static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7584{
7585 u32 val, rpe;
7586
7587 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
7588 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
7589
7590 return rpe;
7591}
7592
Deepak S7707df42014-07-12 18:46:14 +05307593static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
7594{
7595 u32 val, rp1;
7596
Jani Nikula5b5929c2015-10-07 11:17:46 +03007597 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
7598 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
7599
Deepak S7707df42014-07-12 18:46:14 +05307600 return rp1;
7601}
7602
Deepak S96676fe2016-08-12 18:46:41 +05307603static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
7604{
7605 u32 val, rpn;
7606
7607 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7608 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7609 FB_GFX_FREQ_FUSE_MASK);
7610
7611 return rpn;
7612}
7613
Deepak Sf8f2b002014-07-10 13:16:21 +05307614static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7615{
7616 u32 val, rp1;
7617
7618 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7619
7620 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7621
7622 return rp1;
7623}
7624
Ville Syrjälä03af2042014-06-28 02:03:53 +03007625static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007626{
7627 u32 val, rp0;
7628
Jani Nikula64936252013-05-22 15:36:20 +03007629 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007630
7631 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7632 /* Clamp to max */
7633 rp0 = min_t(u32, rp0, 0xea);
7634
7635 return rp0;
7636}
7637
7638static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7639{
7640 u32 val, rpe;
7641
Jani Nikula64936252013-05-22 15:36:20 +03007642 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007643 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007644 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007645 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7646
7647 return rpe;
7648}
7649
Ville Syrjälä03af2042014-06-28 02:03:53 +03007650static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007651{
Imre Deak36146032014-12-04 18:39:35 +02007652 u32 val;
7653
7654 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7655 /*
7656 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7657 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7658 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7659 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7660 * to make sure it matches what Punit accepts.
7661 */
7662 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007663}
7664
Imre Deakae484342014-03-31 15:10:44 +03007665/* Check that the pctx buffer wasn't move under us. */
7666static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
7667{
7668 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7669
Matthew Auld77894222017-12-11 15:18:18 +00007670 WARN_ON(pctx_addr != dev_priv->dsm.start +
Imre Deakae484342014-03-31 15:10:44 +03007671 dev_priv->vlv_pctx->stolen->start);
7672}
7673
Deepak S38807742014-05-23 21:00:15 +05307674
7675/* Check that the pcbr address is not empty. */
7676static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
7677{
7678 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7679
7680 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
7681}
7682
Chris Wilsondc979972016-05-10 14:10:04 +01007683static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307684{
Matthew Auldb7128ef2017-12-11 15:18:22 +00007685 resource_size_t pctx_paddr, paddr;
7686 resource_size_t pctx_size = 32*1024;
Deepak S38807742014-05-23 21:00:15 +05307687 u32 pcbr;
Deepak S38807742014-05-23 21:00:15 +05307688
Deepak S38807742014-05-23 21:00:15 +05307689 pcbr = I915_READ(VLV_PCBR);
7690 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007691 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Matthew Auld77894222017-12-11 15:18:18 +00007692 paddr = dev_priv->dsm.end + 1 - pctx_size;
7693 GEM_BUG_ON(paddr > U32_MAX);
Deepak S38807742014-05-23 21:00:15 +05307694
7695 pctx_paddr = (paddr & (~4095));
7696 I915_WRITE(VLV_PCBR, pctx_paddr);
7697 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007698
7699 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05307700}
7701
Chris Wilsondc979972016-05-10 14:10:04 +01007702static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007703{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007704 struct drm_i915_gem_object *pctx;
Matthew Auldb7128ef2017-12-11 15:18:22 +00007705 resource_size_t pctx_paddr;
7706 resource_size_t pctx_size = 24*1024;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007707 u32 pcbr;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007708
7709 pcbr = I915_READ(VLV_PCBR);
7710 if (pcbr) {
7711 /* BIOS set it up already, grab the pre-alloc'd space */
Matthew Auldb7128ef2017-12-11 15:18:22 +00007712 resource_size_t pcbr_offset;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007713
Matthew Auld77894222017-12-11 15:18:18 +00007714 pcbr_offset = (pcbr & (~4095)) - dev_priv->dsm.start;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007715 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007716 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02007717 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007718 pctx_size);
7719 goto out;
7720 }
7721
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007722 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
7723
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007724 /*
7725 * From the Gunit register HAS:
7726 * The Gfx driver is expected to program this register and ensure
7727 * proper allocation within Gfx stolen memory. For example, this
7728 * register should be programmed such than the PCBR range does not
7729 * overlap with other ranges, such as the frame buffer, protected
7730 * memory, or any other relevant ranges.
7731 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007732 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007733 if (!pctx) {
7734 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00007735 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007736 }
7737
Matthew Auld77894222017-12-11 15:18:18 +00007738 GEM_BUG_ON(range_overflows_t(u64,
7739 dev_priv->dsm.start,
7740 pctx->stolen->start,
7741 U32_MAX));
7742 pctx_paddr = dev_priv->dsm.start + pctx->stolen->start;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007743 I915_WRITE(VLV_PCBR, pctx_paddr);
7744
7745out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007746 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007747 dev_priv->vlv_pctx = pctx;
7748}
7749
Chris Wilsondc979972016-05-10 14:10:04 +01007750static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007751{
Chris Wilson818fed42018-07-12 11:54:54 +01007752 struct drm_i915_gem_object *pctx;
Imre Deakae484342014-03-31 15:10:44 +03007753
Chris Wilson818fed42018-07-12 11:54:54 +01007754 pctx = fetch_and_zero(&dev_priv->vlv_pctx);
7755 if (pctx)
7756 i915_gem_object_put(pctx);
Imre Deakae484342014-03-31 15:10:44 +03007757}
7758
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007759static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7760{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007761 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007762 vlv_get_cck_clock(dev_priv, "GPLL ref",
7763 CCK_GPLL_CLOCK_CONTROL,
7764 dev_priv->czclk_freq);
7765
7766 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007767 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007768}
7769
Chris Wilsondc979972016-05-10 14:10:04 +01007770static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007771{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007772 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007773 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007774
Chris Wilsondc979972016-05-10 14:10:04 +01007775 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007776
Chris Wilson337fa6e2019-04-26 09:17:20 +01007777 vlv_iosf_sb_get(dev_priv,
7778 BIT(VLV_IOSF_SB_PUNIT) |
7779 BIT(VLV_IOSF_SB_NC) |
7780 BIT(VLV_IOSF_SB_CCK));
7781
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007782 vlv_init_gpll_ref_freq(dev_priv);
7783
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007784 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7785 switch ((val >> 6) & 3) {
7786 case 0:
7787 case 1:
7788 dev_priv->mem_freq = 800;
7789 break;
7790 case 2:
7791 dev_priv->mem_freq = 1066;
7792 break;
7793 case 3:
7794 dev_priv->mem_freq = 1333;
7795 break;
7796 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007797 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007798
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007799 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7800 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007801 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007802 intel_gpu_freq(dev_priv, rps->max_freq),
7803 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007804
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007805 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007806 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007807 intel_gpu_freq(dev_priv, rps->efficient_freq),
7808 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007809
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007810 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307811 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007812 intel_gpu_freq(dev_priv, rps->rp1_freq),
7813 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307814
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007815 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007816 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007817 intel_gpu_freq(dev_priv, rps->min_freq),
7818 rps->min_freq);
Chris Wilson337fa6e2019-04-26 09:17:20 +01007819
7820 vlv_iosf_sb_put(dev_priv,
7821 BIT(VLV_IOSF_SB_PUNIT) |
7822 BIT(VLV_IOSF_SB_NC) |
7823 BIT(VLV_IOSF_SB_CCK));
Imre Deak4e805192014-04-14 20:24:41 +03007824}
7825
Chris Wilsondc979972016-05-10 14:10:04 +01007826static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307827{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007828 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007829 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307830
Chris Wilsondc979972016-05-10 14:10:04 +01007831 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307832
Chris Wilson337fa6e2019-04-26 09:17:20 +01007833 vlv_iosf_sb_get(dev_priv,
7834 BIT(VLV_IOSF_SB_PUNIT) |
7835 BIT(VLV_IOSF_SB_NC) |
7836 BIT(VLV_IOSF_SB_CCK));
7837
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007838 vlv_init_gpll_ref_freq(dev_priv);
7839
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007840 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007841
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007842 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007843 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007844 dev_priv->mem_freq = 2000;
7845 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007846 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007847 dev_priv->mem_freq = 1600;
7848 break;
7849 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007850 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007851
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007852 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7853 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307854 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007855 intel_gpu_freq(dev_priv, rps->max_freq),
7856 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307857
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007858 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307859 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007860 intel_gpu_freq(dev_priv, rps->efficient_freq),
7861 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307862
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007863 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307864 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007865 intel_gpu_freq(dev_priv, rps->rp1_freq),
7866 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307867
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007868 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307869 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007870 intel_gpu_freq(dev_priv, rps->min_freq),
7871 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307872
Chris Wilson337fa6e2019-04-26 09:17:20 +01007873 vlv_iosf_sb_put(dev_priv,
7874 BIT(VLV_IOSF_SB_PUNIT) |
7875 BIT(VLV_IOSF_SB_NC) |
7876 BIT(VLV_IOSF_SB_CCK));
7877
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007878 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7879 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007880 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307881}
7882
Chris Wilsondc979972016-05-10 14:10:04 +01007883static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007884{
Chris Wilsondc979972016-05-10 14:10:04 +01007885 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007886}
7887
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007888static void cherryview_enable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307889{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007890 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307891 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007892 u32 gtfifodbg, rc6_mode, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307893
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007894 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7895 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307896 if (gtfifodbg) {
7897 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7898 gtfifodbg);
7899 I915_WRITE(GTFIFODBG, gtfifodbg);
7900 }
7901
7902 cherryview_check_pctx(dev_priv);
7903
7904 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7905 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007906 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307907
Ville Syrjälä160614a2015-01-19 13:50:47 +02007908 /* Disable RC states. */
7909 I915_WRITE(GEN6_RC_CONTROL, 0);
7910
Deepak S38807742014-05-23 21:00:15 +05307911 /* 2a: Program RC6 thresholds.*/
7912 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7913 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7914 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7915
Akash Goel3b3f1652016-10-13 22:44:48 +05307916 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007917 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307918 I915_WRITE(GEN6_RC_SLEEP, 0);
7919
Deepak Sf4f71c72015-03-28 15:23:35 +05307920 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7921 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307922
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007923 /* Allows RC6 residency counter to work */
Deepak S38807742014-05-23 21:00:15 +05307924 I915_WRITE(VLV_COUNTER_CONTROL,
7925 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7926 VLV_MEDIA_RC6_COUNT_EN |
7927 VLV_RENDER_RC6_COUNT_EN));
7928
7929 /* For now we assume BIOS is allocating and populating the PCBR */
7930 pcbr = I915_READ(VLV_PCBR);
7931
Deepak S38807742014-05-23 21:00:15 +05307932 /* 3: Enable RC6 */
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007933 rc6_mode = 0;
7934 if (pcbr >> VLV_PCBR_ADDR_SHIFT)
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007935 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307936 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7937
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007938 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007939}
7940
7941static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7942{
7943 u32 val;
7944
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007945 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007946
7947 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007948 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307949 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7950 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7951 I915_WRITE(GEN6_RP_UP_EI, 66000);
7952 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7953
7954 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7955
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007956 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307957 I915_WRITE(GEN6_RP_CONTROL,
7958 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007959 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307960 GEN6_RP_ENABLE |
7961 GEN6_RP_UP_BUSY_AVG |
7962 GEN6_RP_DOWN_IDLE_AVG);
7963
Deepak S3ef62342015-04-29 08:36:24 +05307964 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007965 vlv_punit_get(dev_priv);
7966
7967 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
Deepak S3ef62342015-04-29 08:36:24 +05307968 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7969
Deepak S2b6b3a02014-05-27 15:59:30 +05307970 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7971
Chris Wilson337fa6e2019-04-26 09:17:20 +01007972 vlv_punit_put(dev_priv);
7973
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007974 /* RPS code assumes GPLL is used */
7975 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7976
Jani Nikula742f4912015-09-03 11:16:09 +03007977 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307978 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7979
Chris Wilson3a45b052016-07-13 09:10:32 +01007980 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307981
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007982 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307983}
7984
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007985static void valleyview_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007986{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007987 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307988 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007989 u32 gtfifodbg;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007990
Imre Deakae484342014-03-31 15:10:44 +03007991 valleyview_check_pctx(dev_priv);
7992
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007993 gtfifodbg = I915_READ(GTFIFODBG);
7994 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007995 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7996 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007997 I915_WRITE(GTFIFODBG, gtfifodbg);
7998 }
7999
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008000 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008001
Ville Syrjälä160614a2015-01-19 13:50:47 +02008002 /* Disable RC states. */
8003 I915_WRITE(GEN6_RC_CONTROL, 0);
8004
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008005 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
8006 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
8007 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
8008
8009 for_each_engine(engine, dev_priv, id)
8010 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
8011
8012 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
8013
8014 /* Allows RC6 residency counter to work */
8015 I915_WRITE(VLV_COUNTER_CONTROL,
8016 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
8017 VLV_MEDIA_RC0_COUNT_EN |
8018 VLV_RENDER_RC0_COUNT_EN |
8019 VLV_MEDIA_RC6_COUNT_EN |
8020 VLV_RENDER_RC6_COUNT_EN));
8021
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008022 I915_WRITE(GEN6_RC_CONTROL,
8023 GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008024
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008025 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008026}
8027
8028static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
8029{
8030 u32 val;
8031
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008032 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008033
Ville Syrjäläcad725f2015-01-19 13:50:48 +02008034 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008035 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
8036 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
8037 I915_WRITE(GEN6_RP_UP_EI, 66000);
8038 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
8039
8040 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
8041
8042 I915_WRITE(GEN6_RP_CONTROL,
8043 GEN6_RP_MEDIA_TURBO |
8044 GEN6_RP_MEDIA_HW_NORMAL_MODE |
8045 GEN6_RP_MEDIA_IS_GFX |
8046 GEN6_RP_ENABLE |
8047 GEN6_RP_UP_BUSY_AVG |
8048 GEN6_RP_DOWN_IDLE_CONT);
8049
Chris Wilson337fa6e2019-04-26 09:17:20 +01008050 vlv_punit_get(dev_priv);
8051
Deepak S3ef62342015-04-29 08:36:24 +05308052 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01008053 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
Deepak S3ef62342015-04-29 08:36:24 +05308054 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
8055
Jani Nikula64936252013-05-22 15:36:20 +03008056 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008057
Chris Wilson337fa6e2019-04-26 09:17:20 +01008058 vlv_punit_put(dev_priv);
8059
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02008060 /* RPS code assumes GPLL is used */
8061 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
8062
Jani Nikula742f4912015-09-03 11:16:09 +03008063 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07008064 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
8065
Chris Wilson3a45b052016-07-13 09:10:32 +01008066 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008067
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008068 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008069}
8070
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008071static unsigned long intel_pxfreq(u32 vidfreq)
8072{
8073 unsigned long freq;
8074 int div = (vidfreq & 0x3f0000) >> 16;
8075 int post = (vidfreq & 0x3000) >> 12;
8076 int pre = (vidfreq & 0x7);
8077
8078 if (!pre)
8079 return 0;
8080
8081 freq = ((div * 133333) / ((1<<post) * pre));
8082
8083 return freq;
8084}
8085
Daniel Vettereb48eb02012-04-26 23:28:12 +02008086static const struct cparams {
8087 u16 i;
8088 u16 t;
8089 u16 m;
8090 u16 c;
8091} cparams[] = {
8092 { 1, 1333, 301, 28664 },
8093 { 1, 1066, 294, 24460 },
8094 { 1, 800, 294, 25192 },
8095 { 0, 1333, 276, 27605 },
8096 { 0, 1066, 276, 27605 },
8097 { 0, 800, 231, 23784 },
8098};
8099
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008100static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008101{
8102 u64 total_count, diff, ret;
8103 u32 count1, count2, count3, m = 0, c = 0;
8104 unsigned long now = jiffies_to_msecs(jiffies), diff1;
8105 int i;
8106
Chris Wilson67520412017-03-02 13:28:01 +00008107 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02008108
Daniel Vetter20e4d402012-08-08 23:35:39 +02008109 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008110
8111 /* Prevent division-by-zero if we are asking too fast.
8112 * Also, we don't get interesting results if we are polling
8113 * faster than once in 10ms, so just return the saved value
8114 * in such cases.
8115 */
8116 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02008117 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008118
8119 count1 = I915_READ(DMIEC);
8120 count2 = I915_READ(DDREC);
8121 count3 = I915_READ(CSIEC);
8122
8123 total_count = count1 + count2 + count3;
8124
8125 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02008126 if (total_count < dev_priv->ips.last_count1) {
8127 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008128 diff += total_count;
8129 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02008130 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008131 }
8132
8133 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02008134 if (cparams[i].i == dev_priv->ips.c_m &&
8135 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02008136 m = cparams[i].m;
8137 c = cparams[i].c;
8138 break;
8139 }
8140 }
8141
8142 diff = div_u64(diff, diff1);
8143 ret = ((m * diff) + c);
8144 ret = div_u64(ret, 10);
8145
Daniel Vetter20e4d402012-08-08 23:35:39 +02008146 dev_priv->ips.last_count1 = total_count;
8147 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008148
Daniel Vetter20e4d402012-08-08 23:35:39 +02008149 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008150
8151 return ret;
8152}
8153
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008154unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
8155{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008156 intel_wakeref_t wakeref;
8157 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008158
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008159 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008160 return 0;
8161
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008162 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008163 spin_lock_irq(&mchdev_lock);
8164 val = __i915_chipset_val(dev_priv);
8165 spin_unlock_irq(&mchdev_lock);
8166 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008167
8168 return val;
8169}
8170
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01008171unsigned long i915_mch_val(struct drm_i915_private *i915)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008172{
8173 unsigned long m, x, b;
8174 u32 tsfs;
8175
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01008176 tsfs = intel_uncore_read(&i915->uncore, TSFS);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008177
8178 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01008179 x = intel_uncore_read8(&i915->uncore, TR1);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008180
8181 b = tsfs & TSFS_INTR_MASK;
8182
8183 return ((m * x) / 127) - b;
8184}
8185
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02008186static int _pxvid_to_vd(u8 pxvid)
8187{
8188 if (pxvid == 0)
8189 return 0;
8190
8191 if (pxvid >= 8 && pxvid < 31)
8192 pxvid = 31;
8193
8194 return (pxvid + 2) * 125;
8195}
8196
8197static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008198{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02008199 const int vd = _pxvid_to_vd(pxvid);
8200 const int vm = vd - 1125;
8201
Chris Wilsondc979972016-05-10 14:10:04 +01008202 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02008203 return vm > 0 ? vm : 0;
8204
8205 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008206}
8207
Daniel Vetter02d71952012-08-09 16:44:54 +02008208static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008209{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00008210 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008211 u32 count;
8212
Chris Wilson67520412017-03-02 13:28:01 +00008213 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008214
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00008215 now = ktime_get_raw_ns();
8216 diffms = now - dev_priv->ips.last_time2;
8217 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008218
8219 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02008220 if (!diffms)
8221 return;
8222
8223 count = I915_READ(GFXEC);
8224
Daniel Vetter20e4d402012-08-08 23:35:39 +02008225 if (count < dev_priv->ips.last_count2) {
8226 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008227 diff += count;
8228 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02008229 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008230 }
8231
Daniel Vetter20e4d402012-08-08 23:35:39 +02008232 dev_priv->ips.last_count2 = count;
8233 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008234
8235 /* More magic constants... */
8236 diff = diff * 1181;
8237 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02008238 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008239}
8240
Daniel Vetter02d71952012-08-09 16:44:54 +02008241void i915_update_gfx_val(struct drm_i915_private *dev_priv)
8242{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008243 intel_wakeref_t wakeref;
8244
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008245 if (!IS_GEN(dev_priv, 5))
Daniel Vetter02d71952012-08-09 16:44:54 +02008246 return;
8247
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008248 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008249 spin_lock_irq(&mchdev_lock);
8250 __i915_update_gfx_val(dev_priv);
8251 spin_unlock_irq(&mchdev_lock);
8252 }
Daniel Vetter02d71952012-08-09 16:44:54 +02008253}
8254
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008255static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008256{
8257 unsigned long t, corr, state1, corr2, state2;
8258 u32 pxvid, ext_v;
8259
Chris Wilson67520412017-03-02 13:28:01 +00008260 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02008261
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008262 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02008263 pxvid = (pxvid >> 24) & 0x7f;
8264 ext_v = pvid_to_extvid(dev_priv, pxvid);
8265
8266 state1 = ext_v;
8267
8268 t = i915_mch_val(dev_priv);
8269
8270 /* Revel in the empirically derived constants */
8271
8272 /* Correction factor in 1/100000 units */
8273 if (t > 80)
8274 corr = ((t * 2349) + 135940);
8275 else if (t >= 50)
8276 corr = ((t * 964) + 29317);
8277 else /* < 50 */
8278 corr = ((t * 301) + 1004);
8279
8280 corr = corr * ((150142 * state1) / 10000 - 78642);
8281 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02008282 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008283
8284 state2 = (corr2 * state1) / 10000;
8285 state2 /= 100; /* convert to mW */
8286
Daniel Vetter02d71952012-08-09 16:44:54 +02008287 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008288
Daniel Vetter20e4d402012-08-08 23:35:39 +02008289 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008290}
8291
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008292unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
8293{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008294 intel_wakeref_t wakeref;
8295 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008296
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008297 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008298 return 0;
8299
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008300 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008301 spin_lock_irq(&mchdev_lock);
8302 val = __i915_gfx_val(dev_priv);
8303 spin_unlock_irq(&mchdev_lock);
8304 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008305
8306 return val;
8307}
8308
Chris Wilsonadc674c2019-04-12 09:53:22 +01008309static struct drm_i915_private __rcu *i915_mch_dev;
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008310
8311static struct drm_i915_private *mchdev_get(void)
8312{
8313 struct drm_i915_private *i915;
8314
8315 rcu_read_lock();
Chris Wilsonadc674c2019-04-12 09:53:22 +01008316 i915 = rcu_dereference(i915_mch_dev);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008317 if (!kref_get_unless_zero(&i915->drm.ref))
8318 i915 = NULL;
8319 rcu_read_unlock();
8320
8321 return i915;
8322}
8323
Daniel Vettereb48eb02012-04-26 23:28:12 +02008324/**
8325 * i915_read_mch_val - return value for IPS use
8326 *
8327 * Calculate and return a value for the IPS driver to use when deciding whether
8328 * we have thermal and power headroom to increase CPU or GPU power budget.
8329 */
8330unsigned long i915_read_mch_val(void)
8331{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008332 struct drm_i915_private *i915;
8333 unsigned long chipset_val = 0;
8334 unsigned long graphics_val = 0;
8335 intel_wakeref_t wakeref;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008336
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008337 i915 = mchdev_get();
8338 if (!i915)
8339 return 0;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008340
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008341 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008342 spin_lock_irq(&mchdev_lock);
8343 chipset_val = __i915_chipset_val(i915);
8344 graphics_val = __i915_gfx_val(i915);
8345 spin_unlock_irq(&mchdev_lock);
8346 }
Daniel Vettereb48eb02012-04-26 23:28:12 +02008347
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008348 drm_dev_put(&i915->drm);
8349 return chipset_val + graphics_val;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008350}
8351EXPORT_SYMBOL_GPL(i915_read_mch_val);
8352
8353/**
8354 * i915_gpu_raise - raise GPU frequency limit
8355 *
8356 * Raise the limit; IPS indicates we have thermal headroom.
8357 */
8358bool i915_gpu_raise(void)
8359{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008360 struct drm_i915_private *i915;
8361
8362 i915 = mchdev_get();
8363 if (!i915)
8364 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008365
Daniel Vetter92703882012-08-09 16:46:01 +02008366 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008367 if (i915->ips.max_delay > i915->ips.fmax)
8368 i915->ips.max_delay--;
Daniel Vetter92703882012-08-09 16:46:01 +02008369 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008370
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008371 drm_dev_put(&i915->drm);
8372 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008373}
8374EXPORT_SYMBOL_GPL(i915_gpu_raise);
8375
8376/**
8377 * i915_gpu_lower - lower GPU frequency limit
8378 *
8379 * IPS indicates we're close to a thermal limit, so throttle back the GPU
8380 * frequency maximum.
8381 */
8382bool i915_gpu_lower(void)
8383{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008384 struct drm_i915_private *i915;
8385
8386 i915 = mchdev_get();
8387 if (!i915)
8388 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008389
Daniel Vetter92703882012-08-09 16:46:01 +02008390 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008391 if (i915->ips.max_delay < i915->ips.min_delay)
8392 i915->ips.max_delay++;
Daniel Vetter92703882012-08-09 16:46:01 +02008393 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008394
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008395 drm_dev_put(&i915->drm);
8396 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008397}
8398EXPORT_SYMBOL_GPL(i915_gpu_lower);
8399
8400/**
8401 * i915_gpu_busy - indicate GPU business to IPS
8402 *
8403 * Tell the IPS driver whether or not the GPU is busy.
8404 */
8405bool i915_gpu_busy(void)
8406{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008407 struct drm_i915_private *i915;
8408 bool ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008409
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008410 i915 = mchdev_get();
8411 if (!i915)
8412 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008413
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008414 ret = i915->gt.awake;
8415
8416 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008417 return ret;
8418}
8419EXPORT_SYMBOL_GPL(i915_gpu_busy);
8420
8421/**
8422 * i915_gpu_turbo_disable - disable graphics turbo
8423 *
8424 * Disable graphics turbo by resetting the max frequency and setting the
8425 * current frequency to the default.
8426 */
8427bool i915_gpu_turbo_disable(void)
8428{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008429 struct drm_i915_private *i915;
8430 bool ret;
8431
8432 i915 = mchdev_get();
8433 if (!i915)
8434 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008435
Daniel Vetter92703882012-08-09 16:46:01 +02008436 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008437 i915->ips.max_delay = i915->ips.fstart;
8438 ret = ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02008439 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008440
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008441 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008442 return ret;
8443}
8444EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
8445
8446/**
8447 * Tells the intel_ips driver that the i915 driver is now loaded, if
8448 * IPS got loaded first.
8449 *
8450 * This awkward dance is so that neither module has to depend on the
8451 * other in order for IPS to do the appropriate communication of
8452 * GPU turbo limits to i915.
8453 */
8454static void
8455ips_ping_for_i915_load(void)
8456{
8457 void (*link)(void);
8458
8459 link = symbol_get(ips_link_to_i915_driver);
8460 if (link) {
8461 link();
8462 symbol_put(ips_link_to_i915_driver);
8463 }
8464}
8465
8466void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
8467{
Daniel Vetter02d71952012-08-09 16:44:54 +02008468 /* We only register the i915 ips part with intel-ips once everything is
8469 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008470 rcu_assign_pointer(i915_mch_dev, dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008471
8472 ips_ping_for_i915_load();
8473}
8474
8475void intel_gpu_ips_teardown(void)
8476{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008477 rcu_assign_pointer(i915_mch_dev, NULL);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008478}
Deepak S76c3552f2014-01-30 23:08:16 +05308479
Chris Wilsondc979972016-05-10 14:10:04 +01008480static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008481{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008482 u32 lcfuse;
8483 u8 pxw[16];
8484 int i;
8485
8486 /* Disable to program */
8487 I915_WRITE(ECR, 0);
8488 POSTING_READ(ECR);
8489
8490 /* Program energy weights for various events */
8491 I915_WRITE(SDEW, 0x15040d00);
8492 I915_WRITE(CSIEW0, 0x007f0000);
8493 I915_WRITE(CSIEW1, 0x1e220004);
8494 I915_WRITE(CSIEW2, 0x04000004);
8495
8496 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03008497 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008498 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03008499 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008500
8501 /* Program P-state weights to account for frequency power adjustment */
8502 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03008503 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008504 unsigned long freq = intel_pxfreq(pxvidfreq);
8505 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
8506 PXVFREQ_PX_SHIFT;
8507 unsigned long val;
8508
8509 val = vid * vid;
8510 val *= (freq / 1000);
8511 val *= 255;
8512 val /= (127*127*900);
8513 if (val > 0xff)
8514 DRM_ERROR("bad pxval: %ld\n", val);
8515 pxw[i] = val;
8516 }
8517 /* Render standby states get 0 weight */
8518 pxw[14] = 0;
8519 pxw[15] = 0;
8520
8521 for (i = 0; i < 4; i++) {
8522 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
8523 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03008524 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008525 }
8526
8527 /* Adjust magic regs to magic values (more experimental results) */
8528 I915_WRITE(OGW0, 0);
8529 I915_WRITE(OGW1, 0);
8530 I915_WRITE(EG0, 0x00007f00);
8531 I915_WRITE(EG1, 0x0000000e);
8532 I915_WRITE(EG2, 0x000e0000);
8533 I915_WRITE(EG3, 0x68000300);
8534 I915_WRITE(EG4, 0x42000000);
8535 I915_WRITE(EG5, 0x00140031);
8536 I915_WRITE(EG6, 0);
8537 I915_WRITE(EG7, 0);
8538
8539 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03008540 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008541
8542 /* Enable PMON + select events */
8543 I915_WRITE(ECR, 0x80000019);
8544
8545 lcfuse = I915_READ(LCFUSE02);
8546
Daniel Vetter20e4d402012-08-08 23:35:39 +02008547 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008548}
8549
Chris Wilsondc979972016-05-10 14:10:04 +01008550void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03008551{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008552 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8553
Imre Deakb268c692015-12-15 20:10:31 +02008554 /*
8555 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
8556 * requirement.
8557 */
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008558 if (!sanitize_rc6(dev_priv)) {
Imre Deakb268c692015-12-15 20:10:31 +02008559 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
Chris Wilson08ea70a2018-08-12 23:36:31 +01008560 pm_runtime_get(&dev_priv->drm.pdev->dev);
Imre Deakb268c692015-12-15 20:10:31 +02008561 }
Imre Deake6069ca2014-04-18 16:01:02 +03008562
Chris Wilson773ea9a2016-07-13 09:10:33 +01008563 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01008564 if (IS_CHERRYVIEW(dev_priv))
8565 cherryview_init_gt_powersave(dev_priv);
8566 else if (IS_VALLEYVIEW(dev_priv))
8567 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01008568 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01008569 gen6_init_rps_frequencies(dev_priv);
8570
8571 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008572 rps->max_freq_softlimit = rps->max_freq;
8573 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01008574
Chris Wilson99ac9612016-07-13 09:10:34 +01008575 /* After setting max-softlimit, find the overclock max freq */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008576 if (IS_GEN(dev_priv, 6) ||
Chris Wilson99ac9612016-07-13 09:10:34 +01008577 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
8578 u32 params = 0;
8579
Ville Syrjäläd284d512019-05-21 19:40:24 +03008580 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS,
8581 &params, NULL);
Chris Wilson99ac9612016-07-13 09:10:34 +01008582 if (params & BIT(31)) { /* OC supported */
8583 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008584 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01008585 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008586 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01008587 }
8588 }
8589
Chris Wilson29ecd78d2016-07-13 09:10:35 +01008590 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008591 rps->boost_freq = rps->max_freq;
Chris Wilson844e3312019-04-18 21:53:58 +01008592 rps->idle_freq = rps->min_freq;
8593 rps->cur_freq = rps->idle_freq;
Imre Deakae484342014-03-31 15:10:44 +03008594}
8595
Chris Wilsondc979972016-05-10 14:10:04 +01008596void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03008597{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03008598 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01008599 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02008600
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008601 if (!HAS_RC6(dev_priv))
Chris Wilson08ea70a2018-08-12 23:36:31 +01008602 pm_runtime_put(&dev_priv->drm.pdev->dev);
Imre Deakae484342014-03-31 15:10:44 +03008603}
8604
Chris Wilsonb7137e02016-07-13 09:10:37 +01008605void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
8606{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008607 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
8608 dev_priv->gt_pm.rc6.enabled = true; /* force RC6 disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01008609 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008610
Oscar Mateod02b98b2018-04-05 17:00:50 +03008611 if (INTEL_GEN(dev_priv) >= 11)
8612 gen11_reset_rps_interrupts(dev_priv);
Chris Wilson61e1e372018-08-12 23:36:30 +01008613 else if (INTEL_GEN(dev_priv) >= 6)
Oscar Mateod02b98b2018-04-05 17:00:50 +03008614 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07008615}
8616
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008617static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
8618{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008619 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008620
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008621 if (!i915->gt_pm.llc_pstate.enabled)
8622 return;
8623
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008624 /* Currently there is no HW configuration to be done to disable. */
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008625
8626 i915->gt_pm.llc_pstate.enabled = false;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008627}
8628
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008629static void intel_disable_rc6(struct drm_i915_private *dev_priv)
8630{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008631 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008632
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008633 if (!dev_priv->gt_pm.rc6.enabled)
8634 return;
8635
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008636 if (INTEL_GEN(dev_priv) >= 9)
8637 gen9_disable_rc6(dev_priv);
8638 else if (IS_CHERRYVIEW(dev_priv))
8639 cherryview_disable_rc6(dev_priv);
8640 else if (IS_VALLEYVIEW(dev_priv))
8641 valleyview_disable_rc6(dev_priv);
8642 else if (INTEL_GEN(dev_priv) >= 6)
8643 gen6_disable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008644
8645 dev_priv->gt_pm.rc6.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008646}
8647
8648static void intel_disable_rps(struct drm_i915_private *dev_priv)
8649{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008650 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008651
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008652 if (!dev_priv->gt_pm.rps.enabled)
8653 return;
8654
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008655 if (INTEL_GEN(dev_priv) >= 9)
8656 gen9_disable_rps(dev_priv);
8657 else if (IS_CHERRYVIEW(dev_priv))
8658 cherryview_disable_rps(dev_priv);
8659 else if (IS_VALLEYVIEW(dev_priv))
8660 valleyview_disable_rps(dev_priv);
8661 else if (INTEL_GEN(dev_priv) >= 6)
8662 gen6_disable_rps(dev_priv);
8663 else if (IS_IRONLAKE_M(dev_priv))
8664 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008665
8666 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008667}
8668
Chris Wilsondc979972016-05-10 14:10:04 +01008669void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008670{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008671 mutex_lock(&dev_priv->gt_pm.rps.lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008672
Chris Wilsonab37c4d2019-09-10 17:16:57 +01008673 intel_disable_rc6(dev_priv);
Andi Shyti42014f62019-09-05 14:14:03 +03008674
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008675 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008676 if (HAS_LLC(dev_priv))
8677 intel_disable_llc_pstate(dev_priv);
8678
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008679 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008680}
8681
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008682static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
8683{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008684 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008685
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008686 if (i915->gt_pm.llc_pstate.enabled)
8687 return;
8688
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008689 gen6_update_ring_freq(i915);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008690
8691 i915->gt_pm.llc_pstate.enabled = true;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008692}
8693
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008694static void intel_enable_rc6(struct drm_i915_private *dev_priv)
8695{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008696 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008697
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008698 if (dev_priv->gt_pm.rc6.enabled)
8699 return;
8700
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008701 if (IS_CHERRYVIEW(dev_priv))
8702 cherryview_enable_rc6(dev_priv);
8703 else if (IS_VALLEYVIEW(dev_priv))
8704 valleyview_enable_rc6(dev_priv);
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03008705 else if (INTEL_GEN(dev_priv) >= 11)
8706 gen11_enable_rc6(dev_priv);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008707 else if (INTEL_GEN(dev_priv) >= 9)
8708 gen9_enable_rc6(dev_priv);
8709 else if (IS_BROADWELL(dev_priv))
8710 gen8_enable_rc6(dev_priv);
8711 else if (INTEL_GEN(dev_priv) >= 6)
8712 gen6_enable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008713
8714 dev_priv->gt_pm.rc6.enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008715}
8716
8717static void intel_enable_rps(struct drm_i915_private *dev_priv)
8718{
8719 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8720
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008721 lockdep_assert_held(&rps->lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008722
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008723 if (rps->enabled)
8724 return;
8725
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008726 if (IS_CHERRYVIEW(dev_priv)) {
8727 cherryview_enable_rps(dev_priv);
8728 } else if (IS_VALLEYVIEW(dev_priv)) {
8729 valleyview_enable_rps(dev_priv);
8730 } else if (INTEL_GEN(dev_priv) >= 9) {
8731 gen9_enable_rps(dev_priv);
8732 } else if (IS_BROADWELL(dev_priv)) {
8733 gen8_enable_rps(dev_priv);
8734 } else if (INTEL_GEN(dev_priv) >= 6) {
8735 gen6_enable_rps(dev_priv);
8736 } else if (IS_IRONLAKE_M(dev_priv)) {
8737 ironlake_enable_drps(dev_priv);
8738 intel_init_emon(dev_priv);
8739 }
8740
8741 WARN_ON(rps->max_freq < rps->min_freq);
8742 WARN_ON(rps->idle_freq > rps->max_freq);
8743
8744 WARN_ON(rps->efficient_freq < rps->min_freq);
8745 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008746
8747 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008748}
8749
Chris Wilsonb7137e02016-07-13 09:10:37 +01008750void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
8751{
Chris Wilsonb7137e02016-07-13 09:10:37 +01008752 /* Powersaving is controlled by the host when inside a VM */
8753 if (intel_vgpu_active(dev_priv))
8754 return;
8755
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008756 mutex_lock(&dev_priv->gt_pm.rps.lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02008757
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008758 if (HAS_RC6(dev_priv))
8759 intel_enable_rc6(dev_priv);
Chris Wilson91cbdb82019-04-19 14:48:36 +01008760 if (HAS_RPS(dev_priv))
8761 intel_enable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008762 if (HAS_LLC(dev_priv))
8763 intel_enable_llc_pstate(dev_priv);
8764
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008765 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008766}
Imre Deakc6df39b2014-04-14 20:24:29 +03008767
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008768static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008769{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008770 /*
8771 * On Ibex Peak and Cougar Point, we need to disable clock
8772 * gating for the panel power sequencer or it will fail to
8773 * start up when no ports are active.
8774 */
8775 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8776}
8777
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008778static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008779{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008780 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008781
Damien Lespiau055e3932014-08-18 13:49:10 +01008782 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008783 I915_WRITE(DSPCNTR(pipe),
8784 I915_READ(DSPCNTR(pipe)) |
8785 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008786
8787 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8788 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008789 }
8790}
8791
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008792static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008793{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008794 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008795
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008796 /*
8797 * Required for FBC
8798 * WaFbcDisableDpfcClockGating:ilk
8799 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008800 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8801 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8802 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008803
8804 I915_WRITE(PCH_3DCGDIS0,
8805 MARIUNIT_CLOCK_GATE_DISABLE |
8806 SVSMUNIT_CLOCK_GATE_DISABLE);
8807 I915_WRITE(PCH_3DCGDIS1,
8808 VFMUNIT_CLOCK_GATE_DISABLE);
8809
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008810 /*
8811 * According to the spec the following bits should be set in
8812 * order to enable memory self-refresh
8813 * The bit 22/21 of 0x42004
8814 * The bit 5 of 0x42020
8815 * The bit 15 of 0x45000
8816 */
8817 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8818 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8819 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008820 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008821 I915_WRITE(DISP_ARB_CTL,
8822 (I915_READ(DISP_ARB_CTL) |
8823 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008824
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008825 /*
8826 * Based on the document from hardware guys the following bits
8827 * should be set unconditionally in order to enable FBC.
8828 * The bit 22 of 0x42000
8829 * The bit 22 of 0x42004
8830 * The bit 7,8,9 of 0x42020.
8831 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008832 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008833 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008834 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8835 I915_READ(ILK_DISPLAY_CHICKEN1) |
8836 ILK_FBCQ_DIS);
8837 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8838 I915_READ(ILK_DISPLAY_CHICKEN2) |
8839 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008840 }
8841
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008842 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8843
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008844 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8845 I915_READ(ILK_DISPLAY_CHICKEN2) |
8846 ILK_ELPIN_409_SELECT);
8847 I915_WRITE(_3D_CHICKEN2,
8848 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8849 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008850
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008851 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008852 I915_WRITE(CACHE_MODE_0,
8853 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008854
Akash Goel4e046322014-04-04 17:14:38 +05308855 /* WaDisable_RenderCache_OperationalFlush:ilk */
8856 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8857
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008858 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008859
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008860 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008861}
8862
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008863static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008864{
Ville Syrjäläd048a262019-08-21 20:30:31 +03008865 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008866 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008867
8868 /*
8869 * On Ibex Peak and Cougar Point, we need to disable clock
8870 * gating for the panel power sequencer or it will fail to
8871 * start up when no ports are active.
8872 */
Jesse Barnescd664072013-10-02 10:34:19 -07008873 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8874 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8875 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008876 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8877 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008878 /* The below fixes the weird display corruption, a few pixels shifted
8879 * downward, on (only) LVDS of some HP laptops with IVY.
8880 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008881 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008882 val = I915_READ(TRANS_CHICKEN2(pipe));
8883 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8884 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008885 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008886 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008887 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8888 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8889 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008890 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8891 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008892 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008893 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008894 I915_WRITE(TRANS_CHICKEN1(pipe),
8895 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8896 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008897}
8898
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008899static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008900{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008901 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008902
8903 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008904 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8905 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8906 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008907}
8908
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008909static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008910{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008911 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008912
Damien Lespiau231e54f2012-10-19 17:55:41 +01008913 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008914
8915 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8916 I915_READ(ILK_DISPLAY_CHICKEN2) |
8917 ILK_ELPIN_409_SELECT);
8918
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008919 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008920 I915_WRITE(_3D_CHICKEN,
8921 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8922
Akash Goel4e046322014-04-04 17:14:38 +05308923 /* WaDisable_RenderCache_OperationalFlush:snb */
8924 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8925
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008926 /*
8927 * BSpec recoomends 8x4 when MSAA is used,
8928 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008929 *
8930 * Note that PS/WM thread counts depend on the WIZ hashing
8931 * disable bit, which we don't touch here, but it's good
8932 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008933 */
8934 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008935 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008936
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008937 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008938 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008939
8940 I915_WRITE(GEN6_UCGCTL1,
8941 I915_READ(GEN6_UCGCTL1) |
8942 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8943 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8944
8945 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8946 * gating disable must be set. Failure to set it results in
8947 * flickering pixels due to Z write ordering failures after
8948 * some amount of runtime in the Mesa "fire" demo, and Unigine
8949 * Sanctuary and Tropics, and apparently anything else with
8950 * alpha test or pixel discard.
8951 *
8952 * According to the spec, bit 11 (RCCUNIT) must also be set,
8953 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008954 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008955 * WaDisableRCCUnitClockGating:snb
8956 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008957 */
8958 I915_WRITE(GEN6_UCGCTL2,
8959 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8960 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8961
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008962 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008963 I915_WRITE(_3D_CHICKEN3,
8964 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008965
8966 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008967 * Bspec says:
8968 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8969 * 3DSTATE_SF number of SF output attributes is more than 16."
8970 */
8971 I915_WRITE(_3D_CHICKEN3,
8972 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8973
8974 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008975 * According to the spec the following bits should be
8976 * set in order to enable memory self-refresh and fbc:
8977 * The bit21 and bit22 of 0x42000
8978 * The bit21 and bit22 of 0x42004
8979 * The bit5 and bit7 of 0x42020
8980 * The bit14 of 0x70180
8981 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008982 *
8983 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008984 */
8985 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8986 I915_READ(ILK_DISPLAY_CHICKEN1) |
8987 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8988 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8989 I915_READ(ILK_DISPLAY_CHICKEN2) |
8990 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008991 I915_WRITE(ILK_DSPCLK_GATE_D,
8992 I915_READ(ILK_DSPCLK_GATE_D) |
8993 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8994 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008995
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008996 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008997
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008998 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008999
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009000 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009001}
9002
9003static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
9004{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009005 u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009006
Ville Syrjälä3aad9052014-01-22 21:32:59 +02009007 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02009008 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02009009 *
9010 * This actually overrides the dispatch
9011 * mode for all thread types.
9012 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009013 reg &= ~GEN7_FF_SCHED_MASK;
9014 reg |= GEN7_FF_TS_SCHED_HW;
9015 reg |= GEN7_FF_VS_SCHED_HW;
9016 reg |= GEN7_FF_DS_SCHED_HW;
9017
9018 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
9019}
9020
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009021static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009022{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009023 /*
9024 * TODO: this bit should only be enabled when really needed, then
9025 * disabled when not needed anymore in order to save power.
9026 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009027 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009028 I915_WRITE(SOUTH_DSPCLK_GATE_D,
9029 I915_READ(SOUTH_DSPCLK_GATE_D) |
9030 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03009031
9032 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03009033 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
9034 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03009035 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009036}
9037
Ville Syrjälä712bf362016-10-31 22:37:23 +02009038static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03009039{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009040 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009041 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03009042
9043 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9044 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9045 }
9046}
9047
Imre Deak450174f2016-05-03 15:54:21 +03009048static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
9049 int general_prio_credits,
9050 int high_prio_credits)
9051{
9052 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07009053 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03009054
9055 /* WaTempDisableDOPClkGating:bdw */
9056 misccpctl = I915_READ(GEN7_MISCCPCTL);
9057 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
9058
Oscar Mateo930a7842017-10-17 13:25:45 -07009059 val = I915_READ(GEN8_L3SQCREG1);
9060 val &= ~L3_PRIO_CREDITS_MASK;
9061 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
9062 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
9063 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03009064
9065 /*
9066 * Wait at least 100 clocks before re-enabling clock gating.
9067 * See the definition of L3SQCREG1 in BSpec.
9068 */
9069 POSTING_READ(GEN8_L3SQCREG1);
9070 udelay(1);
9071 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
9072}
9073
Oscar Mateod65dc3e2018-05-08 14:29:24 -07009074static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
9075{
9076 /* This is not an Wa. Enable to reduce Sampler power */
9077 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
9078 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07009079
9080 /* WaEnable32PlaneMode:icl */
9081 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
9082 _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
Oscar Mateod65dc3e2018-05-08 14:29:24 -07009083}
9084
Michel Thierry5d869232019-08-23 01:20:34 -07009085static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
9086{
9087 u32 vd_pg_enable = 0;
9088 unsigned int i;
9089
9090 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
9091 for (i = 0; i < I915_MAX_VCS; i++) {
9092 if (HAS_ENGINE(dev_priv, _VCS(i)))
9093 vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
9094 VDN_MFX_POWERGATE_ENABLE(i);
9095 }
9096
9097 I915_WRITE(POWERGATE_ENABLE,
9098 I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
9099}
9100
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009101static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
9102{
9103 if (!HAS_PCH_CNP(dev_priv))
9104 return;
9105
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08009106 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07009107 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
9108 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009109}
9110
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009111static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009112{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07009113 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009114 cnp_init_clock_gating(dev_priv);
9115
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07009116 /* This is not an Wa. Enable for better image quality */
9117 I915_WRITE(_3D_CHICKEN3,
9118 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
9119
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009120 /* WaEnableChickenDCPR:cnl */
9121 I915_WRITE(GEN8_CHICKEN_DCPR_1,
9122 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
9123
9124 /* WaFbcWakeMemOn:cnl */
9125 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
9126 DISP_FBC_MEMORY_WAKE);
9127
Chris Wilson34991bd2017-11-11 10:03:36 +00009128 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
9129 /* ReadHitWriteOnlyDisable:cnl */
9130 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009131 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
9132 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00009133 val |= SARBUNIT_CLKGATE_DIS;
9134 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08009135
Rodrigo Vivia4713c52018-03-07 14:09:12 -08009136 /* Wa_2201832410:cnl */
9137 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
9138 val |= GWUNIT_CLKGATE_DIS;
9139 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
9140
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08009141 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08009142 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08009143 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
9144 val |= VFUNIT_CLKGATE_DIS;
9145 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009146}
9147
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009148static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
9149{
9150 cnp_init_clock_gating(dev_priv);
9151 gen9_init_clock_gating(dev_priv);
9152
9153 /* WaFbcNukeOnHostModify:cfl */
9154 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
9155 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
9156}
9157
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009158static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03009159{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009160 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03009161
9162 /* WaDisableSDEUnitClockGating:kbl */
9163 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
9164 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
9165 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03009166
9167 /* WaDisableGamClockGating:kbl */
9168 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
9169 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
9170 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03009171
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009172 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03009173 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
9174 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03009175}
9176
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009177static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02009178{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009179 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03009180
9181 /* WAC6entrylatency:skl */
9182 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
9183 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03009184
9185 /* WaFbcNukeOnHostModify:skl */
9186 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
9187 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02009188}
9189
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009190static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07009191{
Damien Lespiau07d27e22014-03-03 17:31:46 +00009192 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07009193
Ben Widawskyab57fff2013-12-12 15:28:04 -08009194 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07009195 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07009196
Ben Widawskyab57fff2013-12-12 15:28:04 -08009197 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07009198 I915_WRITE(CHICKEN_PAR1_1,
9199 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
9200
Ben Widawskyab57fff2013-12-12 15:28:04 -08009201 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01009202 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00009203 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02009204 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02009205 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07009206 }
Ben Widawsky63801f22013-12-12 17:26:03 -08009207
Ben Widawskyab57fff2013-12-12 15:28:04 -08009208 /* WaVSRefCountFullforceMissDisable:bdw */
9209 /* WaDSRefCountFullforceMissDisable:bdw */
9210 I915_WRITE(GEN7_FF_THREAD_MODE,
9211 I915_READ(GEN7_FF_THREAD_MODE) &
9212 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02009213
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02009214 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
9215 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02009216
9217 /* WaDisableSDEUnitClockGating:bdw */
9218 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
9219 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00009220
Imre Deak450174f2016-05-03 15:54:21 +03009221 /* WaProgramL3SqcReg1Default:bdw */
9222 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03009223
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03009224 /* WaKVMNotificationOnConfigChange:bdw */
9225 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
9226 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
9227
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009228 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00009229
9230 /* WaDisableDopClockGating:bdw
9231 *
9232 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
9233 * clock gating.
9234 */
9235 I915_WRITE(GEN6_UCGCTL1,
9236 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07009237}
9238
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009239static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009240{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07009241 /* L3 caching of data atomics doesn't work -- disable it. */
9242 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
9243 I915_WRITE(HSW_ROW_CHICKEN3,
9244 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
9245
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009246 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009247 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
9248 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
9249 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
9250
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02009251 /* WaVSRefCountFullforceMissDisable:hsw */
9252 I915_WRITE(GEN7_FF_THREAD_MODE,
9253 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009254
Akash Goel4e046322014-04-04 17:14:38 +05309255 /* WaDisable_RenderCache_OperationalFlush:hsw */
9256 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9257
Chia-I Wufe27c602014-01-28 13:29:33 +08009258 /* enable HiZ Raw Stall Optimization */
9259 I915_WRITE(CACHE_MODE_0_GEN7,
9260 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
9261
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009262 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009263 I915_WRITE(CACHE_MODE_1,
9264 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03009265
Ville Syrjäläa12c4962014-02-04 21:59:20 +02009266 /*
9267 * BSpec recommends 8x4 when MSAA is used,
9268 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02009269 *
9270 * Note that PS/WM thread counts depend on the WIZ hashing
9271 * disable bit, which we don't touch here, but it's good
9272 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02009273 */
9274 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00009275 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02009276
Kenneth Graunke94411592014-12-31 16:23:00 -08009277 /* WaSampleCChickenBitEnable:hsw */
9278 I915_WRITE(HALF_SLICE_CHICKEN3,
9279 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
9280
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009281 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07009282 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
9283
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009284 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009285}
9286
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009287static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009288{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009289 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009290
Damien Lespiau231e54f2012-10-19 17:55:41 +01009291 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009292
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009293 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05009294 I915_WRITE(_3D_CHICKEN3,
9295 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
9296
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009297 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009298 I915_WRITE(IVB_CHICKEN3,
9299 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
9300 CHICKEN3_DGMG_DONE_FIX_DISABLE);
9301
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009302 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009303 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07009304 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
9305 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07009306
Akash Goel4e046322014-04-04 17:14:38 +05309307 /* WaDisable_RenderCache_OperationalFlush:ivb */
9308 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9309
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009310 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009311 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
9312 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
9313
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009314 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009315 I915_WRITE(GEN7_L3CNTLREG1,
9316 GEN7_WA_FOR_GEN7_L3_CONTROL);
9317 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07009318 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009319 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07009320 I915_WRITE(GEN7_ROW_CHICKEN2,
9321 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02009322 else {
9323 /* must write both registers */
9324 I915_WRITE(GEN7_ROW_CHICKEN2,
9325 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07009326 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
9327 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02009328 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009329
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009330 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05009331 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
9332 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
9333
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02009334 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07009335 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009336 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07009337 */
9338 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02009339 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07009340
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009341 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009342 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
9343 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
9344 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
9345
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009346 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009347
9348 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02009349
Chris Wilson22721342014-03-04 09:41:43 +00009350 if (0) { /* causes HiZ corruption on ivb:gt1 */
9351 /* enable HiZ Raw Stall Optimization */
9352 I915_WRITE(CACHE_MODE_0_GEN7,
9353 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
9354 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08009355
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009356 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02009357 I915_WRITE(CACHE_MODE_1,
9358 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07009359
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02009360 /*
9361 * BSpec recommends 8x4 when MSAA is used,
9362 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02009363 *
9364 * Note that PS/WM thread counts depend on the WIZ hashing
9365 * disable bit, which we don't touch here, but it's good
9366 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02009367 */
9368 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00009369 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02009370
Ben Widawsky20848222012-05-04 18:58:59 -07009371 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
9372 snpcr &= ~GEN6_MBC_SNPCR_MASK;
9373 snpcr |= GEN6_MBC_SNPCR_MED;
9374 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01009375
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009376 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009377 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01009378
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009379 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009380}
9381
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009382static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009383{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009384 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05009385 I915_WRITE(_3D_CHICKEN3,
9386 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
9387
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009388 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009389 I915_WRITE(IVB_CHICKEN3,
9390 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
9391 CHICKEN3_DGMG_DONE_FIX_DISABLE);
9392
Ville Syrjäläfad7d362014-01-22 21:32:39 +02009393 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009394 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07009395 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08009396 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
9397 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07009398
Akash Goel4e046322014-04-04 17:14:38 +05309399 /* WaDisable_RenderCache_OperationalFlush:vlv */
9400 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9401
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009402 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05009403 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
9404 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
9405
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009406 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07009407 I915_WRITE(GEN7_ROW_CHICKEN2,
9408 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
9409
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009410 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009411 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
9412 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
9413 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
9414
Ville Syrjälä46680e02014-01-22 21:33:01 +02009415 gen7_setup_fixed_func_scheduler(dev_priv);
9416
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02009417 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07009418 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009419 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07009420 */
9421 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02009422 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07009423
Akash Goelc98f5062014-03-24 23:00:07 +05309424 /* WaDisableL3Bank2xClockGate:vlv
9425 * Disabling L3 clock gating- MMIO 940c[25] = 1
9426 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
9427 I915_WRITE(GEN7_UCGCTL4,
9428 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07009429
Ville Syrjäläafd58e72014-01-22 21:33:03 +02009430 /*
9431 * BSpec says this must be set, even though
9432 * WaDisable4x2SubspanOptimization isn't listed for VLV.
9433 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02009434 I915_WRITE(CACHE_MODE_1,
9435 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07009436
9437 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02009438 * BSpec recommends 8x4 when MSAA is used,
9439 * however in practice 16x4 seems fastest.
9440 *
9441 * Note that PS/WM thread counts depend on the WIZ hashing
9442 * disable bit, which we don't touch here, but it's good
9443 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
9444 */
9445 I915_WRITE(GEN7_GT_MODE,
9446 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
9447
9448 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02009449 * WaIncreaseL3CreditsForVLVB0:vlv
9450 * This is the hardware default actually.
9451 */
9452 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
9453
9454 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009455 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07009456 * Disable clock gating on th GCFG unit to prevent a delay
9457 * in the reporting of vblank events.
9458 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02009459 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009460}
9461
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009462static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03009463{
Ville Syrjälä232ce332014-04-09 13:28:35 +03009464 /* WaVSRefCountFullforceMissDisable:chv */
9465 /* WaDSRefCountFullforceMissDisable:chv */
9466 I915_WRITE(GEN7_FF_THREAD_MODE,
9467 I915_READ(GEN7_FF_THREAD_MODE) &
9468 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03009469
9470 /* WaDisableSemaphoreAndSyncFlipWait:chv */
9471 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
9472 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03009473
9474 /* WaDisableCSUnitClockGating:chv */
9475 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
9476 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03009477
9478 /* WaDisableSDEUnitClockGating:chv */
9479 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
9480 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03009481
9482 /*
Imre Deak450174f2016-05-03 15:54:21 +03009483 * WaProgramL3SqcReg1Default:chv
9484 * See gfxspecs/Related Documents/Performance Guide/
9485 * LSQC Setting Recommendations.
9486 */
9487 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03009488}
9489
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009490static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009491{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009492 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009493
9494 I915_WRITE(RENCLK_GATE_D1, 0);
9495 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
9496 GS_UNIT_CLOCK_GATE_DISABLE |
9497 CL_UNIT_CLOCK_GATE_DISABLE);
9498 I915_WRITE(RAMCLK_GATE_D, 0);
9499 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
9500 OVRUNIT_CLOCK_GATE_DISABLE |
9501 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009502 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009503 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
9504 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02009505
9506 /* WaDisableRenderCachePipelinedFlush */
9507 I915_WRITE(CACHE_MODE_0,
9508 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03009509
Akash Goel4e046322014-04-04 17:14:38 +05309510 /* WaDisable_RenderCache_OperationalFlush:g4x */
9511 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9512
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009513 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009514}
9515
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009516static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009517{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01009518 struct intel_uncore *uncore = &dev_priv->uncore;
9519
9520 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
9521 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
9522 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
9523 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
9524 intel_uncore_write16(uncore, DEUC, 0);
9525 intel_uncore_write(uncore,
9526 MI_ARB_STATE,
9527 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05309528
9529 /* WaDisable_RenderCache_OperationalFlush:gen4 */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01009530 intel_uncore_write(uncore,
9531 CACHE_MODE_0,
9532 _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009533}
9534
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009535static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009536{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009537 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
9538 I965_RCC_CLOCK_GATE_DISABLE |
9539 I965_RCPB_CLOCK_GATE_DISABLE |
9540 I965_ISC_CLOCK_GATE_DISABLE |
9541 I965_FBC_CLOCK_GATE_DISABLE);
9542 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03009543 I915_WRITE(MI_ARB_STATE,
9544 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05309545
9546 /* WaDisable_RenderCache_OperationalFlush:gen4 */
9547 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009548}
9549
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009550static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009551{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009552 u32 dstate = I915_READ(D_STATE);
9553
9554 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
9555 DSTATE_DOT_CLOCK_GATING;
9556 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01009557
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009558 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01009559 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02009560
9561 /* IIR "flip pending" means done if this bit is set */
9562 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02009563
9564 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02009565 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02009566
9567 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
9568 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03009569
9570 I915_WRITE(MI_ARB_STATE,
9571 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009572}
9573
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009574static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009575{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009576 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02009577
9578 /* interrupts should cause a wake up from C3 */
9579 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
9580 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03009581
9582 I915_WRITE(MEM_MODE,
9583 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009584}
9585
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009586static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009587{
Ville Syrjälä10383922014-08-15 01:21:54 +03009588 I915_WRITE(MEM_MODE,
9589 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
9590 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009591}
9592
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009593void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009594{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009595 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009596}
9597
Ville Syrjälä712bf362016-10-31 22:37:23 +02009598void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03009599{
Ville Syrjälä712bf362016-10-31 22:37:23 +02009600 if (HAS_PCH_LPT(dev_priv))
9601 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03009602}
9603
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009604static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02009605{
9606 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
9607}
9608
9609/**
9610 * intel_init_clock_gating_hooks - setup the clock gating hooks
9611 * @dev_priv: device private
9612 *
9613 * Setup the hooks that configure which clocks of a given platform can be
9614 * gated and also apply various GT and display specific workarounds for these
9615 * platforms. Note that some GT specific workarounds are applied separately
9616 * when GPU contexts or batchbuffers start their execution.
9617 */
9618void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
9619{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07009620 if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07009621 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07009622 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07009623 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07009624 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009625 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009626 else if (IS_COFFEELAKE(dev_priv))
9627 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009628 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009629 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009630 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009631 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02009632 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02009633 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02009634 else if (IS_GEMINILAKE(dev_priv))
9635 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009636 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009637 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009638 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009639 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009640 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009641 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009642 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009643 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009644 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009645 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009646 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02009647 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009648 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009649 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009650 else if (IS_G4X(dev_priv))
9651 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009652 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009653 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009654 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009655 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009656 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02009657 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
9658 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
9659 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009660 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02009661 dev_priv->display.init_clock_gating = i830_init_clock_gating;
9662 else {
9663 MISSING_CASE(INTEL_DEVID(dev_priv));
9664 dev_priv->display.init_clock_gating = nop_init_clock_gating;
9665 }
9666}
9667
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009668/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009669void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009670{
Daniel Vetterc921aba2012-04-26 23:28:17 +02009671 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009672 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009673 i915_pineview_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009674 else if (IS_GEN(dev_priv, 5))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009675 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02009676
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009677 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009678 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009679 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01009680 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01009681 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07009682 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009683 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009684 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03009685
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009686 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009687 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009688 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009689 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07009690 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08009691 dev_priv->display.compute_intermediate_wm =
9692 ilk_compute_intermediate_wm;
9693 dev_priv->display.initial_watermarks =
9694 ilk_initial_watermarks;
9695 dev_priv->display.optimize_watermarks =
9696 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02009697 } else {
9698 DRM_DEBUG_KMS("Failed to read display plane latency. "
9699 "Disable CxSR\n");
9700 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02009701 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009702 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02009703 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009704 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009705 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009706 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009707 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03009708 } else if (IS_G4X(dev_priv)) {
9709 g4x_setup_wm_latency(dev_priv);
9710 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
9711 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
9712 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
9713 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009714 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00009715 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009716 dev_priv->is_ddr3,
9717 dev_priv->fsb_freq,
9718 dev_priv->mem_freq)) {
9719 DRM_INFO("failed to find known CxSR latency "
9720 "(found ddr%s fsb freq %d, mem freq %d), "
9721 "disabling CxSR\n",
9722 (dev_priv->is_ddr3 == 1) ? "3" : "2",
9723 dev_priv->fsb_freq, dev_priv->mem_freq);
9724 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03009725 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009726 dev_priv->display.update_wm = NULL;
9727 } else
9728 dev_priv->display.update_wm = pineview_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009729 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009730 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009731 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009732 dev_priv->display.update_wm = i9xx_update_wm;
9733 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009734 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03009735 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009736 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009737 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009738 } else {
9739 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009740 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009741 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009742 } else {
9743 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009744 }
9745}
9746
Ville Syrjälädd06f882014-11-10 22:55:12 +02009747static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9748{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009749 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9750
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009751 /*
9752 * N = val - 0xb7
9753 * Slow = Fast = GPLL ref * N
9754 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009755 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009756}
9757
Fengguang Wub55dd642014-07-12 11:21:39 +02009758static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009759{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009760 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9761
9762 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009763}
9764
Fengguang Wub55dd642014-07-12 11:21:39 +02009765static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309766{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009767 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9768
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009769 /*
9770 * N = val / 2
9771 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9772 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009773 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309774}
9775
Fengguang Wub55dd642014-07-12 11:21:39 +02009776static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309777{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009778 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9779
Ville Syrjälä1c147622014-08-18 14:42:43 +03009780 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009781 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309782}
9783
Ville Syrjälä616bc822015-01-23 21:04:25 +02009784int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9785{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009786 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009787 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9788 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009789 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009790 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009791 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009792 return byt_gpu_freq(dev_priv, val);
9793 else
9794 return val * GT_FREQUENCY_MULTIPLIER;
9795}
9796
Ville Syrjälä616bc822015-01-23 21:04:25 +02009797int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9798{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009799 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009800 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9801 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009802 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009803 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009804 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009805 return byt_freq_opcode(dev_priv, val);
9806 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009807 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309808}
9809
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009810void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009811{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01009812 mutex_init(&dev_priv->gt_pm.rps.lock);
Chris Wilson60548c52018-07-31 14:26:29 +01009813 mutex_init(&dev_priv->gt_pm.rps.power.mutex);
Daniel Vetterf742a552013-12-06 10:17:53 +01009814
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009815 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009816
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009817 dev_priv->runtime_pm.suspended = false;
9818 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009819}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009820
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009821static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9822 const i915_reg_t reg)
9823{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009824 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009825 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009826
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009827 /*
9828 * The register accessed do not need forcewake. We borrow
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009829 * uncore lock to prevent concurrent access to range reg.
9830 */
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009831 lockdep_assert_held(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009832
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009833 /*
9834 * vlv and chv residency counters are 40 bits in width.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009835 * With a control bit, we can choose between upper or lower
9836 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009837 *
9838 * Although we always use the counter in high-range mode elsewhere,
9839 * userspace may attempt to read the value before rc6 is initialised,
9840 * before we have set the default VLV_COUNTER_CONTROL value. So always
9841 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009842 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009843 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9844 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009845 upper = I915_READ_FW(reg);
9846 do {
9847 tmp = upper;
9848
9849 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9850 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9851 lower = I915_READ_FW(reg);
9852
9853 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9854 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9855 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009856 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009857
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009858 /*
9859 * Everywhere else we always use VLV_COUNTER_CONTROL with the
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009860 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9861 * now.
9862 */
9863
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009864 return lower | (u64)upper << 8;
9865}
9866
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009867u64 intel_rc6_residency_ns(struct drm_i915_private *dev_priv,
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009868 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009869{
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009870 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009871 u64 time_hw, prev_hw, overflow_hw;
9872 unsigned int fw_domains;
9873 unsigned long flags;
9874 unsigned int i;
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009875 u32 mul, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009876
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00009877 if (!HAS_RC6(dev_priv))
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009878 return 0;
9879
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009880 /*
9881 * Store previous hw counter values for counter wrap-around handling.
9882 *
9883 * There are only four interesting registers and they live next to each
9884 * other so we can use the relative address, compared to the smallest
9885 * one as the index into driver storage.
9886 */
9887 i = (i915_mmio_reg_offset(reg) -
9888 i915_mmio_reg_offset(GEN6_GT_GFX_RC6_LOCKED)) / sizeof(u32);
9889 if (WARN_ON_ONCE(i >= ARRAY_SIZE(dev_priv->gt_pm.rc6.cur_residency)))
9890 return 0;
9891
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009892 fw_domains = intel_uncore_forcewake_for_reg(uncore, reg, FW_REG_READ);
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009893
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009894 spin_lock_irqsave(&uncore->lock, flags);
9895 intel_uncore_forcewake_get__locked(uncore, fw_domains);
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009896
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009897 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9898 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009899 mul = 1000000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009900 div = dev_priv->czclk_freq;
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009901 overflow_hw = BIT_ULL(40);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009902 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009903 } else {
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009904 /* 833.33ns units on Gen9LP, 1.28us elsewhere. */
9905 if (IS_GEN9_LP(dev_priv)) {
9906 mul = 10000;
9907 div = 12;
9908 } else {
9909 mul = 1280;
9910 div = 1;
9911 }
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009912
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009913 overflow_hw = BIT_ULL(32);
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009914 time_hw = intel_uncore_read_fw(uncore, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009915 }
9916
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009917 /*
9918 * Counter wrap handling.
9919 *
9920 * But relying on a sufficient frequency of queries otherwise counters
9921 * can still wrap.
9922 */
9923 prev_hw = dev_priv->gt_pm.rc6.prev_hw_residency[i];
9924 dev_priv->gt_pm.rc6.prev_hw_residency[i] = time_hw;
9925
9926 /* RC6 delta from last sample. */
9927 if (time_hw >= prev_hw)
9928 time_hw -= prev_hw;
9929 else
9930 time_hw += overflow_hw - prev_hw;
9931
9932 /* Add delta to RC6 extended raw driver copy. */
9933 time_hw += dev_priv->gt_pm.rc6.cur_residency[i];
9934 dev_priv->gt_pm.rc6.cur_residency[i] = time_hw;
9935
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009936 intel_uncore_forcewake_put__locked(uncore, fw_domains);
9937 spin_unlock_irqrestore(&uncore->lock, flags);
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009938
9939 return mul_u64_u32_div(time_hw, mul, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009940}
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009941
Jani Nikulaecbb5fb2019-04-29 15:29:37 +03009942u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9943 i915_reg_t reg)
9944{
9945 return DIV_ROUND_UP_ULL(intel_rc6_residency_ns(dev_priv, reg), 1000);
9946}
9947
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009948u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
9949{
9950 u32 cagf;
9951
9952 if (INTEL_GEN(dev_priv) >= 9)
9953 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
9954 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9955 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
9956 else
9957 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
9958
9959 return cagf;
9960}