blob: b5903ee25dea6448656f9542d590fae8a5296461 [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);
Uma Shankar1d85a292018-08-07 21:15:35 +0530129
130 /*
131 * Lower the display internal timeout.
132 * This is needed to avoid any hard hangs when DSI port PLL
133 * is off and a MMIO access is attempted by any privilege
134 * application, using batch buffers or any other means.
135 */
136 I915_WRITE(RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Imre Deaka82abe42015-03-27 14:00:04 +0200137}
138
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200139static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
140{
141 gen9_init_clock_gating(dev_priv);
142
143 /*
144 * WaDisablePWMClockGating:glk
145 * Backlight PWM may stop in the asserted state, causing backlight
146 * to stay fully on.
147 */
148 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
149 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200150
151 /* WaDDIIOTimeout:glk */
152 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
153 u32 val = I915_READ(CHICKEN_MISC_2);
154 val &= ~(GLK_CL0_PWR_DOWN |
155 GLK_CL1_PWR_DOWN |
156 GLK_CL2_PWR_DOWN);
157 I915_WRITE(CHICKEN_MISC_2, val);
158 }
159
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200160}
161
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200162static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200163{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200164 u32 tmp;
165
166 tmp = I915_READ(CLKCFG);
167
168 switch (tmp & CLKCFG_FSB_MASK) {
169 case CLKCFG_FSB_533:
170 dev_priv->fsb_freq = 533; /* 133*4 */
171 break;
172 case CLKCFG_FSB_800:
173 dev_priv->fsb_freq = 800; /* 200*4 */
174 break;
175 case CLKCFG_FSB_667:
176 dev_priv->fsb_freq = 667; /* 167*4 */
177 break;
178 case CLKCFG_FSB_400:
179 dev_priv->fsb_freq = 400; /* 100*4 */
180 break;
181 }
182
183 switch (tmp & CLKCFG_MEM_MASK) {
184 case CLKCFG_MEM_533:
185 dev_priv->mem_freq = 533;
186 break;
187 case CLKCFG_MEM_667:
188 dev_priv->mem_freq = 667;
189 break;
190 case CLKCFG_MEM_800:
191 dev_priv->mem_freq = 800;
192 break;
193 }
194
195 /* detect pineview DDR3 setting */
196 tmp = I915_READ(CSHRDDR3CTL);
197 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
198}
199
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200200static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200201{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200202 u16 ddrpll, csipll;
203
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100204 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
205 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200206
207 switch (ddrpll & 0xff) {
208 case 0xc:
209 dev_priv->mem_freq = 800;
210 break;
211 case 0x10:
212 dev_priv->mem_freq = 1066;
213 break;
214 case 0x14:
215 dev_priv->mem_freq = 1333;
216 break;
217 case 0x18:
218 dev_priv->mem_freq = 1600;
219 break;
220 default:
221 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
222 ddrpll & 0xff);
223 dev_priv->mem_freq = 0;
224 break;
225 }
226
Daniel Vetter20e4d402012-08-08 23:35:39 +0200227 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200228
229 switch (csipll & 0x3ff) {
230 case 0x00c:
231 dev_priv->fsb_freq = 3200;
232 break;
233 case 0x00e:
234 dev_priv->fsb_freq = 3733;
235 break;
236 case 0x010:
237 dev_priv->fsb_freq = 4266;
238 break;
239 case 0x012:
240 dev_priv->fsb_freq = 4800;
241 break;
242 case 0x014:
243 dev_priv->fsb_freq = 5333;
244 break;
245 case 0x016:
246 dev_priv->fsb_freq = 5866;
247 break;
248 case 0x018:
249 dev_priv->fsb_freq = 6400;
250 break;
251 default:
252 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
253 csipll & 0x3ff);
254 dev_priv->fsb_freq = 0;
255 break;
256 }
257
258 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200259 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200260 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200261 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200262 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200263 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200264 }
265}
266
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300267static const struct cxsr_latency cxsr_latency_table[] = {
268 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
269 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
270 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
271 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
272 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
273
274 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
275 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
276 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
277 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
278 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
279
280 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
281 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
282 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
283 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
284 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
285
286 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
287 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
288 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
289 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
290 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
291
292 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
293 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
294 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
295 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
296 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
297
298 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
299 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
300 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
301 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
302 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
303};
304
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100305static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
306 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300307 int fsb,
308 int mem)
309{
310 const struct cxsr_latency *latency;
311 int i;
312
313 if (fsb == 0 || mem == 0)
314 return NULL;
315
316 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
317 latency = &cxsr_latency_table[i];
318 if (is_desktop == latency->is_desktop &&
319 is_ddr3 == latency->is_ddr3 &&
320 fsb == latency->fsb_freq && mem == latency->mem_freq)
321 return latency;
322 }
323
324 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
325
326 return NULL;
327}
328
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200329static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
330{
331 u32 val;
332
Chris Wilson337fa6e2019-04-26 09:17:20 +0100333 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200334
335 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
336 if (enable)
337 val &= ~FORCE_DDR_HIGH_FREQ;
338 else
339 val |= FORCE_DDR_HIGH_FREQ;
340 val &= ~FORCE_DDR_LOW_FREQ;
341 val |= FORCE_DDR_FREQ_REQ_ACK;
342 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
343
344 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
345 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
346 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
347
Chris Wilson337fa6e2019-04-26 09:17:20 +0100348 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200349}
350
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200351static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
352{
353 u32 val;
354
Chris Wilson337fa6e2019-04-26 09:17:20 +0100355 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200356
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200357 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200358 if (enable)
359 val |= DSP_MAXFIFO_PM5_ENABLE;
360 else
361 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200362 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200363
Chris Wilson337fa6e2019-04-26 09:17:20 +0100364 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200365}
366
Ville Syrjäläf4998962015-03-10 17:02:21 +0200367#define FW_WM(value, plane) \
368 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
369
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200370static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300371{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200372 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300373 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300374
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100375 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200376 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300377 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300378 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200379 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200380 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300381 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300382 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200383 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200384 val = I915_READ(DSPFW3);
385 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
386 if (enable)
387 val |= PINEVIEW_SELF_REFRESH_EN;
388 else
389 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300390 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300391 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100392 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200393 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300394 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
395 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
396 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300397 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100398 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300399 /*
400 * FIXME can't find a bit like this for 915G, and
401 * and yet it does have the related watermark in
402 * FW_BLC_SELF. What's going on?
403 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200404 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300405 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
406 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
407 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300408 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300409 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200410 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300411 }
412
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200413 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
414
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200415 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
416 enableddisabled(enable),
417 enableddisabled(was_enabled));
418
419 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300420}
421
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300422/**
423 * intel_set_memory_cxsr - Configure CxSR state
424 * @dev_priv: i915 device
425 * @enable: Allow vs. disallow CxSR
426 *
427 * Allow or disallow the system to enter a special CxSR
428 * (C-state self refresh) state. What typically happens in CxSR mode
429 * is that several display FIFOs may get combined into a single larger
430 * FIFO for a particular plane (so called max FIFO mode) to allow the
431 * system to defer memory fetches longer, and the memory will enter
432 * self refresh.
433 *
434 * Note that enabling CxSR does not guarantee that the system enter
435 * this special mode, nor does it guarantee that the system stays
436 * in that mode once entered. So this just allows/disallows the system
437 * to autonomously utilize the CxSR mode. Other factors such as core
438 * C-states will affect when/if the system actually enters/exits the
439 * CxSR mode.
440 *
441 * Note that on VLV/CHV this actually only controls the max FIFO mode,
442 * and the system is free to enter/exit memory self refresh at any time
443 * even when the use of CxSR has been disallowed.
444 *
445 * While the system is actually in the CxSR/max FIFO mode, some plane
446 * control registers will not get latched on vblank. Thus in order to
447 * guarantee the system will respond to changes in the plane registers
448 * we must always disallow CxSR prior to making changes to those registers.
449 * Unfortunately the system will re-evaluate the CxSR conditions at
450 * frame start which happens after vblank start (which is when the plane
451 * registers would get latched), so we can't proceed with the plane update
452 * during the same frame where we disallowed CxSR.
453 *
454 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
455 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
456 * the hardware w.r.t. HPLL SR when writing to plane registers.
457 * Disallowing just CxSR is sufficient.
458 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200459bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200460{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200461 bool ret;
462
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200463 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200464 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300465 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
466 dev_priv->wm.vlv.cxsr = enable;
467 else if (IS_G4X(dev_priv))
468 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200469 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200470
471 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200472}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200473
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300474/*
475 * Latency for FIFO fetches is dependent on several factors:
476 * - memory configuration (speed, channels)
477 * - chipset
478 * - current MCH state
479 * It can be fairly high in some situations, so here we assume a fairly
480 * pessimal value. It's a tradeoff between extra memory fetches (if we
481 * set this value too high, the FIFO will fetch frequently to stay full)
482 * and power consumption (set it too low to save power and we might see
483 * FIFO underruns and display "flicker").
484 *
485 * A value of 5us seems to be a good balance; safe for very low end
486 * platforms but not overly aggressive on lower latency configs.
487 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100488static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300489
Ville Syrjäläb5004722015-03-05 21:19:47 +0200490#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
491 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
492
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200493static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200494{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200495 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200496 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200497 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200498 enum pipe pipe = crtc->pipe;
499 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200500
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200501 switch (pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200502 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200503 case PIPE_A:
504 dsparb = I915_READ(DSPARB);
505 dsparb2 = I915_READ(DSPARB2);
506 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
507 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
508 break;
509 case PIPE_B:
510 dsparb = I915_READ(DSPARB);
511 dsparb2 = I915_READ(DSPARB2);
512 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
513 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
514 break;
515 case PIPE_C:
516 dsparb2 = I915_READ(DSPARB2);
517 dsparb3 = I915_READ(DSPARB3);
518 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
519 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
520 break;
521 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200522 MISSING_CASE(pipe);
523 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200524 }
525
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200526 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
527 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
528 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
529 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200530}
531
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200532static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
533 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300534{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200535 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300536 int size;
537
538 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200539 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300540 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
541
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200542 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
543 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300544
545 return size;
546}
547
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200548static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
549 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300550{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200551 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552 int size;
553
554 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200555 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300556 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
557 size >>= 1; /* Convert to cachelines */
558
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200559 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
560 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300561
562 return size;
563}
564
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200565static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
566 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300567{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200568 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300569 int size;
570
571 size = dsparb & 0x7f;
572 size >>= 2; /* Convert to cachelines */
573
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200574 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
575 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300576
577 return size;
578}
579
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300580/* Pineview has different values for various configs */
581static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300582 .fifo_size = PINEVIEW_DISPLAY_FIFO,
583 .max_wm = PINEVIEW_MAX_WM,
584 .default_wm = PINEVIEW_DFT_WM,
585 .guard_size = PINEVIEW_GUARD_WM,
586 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300587};
588static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300589 .fifo_size = PINEVIEW_DISPLAY_FIFO,
590 .max_wm = PINEVIEW_MAX_WM,
591 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
592 .guard_size = PINEVIEW_GUARD_WM,
593 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300594};
595static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300596 .fifo_size = PINEVIEW_CURSOR_FIFO,
597 .max_wm = PINEVIEW_CURSOR_MAX_WM,
598 .default_wm = PINEVIEW_CURSOR_DFT_WM,
599 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
600 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601};
602static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300603 .fifo_size = PINEVIEW_CURSOR_FIFO,
604 .max_wm = PINEVIEW_CURSOR_MAX_WM,
605 .default_wm = PINEVIEW_CURSOR_DFT_WM,
606 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
607 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300608};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300609static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300610 .fifo_size = I965_CURSOR_FIFO,
611 .max_wm = I965_CURSOR_MAX_WM,
612 .default_wm = I965_CURSOR_DFT_WM,
613 .guard_size = 2,
614 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300615};
616static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300617 .fifo_size = I945_FIFO_SIZE,
618 .max_wm = I915_MAX_WM,
619 .default_wm = 1,
620 .guard_size = 2,
621 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300622};
623static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300624 .fifo_size = I915_FIFO_SIZE,
625 .max_wm = I915_MAX_WM,
626 .default_wm = 1,
627 .guard_size = 2,
628 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300629};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300630static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300631 .fifo_size = I855GM_FIFO_SIZE,
632 .max_wm = I915_MAX_WM,
633 .default_wm = 1,
634 .guard_size = 2,
635 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300636};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300637static const struct intel_watermark_params i830_bc_wm_info = {
638 .fifo_size = I855GM_FIFO_SIZE,
639 .max_wm = I915_MAX_WM/2,
640 .default_wm = 1,
641 .guard_size = 2,
642 .cacheline_size = I830_FIFO_LINE_SIZE,
643};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200644static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300645 .fifo_size = I830_FIFO_SIZE,
646 .max_wm = I915_MAX_WM,
647 .default_wm = 1,
648 .guard_size = 2,
649 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300650};
651
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300652/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300653 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
654 * @pixel_rate: Pipe pixel rate in kHz
655 * @cpp: Plane bytes per pixel
656 * @latency: Memory wakeup latency in 0.1us units
657 *
658 * Compute the watermark using the method 1 or "small buffer"
659 * formula. The caller may additonally add extra cachelines
660 * to account for TLB misses and clock crossings.
661 *
662 * This method is concerned with the short term drain rate
663 * of the FIFO, ie. it does not account for blanking periods
664 * which would effectively reduce the average drain rate across
665 * a longer period. The name "small" refers to the fact the
666 * FIFO is relatively small compared to the amount of data
667 * fetched.
668 *
669 * The FIFO level vs. time graph might look something like:
670 *
671 * |\ |\
672 * | \ | \
673 * __---__---__ (- plane active, _ blanking)
674 * -> time
675 *
676 * or perhaps like this:
677 *
678 * |\|\ |\|\
679 * __----__----__ (- plane active, _ blanking)
680 * -> time
681 *
682 * Returns:
683 * The watermark in bytes
684 */
685static unsigned int intel_wm_method1(unsigned int pixel_rate,
686 unsigned int cpp,
687 unsigned int latency)
688{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200689 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300690
Ville Syrjäläd492a292019-04-08 18:27:01 +0300691 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300692 ret = DIV_ROUND_UP_ULL(ret, 10000);
693
694 return ret;
695}
696
697/**
698 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
699 * @pixel_rate: Pipe pixel rate in kHz
700 * @htotal: Pipe horizontal total
701 * @width: Plane width in pixels
702 * @cpp: Plane bytes per pixel
703 * @latency: Memory wakeup latency in 0.1us units
704 *
705 * Compute the watermark using the method 2 or "large buffer"
706 * formula. The caller may additonally add extra cachelines
707 * to account for TLB misses and clock crossings.
708 *
709 * This method is concerned with the long term drain rate
710 * of the FIFO, ie. it does account for blanking periods
711 * which effectively reduce the average drain rate across
712 * a longer period. The name "large" refers to the fact the
713 * FIFO is relatively large compared to the amount of data
714 * fetched.
715 *
716 * The FIFO level vs. time graph might look something like:
717 *
718 * |\___ |\___
719 * | \___ | \___
720 * | \ | \
721 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
722 * -> time
723 *
724 * Returns:
725 * The watermark in bytes
726 */
727static unsigned int intel_wm_method2(unsigned int pixel_rate,
728 unsigned int htotal,
729 unsigned int width,
730 unsigned int cpp,
731 unsigned int latency)
732{
733 unsigned int ret;
734
735 /*
736 * FIXME remove once all users are computing
737 * watermarks in the correct place.
738 */
739 if (WARN_ON_ONCE(htotal == 0))
740 htotal = 1;
741
742 ret = (latency * pixel_rate) / (htotal * 10000);
743 ret = (ret + 1) * width * cpp;
744
745 return ret;
746}
747
748/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300749 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300750 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300751 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000752 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200753 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300754 * @latency_ns: memory latency for the platform
755 *
756 * Calculate the watermark level (the level at which the display plane will
757 * start fetching from memory again). Each chip has a different display
758 * FIFO size and allocation, so the caller needs to figure that out and pass
759 * in the correct intel_watermark_params structure.
760 *
761 * As the pixel clock runs, the FIFO will be drained at a rate that depends
762 * on the pixel size. When it reaches the watermark level, it'll start
763 * fetching FIFO line sized based chunks from memory until the FIFO fills
764 * past the watermark point. If the FIFO drains completely, a FIFO underrun
765 * will occur, and a display engine hang could result.
766 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300767static unsigned int intel_calculate_wm(int pixel_rate,
768 const struct intel_watermark_params *wm,
769 int fifo_size, int cpp,
770 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300772 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300773
774 /*
775 * Note: we need to make sure we don't overflow for various clock &
776 * latency values.
777 * clocks go from a few thousand to several hundred thousand.
778 * latency is usually a few thousand
779 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300780 entries = intel_wm_method1(pixel_rate, cpp,
781 latency_ns / 100);
782 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
783 wm->guard_size;
784 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300785
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300786 wm_size = fifo_size - entries;
787 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300788
789 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300790 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300791 wm_size = wm->max_wm;
792 if (wm_size <= 0)
793 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300794
795 /*
796 * Bspec seems to indicate that the value shouldn't be lower than
797 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
798 * Lets go for 8 which is the burst size since certain platforms
799 * already use a hardcoded 8 (which is what the spec says should be
800 * done).
801 */
802 if (wm_size <= 8)
803 wm_size = 8;
804
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300805 return wm_size;
806}
807
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300808static bool is_disabling(int old, int new, int threshold)
809{
810 return old >= threshold && new < threshold;
811}
812
813static bool is_enabling(int old, int new, int threshold)
814{
815 return old < threshold && new >= threshold;
816}
817
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300818static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
819{
820 return dev_priv->wm.max_level + 1;
821}
822
Ville Syrjälä24304d812017-03-14 17:10:49 +0200823static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
824 const struct intel_plane_state *plane_state)
825{
826 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
827
828 /* FIXME check the 'enable' instead */
829 if (!crtc_state->base.active)
830 return false;
831
832 /*
833 * Treat cursor with fb as always visible since cursor updates
834 * can happen faster than the vrefresh rate, and the current
835 * watermark code doesn't handle that correctly. Cursor updates
836 * which set/clear the fb or change the cursor size are going
837 * to get throttled by intel_legacy_cursor_update() to work
838 * around this problem with the watermark code.
839 */
840 if (plane->id == PLANE_CURSOR)
841 return plane_state->base.fb != NULL;
842 else
843 return plane_state->base.visible;
844}
845
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200846static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300847{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200848 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300849
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200850 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200851 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300852 if (enabled)
853 return NULL;
854 enabled = crtc;
855 }
856 }
857
858 return enabled;
859}
860
Ville Syrjälä432081b2016-10-31 22:37:03 +0200861static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300862{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200863 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200864 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300865 const struct cxsr_latency *latency;
866 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300867 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000869 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100870 dev_priv->is_ddr3,
871 dev_priv->fsb_freq,
872 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300873 if (!latency) {
874 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300875 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300876 return;
877 }
878
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200879 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300880 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200881 const struct drm_display_mode *adjusted_mode =
882 &crtc->config->base.adjusted_mode;
883 const struct drm_framebuffer *fb =
884 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200885 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300886 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887
888 /* Display SR */
889 wm = intel_calculate_wm(clock, &pineview_display_wm,
890 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200891 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300892 reg = I915_READ(DSPFW1);
893 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200894 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300895 I915_WRITE(DSPFW1, reg);
896 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
897
898 /* cursor SR */
899 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
900 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300901 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300902 reg = I915_READ(DSPFW3);
903 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200904 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300905 I915_WRITE(DSPFW3, reg);
906
907 /* Display HPLL off SR */
908 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
909 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200910 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300911 reg = I915_READ(DSPFW3);
912 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200913 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300914 I915_WRITE(DSPFW3, reg);
915
916 /* cursor HPLL off SR */
917 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
918 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300919 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300920 reg = I915_READ(DSPFW3);
921 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200922 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300923 I915_WRITE(DSPFW3, reg);
924 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
925
Imre Deak5209b1f2014-07-01 12:36:17 +0300926 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300927 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300928 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300929 }
930}
931
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300932/*
933 * Documentation says:
934 * "If the line size is small, the TLB fetches can get in the way of the
935 * data fetches, causing some lag in the pixel data return which is not
936 * accounted for in the above formulas. The following adjustment only
937 * needs to be applied if eight whole lines fit in the buffer at once.
938 * The WM is adjusted upwards by the difference between the FIFO size
939 * and the size of 8 whole lines. This adjustment is always performed
940 * in the actual pixel depth regardless of whether FBC is enabled or not."
941 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000942static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300943{
944 int tlb_miss = fifo_size * 64 - width * cpp * 8;
945
946 return max(0, tlb_miss);
947}
948
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300949static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
950 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300951{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300952 enum pipe pipe;
953
954 for_each_pipe(dev_priv, pipe)
955 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
956
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300957 I915_WRITE(DSPFW1,
958 FW_WM(wm->sr.plane, SR) |
959 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
960 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
961 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
962 I915_WRITE(DSPFW2,
963 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
964 FW_WM(wm->sr.fbc, FBC_SR) |
965 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
966 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
967 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
968 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
969 I915_WRITE(DSPFW3,
970 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
971 FW_WM(wm->sr.cursor, CURSOR_SR) |
972 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
973 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300974
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300975 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300976}
977
Ville Syrjälä15665972015-03-10 16:16:28 +0200978#define FW_WM_VLV(value, plane) \
979 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
980
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200981static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200982 const struct vlv_wm_values *wm)
983{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200984 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200985
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200986 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200987 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
988
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200989 I915_WRITE(VLV_DDL(pipe),
990 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
991 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
992 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
993 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
994 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200995
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200996 /*
997 * Zero the (unused) WM1 watermarks, and also clear all the
998 * high order bits so that there are no out of bounds values
999 * present in the registers during the reprogramming.
1000 */
1001 I915_WRITE(DSPHOWM, 0);
1002 I915_WRITE(DSPHOWM1, 0);
1003 I915_WRITE(DSPFW4, 0);
1004 I915_WRITE(DSPFW5, 0);
1005 I915_WRITE(DSPFW6, 0);
1006
Ville Syrjäläae801522015-03-05 21:19:49 +02001007 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001008 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001009 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1010 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1011 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001012 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001013 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1014 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1015 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001016 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001017 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001018
1019 if (IS_CHERRYVIEW(dev_priv)) {
1020 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001021 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1022 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001023 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001024 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1025 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001026 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001027 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1028 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001029 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001030 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001031 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1032 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1033 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1034 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1035 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1036 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1037 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1038 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1039 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001040 } else {
1041 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001042 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1043 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001044 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001045 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001046 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1047 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1048 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1049 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1050 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1051 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001052 }
1053
1054 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001055}
1056
Ville Syrjälä15665972015-03-10 16:16:28 +02001057#undef FW_WM_VLV
1058
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001059static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1060{
1061 /* all latencies in usec */
1062 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1063 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001064 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001065
Ville Syrjälä79d94302017-04-21 21:14:30 +03001066 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001067}
1068
1069static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1070{
1071 /*
1072 * DSPCNTR[13] supposedly controls whether the
1073 * primary plane can use the FIFO space otherwise
1074 * reserved for the sprite plane. It's not 100% clear
1075 * what the actual FIFO size is, but it looks like we
1076 * can happily set both primary and sprite watermarks
1077 * up to 127 cachelines. So that would seem to mean
1078 * that either DSPCNTR[13] doesn't do anything, or that
1079 * the total FIFO is >= 256 cachelines in size. Either
1080 * way, we don't seem to have to worry about this
1081 * repartitioning as the maximum watermark value the
1082 * register can hold for each plane is lower than the
1083 * minimum FIFO size.
1084 */
1085 switch (plane_id) {
1086 case PLANE_CURSOR:
1087 return 63;
1088 case PLANE_PRIMARY:
1089 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1090 case PLANE_SPRITE0:
1091 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1092 default:
1093 MISSING_CASE(plane_id);
1094 return 0;
1095 }
1096}
1097
1098static int g4x_fbc_fifo_size(int level)
1099{
1100 switch (level) {
1101 case G4X_WM_LEVEL_SR:
1102 return 7;
1103 case G4X_WM_LEVEL_HPLL:
1104 return 15;
1105 default:
1106 MISSING_CASE(level);
1107 return 0;
1108 }
1109}
1110
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001111static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1112 const struct intel_plane_state *plane_state,
1113 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001114{
1115 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1116 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1117 const struct drm_display_mode *adjusted_mode =
1118 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001119 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1120 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001121
1122 if (latency == 0)
1123 return USHRT_MAX;
1124
1125 if (!intel_wm_plane_visible(crtc_state, plane_state))
1126 return 0;
1127
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001128 cpp = plane_state->base.fb->format->cpp[0];
1129
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001130 /*
1131 * Not 100% sure which way ELK should go here as the
1132 * spec only says CL/CTG should assume 32bpp and BW
1133 * doesn't need to. But as these things followed the
1134 * mobile vs. desktop lines on gen3 as well, let's
1135 * assume ELK doesn't need this.
1136 *
1137 * The spec also fails to list such a restriction for
1138 * the HPLL watermark, which seems a little strange.
1139 * Let's use 32bpp for the HPLL watermark as well.
1140 */
1141 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1142 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001143 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001144
1145 clock = adjusted_mode->crtc_clock;
1146 htotal = adjusted_mode->crtc_htotal;
1147
1148 if (plane->id == PLANE_CURSOR)
1149 width = plane_state->base.crtc_w;
1150 else
1151 width = drm_rect_width(&plane_state->base.dst);
1152
1153 if (plane->id == PLANE_CURSOR) {
1154 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1155 } else if (plane->id == PLANE_PRIMARY &&
1156 level == G4X_WM_LEVEL_NORMAL) {
1157 wm = intel_wm_method1(clock, cpp, latency);
1158 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001159 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001160
1161 small = intel_wm_method1(clock, cpp, latency);
1162 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1163
1164 wm = min(small, large);
1165 }
1166
1167 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1168 width, cpp);
1169
1170 wm = DIV_ROUND_UP(wm, 64) + 2;
1171
Chris Wilson1a1f1282017-11-07 14:03:38 +00001172 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001173}
1174
1175static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1176 int level, enum plane_id plane_id, u16 value)
1177{
1178 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1179 bool dirty = false;
1180
1181 for (; level < intel_wm_num_levels(dev_priv); level++) {
1182 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1183
1184 dirty |= raw->plane[plane_id] != value;
1185 raw->plane[plane_id] = value;
1186 }
1187
1188 return dirty;
1189}
1190
1191static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1192 int level, u16 value)
1193{
1194 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1195 bool dirty = false;
1196
1197 /* NORMAL level doesn't have an FBC watermark */
1198 level = max(level, G4X_WM_LEVEL_SR);
1199
1200 for (; level < intel_wm_num_levels(dev_priv); level++) {
1201 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1202
1203 dirty |= raw->fbc != value;
1204 raw->fbc = value;
1205 }
1206
1207 return dirty;
1208}
1209
Maarten Lankhorstec193642019-06-28 10:55:17 +02001210static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1211 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001212 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001213
1214static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1215 const struct intel_plane_state *plane_state)
1216{
1217 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1218 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1219 enum plane_id plane_id = plane->id;
1220 bool dirty = false;
1221 int level;
1222
1223 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1224 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1225 if (plane_id == PLANE_PRIMARY)
1226 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1227 goto out;
1228 }
1229
1230 for (level = 0; level < num_levels; level++) {
1231 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1232 int wm, max_wm;
1233
1234 wm = g4x_compute_wm(crtc_state, plane_state, level);
1235 max_wm = g4x_plane_fifo_size(plane_id, level);
1236
1237 if (wm > max_wm)
1238 break;
1239
1240 dirty |= raw->plane[plane_id] != wm;
1241 raw->plane[plane_id] = wm;
1242
1243 if (plane_id != PLANE_PRIMARY ||
1244 level == G4X_WM_LEVEL_NORMAL)
1245 continue;
1246
1247 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1248 raw->plane[plane_id]);
1249 max_wm = g4x_fbc_fifo_size(level);
1250
1251 /*
1252 * FBC wm is not mandatory as we
1253 * can always just disable its use.
1254 */
1255 if (wm > max_wm)
1256 wm = USHRT_MAX;
1257
1258 dirty |= raw->fbc != wm;
1259 raw->fbc = wm;
1260 }
1261
1262 /* mark watermarks as invalid */
1263 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1264
1265 if (plane_id == PLANE_PRIMARY)
1266 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1267
1268 out:
1269 if (dirty) {
1270 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1271 plane->base.name,
1272 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1273 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1274 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1275
1276 if (plane_id == PLANE_PRIMARY)
1277 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1278 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1279 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1280 }
1281
1282 return dirty;
1283}
1284
1285static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1286 enum plane_id plane_id, int level)
1287{
1288 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1289
1290 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1291}
1292
1293static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1294 int level)
1295{
1296 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1297
1298 if (level > dev_priv->wm.max_level)
1299 return false;
1300
1301 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1302 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1303 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1304}
1305
1306/* mark all levels starting from 'level' as invalid */
1307static void g4x_invalidate_wms(struct intel_crtc *crtc,
1308 struct g4x_wm_state *wm_state, int level)
1309{
1310 if (level <= G4X_WM_LEVEL_NORMAL) {
1311 enum plane_id plane_id;
1312
1313 for_each_plane_id_on_crtc(crtc, plane_id)
1314 wm_state->wm.plane[plane_id] = USHRT_MAX;
1315 }
1316
1317 if (level <= G4X_WM_LEVEL_SR) {
1318 wm_state->cxsr = false;
1319 wm_state->sr.cursor = USHRT_MAX;
1320 wm_state->sr.plane = USHRT_MAX;
1321 wm_state->sr.fbc = USHRT_MAX;
1322 }
1323
1324 if (level <= G4X_WM_LEVEL_HPLL) {
1325 wm_state->hpll_en = false;
1326 wm_state->hpll.cursor = USHRT_MAX;
1327 wm_state->hpll.plane = USHRT_MAX;
1328 wm_state->hpll.fbc = USHRT_MAX;
1329 }
1330}
1331
1332static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1333{
1334 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1335 struct intel_atomic_state *state =
1336 to_intel_atomic_state(crtc_state->base.state);
1337 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1338 int num_active_planes = hweight32(crtc_state->active_planes &
1339 ~BIT(PLANE_CURSOR));
1340 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001341 const struct intel_plane_state *old_plane_state;
1342 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001343 struct intel_plane *plane;
1344 enum plane_id plane_id;
1345 int i, level;
1346 unsigned int dirty = 0;
1347
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001348 for_each_oldnew_intel_plane_in_state(state, plane,
1349 old_plane_state,
1350 new_plane_state, i) {
1351 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001352 old_plane_state->base.crtc != &crtc->base)
1353 continue;
1354
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001355 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001356 dirty |= BIT(plane->id);
1357 }
1358
1359 if (!dirty)
1360 return 0;
1361
1362 level = G4X_WM_LEVEL_NORMAL;
1363 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1364 goto out;
1365
1366 raw = &crtc_state->wm.g4x.raw[level];
1367 for_each_plane_id_on_crtc(crtc, plane_id)
1368 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1369
1370 level = G4X_WM_LEVEL_SR;
1371
1372 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1373 goto out;
1374
1375 raw = &crtc_state->wm.g4x.raw[level];
1376 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1377 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1378 wm_state->sr.fbc = raw->fbc;
1379
1380 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1381
1382 level = G4X_WM_LEVEL_HPLL;
1383
1384 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1385 goto out;
1386
1387 raw = &crtc_state->wm.g4x.raw[level];
1388 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1389 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1390 wm_state->hpll.fbc = raw->fbc;
1391
1392 wm_state->hpll_en = wm_state->cxsr;
1393
1394 level++;
1395
1396 out:
1397 if (level == G4X_WM_LEVEL_NORMAL)
1398 return -EINVAL;
1399
1400 /* invalidate the higher levels */
1401 g4x_invalidate_wms(crtc, wm_state, level);
1402
1403 /*
1404 * Determine if the FBC watermark(s) can be used. IF
1405 * this isn't the case we prefer to disable the FBC
1406 ( watermark(s) rather than disable the SR/HPLL
1407 * level(s) entirely.
1408 */
1409 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1410
1411 if (level >= G4X_WM_LEVEL_SR &&
1412 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1413 wm_state->fbc_en = false;
1414 else if (level >= G4X_WM_LEVEL_HPLL &&
1415 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1416 wm_state->fbc_en = false;
1417
1418 return 0;
1419}
1420
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001421static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001422{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001423 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001424 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1425 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1426 struct intel_atomic_state *intel_state =
1427 to_intel_atomic_state(new_crtc_state->base.state);
1428 const struct intel_crtc_state *old_crtc_state =
1429 intel_atomic_get_old_crtc_state(intel_state, crtc);
1430 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001431 enum plane_id plane_id;
1432
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001433 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
1434 *intermediate = *optimal;
1435
1436 intermediate->cxsr = false;
1437 intermediate->hpll_en = false;
1438 goto out;
1439 }
1440
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001441 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001442 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001443 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001444 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001445 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1446
1447 for_each_plane_id_on_crtc(crtc, plane_id) {
1448 intermediate->wm.plane[plane_id] =
1449 max(optimal->wm.plane[plane_id],
1450 active->wm.plane[plane_id]);
1451
1452 WARN_ON(intermediate->wm.plane[plane_id] >
1453 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1454 }
1455
1456 intermediate->sr.plane = max(optimal->sr.plane,
1457 active->sr.plane);
1458 intermediate->sr.cursor = max(optimal->sr.cursor,
1459 active->sr.cursor);
1460 intermediate->sr.fbc = max(optimal->sr.fbc,
1461 active->sr.fbc);
1462
1463 intermediate->hpll.plane = max(optimal->hpll.plane,
1464 active->hpll.plane);
1465 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1466 active->hpll.cursor);
1467 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1468 active->hpll.fbc);
1469
1470 WARN_ON((intermediate->sr.plane >
1471 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1472 intermediate->sr.cursor >
1473 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1474 intermediate->cxsr);
1475 WARN_ON((intermediate->sr.plane >
1476 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1477 intermediate->sr.cursor >
1478 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1479 intermediate->hpll_en);
1480
1481 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1482 intermediate->fbc_en && intermediate->cxsr);
1483 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1484 intermediate->fbc_en && intermediate->hpll_en);
1485
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001486out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001487 /*
1488 * If our intermediate WM are identical to the final WM, then we can
1489 * omit the post-vblank programming; only update if it's different.
1490 */
1491 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001492 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001493
1494 return 0;
1495}
1496
1497static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1498 struct g4x_wm_values *wm)
1499{
1500 struct intel_crtc *crtc;
1501 int num_active_crtcs = 0;
1502
1503 wm->cxsr = true;
1504 wm->hpll_en = true;
1505 wm->fbc_en = true;
1506
1507 for_each_intel_crtc(&dev_priv->drm, crtc) {
1508 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1509
1510 if (!crtc->active)
1511 continue;
1512
1513 if (!wm_state->cxsr)
1514 wm->cxsr = false;
1515 if (!wm_state->hpll_en)
1516 wm->hpll_en = false;
1517 if (!wm_state->fbc_en)
1518 wm->fbc_en = false;
1519
1520 num_active_crtcs++;
1521 }
1522
1523 if (num_active_crtcs != 1) {
1524 wm->cxsr = false;
1525 wm->hpll_en = false;
1526 wm->fbc_en = false;
1527 }
1528
1529 for_each_intel_crtc(&dev_priv->drm, crtc) {
1530 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1531 enum pipe pipe = crtc->pipe;
1532
1533 wm->pipe[pipe] = wm_state->wm;
1534 if (crtc->active && wm->cxsr)
1535 wm->sr = wm_state->sr;
1536 if (crtc->active && wm->hpll_en)
1537 wm->hpll = wm_state->hpll;
1538 }
1539}
1540
1541static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1542{
1543 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1544 struct g4x_wm_values new_wm = {};
1545
1546 g4x_merge_wm(dev_priv, &new_wm);
1547
1548 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1549 return;
1550
1551 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1552 _intel_set_memory_cxsr(dev_priv, false);
1553
1554 g4x_write_wm_values(dev_priv, &new_wm);
1555
1556 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1557 _intel_set_memory_cxsr(dev_priv, true);
1558
1559 *old_wm = new_wm;
1560}
1561
1562static void g4x_initial_watermarks(struct intel_atomic_state *state,
1563 struct intel_crtc_state *crtc_state)
1564{
1565 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1566 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1567
1568 mutex_lock(&dev_priv->wm.wm_mutex);
1569 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1570 g4x_program_watermarks(dev_priv);
1571 mutex_unlock(&dev_priv->wm.wm_mutex);
1572}
1573
1574static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1575 struct intel_crtc_state *crtc_state)
1576{
1577 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001578 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001579
1580 if (!crtc_state->wm.need_postvbl_update)
1581 return;
1582
1583 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001584 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001585 g4x_program_watermarks(dev_priv);
1586 mutex_unlock(&dev_priv->wm.wm_mutex);
1587}
1588
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001589/* latency must be in 0.1us units. */
1590static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001591 unsigned int htotal,
1592 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001593 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001594 unsigned int latency)
1595{
1596 unsigned int ret;
1597
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001598 ret = intel_wm_method2(pixel_rate, htotal,
1599 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001600 ret = DIV_ROUND_UP(ret, 64);
1601
1602 return ret;
1603}
1604
Ville Syrjäläbb726512016-10-31 22:37:24 +02001605static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001606{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001607 /* all latencies in usec */
1608 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1609
Ville Syrjälä58590c12015-09-08 21:05:12 +03001610 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1611
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001612 if (IS_CHERRYVIEW(dev_priv)) {
1613 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1614 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001615
1616 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001617 }
1618}
1619
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001620static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1621 const struct intel_plane_state *plane_state,
1622 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001623{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001624 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001625 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001626 const struct drm_display_mode *adjusted_mode =
1627 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001628 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001629
1630 if (dev_priv->wm.pri_latency[level] == 0)
1631 return USHRT_MAX;
1632
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001633 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001634 return 0;
1635
Daniel Vetteref426c12017-01-04 11:41:10 +01001636 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001637 clock = adjusted_mode->crtc_clock;
1638 htotal = adjusted_mode->crtc_htotal;
1639 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001640
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001641 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001642 /*
1643 * FIXME the formula gives values that are
1644 * too big for the cursor FIFO, and hence we
1645 * would never be able to use cursors. For
1646 * now just hardcode the watermark.
1647 */
1648 wm = 63;
1649 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001650 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001651 dev_priv->wm.pri_latency[level] * 10);
1652 }
1653
Chris Wilson1a1f1282017-11-07 14:03:38 +00001654 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001655}
1656
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001657static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1658{
1659 return (active_planes & (BIT(PLANE_SPRITE0) |
1660 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1661}
1662
Ville Syrjälä5012e602017-03-02 19:14:56 +02001663static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001664{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001665 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001666 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001667 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001668 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001669 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1670 int num_active_planes = hweight32(active_planes);
1671 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001672 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001673 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001674 unsigned int total_rate;
1675 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001676
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001677 /*
1678 * When enabling sprite0 after sprite1 has already been enabled
1679 * we tend to get an underrun unless sprite0 already has some
1680 * FIFO space allcoated. Hence we always allocate at least one
1681 * cacheline for sprite0 whenever sprite1 is enabled.
1682 *
1683 * All other plane enable sequences appear immune to this problem.
1684 */
1685 if (vlv_need_sprite0_fifo_workaround(active_planes))
1686 sprite0_fifo_extra = 1;
1687
Ville Syrjälä5012e602017-03-02 19:14:56 +02001688 total_rate = raw->plane[PLANE_PRIMARY] +
1689 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001690 raw->plane[PLANE_SPRITE1] +
1691 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001692
Ville Syrjälä5012e602017-03-02 19:14:56 +02001693 if (total_rate > fifo_size)
1694 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001695
Ville Syrjälä5012e602017-03-02 19:14:56 +02001696 if (total_rate == 0)
1697 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001698
Ville Syrjälä5012e602017-03-02 19:14:56 +02001699 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001700 unsigned int rate;
1701
Ville Syrjälä5012e602017-03-02 19:14:56 +02001702 if ((active_planes & BIT(plane_id)) == 0) {
1703 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001704 continue;
1705 }
1706
Ville Syrjälä5012e602017-03-02 19:14:56 +02001707 rate = raw->plane[plane_id];
1708 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1709 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001710 }
1711
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001712 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1713 fifo_left -= sprite0_fifo_extra;
1714
Ville Syrjälä5012e602017-03-02 19:14:56 +02001715 fifo_state->plane[PLANE_CURSOR] = 63;
1716
1717 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001718
1719 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001720 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001721 int plane_extra;
1722
1723 if (fifo_left == 0)
1724 break;
1725
Ville Syrjälä5012e602017-03-02 19:14:56 +02001726 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001727 continue;
1728
1729 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001730 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001731 fifo_left -= plane_extra;
1732 }
1733
Ville Syrjälä5012e602017-03-02 19:14:56 +02001734 WARN_ON(active_planes != 0 && fifo_left != 0);
1735
1736 /* give it all to the first plane if none are active */
1737 if (active_planes == 0) {
1738 WARN_ON(fifo_left != fifo_size);
1739 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1740 }
1741
1742 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001743}
1744
Ville Syrjäläff32c542017-03-02 19:14:57 +02001745/* mark all levels starting from 'level' as invalid */
1746static void vlv_invalidate_wms(struct intel_crtc *crtc,
1747 struct vlv_wm_state *wm_state, int level)
1748{
1749 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1750
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001751 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001752 enum plane_id plane_id;
1753
1754 for_each_plane_id_on_crtc(crtc, plane_id)
1755 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1756
1757 wm_state->sr[level].cursor = USHRT_MAX;
1758 wm_state->sr[level].plane = USHRT_MAX;
1759 }
1760}
1761
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001762static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1763{
1764 if (wm > fifo_size)
1765 return USHRT_MAX;
1766 else
1767 return fifo_size - wm;
1768}
1769
Ville Syrjäläff32c542017-03-02 19:14:57 +02001770/*
1771 * Starting from 'level' set all higher
1772 * levels to 'value' in the "raw" watermarks.
1773 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001774static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001775 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001776{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001777 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001778 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001779 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001780
Ville Syrjäläff32c542017-03-02 19:14:57 +02001781 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001782 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001783
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001784 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001785 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001786 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001787
1788 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001789}
1790
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001791static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1792 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001793{
1794 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1795 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001796 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001797 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001798 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001799
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001800 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001801 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1802 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001803 }
1804
1805 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001806 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001807 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1808 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1809
Ville Syrjäläff32c542017-03-02 19:14:57 +02001810 if (wm > max_wm)
1811 break;
1812
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001813 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001814 raw->plane[plane_id] = wm;
1815 }
1816
1817 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001818 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001819
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001820out:
1821 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001822 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001823 plane->base.name,
1824 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1825 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1826 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1827
1828 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001829}
1830
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001831static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1832 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001833{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001834 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835 &crtc_state->wm.vlv.raw[level];
1836 const struct vlv_fifo_state *fifo_state =
1837 &crtc_state->wm.vlv.fifo_state;
1838
1839 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1840}
1841
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001842static 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 +02001843{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001844 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1845 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1846 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1847 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001848}
1849
1850static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001851{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001852 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001853 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001854 struct intel_atomic_state *state =
1855 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001856 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001857 const struct vlv_fifo_state *fifo_state =
1858 &crtc_state->wm.vlv.fifo_state;
1859 int num_active_planes = hweight32(crtc_state->active_planes &
1860 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001861 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001862 const struct intel_plane_state *old_plane_state;
1863 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001864 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001865 enum plane_id plane_id;
1866 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001867 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001868
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001869 for_each_oldnew_intel_plane_in_state(state, plane,
1870 old_plane_state,
1871 new_plane_state, i) {
1872 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001873 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001874 continue;
1875
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001876 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001877 dirty |= BIT(plane->id);
1878 }
1879
1880 /*
1881 * DSPARB registers may have been reset due to the
1882 * power well being turned off. Make sure we restore
1883 * them to a consistent state even if no primary/sprite
1884 * planes are initially active.
1885 */
1886 if (needs_modeset)
1887 crtc_state->fifo_changed = true;
1888
1889 if (!dirty)
1890 return 0;
1891
1892 /* cursor changes don't warrant a FIFO recompute */
1893 if (dirty & ~BIT(PLANE_CURSOR)) {
1894 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001895 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001896 const struct vlv_fifo_state *old_fifo_state =
1897 &old_crtc_state->wm.vlv.fifo_state;
1898
1899 ret = vlv_compute_fifo(crtc_state);
1900 if (ret)
1901 return ret;
1902
1903 if (needs_modeset ||
1904 memcmp(old_fifo_state, fifo_state,
1905 sizeof(*fifo_state)) != 0)
1906 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001907 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001908
Ville Syrjäläff32c542017-03-02 19:14:57 +02001909 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001910 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001911 /*
1912 * Note that enabling cxsr with no primary/sprite planes
1913 * enabled can wedge the pipe. Hence we only allow cxsr
1914 * with exactly one enabled primary/sprite plane.
1915 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001916 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001917
Ville Syrjälä5012e602017-03-02 19:14:56 +02001918 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001919 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001920 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001921
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001922 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001923 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001924
Ville Syrjäläff32c542017-03-02 19:14:57 +02001925 for_each_plane_id_on_crtc(crtc, plane_id) {
1926 wm_state->wm[level].plane[plane_id] =
1927 vlv_invert_wm_value(raw->plane[plane_id],
1928 fifo_state->plane[plane_id]);
1929 }
1930
1931 wm_state->sr[level].plane =
1932 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001933 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001934 raw->plane[PLANE_SPRITE1]),
1935 sr_fifo_size);
1936
1937 wm_state->sr[level].cursor =
1938 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1939 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001940 }
1941
Ville Syrjäläff32c542017-03-02 19:14:57 +02001942 if (level == 0)
1943 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001944
Ville Syrjäläff32c542017-03-02 19:14:57 +02001945 /* limit to only levels we can actually handle */
1946 wm_state->num_levels = level;
1947
1948 /* invalidate the higher levels */
1949 vlv_invalidate_wms(crtc, wm_state, level);
1950
1951 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001952}
1953
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001954#define VLV_FIFO(plane, value) \
1955 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1956
Ville Syrjäläff32c542017-03-02 19:14:57 +02001957static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1958 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001959{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001960 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001961 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001962 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001963 const struct vlv_fifo_state *fifo_state =
1964 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001965 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001966
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001967 if (!crtc_state->fifo_changed)
1968 return;
1969
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001970 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1971 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1972 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001973
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001974 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1975 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001976
Ville Syrjäläc137d662017-03-02 19:15:06 +02001977 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1978
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001979 /*
1980 * uncore.lock serves a double purpose here. It allows us to
1981 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1982 * it protects the DSPARB registers from getting clobbered by
1983 * parallel updates from multiple pipes.
1984 *
1985 * intel_pipe_update_start() has already disabled interrupts
1986 * for us, so a plain spin_lock() is sufficient here.
1987 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001988 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001989
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001990 switch (crtc->pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001991 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001992 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001993 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1994 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001995
1996 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1997 VLV_FIFO(SPRITEB, 0xff));
1998 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1999 VLV_FIFO(SPRITEB, sprite1_start));
2000
2001 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2002 VLV_FIFO(SPRITEB_HI, 0x1));
2003 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2004 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2005
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002006 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2007 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002008 break;
2009 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002010 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2011 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002012
2013 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2014 VLV_FIFO(SPRITED, 0xff));
2015 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2016 VLV_FIFO(SPRITED, sprite1_start));
2017
2018 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2019 VLV_FIFO(SPRITED_HI, 0xff));
2020 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2021 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2022
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002023 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2024 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002025 break;
2026 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002027 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2028 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002029
2030 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2031 VLV_FIFO(SPRITEF, 0xff));
2032 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2033 VLV_FIFO(SPRITEF, sprite1_start));
2034
2035 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2036 VLV_FIFO(SPRITEF_HI, 0xff));
2037 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2038 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2039
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002040 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2041 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002042 break;
2043 default:
2044 break;
2045 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002046
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002047 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002048
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002049 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002050}
2051
2052#undef VLV_FIFO
2053
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002054static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002055{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002056 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002057 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2058 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2059 struct intel_atomic_state *intel_state =
2060 to_intel_atomic_state(new_crtc_state->base.state);
2061 const struct intel_crtc_state *old_crtc_state =
2062 intel_atomic_get_old_crtc_state(intel_state, crtc);
2063 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002064 int level;
2065
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002066 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
2067 *intermediate = *optimal;
2068
2069 intermediate->cxsr = false;
2070 goto out;
2071 }
2072
Ville Syrjälä4841da52017-03-02 19:14:59 +02002073 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002074 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002075 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002076
2077 for (level = 0; level < intermediate->num_levels; level++) {
2078 enum plane_id plane_id;
2079
2080 for_each_plane_id_on_crtc(crtc, plane_id) {
2081 intermediate->wm[level].plane[plane_id] =
2082 min(optimal->wm[level].plane[plane_id],
2083 active->wm[level].plane[plane_id]);
2084 }
2085
2086 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2087 active->sr[level].plane);
2088 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2089 active->sr[level].cursor);
2090 }
2091
2092 vlv_invalidate_wms(crtc, intermediate, level);
2093
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002094out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002095 /*
2096 * If our intermediate WM are identical to the final WM, then we can
2097 * omit the post-vblank programming; only update if it's different.
2098 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002099 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002100 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002101
2102 return 0;
2103}
2104
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002105static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002106 struct vlv_wm_values *wm)
2107{
2108 struct intel_crtc *crtc;
2109 int num_active_crtcs = 0;
2110
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002111 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002112 wm->cxsr = true;
2113
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002114 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002115 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002116
2117 if (!crtc->active)
2118 continue;
2119
2120 if (!wm_state->cxsr)
2121 wm->cxsr = false;
2122
2123 num_active_crtcs++;
2124 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2125 }
2126
2127 if (num_active_crtcs != 1)
2128 wm->cxsr = false;
2129
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002130 if (num_active_crtcs > 1)
2131 wm->level = VLV_WM_LEVEL_PM2;
2132
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002133 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002134 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002135 enum pipe pipe = crtc->pipe;
2136
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002137 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002138 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002139 wm->sr = wm_state->sr[wm->level];
2140
Ville Syrjälä1b313892016-11-28 19:37:08 +02002141 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2142 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2143 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2144 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002145 }
2146}
2147
Ville Syrjäläff32c542017-03-02 19:14:57 +02002148static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002149{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002150 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2151 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002152
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002153 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002154
Ville Syrjäläff32c542017-03-02 19:14:57 +02002155 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002156 return;
2157
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002158 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002159 chv_set_memory_dvfs(dev_priv, false);
2160
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002161 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002162 chv_set_memory_pm5(dev_priv, false);
2163
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002164 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002165 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002166
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002167 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002168
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002169 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002170 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002171
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002172 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002173 chv_set_memory_pm5(dev_priv, true);
2174
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002175 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002176 chv_set_memory_dvfs(dev_priv, true);
2177
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002178 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002179}
2180
Ville Syrjäläff32c542017-03-02 19:14:57 +02002181static void vlv_initial_watermarks(struct intel_atomic_state *state,
2182 struct intel_crtc_state *crtc_state)
2183{
2184 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2185 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2186
2187 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002188 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2189 vlv_program_watermarks(dev_priv);
2190 mutex_unlock(&dev_priv->wm.wm_mutex);
2191}
2192
2193static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2194 struct intel_crtc_state *crtc_state)
2195{
2196 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002197 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002198
2199 if (!crtc_state->wm.need_postvbl_update)
2200 return;
2201
2202 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002203 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002204 vlv_program_watermarks(dev_priv);
2205 mutex_unlock(&dev_priv->wm.wm_mutex);
2206}
2207
Ville Syrjälä432081b2016-10-31 22:37:03 +02002208static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002209{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002210 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002211 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002212 int srwm = 1;
2213 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002214 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002215
2216 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002217 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002218 if (crtc) {
2219 /* self-refresh has much higher latency */
2220 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002221 const struct drm_display_mode *adjusted_mode =
2222 &crtc->config->base.adjusted_mode;
2223 const struct drm_framebuffer *fb =
2224 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002225 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002226 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002227 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002228 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002229 int entries;
2230
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002231 entries = intel_wm_method2(clock, htotal,
2232 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002233 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2234 srwm = I965_FIFO_SIZE - entries;
2235 if (srwm < 0)
2236 srwm = 1;
2237 srwm &= 0x1ff;
2238 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2239 entries, srwm);
2240
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002241 entries = intel_wm_method2(clock, htotal,
2242 crtc->base.cursor->state->crtc_w, 4,
2243 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002244 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002245 i965_cursor_wm_info.cacheline_size) +
2246 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002247
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002248 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002249 if (cursor_sr > i965_cursor_wm_info.max_wm)
2250 cursor_sr = i965_cursor_wm_info.max_wm;
2251
2252 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2253 "cursor %d\n", srwm, cursor_sr);
2254
Imre Deak98584252014-06-13 14:54:20 +03002255 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002256 } else {
Imre Deak98584252014-06-13 14:54:20 +03002257 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002258 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002259 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002260 }
2261
2262 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2263 srwm);
2264
2265 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002266 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2267 FW_WM(8, CURSORB) |
2268 FW_WM(8, PLANEB) |
2269 FW_WM(8, PLANEA));
2270 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2271 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002272 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002273 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002274
2275 if (cxsr_enabled)
2276 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002277}
2278
Ville Syrjäläf4998962015-03-10 17:02:21 +02002279#undef FW_WM
2280
Ville Syrjälä432081b2016-10-31 22:37:03 +02002281static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002282{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002283 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002284 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002285 u32 fwater_lo;
2286 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002287 int cwm, srwm = 1;
2288 int fifo_size;
2289 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002290 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002291
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002292 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002293 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002294 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002295 wm_info = &i915_wm_info;
2296 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002297 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002298
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002299 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2300 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002301 if (intel_crtc_active(crtc)) {
2302 const struct drm_display_mode *adjusted_mode =
2303 &crtc->config->base.adjusted_mode;
2304 const struct drm_framebuffer *fb =
2305 crtc->base.primary->state->fb;
2306 int cpp;
2307
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002308 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002309 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002310 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002311 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002312
Damien Lespiau241bfc32013-09-25 16:45:37 +01002313 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002314 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002315 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002316 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002317 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002318 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002319 if (planea_wm > (long)wm_info->max_wm)
2320 planea_wm = wm_info->max_wm;
2321 }
2322
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002323 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002324 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002325
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002326 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2327 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002328 if (intel_crtc_active(crtc)) {
2329 const struct drm_display_mode *adjusted_mode =
2330 &crtc->config->base.adjusted_mode;
2331 const struct drm_framebuffer *fb =
2332 crtc->base.primary->state->fb;
2333 int cpp;
2334
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002335 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002336 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002337 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002338 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002339
Damien Lespiau241bfc32013-09-25 16:45:37 +01002340 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002341 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002342 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002343 if (enabled == NULL)
2344 enabled = crtc;
2345 else
2346 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002347 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002348 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002349 if (planeb_wm > (long)wm_info->max_wm)
2350 planeb_wm = wm_info->max_wm;
2351 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002352
2353 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2354
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002355 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002356 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002357
Ville Syrjäläefc26112016-10-31 22:37:04 +02002358 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002359
2360 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002361 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002362 enabled = NULL;
2363 }
2364
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002365 /*
2366 * Overlay gets an aggressive default since video jitter is bad.
2367 */
2368 cwm = 2;
2369
2370 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002371 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002372
2373 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002374 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002375 /* self-refresh has much higher latency */
2376 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002377 const struct drm_display_mode *adjusted_mode =
2378 &enabled->config->base.adjusted_mode;
2379 const struct drm_framebuffer *fb =
2380 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002381 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002382 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002383 int hdisplay = enabled->config->pipe_src_w;
2384 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002385 int entries;
2386
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002387 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002388 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002389 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002390 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002391
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002392 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2393 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002394 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2395 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2396 srwm = wm_info->fifo_size - entries;
2397 if (srwm < 0)
2398 srwm = 1;
2399
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002400 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002401 I915_WRITE(FW_BLC_SELF,
2402 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002403 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002404 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2405 }
2406
2407 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2408 planea_wm, planeb_wm, cwm, srwm);
2409
2410 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2411 fwater_hi = (cwm & 0x1f);
2412
2413 /* Set request length to 8 cachelines per fetch */
2414 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2415 fwater_hi = fwater_hi | (1 << 8);
2416
2417 I915_WRITE(FW_BLC, fwater_lo);
2418 I915_WRITE(FW_BLC2, fwater_hi);
2419
Imre Deak5209b1f2014-07-01 12:36:17 +03002420 if (enabled)
2421 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002422}
2423
Ville Syrjälä432081b2016-10-31 22:37:03 +02002424static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002425{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002426 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002427 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002428 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002429 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002430 int planea_wm;
2431
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002432 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002433 if (crtc == NULL)
2434 return;
2435
Ville Syrjäläefc26112016-10-31 22:37:04 +02002436 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002437 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002438 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002439 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002440 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002441 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2442 fwater_lo |= (3<<8) | planea_wm;
2443
2444 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2445
2446 I915_WRITE(FW_BLC, fwater_lo);
2447}
2448
Ville Syrjälä37126462013-08-01 16:18:55 +03002449/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002450static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2451 unsigned int cpp,
2452 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002453{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002454 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002455
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002456 ret = intel_wm_method1(pixel_rate, cpp, latency);
2457 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002458
2459 return ret;
2460}
2461
Ville Syrjälä37126462013-08-01 16:18:55 +03002462/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002463static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2464 unsigned int htotal,
2465 unsigned int width,
2466 unsigned int cpp,
2467 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002468{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002469 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002470
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002471 ret = intel_wm_method2(pixel_rate, htotal,
2472 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002473 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002474
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002475 return ret;
2476}
2477
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002478static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002479{
Matt Roper15126882015-12-03 11:37:40 -08002480 /*
2481 * Neither of these should be possible since this function shouldn't be
2482 * called if the CRTC is off or the plane is invisible. But let's be
2483 * extra paranoid to avoid a potential divide-by-zero if we screw up
2484 * elsewhere in the driver.
2485 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002486 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002487 return 0;
2488 if (WARN_ON(!horiz_pixels))
2489 return 0;
2490
Ville Syrjäläac484962016-01-20 21:05:26 +02002491 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002492}
2493
Imre Deak820c1982013-12-17 14:46:36 +02002494struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002495 u16 pri;
2496 u16 spr;
2497 u16 cur;
2498 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002499};
2500
Ville Syrjälä37126462013-08-01 16:18:55 +03002501/*
2502 * For both WM_PIPE and WM_LP.
2503 * mem_value must be in 0.1us units.
2504 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002505static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2506 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002507 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002509 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002510 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002511
Ville Syrjälä03981c62018-11-14 19:34:40 +02002512 if (mem_value == 0)
2513 return U32_MAX;
2514
Maarten Lankhorstec193642019-06-28 10:55:17 +02002515 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002516 return 0;
2517
Maarten Lankhorstec193642019-06-28 10:55:17 +02002518 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002519
Maarten Lankhorstec193642019-06-28 10:55:17 +02002520 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002521
2522 if (!is_lp)
2523 return method1;
2524
Maarten Lankhorstec193642019-06-28 10:55:17 +02002525 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2526 crtc_state->base.adjusted_mode.crtc_htotal,
2527 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002528 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002529
2530 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002531}
2532
Ville Syrjälä37126462013-08-01 16:18:55 +03002533/*
2534 * For both WM_PIPE and WM_LP.
2535 * mem_value must be in 0.1us units.
2536 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002537static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2538 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002539 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002540{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002541 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002542 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002543
Ville Syrjälä03981c62018-11-14 19:34:40 +02002544 if (mem_value == 0)
2545 return U32_MAX;
2546
Maarten Lankhorstec193642019-06-28 10:55:17 +02002547 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002548 return 0;
2549
Maarten Lankhorstec193642019-06-28 10:55:17 +02002550 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002551
Maarten Lankhorstec193642019-06-28 10:55:17 +02002552 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2553 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2554 crtc_state->base.adjusted_mode.crtc_htotal,
2555 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002556 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002557 return min(method1, method2);
2558}
2559
Ville Syrjälä37126462013-08-01 16:18:55 +03002560/*
2561 * For both WM_PIPE and WM_LP.
2562 * mem_value must be in 0.1us units.
2563 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002564static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2565 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002566 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002567{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002568 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002569
Ville Syrjälä03981c62018-11-14 19:34:40 +02002570 if (mem_value == 0)
2571 return U32_MAX;
2572
Maarten Lankhorstec193642019-06-28 10:55:17 +02002573 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002574 return 0;
2575
Maarten Lankhorstec193642019-06-28 10:55:17 +02002576 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002577
Maarten Lankhorstec193642019-06-28 10:55:17 +02002578 return ilk_wm_method2(crtc_state->pixel_rate,
2579 crtc_state->base.adjusted_mode.crtc_htotal,
2580 plane_state->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002581}
2582
Paulo Zanonicca32e92013-05-31 11:45:06 -03002583/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002584static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2585 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002586 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002587{
Ville Syrjälä83054942016-11-18 21:53:00 +02002588 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002589
Maarten Lankhorstec193642019-06-28 10:55:17 +02002590 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002591 return 0;
2592
Maarten Lankhorstec193642019-06-28 10:55:17 +02002593 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002594
Maarten Lankhorstec193642019-06-28 10:55:17 +02002595 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002596}
2597
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002598static unsigned int
2599ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002600{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002601 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002602 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002603 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002604 return 768;
2605 else
2606 return 512;
2607}
2608
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002609static unsigned int
2610ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2611 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002612{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002613 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002614 /* BDW primary/sprite plane watermarks */
2615 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002616 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002617 /* IVB/HSW primary/sprite plane watermarks */
2618 return level == 0 ? 127 : 1023;
2619 else if (!is_sprite)
2620 /* ILK/SNB primary plane watermarks */
2621 return level == 0 ? 127 : 511;
2622 else
2623 /* ILK/SNB sprite plane watermarks */
2624 return level == 0 ? 63 : 255;
2625}
2626
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002627static unsigned int
2628ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002629{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002630 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002631 return level == 0 ? 63 : 255;
2632 else
2633 return level == 0 ? 31 : 63;
2634}
2635
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002636static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002637{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002638 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002639 return 31;
2640 else
2641 return 15;
2642}
2643
Ville Syrjälä158ae642013-08-07 13:28:19 +03002644/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002645static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002646 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002647 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002648 enum intel_ddb_partitioning ddb_partitioning,
2649 bool is_sprite)
2650{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002651 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002652
2653 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002654 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002655 return 0;
2656
2657 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002658 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002659 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002660
2661 /*
2662 * For some reason the non self refresh
2663 * FIFO size is only half of the self
2664 * refresh FIFO size on ILK/SNB.
2665 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002666 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002667 fifo_size /= 2;
2668 }
2669
Ville Syrjälä240264f2013-08-07 13:29:12 +03002670 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002671 /* level 0 is always calculated with 1:1 split */
2672 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2673 if (is_sprite)
2674 fifo_size *= 5;
2675 fifo_size /= 6;
2676 } else {
2677 fifo_size /= 2;
2678 }
2679 }
2680
2681 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002682 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002683}
2684
2685/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002686static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002687 int level,
2688 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002689{
2690 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002691 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002692 return 64;
2693
2694 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002695 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002696}
2697
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002698static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002699 int level,
2700 const struct intel_wm_config *config,
2701 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002702 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002703{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002704 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2705 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2706 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2707 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002708}
2709
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002710static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002711 int level,
2712 struct ilk_wm_maximums *max)
2713{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002714 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2715 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2716 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2717 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002718}
2719
Ville Syrjäläd9395652013-10-09 19:18:10 +03002720static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002721 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002722 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002723{
2724 bool ret;
2725
2726 /* already determined to be invalid? */
2727 if (!result->enable)
2728 return false;
2729
2730 result->enable = result->pri_val <= max->pri &&
2731 result->spr_val <= max->spr &&
2732 result->cur_val <= max->cur;
2733
2734 ret = result->enable;
2735
2736 /*
2737 * HACK until we can pre-compute everything,
2738 * and thus fail gracefully if LP0 watermarks
2739 * are exceeded...
2740 */
2741 if (level == 0 && !result->enable) {
2742 if (result->pri_val > max->pri)
2743 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2744 level, result->pri_val, max->pri);
2745 if (result->spr_val > max->spr)
2746 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2747 level, result->spr_val, max->spr);
2748 if (result->cur_val > max->cur)
2749 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2750 level, result->cur_val, max->cur);
2751
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002752 result->pri_val = min_t(u32, result->pri_val, max->pri);
2753 result->spr_val = min_t(u32, result->spr_val, max->spr);
2754 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002755 result->enable = true;
2756 }
2757
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002758 return ret;
2759}
2760
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002761static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002762 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002763 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002764 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002765 const struct intel_plane_state *pristate,
2766 const struct intel_plane_state *sprstate,
2767 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002768 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002769{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002770 u16 pri_latency = dev_priv->wm.pri_latency[level];
2771 u16 spr_latency = dev_priv->wm.spr_latency[level];
2772 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002773
2774 /* WM1+ latency values stored in 0.5us units */
2775 if (level > 0) {
2776 pri_latency *= 5;
2777 spr_latency *= 5;
2778 cur_latency *= 5;
2779 }
2780
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002781 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002782 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002783 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002784 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002785 }
2786
2787 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002788 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002789
2790 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002791 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002792
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002793 result->enable = true;
2794}
2795
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002796static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02002797hsw_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002798{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002799 const struct intel_atomic_state *intel_state =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002800 to_intel_atomic_state(crtc_state->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002801 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002802 &crtc_state->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002803 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002804
Maarten Lankhorstec193642019-06-28 10:55:17 +02002805 if (!crtc_state->base.active)
Matt Roperee91a152015-12-03 11:37:39 -08002806 return 0;
2807 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2808 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002809 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002810 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002811
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002812 /* The WM are computed with base on how long it takes to fill a single
2813 * row at the given clock rate, multiplied by 8.
2814 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002815 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2816 adjusted_mode->crtc_clock);
2817 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002818 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002819
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002820 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2821 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002822}
2823
Ville Syrjäläbb726512016-10-31 22:37:24 +02002824static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002825 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002826{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002827 struct intel_uncore *uncore = &dev_priv->uncore;
2828
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002829 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002830 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002831 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002832 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002833
2834 /* read the first set of memory latencies[0:3] */
2835 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002836 ret = sandybridge_pcode_read(dev_priv,
2837 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002838 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002839
2840 if (ret) {
2841 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2842 return;
2843 }
2844
2845 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2846 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2847 GEN9_MEM_LATENCY_LEVEL_MASK;
2848 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2849 GEN9_MEM_LATENCY_LEVEL_MASK;
2850 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2851 GEN9_MEM_LATENCY_LEVEL_MASK;
2852
2853 /* read the second set of memory latencies[4:7] */
2854 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002855 ret = sandybridge_pcode_read(dev_priv,
2856 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002857 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002858 if (ret) {
2859 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2860 return;
2861 }
2862
2863 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2864 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2865 GEN9_MEM_LATENCY_LEVEL_MASK;
2866 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2867 GEN9_MEM_LATENCY_LEVEL_MASK;
2868 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2869 GEN9_MEM_LATENCY_LEVEL_MASK;
2870
Vandana Kannan367294b2014-11-04 17:06:46 +00002871 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002872 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2873 * need to be disabled. We make sure to sanitize the values out
2874 * of the punit to satisfy this requirement.
2875 */
2876 for (level = 1; level <= max_level; level++) {
2877 if (wm[level] == 0) {
2878 for (i = level + 1; i <= max_level; i++)
2879 wm[i] = 0;
2880 break;
2881 }
2882 }
2883
2884 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002885 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002886 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002887 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002888 * to add 2us to the various latency levels we retrieve from the
2889 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002890 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002891 if (wm[0] == 0) {
2892 wm[0] += 2;
2893 for (level = 1; level <= max_level; level++) {
2894 if (wm[level] == 0)
2895 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002896 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002897 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002898 }
2899
Mahesh Kumar86b59282018-08-31 16:39:42 +05302900 /*
2901 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2902 * If we could not get dimm info enable this WA to prevent from
2903 * any underrun. If not able to get Dimm info assume 16GB dimm
2904 * to avoid any underrun.
2905 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002906 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302907 wm[0] += 1;
2908
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002909 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002910 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002911
2912 wm[0] = (sskpd >> 56) & 0xFF;
2913 if (wm[0] == 0)
2914 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002915 wm[1] = (sskpd >> 4) & 0xFF;
2916 wm[2] = (sskpd >> 12) & 0xFF;
2917 wm[3] = (sskpd >> 20) & 0x1FF;
2918 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002919 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002920 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002921
2922 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2923 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2924 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2925 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002926 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002927 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002928
2929 /* ILK primary LP0 latency is 700 ns */
2930 wm[0] = 7;
2931 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2932 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002933 } else {
2934 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002935 }
2936}
2937
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002938static void intel_fixup_spr_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 sprite 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;
2944}
2945
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002946static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002947 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002948{
2949 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002950 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002951 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002952}
2953
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002954int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002955{
2956 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002957 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002958 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002959 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002960 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002961 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002962 return 3;
2963 else
2964 return 2;
2965}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002966
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002967static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002968 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002969 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002970{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002971 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002972
2973 for (level = 0; level <= max_level; level++) {
2974 unsigned int latency = wm[level];
2975
2976 if (latency == 0) {
Chris Wilson86c1c872018-07-26 17:15:27 +01002977 DRM_DEBUG_KMS("%s WM%d latency not provided\n",
2978 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002979 continue;
2980 }
2981
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002982 /*
2983 * - latencies are in us on gen9.
2984 * - before then, WM1+ latency values are in 0.5us units
2985 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002986 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002987 latency *= 10;
2988 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002989 latency *= 5;
2990
2991 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2992 name, level, wm[level],
2993 latency / 10, latency % 10);
2994 }
2995}
2996
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002997static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002998 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002999{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003000 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003001
3002 if (wm[0] >= min)
3003 return false;
3004
3005 wm[0] = max(wm[0], min);
3006 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003007 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003008
3009 return true;
3010}
3011
Ville Syrjäläbb726512016-10-31 22:37:24 +02003012static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003013{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003014 bool changed;
3015
3016 /*
3017 * The BIOS provided WM memory latency values are often
3018 * inadequate for high resolution displays. Adjust them.
3019 */
3020 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3021 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3022 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3023
3024 if (!changed)
3025 return;
3026
3027 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003028 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3029 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3030 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003031}
3032
Ville Syrjälä03981c62018-11-14 19:34:40 +02003033static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3034{
3035 /*
3036 * On some SNB machines (Thinkpad X220 Tablet at least)
3037 * LP3 usage can cause vblank interrupts to be lost.
3038 * The DEIIR bit will go high but it looks like the CPU
3039 * never gets interrupted.
3040 *
3041 * It's not clear whether other interrupt source could
3042 * be affected or if this is somehow limited to vblank
3043 * interrupts only. To play it safe we disable LP3
3044 * watermarks entirely.
3045 */
3046 if (dev_priv->wm.pri_latency[3] == 0 &&
3047 dev_priv->wm.spr_latency[3] == 0 &&
3048 dev_priv->wm.cur_latency[3] == 0)
3049 return;
3050
3051 dev_priv->wm.pri_latency[3] = 0;
3052 dev_priv->wm.spr_latency[3] = 0;
3053 dev_priv->wm.cur_latency[3] = 0;
3054
3055 DRM_DEBUG_KMS("LP3 watermarks disabled due to potential for lost interrupts\n");
3056 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3057 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3058 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3059}
3060
Ville Syrjäläbb726512016-10-31 22:37:24 +02003061static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003062{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003063 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003064
3065 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3066 sizeof(dev_priv->wm.pri_latency));
3067 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3068 sizeof(dev_priv->wm.pri_latency));
3069
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003070 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003071 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003072
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003073 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3074 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3075 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003076
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003077 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003078 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003079 snb_wm_lp3_irq_quirk(dev_priv);
3080 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003081}
3082
Ville Syrjäläbb726512016-10-31 22:37:24 +02003083static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003084{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003085 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003086 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003087}
3088
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003089static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003090 struct intel_pipe_wm *pipe_wm)
3091{
3092 /* LP0 watermark maximums depend on this pipe alone */
3093 const struct intel_wm_config config = {
3094 .num_pipes_active = 1,
3095 .sprites_enabled = pipe_wm->sprites_enabled,
3096 .sprites_scaled = pipe_wm->sprites_scaled,
3097 };
3098 struct ilk_wm_maximums max;
3099
3100 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003101 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003102
3103 /* At least LP0 must be valid */
3104 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3105 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3106 return false;
3107 }
3108
3109 return true;
3110}
3111
Matt Roper261a27d2015-10-08 15:28:25 -07003112/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003113static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003114{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003115 struct drm_atomic_state *state = crtc_state->base.state;
3116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003117 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003118 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003119 const struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003120 struct drm_plane *plane;
3121 const struct drm_plane_state *plane_state;
3122 const struct intel_plane_state *pristate = NULL;
3123 const struct intel_plane_state *sprstate = NULL;
3124 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003125 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003126 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003127
Maarten Lankhorstec193642019-06-28 10:55:17 +02003128 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003129
Maarten Lankhorstec193642019-06-28 10:55:17 +02003130 drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, &crtc_state->base) {
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003131 const struct intel_plane_state *ps = to_intel_plane_state(plane_state);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003132
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003133 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003134 pristate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003135 else if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003136 sprstate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003137 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003138 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003139 }
3140
Maarten Lankhorstec193642019-06-28 10:55:17 +02003141 pipe_wm->pipe_enabled = crtc_state->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003142 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003143 pipe_wm->sprites_enabled = sprstate->base.visible;
3144 pipe_wm->sprites_scaled = sprstate->base.visible &&
3145 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3146 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003147 }
3148
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003149 usable_level = max_level;
3150
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003151 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003152 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003153 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003154
3155 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003156 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003157 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003158
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003159 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorstec193642019-06-28 10:55:17 +02003160 ilk_compute_wm_level(dev_priv, intel_crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003161 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003162
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003163 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Maarten Lankhorstec193642019-06-28 10:55:17 +02003164 pipe_wm->linetime = hsw_compute_linetime_wm(crtc_state);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003165
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003166 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003167 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003168
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003169 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003170
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003171 for (level = 1; level <= usable_level; level++) {
3172 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003173
Maarten Lankhorstec193642019-06-28 10:55:17 +02003174 ilk_compute_wm_level(dev_priv, intel_crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003175 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003176
3177 /*
3178 * Disable any watermark level that exceeds the
3179 * register maximums since such watermarks are
3180 * always invalid.
3181 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003182 if (!ilk_validate_wm_level(level, &max, wm)) {
3183 memset(wm, 0, sizeof(*wm));
3184 break;
3185 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003186 }
3187
Matt Roper86c8bbb2015-09-24 15:53:16 -07003188 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003189}
3190
3191/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003192 * Build a set of 'intermediate' watermark values that satisfy both the old
3193 * state and the new state. These can be programmed to the hardware
3194 * immediately.
3195 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003196static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003197{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003198 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->base.crtc);
3199 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003200 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003201 struct intel_atomic_state *intel_state =
3202 to_intel_atomic_state(newstate->base.state);
3203 const struct intel_crtc_state *oldstate =
3204 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3205 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003206 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003207
3208 /*
3209 * Start with the final, target watermarks, then combine with the
3210 * currently active watermarks to get values that are safe both before
3211 * and after the vblank.
3212 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003213 *a = newstate->wm.ilk.optimal;
Ville Syrjäläf255c622018-11-08 17:10:13 +02003214 if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base) ||
3215 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003216 return 0;
3217
Matt Ropered4a6a72016-02-23 17:20:13 -08003218 a->pipe_enabled |= b->pipe_enabled;
3219 a->sprites_enabled |= b->sprites_enabled;
3220 a->sprites_scaled |= b->sprites_scaled;
3221
3222 for (level = 0; level <= max_level; level++) {
3223 struct intel_wm_level *a_wm = &a->wm[level];
3224 const struct intel_wm_level *b_wm = &b->wm[level];
3225
3226 a_wm->enable &= b_wm->enable;
3227 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3228 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3229 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3230 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3231 }
3232
3233 /*
3234 * We need to make sure that these merged watermark values are
3235 * actually a valid configuration themselves. If they're not,
3236 * there's no safe way to transition from the old state to
3237 * the new state, so we need to fail the atomic transaction.
3238 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003239 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003240 return -EINVAL;
3241
3242 /*
3243 * If our intermediate WM are identical to the final WM, then we can
3244 * omit the post-vblank programming; only update if it's different.
3245 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003246 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3247 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003248
3249 return 0;
3250}
3251
3252/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003253 * Merge the watermarks from all active pipes for a specific level.
3254 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003255static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003256 int level,
3257 struct intel_wm_level *ret_wm)
3258{
3259 const struct intel_crtc *intel_crtc;
3260
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003261 ret_wm->enable = true;
3262
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003263 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003264 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003265 const struct intel_wm_level *wm = &active->wm[level];
3266
3267 if (!active->pipe_enabled)
3268 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003269
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003270 /*
3271 * The watermark values may have been used in the past,
3272 * so we must maintain them in the registers for some
3273 * time even if the level is now disabled.
3274 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003275 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003276 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003277
3278 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3279 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3280 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3281 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3282 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003283}
3284
3285/*
3286 * Merge all low power watermarks for all active pipes.
3287 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003288static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003289 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003290 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003291 struct intel_pipe_wm *merged)
3292{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003293 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003294 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003295
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003296 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003297 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003298 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003299 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003300
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003301 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003302 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003303
3304 /* merge each WM1+ level */
3305 for (level = 1; level <= max_level; level++) {
3306 struct intel_wm_level *wm = &merged->wm[level];
3307
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003308 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003309
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003310 if (level > last_enabled_level)
3311 wm->enable = false;
3312 else if (!ilk_validate_wm_level(level, max, wm))
3313 /* make sure all following levels get disabled */
3314 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003315
3316 /*
3317 * The spec says it is preferred to disable
3318 * FBC WMs instead of disabling a WM level.
3319 */
3320 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003321 if (wm->enable)
3322 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003323 wm->fbc_val = 0;
3324 }
3325 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003326
3327 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3328 /*
3329 * FIXME this is racy. FBC might get enabled later.
3330 * What we should check here is whether FBC can be
3331 * enabled sometime later.
3332 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003333 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003334 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003335 for (level = 2; level <= max_level; level++) {
3336 struct intel_wm_level *wm = &merged->wm[level];
3337
3338 wm->enable = false;
3339 }
3340 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003341}
3342
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003343static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3344{
3345 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3346 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3347}
3348
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003349/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003350static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3351 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003352{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003353 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003354 return 2 * level;
3355 else
3356 return dev_priv->wm.pri_latency[level];
3357}
3358
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003359static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003360 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003361 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003362 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003363{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003364 struct intel_crtc *intel_crtc;
3365 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003366
Ville Syrjälä0362c782013-10-09 19:17:57 +03003367 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003368 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003369
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003370 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003371 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003372 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003373
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003374 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003375
Ville Syrjälä0362c782013-10-09 19:17:57 +03003376 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003377
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003378 /*
3379 * Maintain the watermark values even if the level is
3380 * disabled. Doing otherwise could cause underruns.
3381 */
3382 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003383 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003384 (r->pri_val << WM1_LP_SR_SHIFT) |
3385 r->cur_val;
3386
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003387 if (r->enable)
3388 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3389
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003390 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003391 results->wm_lp[wm_lp - 1] |=
3392 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3393 else
3394 results->wm_lp[wm_lp - 1] |=
3395 r->fbc_val << WM1_LP_FBC_SHIFT;
3396
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003397 /*
3398 * Always set WM1S_LP_EN when spr_val != 0, even if the
3399 * level is disabled. Doing otherwise could cause underruns.
3400 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003401 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003402 WARN_ON(wm_lp != 1);
3403 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3404 } else
3405 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003406 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003407
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003408 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003409 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003410 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003411 const struct intel_wm_level *r =
3412 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003413
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003414 if (WARN_ON(!r->enable))
3415 continue;
3416
Matt Ropered4a6a72016-02-23 17:20:13 -08003417 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003418
3419 results->wm_pipe[pipe] =
3420 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3421 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3422 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003423 }
3424}
3425
Paulo Zanoni861f3382013-05-31 10:19:21 -03003426/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3427 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003428static struct intel_pipe_wm *
3429ilk_find_best_result(struct drm_i915_private *dev_priv,
3430 struct intel_pipe_wm *r1,
3431 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003432{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003433 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003434 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003435
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003436 for (level = 1; level <= max_level; level++) {
3437 if (r1->wm[level].enable)
3438 level1 = level;
3439 if (r2->wm[level].enable)
3440 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003441 }
3442
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003443 if (level1 == level2) {
3444 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003445 return r2;
3446 else
3447 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003448 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003449 return r1;
3450 } else {
3451 return r2;
3452 }
3453}
3454
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003455/* dirty bits used to track which watermarks need changes */
3456#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3457#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3458#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3459#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3460#define WM_DIRTY_FBC (1 << 24)
3461#define WM_DIRTY_DDB (1 << 25)
3462
Damien Lespiau055e3932014-08-18 13:49:10 +01003463static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003464 const struct ilk_wm_values *old,
3465 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003466{
3467 unsigned int dirty = 0;
3468 enum pipe pipe;
3469 int wm_lp;
3470
Damien Lespiau055e3932014-08-18 13:49:10 +01003471 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003472 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3473 dirty |= WM_DIRTY_LINETIME(pipe);
3474 /* Must disable LP1+ watermarks too */
3475 dirty |= WM_DIRTY_LP_ALL;
3476 }
3477
3478 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3479 dirty |= WM_DIRTY_PIPE(pipe);
3480 /* Must disable LP1+ watermarks too */
3481 dirty |= WM_DIRTY_LP_ALL;
3482 }
3483 }
3484
3485 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3486 dirty |= WM_DIRTY_FBC;
3487 /* Must disable LP1+ watermarks too */
3488 dirty |= WM_DIRTY_LP_ALL;
3489 }
3490
3491 if (old->partitioning != new->partitioning) {
3492 dirty |= WM_DIRTY_DDB;
3493 /* Must disable LP1+ watermarks too */
3494 dirty |= WM_DIRTY_LP_ALL;
3495 }
3496
3497 /* LP1+ watermarks already deemed dirty, no need to continue */
3498 if (dirty & WM_DIRTY_LP_ALL)
3499 return dirty;
3500
3501 /* Find the lowest numbered LP1+ watermark in need of an update... */
3502 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3503 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3504 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3505 break;
3506 }
3507
3508 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3509 for (; wm_lp <= 3; wm_lp++)
3510 dirty |= WM_DIRTY_LP(wm_lp);
3511
3512 return dirty;
3513}
3514
Ville Syrjälä8553c182013-12-05 15:51:39 +02003515static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3516 unsigned int dirty)
3517{
Imre Deak820c1982013-12-17 14:46:36 +02003518 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003519 bool changed = false;
3520
3521 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3522 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3523 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3524 changed = true;
3525 }
3526 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3527 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3528 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3529 changed = true;
3530 }
3531 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3532 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3533 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3534 changed = true;
3535 }
3536
3537 /*
3538 * Don't touch WM1S_LP_EN here.
3539 * Doing so could cause underruns.
3540 */
3541
3542 return changed;
3543}
3544
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003545/*
3546 * The spec says we shouldn't write when we don't need, because every write
3547 * causes WMs to be re-evaluated, expending some power.
3548 */
Imre Deak820c1982013-12-17 14:46:36 +02003549static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3550 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003551{
Imre Deak820c1982013-12-17 14:46:36 +02003552 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003553 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003554 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003555
Damien Lespiau055e3932014-08-18 13:49:10 +01003556 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003557 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003558 return;
3559
Ville Syrjälä8553c182013-12-05 15:51:39 +02003560 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003561
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003562 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003563 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003564 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003565 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003566 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003567 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3568
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003569 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003570 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003571 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003572 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003573 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003574 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3575
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003576 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003577 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003578 val = I915_READ(WM_MISC);
3579 if (results->partitioning == INTEL_DDB_PART_1_2)
3580 val &= ~WM_MISC_DATA_PARTITION_5_6;
3581 else
3582 val |= WM_MISC_DATA_PARTITION_5_6;
3583 I915_WRITE(WM_MISC, val);
3584 } else {
3585 val = I915_READ(DISP_ARB_CTL2);
3586 if (results->partitioning == INTEL_DDB_PART_1_2)
3587 val &= ~DISP_DATA_PARTITION_5_6;
3588 else
3589 val |= DISP_DATA_PARTITION_5_6;
3590 I915_WRITE(DISP_ARB_CTL2, val);
3591 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003592 }
3593
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003594 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003595 val = I915_READ(DISP_ARB_CTL);
3596 if (results->enable_fbc_wm)
3597 val &= ~DISP_FBC_WM_DIS;
3598 else
3599 val |= DISP_FBC_WM_DIS;
3600 I915_WRITE(DISP_ARB_CTL, val);
3601 }
3602
Imre Deak954911e2013-12-17 14:46:34 +02003603 if (dirty & WM_DIRTY_LP(1) &&
3604 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3605 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3606
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003607 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003608 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3609 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3610 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3611 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3612 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003613
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003614 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003615 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003616 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003617 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003618 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003619 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003620
3621 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003622}
3623
Matt Ropered4a6a72016-02-23 17:20:13 -08003624bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003625{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003626 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003627
3628 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3629}
3630
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303631static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
3632{
3633 u8 enabled_slices;
3634
3635 /* Slice 1 will always be enabled */
3636 enabled_slices = 1;
3637
3638 /* Gen prior to GEN11 have only one DBuf slice */
3639 if (INTEL_GEN(dev_priv) < 11)
3640 return enabled_slices;
3641
Imre Deak209d7352019-03-07 12:32:35 +02003642 /*
3643 * FIXME: for now we'll only ever use 1 slice; pretend that we have
3644 * only that 1 slice enabled until we have a proper way for on-demand
3645 * toggling of the second slice.
3646 */
3647 if (0 && I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303648 enabled_slices++;
3649
3650 return enabled_slices;
3651}
3652
Matt Roper024c9042015-09-24 15:53:11 -07003653/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003654 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3655 * so assume we'll always need it in order to avoid underruns.
3656 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003657static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003658{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003659 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003660}
3661
Paulo Zanoni56feca92016-09-22 18:00:28 -03003662static bool
3663intel_has_sagv(struct drm_i915_private *dev_priv)
3664{
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003665 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3666 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003667}
3668
Lyude656d1b82016-08-17 15:55:54 -04003669/*
3670 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3671 * depending on power and performance requirements. The display engine access
3672 * to system memory is blocked during the adjustment time. Because of the
3673 * blocking time, having this enabled can cause full system hangs and/or pipe
3674 * underruns if we don't meet all of the following requirements:
3675 *
3676 * - <= 1 pipe enabled
3677 * - All planes can enable watermarks for latencies >= SAGV engine block time
3678 * - We're not using an interlaced display configuration
3679 */
3680int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003681intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003682{
3683 int ret;
3684
Paulo Zanoni56feca92016-09-22 18:00:28 -03003685 if (!intel_has_sagv(dev_priv))
3686 return 0;
3687
3688 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003689 return 0;
3690
Ville Syrjäläff61a972018-12-21 19:14:34 +02003691 DRM_DEBUG_KMS("Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003692 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3693 GEN9_SAGV_ENABLE);
3694
Ville Syrjäläff61a972018-12-21 19:14:34 +02003695 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003696
3697 /*
3698 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003699 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003700 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003701 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003702 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003703 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003704 return 0;
3705 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003706 DRM_ERROR("Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003707 return ret;
3708 }
3709
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003710 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003711 return 0;
3712}
3713
Lyude656d1b82016-08-17 15:55:54 -04003714int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003715intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003716{
Imre Deakb3b8e992016-12-05 18:27:38 +02003717 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003718
Paulo Zanoni56feca92016-09-22 18:00:28 -03003719 if (!intel_has_sagv(dev_priv))
3720 return 0;
3721
3722 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003723 return 0;
3724
Ville Syrjäläff61a972018-12-21 19:14:34 +02003725 DRM_DEBUG_KMS("Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003726 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003727 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3728 GEN9_SAGV_DISABLE,
3729 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3730 1);
Lyude656d1b82016-08-17 15:55:54 -04003731 /*
3732 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003733 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003734 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003735 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003736 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003737 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003738 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003739 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003740 DRM_ERROR("Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003741 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003742 }
3743
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003744 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003745 return 0;
3746}
3747
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003748bool intel_can_enable_sagv(struct intel_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003749{
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003750 struct drm_device *dev = state->base.dev;
Lyude656d1b82016-08-17 15:55:54 -04003751 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003752 struct intel_crtc *crtc;
3753 struct intel_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003754 struct intel_crtc_state *crtc_state;
Lyude656d1b82016-08-17 15:55:54 -04003755 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003756 int level, latency;
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003757 int sagv_block_time_us;
Lyude656d1b82016-08-17 15:55:54 -04003758
Paulo Zanoni56feca92016-09-22 18:00:28 -03003759 if (!intel_has_sagv(dev_priv))
3760 return false;
3761
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003762 if (IS_GEN(dev_priv, 9))
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003763 sagv_block_time_us = 30;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003764 else if (IS_GEN(dev_priv, 10))
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003765 sagv_block_time_us = 20;
3766 else
3767 sagv_block_time_us = 10;
3768
Lyude656d1b82016-08-17 15:55:54 -04003769 /*
Lyude656d1b82016-08-17 15:55:54 -04003770 * If there are no active CRTCs, no additional checks need be performed
3771 */
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003772 if (hweight32(state->active_crtcs) == 0)
Lyude656d1b82016-08-17 15:55:54 -04003773 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003774
3775 /*
3776 * SKL+ workaround: bspec recommends we disable SAGV when we have
3777 * more then one pipe enabled
3778 */
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003779 if (hweight32(state->active_crtcs) > 1)
Lyude656d1b82016-08-17 15:55:54 -04003780 return false;
3781
3782 /* Since we're now guaranteed to only have one active CRTC... */
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003783 pipe = ffs(state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003784 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003785 crtc_state = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003786
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003787 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003788 return false;
3789
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003790 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003791 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003792 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003793
Lyude656d1b82016-08-17 15:55:54 -04003794 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003795 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003796 continue;
3797
3798 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003799 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003800 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003801 { }
3802
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003803 latency = dev_priv->wm.skl_latency[level];
3804
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003805 if (skl_needs_memory_bw_wa(dev_priv) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003806 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003807 I915_FORMAT_MOD_X_TILED)
3808 latency += 15;
3809
Lyude656d1b82016-08-17 15:55:54 -04003810 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003811 * If any of the planes on this pipe don't enable wm levels that
3812 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003813 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003814 */
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003815 if (latency < sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003816 return false;
3817 }
3818
3819 return true;
3820}
3821
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303822static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003823 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003824 const u64 total_data_rate,
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303825 const int num_active,
3826 struct skl_ddb_allocation *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303827{
3828 const struct drm_display_mode *adjusted_mode;
3829 u64 total_data_bw;
3830 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3831
3832 WARN_ON(ddb_size == 0);
3833
3834 if (INTEL_GEN(dev_priv) < 11)
3835 return ddb_size - 4; /* 4 blocks for bypass path allocation */
3836
Maarten Lankhorstec193642019-06-28 10:55:17 +02003837 adjusted_mode = &crtc_state->base.adjusted_mode;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003838 total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303839
3840 /*
3841 * 12GB/s is maximum BW supported by single DBuf slice.
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003842 *
3843 * FIXME dbuf slice code is broken:
3844 * - must wait for planes to stop using the slice before powering it off
3845 * - plane straddling both slices is illegal in multi-pipe scenarios
3846 * - should validate we stay within the hw bandwidth limits
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303847 */
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003848 if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303849 ddb->enabled_slices = 2;
3850 } else {
3851 ddb->enabled_slices = 1;
3852 ddb_size /= 2;
3853 }
3854
3855 return ddb_size;
3856}
3857
Damien Lespiaub9cec072014-11-04 17:06:43 +00003858static void
Maarten Lankhorstb048a002018-10-18 13:51:30 +02003859skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003860 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003861 const u64 total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303862 struct skl_ddb_allocation *ddb,
Matt Roperc107acf2016-05-12 07:06:01 -07003863 struct skl_ddb_entry *alloc, /* out */
3864 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003865{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003866 struct drm_atomic_state *state = crtc_state->base.state;
Matt Roperc107acf2016-05-12 07:06:01 -07003867 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003868 struct drm_crtc *for_crtc = crtc_state->base.crtc;
3869 const struct intel_crtc *crtc;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303870 u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
3871 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
3872 u16 ddb_size;
3873 u32 i;
Matt Roperc107acf2016-05-12 07:06:01 -07003874
Maarten Lankhorstec193642019-06-28 10:55:17 +02003875 if (WARN_ON(!state) || !crtc_state->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003876 alloc->start = 0;
3877 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003878 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003879 return;
3880 }
3881
Matt Ropera6d3460e2016-05-12 07:06:04 -07003882 if (intel_state->active_pipe_changes)
3883 *num_active = hweight32(intel_state->active_crtcs);
3884 else
3885 *num_active = hweight32(dev_priv->active_crtcs);
3886
Maarten Lankhorstec193642019-06-28 10:55:17 +02003887 ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303888 *num_active, ddb);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003889
Matt Roperc107acf2016-05-12 07:06:01 -07003890 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303891 * If the state doesn't change the active CRTC's or there is no
3892 * modeset request, then there's no need to recalculate;
3893 * the existing pipe allocation limits should remain unchanged.
3894 * Note that we're safe from racing commits since any racing commit
3895 * that changes the active CRTC list or do modeset would need to
3896 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003897 */
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303898 if (!intel_state->active_pipe_changes && !intel_state->modeset) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003899 /*
3900 * alloc may be cleared by clear_intel_crtc_state,
3901 * copy from old state to be sure
3902 */
3903 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003904 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003905 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003906
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303907 /*
3908 * Watermark/ddb requirement highly depends upon width of the
3909 * framebuffer, So instead of allocating DDB equally among pipes
3910 * distribute DDB based on resolution/width of the display.
3911 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003912 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3913 const struct drm_display_mode *adjusted_mode =
3914 &crtc_state->base.adjusted_mode;
3915 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303916 int hdisplay, vdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303917
Maarten Lankhorstec193642019-06-28 10:55:17 +02003918 if (!crtc_state->base.enable)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303919 continue;
3920
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303921 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
3922 total_width += hdisplay;
3923
3924 if (pipe < for_pipe)
3925 width_before_pipe += hdisplay;
3926 else if (pipe == for_pipe)
3927 pipe_width = hdisplay;
3928 }
3929
3930 alloc->start = ddb_size * width_before_pipe / total_width;
3931 alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003932}
3933
Ville Syrjälädf331de2019-03-19 18:03:11 +02003934static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
3935 int width, const struct drm_format_info *format,
3936 u64 modifier, unsigned int rotation,
3937 u32 plane_pixel_rate, struct skl_wm_params *wp,
3938 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003939static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02003940 int level,
3941 const struct skl_wm_params *wp,
3942 const struct skl_wm_level *result_prev,
3943 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003944
Ville Syrjälädf331de2019-03-19 18:03:11 +02003945static unsigned int
3946skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
3947 int num_active)
3948{
3949 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3950 int level, max_level = ilk_wm_max_level(dev_priv);
3951 struct skl_wm_level wm = {};
3952 int ret, min_ddb_alloc = 0;
3953 struct skl_wm_params wp;
3954
3955 ret = skl_compute_wm_params(crtc_state, 256,
3956 drm_format_info(DRM_FORMAT_ARGB8888),
3957 DRM_FORMAT_MOD_LINEAR,
3958 DRM_MODE_ROTATE_0,
3959 crtc_state->pixel_rate, &wp, 0);
3960 WARN_ON(ret);
3961
3962 for (level = 0; level <= max_level; level++) {
Ville Syrjälä6086e472019-03-21 19:51:28 +02003963 skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003964 if (wm.min_ddb_alloc == U16_MAX)
3965 break;
3966
3967 min_ddb_alloc = wm.min_ddb_alloc;
3968 }
3969
3970 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003971}
3972
Mahesh Kumar37cde112018-04-26 19:55:17 +05303973static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
3974 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00003975{
Mahesh Kumar37cde112018-04-26 19:55:17 +05303976
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02003977 entry->start = reg & DDB_ENTRY_MASK;
3978 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05303979
Damien Lespiau16160e32014-11-04 17:06:53 +00003980 if (entry->end)
3981 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003982}
3983
Mahesh Kumarddf34312018-04-09 09:11:03 +05303984static void
3985skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
3986 const enum pipe pipe,
3987 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003988 struct skl_ddb_entry *ddb_y,
3989 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05303990{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003991 u32 val, val2;
3992 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05303993
3994 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
3995 if (plane_id == PLANE_CURSOR) {
3996 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003997 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303998 return;
3999 }
4000
4001 val = I915_READ(PLANE_CTL(pipe, plane_id));
4002
4003 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004004 if (val & PLANE_CTL_ENABLE)
4005 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4006 val & PLANE_CTL_ORDER_RGBX,
4007 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304008
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004009 if (INTEL_GEN(dev_priv) >= 11) {
4010 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
4011 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4012 } else {
4013 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07004014 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304015
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304016 if (is_planar_yuv_format(fourcc))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004017 swap(val, val2);
4018
4019 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4020 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304021 }
4022}
4023
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004024void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4025 struct skl_ddb_entry *ddb_y,
4026 struct skl_ddb_entry *ddb_uv)
4027{
4028 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4029 enum intel_display_power_domain power_domain;
4030 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004031 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004032 enum plane_id plane_id;
4033
4034 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004035 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4036 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004037 return;
4038
4039 for_each_plane_id_on_crtc(crtc, plane_id)
4040 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4041 plane_id,
4042 &ddb_y[plane_id],
4043 &ddb_uv[plane_id]);
4044
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004045 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004046}
4047
Damien Lespiau08db6652014-11-04 17:06:52 +00004048void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
4049 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00004050{
Mahesh Kumar74bd8002018-04-26 19:55:15 +05304051 ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
Damien Lespiaua269c582014-11-04 17:06:49 +00004052}
4053
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004054/*
4055 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4056 * The bspec defines downscale amount as:
4057 *
4058 * """
4059 * Horizontal down scale amount = maximum[1, Horizontal source size /
4060 * Horizontal destination size]
4061 * Vertical down scale amount = maximum[1, Vertical source size /
4062 * Vertical destination size]
4063 * Total down scale amount = Horizontal down scale amount *
4064 * Vertical down scale amount
4065 * """
4066 *
4067 * Return value is provided in 16.16 fixed point form to retain fractional part.
4068 * Caller should take care of dividing & rounding off the value.
4069 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304070static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004071skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4072 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004073{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004074 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004075 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304076 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4077 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004078
Maarten Lankhorstec193642019-06-28 10:55:17 +02004079 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304080 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004081
4082 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004083 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004084 /*
4085 * Cursors only support 0/180 degree rotation,
4086 * hence no need to account for rotation here.
4087 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004088 src_w = plane_state->base.src_w >> 16;
4089 src_h = plane_state->base.src_h >> 16;
4090 dst_w = plane_state->base.crtc_w;
4091 dst_h = plane_state->base.crtc_h;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004092 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004093 /*
4094 * Src coordinates are already rotated by 270 degrees for
4095 * the 90/270 degree plane rotation cases (to match the
4096 * GTT mapping), hence no need to account for rotation here.
4097 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004098 src_w = drm_rect_width(&plane_state->base.src) >> 16;
4099 src_h = drm_rect_height(&plane_state->base.src) >> 16;
4100 dst_w = drm_rect_width(&plane_state->base.dst);
4101 dst_h = drm_rect_height(&plane_state->base.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004102 }
4103
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304104 fp_w_ratio = div_fixed16(src_w, dst_w);
4105 fp_h_ratio = div_fixed16(src_h, dst_h);
4106 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4107 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004108
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304109 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004110}
4111
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304112static uint_fixed_16_16_t
4113skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
4114{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304115 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304116
4117 if (!crtc_state->base.enable)
4118 return pipe_downscale;
4119
4120 if (crtc_state->pch_pfit.enabled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004121 u32 src_w, src_h, dst_w, dst_h;
4122 u32 pfit_size = crtc_state->pch_pfit.size;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304123 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4124 uint_fixed_16_16_t downscale_h, downscale_w;
4125
4126 src_w = crtc_state->pipe_src_w;
4127 src_h = crtc_state->pipe_src_h;
4128 dst_w = pfit_size >> 16;
4129 dst_h = pfit_size & 0xffff;
4130
4131 if (!dst_w || !dst_h)
4132 return pipe_downscale;
4133
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304134 fp_w_ratio = div_fixed16(src_w, dst_w);
4135 fp_h_ratio = div_fixed16(src_h, dst_h);
4136 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4137 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304138
4139 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
4140 }
4141
4142 return pipe_downscale;
4143}
4144
4145int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004146 struct intel_crtc_state *crtc_state)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304147{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004148 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004149 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304150 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004151 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004152 int crtc_clock, dotclk;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004153 u32 pipe_max_pixel_rate;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304154 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304155 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304156
Maarten Lankhorstec193642019-06-28 10:55:17 +02004157 if (!crtc_state->base.enable)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304158 return 0;
4159
Maarten Lankhorstec193642019-06-28 10:55:17 +02004160 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304161 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304162 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304163 int bpp;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004164 const struct intel_plane_state *plane_state =
4165 to_intel_plane_state(drm_plane_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304166
Maarten Lankhorstec193642019-06-28 10:55:17 +02004167 if (!intel_wm_plane_visible(crtc_state, plane_state))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304168 continue;
4169
Maarten Lankhorstec193642019-06-28 10:55:17 +02004170 if (WARN_ON(!plane_state->base.fb))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304171 return -EINVAL;
4172
Maarten Lankhorstec193642019-06-28 10:55:17 +02004173 plane_downscale = skl_plane_downscale_amount(crtc_state, plane_state);
4174 bpp = plane_state->base.fb->format->cpp[0] * 8;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304175 if (bpp == 64)
4176 plane_downscale = mul_fixed16(plane_downscale,
4177 fp_9_div_8);
4178
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304179 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304180 }
Maarten Lankhorstec193642019-06-28 10:55:17 +02004181 pipe_downscale = skl_pipe_downscale_amount(crtc_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304182
4183 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
4184
Maarten Lankhorstec193642019-06-28 10:55:17 +02004185 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004186 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
4187
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004188 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004189 dotclk *= 2;
4190
4191 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304192
4193 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004194 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304195 return -EINVAL;
4196 }
4197
4198 return 0;
4199}
4200
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004201static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004202skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4203 const struct intel_plane_state *plane_state,
Mahesh Kumarb879d582018-04-09 09:11:01 +05304204 const int plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004205{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004206 struct intel_plane *intel_plane = to_intel_plane(plane_state->base.plane);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004207 u32 data_rate;
4208 u32 width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004209 struct drm_framebuffer *fb;
4210 u32 format;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304211 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004212 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004213
Maarten Lankhorstec193642019-06-28 10:55:17 +02004214 if (!plane_state->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004215 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004216
Maarten Lankhorstec193642019-06-28 10:55:17 +02004217 fb = plane_state->base.fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004218 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02004219
Mahesh Kumarb879d582018-04-09 09:11:01 +05304220 if (intel_plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004221 return 0;
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304222 if (plane == 1 && !is_planar_yuv_format(format))
Matt Ropera1de91e2016-05-12 07:05:57 -07004223 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004224
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004225 /*
4226 * Src coordinates are already rotated by 270 degrees for
4227 * the 90/270 degree plane rotation cases (to match the
4228 * GTT mapping), hence no need to account for rotation here.
4229 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004230 width = drm_rect_width(&plane_state->base.src) >> 16;
4231 height = drm_rect_height(&plane_state->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004232
Mahesh Kumarb879d582018-04-09 09:11:01 +05304233 /* UV plane does 1/2 pixel sub-sampling */
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304234 if (plane == 1 && is_planar_yuv_format(format)) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304235 width /= 2;
4236 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004237 }
4238
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004239 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304240
Maarten Lankhorstec193642019-06-28 10:55:17 +02004241 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004242
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004243 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4244
4245 rate *= fb->format->cpp[plane];
4246 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004247}
4248
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004249static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004250skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004251 u64 *plane_data_rate,
4252 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004253{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004254 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004255 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004256 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004257 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004258
4259 if (WARN_ON(!state))
4260 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004261
Matt Ropera1de91e2016-05-12 07:05:57 -07004262 /* Calculate and cache data rate for each plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004263 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004264 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004265 const struct intel_plane_state *plane_state =
4266 to_intel_plane_state(drm_plane_state);
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004267 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004268
Mahesh Kumarb879d582018-04-09 09:11:01 +05304269 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004270 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004271 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004272 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004273
Mahesh Kumarb879d582018-04-09 09:11:01 +05304274 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004275 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304276 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004277 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004278 }
4279
4280 return total_data_rate;
4281}
4282
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004283static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004284icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004285 u64 *plane_data_rate)
4286{
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004287 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004288 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004289 u64 total_data_rate = 0;
4290
Maarten Lankhorstec193642019-06-28 10:55:17 +02004291 if (WARN_ON(!crtc_state->base.state))
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004292 return 0;
4293
4294 /* Calculate and cache data rate for each plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004295 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
4296 const struct intel_plane_state *plane_state =
4297 to_intel_plane_state(drm_plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004298 enum plane_id plane_id = to_intel_plane(plane)->id;
4299 u64 rate;
4300
Maarten Lankhorstec193642019-06-28 10:55:17 +02004301 if (!plane_state->linked_plane) {
4302 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004303 plane_data_rate[plane_id] = rate;
4304 total_data_rate += rate;
4305 } else {
4306 enum plane_id y_plane_id;
4307
4308 /*
4309 * The slave plane might not iterate in
4310 * drm_atomic_crtc_state_for_each_plane_state(),
4311 * and needs the master plane state which may be
4312 * NULL if we try get_new_plane_state(), so we
4313 * always calculate from the master.
4314 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004315 if (plane_state->slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004316 continue;
4317
4318 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004319 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
4320 y_plane_id = plane_state->linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004321 plane_data_rate[y_plane_id] = rate;
4322 total_data_rate += rate;
4323
Maarten Lankhorstec193642019-06-28 10:55:17 +02004324 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004325 plane_data_rate[plane_id] = rate;
4326 total_data_rate += rate;
4327 }
4328 }
4329
4330 return total_data_rate;
4331}
4332
Matt Roperc107acf2016-05-12 07:06:01 -07004333static int
Maarten Lankhorstec193642019-06-28 10:55:17 +02004334skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004335 struct skl_ddb_allocation *ddb /* out */)
4336{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004337 struct drm_atomic_state *state = crtc_state->base.state;
4338 struct drm_crtc *crtc = crtc_state->base.crtc;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004339 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004341 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004342 u16 alloc_size, start = 0;
4343 u16 total[I915_MAX_PLANES] = {};
4344 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004345 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004346 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004347 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004348 u64 plane_data_rate[I915_MAX_PLANES] = {};
4349 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004350 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004351 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004352
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004353 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004354 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4355 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004356
Matt Ropera6d3460e2016-05-12 07:06:04 -07004357 if (WARN_ON(!state))
4358 return 0;
4359
Maarten Lankhorstec193642019-06-28 10:55:17 +02004360 if (!crtc_state->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004361 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004362 return 0;
4363 }
4364
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004365 if (INTEL_GEN(dev_priv) >= 11)
4366 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004367 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004368 plane_data_rate);
4369 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004370 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004371 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004372 plane_data_rate,
4373 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004374
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004375
Maarten Lankhorstec193642019-06-28 10:55:17 +02004376 skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004377 ddb, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004378 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304379 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004380 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004381
Matt Roperd8e87492018-12-11 09:31:07 -08004382 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004383 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004384 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004385 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004386 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004387 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004388
Matt Ropera1de91e2016-05-12 07:05:57 -07004389 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004390 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004391
Matt Roperd8e87492018-12-11 09:31:07 -08004392 /*
4393 * Find the highest watermark level for which we can satisfy the block
4394 * requirement of active planes.
4395 */
4396 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004397 blocks = 0;
Matt Roperd8e87492018-12-11 09:31:07 -08004398 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004399 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004400 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004401
4402 if (plane_id == PLANE_CURSOR) {
4403 if (WARN_ON(wm->wm[level].min_ddb_alloc >
4404 total[PLANE_CURSOR])) {
4405 blocks = U32_MAX;
4406 break;
4407 }
4408 continue;
4409 }
4410
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004411 blocks += wm->wm[level].min_ddb_alloc;
4412 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004413 }
4414
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004415 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004416 alloc_size -= blocks;
4417 break;
4418 }
4419 }
4420
4421 if (level < 0) {
4422 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4423 DRM_DEBUG_KMS("minimum required %d/%d\n", blocks,
4424 alloc_size);
4425 return -EINVAL;
4426 }
4427
4428 /*
4429 * Grant each plane the blocks it requires at the highest achievable
4430 * watermark level, plus an extra share of the leftover blocks
4431 * proportional to its relative data rate.
4432 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004433 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004434 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004435 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004436 u64 rate;
4437 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004438
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004439 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004440 continue;
4441
Damien Lespiaub9cec072014-11-04 17:06:43 +00004442 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004443 * We've accounted for all active planes; remaining planes are
4444 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004445 */
Matt Roperd8e87492018-12-11 09:31:07 -08004446 if (total_data_rate == 0)
4447 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004448
Matt Roperd8e87492018-12-11 09:31:07 -08004449 rate = plane_data_rate[plane_id];
4450 extra = min_t(u16, alloc_size,
4451 DIV64_U64_ROUND_UP(alloc_size * rate,
4452 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004453 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004454 alloc_size -= extra;
4455 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004456
Matt Roperd8e87492018-12-11 09:31:07 -08004457 if (total_data_rate == 0)
4458 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004459
Matt Roperd8e87492018-12-11 09:31:07 -08004460 rate = uv_plane_data_rate[plane_id];
4461 extra = min_t(u16, alloc_size,
4462 DIV64_U64_ROUND_UP(alloc_size * rate,
4463 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004464 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004465 alloc_size -= extra;
4466 total_data_rate -= rate;
4467 }
4468 WARN_ON(alloc_size != 0 || total_data_rate != 0);
4469
4470 /* Set the actual DDB start/end points for each plane */
4471 start = alloc->start;
4472 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004473 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004474 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004475 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004476 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004477
4478 if (plane_id == PLANE_CURSOR)
4479 continue;
4480
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004481 /* Gen11+ uses a separate plane for UV watermarks */
Matt Roperd8e87492018-12-11 09:31:07 -08004482 WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004483
Matt Roperd8e87492018-12-11 09:31:07 -08004484 /* Leave disabled planes at (0,0) */
4485 if (total[plane_id]) {
4486 plane_alloc->start = start;
4487 start += total[plane_id];
4488 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004489 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004490
Matt Roperd8e87492018-12-11 09:31:07 -08004491 if (uv_total[plane_id]) {
4492 uv_plane_alloc->start = start;
4493 start += uv_total[plane_id];
4494 uv_plane_alloc->end = start;
4495 }
4496 }
4497
4498 /*
4499 * When we calculated watermark values we didn't know how high
4500 * of a level we'd actually be able to hit, so we just marked
4501 * all levels as "enabled." Go back now and disable the ones
4502 * that aren't actually possible.
4503 */
4504 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
4505 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004506 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004507 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004508
4509 /*
4510 * We only disable the watermarks for each plane if
4511 * they exceed the ddb allocation of said plane. This
4512 * is done so that we don't end up touching cursor
4513 * watermarks needlessly when some other plane reduces
4514 * our max possible watermark level.
4515 *
4516 * Bspec has this to say about the PLANE_WM enable bit:
4517 * "All the watermarks at this level for all enabled
4518 * planes must be enabled before the level will be used."
4519 * So this is actually safe to do.
4520 */
4521 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
4522 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
4523 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02004524
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004525 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004526 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004527 * Underruns with WM1+ disabled
4528 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07004529 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02004530 level == 1 && wm->wm[0].plane_en) {
4531 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004532 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
4533 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004534 }
Matt Roperd8e87492018-12-11 09:31:07 -08004535 }
4536 }
4537
4538 /*
4539 * Go back and disable the transition watermark if it turns out we
4540 * don't have enough DDB blocks for it.
4541 */
4542 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004543 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004544 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004545
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02004546 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08004547 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00004548 }
4549
Matt Roperc107acf2016-05-12 07:06:01 -07004550 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004551}
4552
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004553/*
4554 * 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 +02004555 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004556 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4557 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4558*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004559static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004560skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
4561 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004562{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004563 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304564 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004565
4566 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304567 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004568
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304569 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004570 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004571
4572 if (INTEL_GEN(dev_priv) >= 10)
4573 ret = add_fixed16_u32(ret, 1);
4574
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004575 return ret;
4576}
4577
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004578static uint_fixed_16_16_t
4579skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
4580 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004581{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004582 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304583 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004584
4585 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304586 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004587
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004588 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304589 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4590 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304591 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004592 return ret;
4593}
4594
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304595static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004596intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304597{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004598 u32 pixel_rate;
4599 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304600 uint_fixed_16_16_t linetime_us;
4601
Maarten Lankhorstec193642019-06-28 10:55:17 +02004602 if (!crtc_state->base.active)
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 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304606
4607 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304608 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304609
Maarten Lankhorstec193642019-06-28 10:55:17 +02004610 crtc_htotal = crtc_state->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304611 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304612
4613 return linetime_us;
4614}
4615
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004616static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004617skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
4618 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004619{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004620 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304621 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004622
4623 /* Shouldn't reach here on disabled planes... */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004624 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004625 return 0;
4626
4627 /*
4628 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4629 * with additional adjustments for plane-specific scaling.
4630 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004631 adjusted_pixel_rate = crtc_state->pixel_rate;
4632 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004633
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304634 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4635 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004636}
4637
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304638static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004639skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4640 int width, const struct drm_format_info *format,
4641 u64 modifier, unsigned int rotation,
4642 u32 plane_pixel_rate, struct skl_wm_params *wp,
4643 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304644{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004645 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4646 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004647 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304648
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304649 /* only planar format has two planes */
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004650 if (color_plane == 1 && !is_planar_yuv_format(format->format)) {
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304651 DRM_DEBUG_KMS("Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304652 return -EINVAL;
4653 }
4654
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004655 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
4656 modifier == I915_FORMAT_MOD_Yf_TILED ||
4657 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4658 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4659 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
4660 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4661 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4662 wp->is_planar = is_planar_yuv_format(format->format);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304663
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004664 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02004665 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304666 wp->width /= 2;
4667
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004668 wp->cpp = format->cpp[color_plane];
4669 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304670
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004671 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004672 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004673 wp->dbuf_block_size = 256;
4674 else
4675 wp->dbuf_block_size = 512;
4676
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004677 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304678 switch (wp->cpp) {
4679 case 1:
4680 wp->y_min_scanlines = 16;
4681 break;
4682 case 2:
4683 wp->y_min_scanlines = 8;
4684 break;
4685 case 4:
4686 wp->y_min_scanlines = 4;
4687 break;
4688 default:
4689 MISSING_CASE(wp->cpp);
4690 return -EINVAL;
4691 }
4692 } else {
4693 wp->y_min_scanlines = 4;
4694 }
4695
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004696 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304697 wp->y_min_scanlines *= 2;
4698
4699 wp->plane_bytes_per_line = wp->width * wp->cpp;
4700 if (wp->y_tiled) {
4701 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004702 wp->y_min_scanlines,
4703 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304704
4705 if (INTEL_GEN(dev_priv) >= 10)
4706 interm_pbpl++;
4707
4708 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4709 wp->y_min_scanlines);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004710 } else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004711 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4712 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304713 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4714 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004715 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4716 wp->dbuf_block_size) + 1;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304717 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4718 }
4719
4720 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4721 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004722
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304723 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004724 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304725
4726 return 0;
4727}
4728
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004729static int
4730skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
4731 const struct intel_plane_state *plane_state,
4732 struct skl_wm_params *wp, int color_plane)
4733{
4734 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4735 const struct drm_framebuffer *fb = plane_state->base.fb;
4736 int width;
4737
4738 if (plane->id == PLANE_CURSOR) {
4739 width = plane_state->base.crtc_w;
4740 } else {
4741 /*
4742 * Src coordinates are already rotated by 270 degrees for
4743 * the 90/270 degree plane rotation cases (to match the
4744 * GTT mapping), hence no need to account for rotation here.
4745 */
4746 width = drm_rect_width(&plane_state->base.src) >> 16;
4747 }
4748
4749 return skl_compute_wm_params(crtc_state, width,
4750 fb->format, fb->modifier,
4751 plane_state->base.rotation,
4752 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
4753 wp, color_plane);
4754}
4755
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004756static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
4757{
4758 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4759 return true;
4760
4761 /* The number of lines are ignored for the level 0 watermark. */
4762 return level > 0;
4763}
4764
Maarten Lankhorstec193642019-06-28 10:55:17 +02004765static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08004766 int level,
4767 const struct skl_wm_params *wp,
4768 const struct skl_wm_level *result_prev,
4769 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004770{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004771 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004772 u32 latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304773 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304774 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004775 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02004776
Ville Syrjälä0aded172019-02-05 17:50:53 +02004777 if (latency == 0) {
4778 /* reject it */
4779 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02004780 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004781 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02004782
Ville Syrjälä25312ef2019-05-03 20:38:05 +03004783 /*
4784 * WaIncreaseLatencyIPCEnabled: kbl,cfl
4785 * Display WA #1141: kbl,cfl
4786 */
Ville Syrjälä5a7d2022019-05-03 20:38:06 +03004787 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004788 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304789 latency += 4;
4790
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004791 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004792 latency += 15;
4793
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304794 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004795 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304796 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004797 crtc_state->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004798 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304799 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004800
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304801 if (wp->y_tiled) {
4802 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004803 } else {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004804 if ((wp->cpp * crtc_state->base.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004805 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004806 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004807 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004808 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004809 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004810 !IS_GEMINILAKE(dev_priv))
4811 selected_result = min_fixed16(method1, method2);
4812 else
4813 selected_result = method2;
4814 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004815 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004816 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004817 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004818
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304819 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304820 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304821 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004822
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004823 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
4824 /* Display WA #1125: skl,bxt,kbl */
4825 if (level == 0 && wp->rc_surface)
4826 res_blocks +=
4827 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004828
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004829 /* Display WA #1126: skl,bxt,kbl */
4830 if (level >= 1 && level <= 7) {
4831 if (wp->y_tiled) {
4832 res_blocks +=
4833 fixed16_to_u32_round_up(wp->y_tile_minimum);
4834 res_lines += wp->y_min_scanlines;
4835 } else {
4836 res_blocks++;
4837 }
4838
4839 /*
4840 * Make sure result blocks for higher latency levels are
4841 * atleast as high as level below the current level.
4842 * Assumption in DDB algorithm optimization for special
4843 * cases. Also covers Display WA #1125 for RC.
4844 */
4845 if (result_prev->plane_res_b > res_blocks)
4846 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004847 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004848 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004849
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004850 if (INTEL_GEN(dev_priv) >= 11) {
4851 if (wp->y_tiled) {
4852 int extra_lines;
4853
4854 if (res_lines % wp->y_min_scanlines == 0)
4855 extra_lines = wp->y_min_scanlines;
4856 else
4857 extra_lines = wp->y_min_scanlines * 2 -
4858 res_lines % wp->y_min_scanlines;
4859
4860 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
4861 wp->plane_blocks_per_line);
4862 } else {
4863 min_ddb_alloc = res_blocks +
4864 DIV_ROUND_UP(res_blocks, 10);
4865 }
4866 }
4867
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004868 if (!skl_wm_has_lines(dev_priv, level))
4869 res_lines = 0;
4870
Ville Syrjälä0aded172019-02-05 17:50:53 +02004871 if (res_lines > 31) {
4872 /* reject it */
4873 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08004874 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004875 }
Matt Roperd8e87492018-12-11 09:31:07 -08004876
4877 /*
4878 * If res_lines is valid, assume we can use this watermark level
4879 * for now. We'll come back and disable it after we calculate the
4880 * DDB allocation if it turns out we don't actually have enough
4881 * blocks to satisfy it.
4882 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05304883 result->plane_res_b = res_blocks;
4884 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004885 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
4886 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05304887 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004888}
4889
Matt Roperd8e87492018-12-11 09:31:07 -08004890static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02004891skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304892 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004893 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004894{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004895 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304896 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004897 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04004898
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304899 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004900 struct skl_wm_level *result = &levels[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304901
Maarten Lankhorstec193642019-06-28 10:55:17 +02004902 skl_compute_plane_wm(crtc_state, level, wm_params,
Matt Roperd8e87492018-12-11 09:31:07 -08004903 result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004904
4905 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304906 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004907}
4908
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004909static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004910skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004911{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004912 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304913 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304914 uint_fixed_16_16_t linetime_us;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004915 u32 linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004916
Maarten Lankhorstec193642019-06-28 10:55:17 +02004917 linetime_us = intel_get_linetime_us(crtc_state);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304918 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304919
Ville Syrjälä717671c2018-12-21 19:14:36 +02004920 /* Display WA #1135: BXT:ALL GLK:ALL */
4921 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304922 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304923
4924 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004925}
4926
Maarten Lankhorstec193642019-06-28 10:55:17 +02004927static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004928 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08004929 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004930{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004931 struct drm_device *dev = crtc_state->base.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05304932 const struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004933 u16 trans_min, trans_y_tile_min;
4934 const u16 trans_amount = 10; /* This is configurable amount */
4935 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004936
Kumar, Maheshca476672017-08-17 19:15:24 +05304937 /* Transition WM are not recommended by HW team for GEN9 */
4938 if (INTEL_GEN(dev_priv) <= 9)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004939 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304940
4941 /* Transition WM don't make any sense if ipc is disabled */
4942 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004943 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304944
Paulo Zanoni91961a82018-10-04 16:15:56 -07004945 trans_min = 14;
4946 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05304947 trans_min = 4;
4948
4949 trans_offset_b = trans_min + trans_amount;
4950
Paulo Zanonicbacc792018-10-04 16:15:58 -07004951 /*
4952 * The spec asks for Selected Result Blocks for wm0 (the real value),
4953 * not Result Blocks (the integer value). Pay attention to the capital
4954 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
4955 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
4956 * and since we later will have to get the ceiling of the sum in the
4957 * transition watermarks calculation, we can just pretend Selected
4958 * Result Blocks is Result Blocks minus 1 and it should work for the
4959 * current platforms.
4960 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004961 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07004962
Kumar, Maheshca476672017-08-17 19:15:24 +05304963 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004964 trans_y_tile_min =
4965 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07004966 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05304967 trans_offset_b;
4968 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07004969 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05304970
4971 /* WA BUG:1938466 add one block for non y-tile planes */
4972 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4973 res_blocks += 1;
4974
4975 }
4976
Matt Roperd8e87492018-12-11 09:31:07 -08004977 /*
4978 * Just assume we can enable the transition watermark. After
4979 * computing the DDB we'll come back and disable it if that
4980 * assumption turns out to be false.
4981 */
4982 wm->trans_wm.plane_res_b = res_blocks + 1;
4983 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004984}
4985
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004986static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004987 const struct intel_plane_state *plane_state,
4988 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004989{
Ville Syrjälä83158472018-11-27 18:57:26 +02004990 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004991 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004992 int ret;
4993
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004994 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004995 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004996 if (ret)
4997 return ret;
4998
Ville Syrjälä67155a62019-03-12 22:58:37 +02004999 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Matt Roperd8e87492018-12-11 09:31:07 -08005000 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005001
5002 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005003}
5004
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005005static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005006 const struct intel_plane_state *plane_state,
5007 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005008{
Ville Syrjälä83158472018-11-27 18:57:26 +02005009 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
5010 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005011 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005012
Ville Syrjälä83158472018-11-27 18:57:26 +02005013 wm->is_planar = true;
5014
5015 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005016 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005017 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005018 if (ret)
5019 return ret;
5020
Ville Syrjälä67155a62019-03-12 22:58:37 +02005021 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005022
5023 return 0;
5024}
5025
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005026static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005027 const struct intel_plane_state *plane_state)
5028{
5029 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
5030 const struct drm_framebuffer *fb = plane_state->base.fb;
5031 enum plane_id plane_id = plane->id;
5032 int ret;
5033
5034 if (!intel_wm_plane_visible(crtc_state, plane_state))
5035 return 0;
5036
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005037 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005038 plane_id, 0);
5039 if (ret)
5040 return ret;
5041
5042 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005043 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005044 plane_id);
5045 if (ret)
5046 return ret;
5047 }
5048
5049 return 0;
5050}
5051
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005052static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005053 const struct intel_plane_state *plane_state)
5054{
5055 enum plane_id plane_id = to_intel_plane(plane_state->base.plane)->id;
5056 int ret;
5057
5058 /* Watermarks calculated in master */
5059 if (plane_state->slave)
5060 return 0;
5061
5062 if (plane_state->linked_plane) {
5063 const struct drm_framebuffer *fb = plane_state->base.fb;
5064 enum plane_id y_plane_id = plane_state->linked_plane->id;
5065
5066 WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
5067 WARN_ON(!fb->format->is_yuv ||
5068 fb->format->num_planes == 1);
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 y_plane_id, 0);
5072 if (ret)
5073 return ret;
5074
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, 1);
5077 if (ret)
5078 return ret;
5079 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005080 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005081 plane_id, 0);
5082 if (ret)
5083 return ret;
5084 }
5085
5086 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005087}
5088
Maarten Lankhorstec193642019-06-28 10:55:17 +02005089static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005090{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005091 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
5092 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305093 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005094 const struct drm_plane_state *drm_plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005095 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005096
Lyudea62163e2016-10-04 14:28:20 -04005097 /*
5098 * We'll only calculate watermarks for planes that are actually
5099 * enabled, so make sure all other planes are set as disabled.
5100 */
5101 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5102
Maarten Lankhorstec193642019-06-28 10:55:17 +02005103 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state,
5104 &crtc_state->base) {
5105 const struct intel_plane_state *plane_state =
5106 to_intel_plane_state(drm_plane_state);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305107
Ville Syrjälä83158472018-11-27 18:57:26 +02005108 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005109 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005110 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005111 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305112 if (ret)
5113 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005114 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305115
Maarten Lankhorstec193642019-06-28 10:55:17 +02005116 pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005117
Matt Roper55994c22016-05-12 07:06:08 -07005118 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005119}
5120
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005121static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5122 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005123 const struct skl_ddb_entry *entry)
5124{
5125 if (entry->end)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005126 I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005127 else
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005128 I915_WRITE_FW(reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005129}
5130
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005131static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5132 i915_reg_t reg,
5133 const struct skl_wm_level *level)
5134{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005135 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005136
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005137 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005138 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005139 if (level->ignore_lines)
5140 val |= PLANE_WM_IGNORE_LINES;
5141 val |= level->plane_res_b;
5142 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005143
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005144 I915_WRITE_FW(reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005145}
5146
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005147void skl_write_plane_wm(struct intel_plane *plane,
5148 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005149{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005150 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005151 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005152 enum plane_id plane_id = plane->id;
5153 enum pipe pipe = plane->pipe;
5154 const struct skl_plane_wm *wm =
5155 &crtc_state->wm.skl.optimal.planes[plane_id];
5156 const struct skl_ddb_entry *ddb_y =
5157 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5158 const struct skl_ddb_entry *ddb_uv =
5159 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005160
5161 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005162 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005163 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005164 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005165 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005166 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005167
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005168 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005169 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005170 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5171 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305172 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005173
5174 if (wm->is_planar)
5175 swap(ddb_y, ddb_uv);
5176
5177 skl_ddb_entry_write(dev_priv,
5178 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5179 skl_ddb_entry_write(dev_priv,
5180 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005181}
5182
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005183void skl_write_cursor_wm(struct intel_plane *plane,
5184 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005185{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005186 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005187 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005188 enum plane_id plane_id = plane->id;
5189 enum pipe pipe = plane->pipe;
5190 const struct skl_plane_wm *wm =
5191 &crtc_state->wm.skl.optimal.planes[plane_id];
5192 const struct skl_ddb_entry *ddb =
5193 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005194
5195 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005196 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5197 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005198 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005199 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005200
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005201 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005202}
5203
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005204bool skl_wm_level_equals(const struct skl_wm_level *l1,
5205 const struct skl_wm_level *l2)
5206{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005207 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005208 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005209 l1->plane_res_l == l2->plane_res_l &&
5210 l1->plane_res_b == l2->plane_res_b;
5211}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005212
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005213static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5214 const struct skl_plane_wm *wm1,
5215 const struct skl_plane_wm *wm2)
5216{
5217 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005218
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005219 for (level = 0; level <= max_level; level++) {
5220 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
5221 !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
5222 return false;
5223 }
5224
5225 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005226}
5227
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005228static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
5229 const struct skl_pipe_wm *wm1,
5230 const struct skl_pipe_wm *wm2)
5231{
5232 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5233 enum plane_id plane_id;
5234
5235 for_each_plane_id_on_crtc(crtc, plane_id) {
5236 if (!skl_plane_wm_equals(dev_priv,
5237 &wm1->planes[plane_id],
5238 &wm2->planes[plane_id]))
5239 return false;
5240 }
5241
5242 return wm1->linetime == wm2->linetime;
5243}
5244
Lyude27082492016-08-24 07:48:10 +02005245static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5246 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005247{
Lyude27082492016-08-24 07:48:10 +02005248 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005249}
5250
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005251bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005252 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005253 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005254{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005255 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005256
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005257 for (i = 0; i < num_entries; i++) {
5258 if (i != ignore_idx &&
5259 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005260 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005261 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005262
Lyude27082492016-08-24 07:48:10 +02005263 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005264}
5265
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005266static u32
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005267pipes_modified(struct intel_atomic_state *state)
Matt Roper9b613022016-06-27 16:42:44 -07005268{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005269 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005270 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005271 u32 i, ret = 0;
Matt Roper9b613022016-06-27 16:42:44 -07005272
Maarten Lankhorstec193642019-06-28 10:55:17 +02005273 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005274 ret |= drm_crtc_mask(&crtc->base);
Matt Roper9b613022016-06-27 16:42:44 -07005275
5276 return ret;
5277}
5278
Jani Nikulabb7791b2016-10-04 12:29:17 +03005279static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005280skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5281 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005282{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005283 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->base.state);
5284 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
5285 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5286 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005287
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005288 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5289 struct intel_plane_state *plane_state;
5290 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005291
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005292 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5293 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5294 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5295 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005296 continue;
5297
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005298 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005299 if (IS_ERR(plane_state))
5300 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005301
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005302 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005303 }
5304
5305 return 0;
5306}
5307
5308static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005309skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005310{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005311 const struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5312 struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005313 struct intel_crtc_state *old_crtc_state;
5314 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305315 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305316 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005317
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005318 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
5319
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005320 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005321 new_crtc_state, i) {
5322 ret = skl_allocate_pipe_ddb(new_crtc_state, ddb);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005323 if (ret)
5324 return ret;
5325
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005326 ret = skl_ddb_add_affected_planes(old_crtc_state,
5327 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005328 if (ret)
5329 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005330 }
5331
5332 return 0;
5333}
5334
Ville Syrjäläab98e942019-02-08 22:05:27 +02005335static char enast(bool enable)
5336{
5337 return enable ? '*' : ' ';
5338}
5339
Matt Roper2722efb2016-08-17 15:55:55 -04005340static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005341skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005342{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005343 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5344 const struct intel_crtc_state *old_crtc_state;
5345 const struct intel_crtc_state *new_crtc_state;
5346 struct intel_plane *plane;
5347 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005348 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005349
Ville Syrjäläab98e942019-02-08 22:05:27 +02005350 if ((drm_debug & DRM_UT_KMS) == 0)
5351 return;
5352
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005353 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5354 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005355 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5356
5357 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5358 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5359
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005360 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5361 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005362 const struct skl_ddb_entry *old, *new;
5363
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005364 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5365 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005366
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005367 if (skl_ddb_entry_equal(old, new))
5368 continue;
5369
Ville Syrjäläab98e942019-02-08 22:05:27 +02005370 DRM_DEBUG_KMS("[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005371 plane->base.base.id, plane->base.name,
Ville Syrjäläab98e942019-02-08 22:05:27 +02005372 old->start, old->end, new->start, new->end,
5373 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
5374 }
5375
5376 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5377 enum plane_id plane_id = plane->id;
5378 const struct skl_plane_wm *old_wm, *new_wm;
5379
5380 old_wm = &old_pipe_wm->planes[plane_id];
5381 new_wm = &new_pipe_wm->planes[plane_id];
5382
5383 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5384 continue;
5385
5386 DRM_DEBUG_KMS("[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm"
5387 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm\n",
5388 plane->base.base.id, plane->base.name,
5389 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5390 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5391 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5392 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5393 enast(old_wm->trans_wm.plane_en),
5394 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5395 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5396 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5397 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
5398 enast(new_wm->trans_wm.plane_en));
5399
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005400 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"
5401 " -> %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 +02005402 plane->base.base.id, plane->base.name,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005403 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5404 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5405 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5406 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5407 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5408 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5409 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5410 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5411 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
5412
5413 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5414 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5415 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5416 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5417 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5418 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5419 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5420 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
5421 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005422
5423 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5424 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5425 plane->base.base.id, plane->base.name,
5426 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5427 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5428 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5429 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5430 old_wm->trans_wm.plane_res_b,
5431 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5432 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5433 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5434 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
5435 new_wm->trans_wm.plane_res_b);
5436
5437 DRM_DEBUG_KMS("[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5438 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5439 plane->base.base.id, plane->base.name,
5440 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5441 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5442 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5443 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5444 old_wm->trans_wm.min_ddb_alloc,
5445 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5446 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5447 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5448 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
5449 new_wm->trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005450 }
5451 }
5452}
5453
Matt Roper98d39492016-05-12 07:06:03 -07005454static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005455skl_ddb_add_affected_pipes(struct intel_atomic_state *state, bool *changed)
Matt Roper98d39492016-05-12 07:06:03 -07005456{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005457 struct drm_device *dev = state->base.dev;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305458 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005459 struct intel_crtc *crtc;
5460 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005461 u32 realloc_pipes = pipes_modified(state);
Matt Roper734fa012016-05-12 15:11:40 -07005462 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005463
5464 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005465 * When we distrust bios wm we always need to recompute to set the
5466 * expected DDB allocations for each CRTC.
5467 */
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305468 if (dev_priv->wm.distrust_bios_wm)
5469 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005470
5471 /*
Matt Roper98d39492016-05-12 07:06:03 -07005472 * If this transaction isn't actually touching any CRTC's, don't
5473 * bother with watermark calculation. Note that if we pass this
5474 * test, we're guaranteed to hold at least one CRTC state mutex,
5475 * which means we can safely use values like dev_priv->active_crtcs
5476 * since any racing commits that want to update them would need to
5477 * hold _all_ CRTC state mutexes.
5478 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005479 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305480 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005481
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305482 if (!*changed)
Matt Roper98d39492016-05-12 07:06:03 -07005483 return 0;
5484
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305485 /*
5486 * If this is our first atomic update following hardware readout,
5487 * we can't trust the DDB that the BIOS programmed for us. Let's
5488 * pretend that all pipes switched active status so that we'll
5489 * ensure a full DDB recompute.
5490 */
5491 if (dev_priv->wm.distrust_bios_wm) {
5492 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005493 state->base.acquire_ctx);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305494 if (ret)
5495 return ret;
5496
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005497 state->active_pipe_changes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305498
5499 /*
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005500 * We usually only initialize state->active_crtcs if we
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305501 * we're doing a modeset; make sure this field is always
5502 * initialized during the sanitization process that happens
5503 * on the first commit too.
5504 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005505 if (!state->modeset)
5506 state->active_crtcs = dev_priv->active_crtcs;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305507 }
5508
5509 /*
5510 * If the modeset changes which CRTC's are active, we need to
5511 * recompute the DDB allocation for *all* active pipes, even
5512 * those that weren't otherwise being modified in any way by this
5513 * atomic commit. Due to the shrinking of the per-pipe allocations
5514 * when new active CRTC's are added, it's possible for a pipe that
5515 * we were already using and aren't changing at all here to suddenly
5516 * become invalid if its DDB needs exceeds its new allocation.
5517 *
5518 * Note that if we wind up doing a full DDB recompute, we can't let
5519 * any other display updates race with this transaction, so we need
5520 * to grab the lock on *all* CRTC's.
5521 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005522 if (state->active_pipe_changes || state->modeset) {
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305523 realloc_pipes = ~0;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005524 state->wm_results.dirty_pipes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305525 }
5526
5527 /*
5528 * We're not recomputing for the pipes not included in the commit, so
5529 * make sure we start with the current state.
5530 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005531 for_each_intel_crtc_mask(dev, crtc, realloc_pipes) {
5532 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5533 if (IS_ERR(crtc_state))
5534 return PTR_ERR(crtc_state);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305535 }
5536
5537 return 0;
5538}
5539
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005540/*
5541 * To make sure the cursor watermark registers are always consistent
5542 * with our computed state the following scenario needs special
5543 * treatment:
5544 *
5545 * 1. enable cursor
5546 * 2. move cursor entirely offscreen
5547 * 3. disable cursor
5548 *
5549 * Step 2. does call .disable_plane() but does not zero the watermarks
5550 * (since we consider an offscreen cursor still active for the purposes
5551 * of watermarks). Step 3. would not normally call .disable_plane()
5552 * because the actual plane visibility isn't changing, and we don't
5553 * deallocate the cursor ddb until the pipe gets disabled. So we must
5554 * force step 3. to call .disable_plane() to update the watermark
5555 * registers properly.
5556 *
5557 * Other planes do not suffer from this issues as their watermarks are
5558 * calculated based on the actual plane visibility. The only time this
5559 * can trigger for the other planes is during the initial readout as the
5560 * default value of the watermarks registers is not zero.
5561 */
5562static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
5563 struct intel_crtc *crtc)
5564{
5565 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5566 const struct intel_crtc_state *old_crtc_state =
5567 intel_atomic_get_old_crtc_state(state, crtc);
5568 struct intel_crtc_state *new_crtc_state =
5569 intel_atomic_get_new_crtc_state(state, crtc);
5570 struct intel_plane *plane;
5571
5572 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5573 struct intel_plane_state *plane_state;
5574 enum plane_id plane_id = plane->id;
5575
5576 /*
5577 * Force a full wm update for every plane on modeset.
5578 * Required because the reset value of the wm registers
5579 * is non-zero, whereas we want all disabled planes to
5580 * have zero watermarks. So if we turn off the relevant
5581 * power well the hardware state will go out of sync
5582 * with the software state.
5583 */
5584 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) &&
5585 skl_plane_wm_equals(dev_priv,
5586 &old_crtc_state->wm.skl.optimal.planes[plane_id],
5587 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
5588 continue;
5589
5590 plane_state = intel_atomic_get_plane_state(state, plane);
5591 if (IS_ERR(plane_state))
5592 return PTR_ERR(plane_state);
5593
5594 new_crtc_state->update_planes |= BIT(plane_id);
5595 }
5596
5597 return 0;
5598}
5599
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305600static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005601skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305602{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005603 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005604 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005605 struct intel_crtc_state *old_crtc_state;
5606 struct skl_ddb_values *results = &state->wm_results;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305607 bool changed = false;
5608 int ret, i;
5609
Matt Roper734fa012016-05-12 15:11:40 -07005610 /* Clear all dirty flags */
5611 results->dirty_pipes = 0;
5612
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305613 ret = skl_ddb_add_affected_pipes(state, &changed);
5614 if (ret || !changed)
5615 return ret;
5616
Matt Roper734fa012016-05-12 15:11:40 -07005617 /*
5618 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08005619 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Matt Roper734fa012016-05-12 15:11:40 -07005620 * weren't otherwise being modified (and set bits in dirty_pipes) if
5621 * pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07005622 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005623 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005624 new_crtc_state, i) {
5625 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005626 if (ret)
5627 return ret;
5628
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005629 ret = skl_wm_add_affected_planes(state, crtc);
Matt Roper734fa012016-05-12 15:11:40 -07005630 if (ret)
5631 return ret;
5632
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005633 if (!skl_pipe_wm_equals(crtc,
5634 &old_crtc_state->wm.skl.optimal,
5635 &new_crtc_state->wm.skl.optimal))
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005636 results->dirty_pipes |= drm_crtc_mask(&crtc->base);
Matt Roper734fa012016-05-12 15:11:40 -07005637 }
5638
Matt Roperd8e87492018-12-11 09:31:07 -08005639 ret = skl_compute_ddb(state);
5640 if (ret)
5641 return ret;
5642
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005643 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005644
Matt Roper98d39492016-05-12 07:06:03 -07005645 return 0;
5646}
5647
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005648static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005649 struct intel_crtc_state *crtc_state)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005650{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005651 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005652 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005653 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005654 enum pipe pipe = crtc->pipe;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005655
5656 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5657 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005658
5659 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
5660}
5661
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005662static void skl_initial_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005663 struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005664{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005666 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005667 struct drm_i915_private *dev_priv = to_i915(dev);
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305668 struct skl_ddb_values *results = &state->wm_results;
Bob Paauweadda50b2015-07-21 10:42:53 -07005669
Ville Syrjälä432081b2016-10-31 22:37:03 +02005670 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005671 return;
5672
Matt Roper734fa012016-05-12 15:11:40 -07005673 mutex_lock(&dev_priv->wm.wm_mutex);
5674
Maarten Lankhorstec193642019-06-28 10:55:17 +02005675 if (crtc_state->base.active_changed)
5676 skl_atomic_update_crtc_wm(state, crtc_state);
Lyude27082492016-08-24 07:48:10 +02005677
Matt Roper734fa012016-05-12 15:11:40 -07005678 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005679}
5680
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005681static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02005682 struct intel_wm_config *config)
5683{
5684 struct intel_crtc *crtc;
5685
5686 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005687 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02005688 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5689
5690 if (!wm->pipe_enabled)
5691 continue;
5692
5693 config->sprites_enabled |= wm->sprites_enabled;
5694 config->sprites_scaled |= wm->sprites_scaled;
5695 config->num_pipes_active++;
5696 }
5697}
5698
Matt Ropered4a6a72016-02-23 17:20:13 -08005699static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005700{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005701 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005702 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005703 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005704 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005705 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005706
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005707 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02005708
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005709 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
5710 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005711
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005712 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005713 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005714 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005715 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
5716 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005717
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005718 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005719 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005720 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005721 }
5722
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005723 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005724 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005725
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005726 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005727
Imre Deak820c1982013-12-17 14:46:36 +02005728 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005729}
5730
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005731static void ilk_initial_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005732 struct intel_crtc_state *crtc_state)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005733{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005734 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005735 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005736
Matt Ropered4a6a72016-02-23 17:20:13 -08005737 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005738 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005739 ilk_program_watermarks(dev_priv);
5740 mutex_unlock(&dev_priv->wm.wm_mutex);
5741}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005742
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005743static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005744 struct intel_crtc_state *crtc_state)
Matt Ropered4a6a72016-02-23 17:20:13 -08005745{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005746 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005747 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5748
5749 if (!crtc_state->wm.need_postvbl_update)
5750 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08005751
5752 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005753 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
5754 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08005755 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005756}
5757
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005758static inline void skl_wm_level_from_reg_val(u32 val,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005759 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005760{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005761 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005762 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005763 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5764 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5765 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005766}
5767
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005768void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005769 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005770{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005771 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5772 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005773 int level, max_level;
5774 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005775 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005776
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005777 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005778
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005779 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005780 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005781
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005782 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005783 if (plane_id != PLANE_CURSOR)
5784 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005785 else
5786 val = I915_READ(CUR_WM(pipe, level));
5787
5788 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5789 }
5790
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005791 if (plane_id != PLANE_CURSOR)
5792 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005793 else
5794 val = I915_READ(CUR_WM_TRANS(pipe));
5795
5796 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5797 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005798
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005799 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005800 return;
5801
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005802 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005803}
5804
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005805void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00005806{
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305807 struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005808 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005809 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005810 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00005811
Damien Lespiaua269c582014-11-04 17:06:49 +00005812 skl_ddb_get_hw_state(dev_priv, ddb);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005813 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02005814 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005815
Maarten Lankhorstec193642019-06-28 10:55:17 +02005816 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005817
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005818 if (crtc->active)
5819 hw->dirty_pipes |= drm_crtc_mask(&crtc->base);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005820 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005821
Matt Roper279e99d2016-05-12 07:06:02 -07005822 if (dev_priv->active_crtcs) {
5823 /* Fully recompute DDB on first atomic commit */
5824 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07005825 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005826}
5827
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005828static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005829{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005830 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005831 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005832 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005833 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
5834 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005835 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005836 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005837 [PIPE_A] = WM0_PIPEA_ILK,
5838 [PIPE_B] = WM0_PIPEB_ILK,
5839 [PIPE_C] = WM0_PIPEC_IVB,
5840 };
5841
5842 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005843 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005844 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005845
Ville Syrjälä15606532016-05-13 17:55:17 +03005846 memset(active, 0, sizeof(*active));
5847
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005848 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005849
5850 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005851 u32 tmp = hw->wm_pipe[pipe];
5852
5853 /*
5854 * For active pipes LP0 watermark is marked as
5855 * enabled, and LP1+ watermaks as disabled since
5856 * we can't really reverse compute them in case
5857 * multiple pipes are active.
5858 */
5859 active->wm[0].enable = true;
5860 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5861 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5862 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5863 active->linetime = hw->wm_linetime[pipe];
5864 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005865 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005866
5867 /*
5868 * For inactive pipes, all watermark levels
5869 * should be marked as enabled but zeroed,
5870 * which is what we'd compute them to.
5871 */
5872 for (level = 0; level <= max_level; level++)
5873 active->wm[level].enable = true;
5874 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005875
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005876 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005877}
5878
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005879#define _FW_WM(value, plane) \
5880 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5881#define _FW_WM_VLV(value, plane) \
5882 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5883
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005884static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5885 struct g4x_wm_values *wm)
5886{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005887 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005888
5889 tmp = I915_READ(DSPFW1);
5890 wm->sr.plane = _FW_WM(tmp, SR);
5891 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5892 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5893 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5894
5895 tmp = I915_READ(DSPFW2);
5896 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5897 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5898 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5899 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5900 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5901 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5902
5903 tmp = I915_READ(DSPFW3);
5904 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5905 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5906 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5907 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5908}
5909
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005910static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5911 struct vlv_wm_values *wm)
5912{
5913 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005914 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005915
5916 for_each_pipe(dev_priv, pipe) {
5917 tmp = I915_READ(VLV_DDL(pipe));
5918
Ville Syrjälä1b313892016-11-28 19:37:08 +02005919 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005920 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005921 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005922 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005923 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005924 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005925 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005926 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5927 }
5928
5929 tmp = I915_READ(DSPFW1);
5930 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005931 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5932 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5933 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005934
5935 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005936 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5937 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5938 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005939
5940 tmp = I915_READ(DSPFW3);
5941 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5942
5943 if (IS_CHERRYVIEW(dev_priv)) {
5944 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005945 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5946 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005947
5948 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005949 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5950 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005951
5952 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005953 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5954 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005955
5956 tmp = I915_READ(DSPHOWM);
5957 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005958 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5959 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5960 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5961 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5962 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5963 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5964 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5965 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5966 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005967 } else {
5968 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005969 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5970 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005971
5972 tmp = I915_READ(DSPHOWM);
5973 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005974 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5975 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5976 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5977 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5978 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5979 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005980 }
5981}
5982
5983#undef _FW_WM
5984#undef _FW_WM_VLV
5985
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005986void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005987{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005988 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5989 struct intel_crtc *crtc;
5990
5991 g4x_read_wm_values(dev_priv, wm);
5992
5993 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5994
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005995 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005996 struct intel_crtc_state *crtc_state =
5997 to_intel_crtc_state(crtc->base.state);
5998 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5999 struct g4x_pipe_wm *raw;
6000 enum pipe pipe = crtc->pipe;
6001 enum plane_id plane_id;
6002 int level, max_level;
6003
6004 active->cxsr = wm->cxsr;
6005 active->hpll_en = wm->hpll_en;
6006 active->fbc_en = wm->fbc_en;
6007
6008 active->sr = wm->sr;
6009 active->hpll = wm->hpll;
6010
6011 for_each_plane_id_on_crtc(crtc, plane_id) {
6012 active->wm.plane[plane_id] =
6013 wm->pipe[pipe].plane[plane_id];
6014 }
6015
6016 if (wm->cxsr && wm->hpll_en)
6017 max_level = G4X_WM_LEVEL_HPLL;
6018 else if (wm->cxsr)
6019 max_level = G4X_WM_LEVEL_SR;
6020 else
6021 max_level = G4X_WM_LEVEL_NORMAL;
6022
6023 level = G4X_WM_LEVEL_NORMAL;
6024 raw = &crtc_state->wm.g4x.raw[level];
6025 for_each_plane_id_on_crtc(crtc, plane_id)
6026 raw->plane[plane_id] = active->wm.plane[plane_id];
6027
6028 if (++level > max_level)
6029 goto out;
6030
6031 raw = &crtc_state->wm.g4x.raw[level];
6032 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6033 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6034 raw->plane[PLANE_SPRITE0] = 0;
6035 raw->fbc = active->sr.fbc;
6036
6037 if (++level > max_level)
6038 goto out;
6039
6040 raw = &crtc_state->wm.g4x.raw[level];
6041 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6042 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6043 raw->plane[PLANE_SPRITE0] = 0;
6044 raw->fbc = active->hpll.fbc;
6045
6046 out:
6047 for_each_plane_id_on_crtc(crtc, plane_id)
6048 g4x_raw_plane_wm_set(crtc_state, level,
6049 plane_id, USHRT_MAX);
6050 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6051
6052 crtc_state->wm.g4x.optimal = *active;
6053 crtc_state->wm.g4x.intermediate = *active;
6054
6055 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6056 pipe_name(pipe),
6057 wm->pipe[pipe].plane[PLANE_PRIMARY],
6058 wm->pipe[pipe].plane[PLANE_CURSOR],
6059 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6060 }
6061
6062 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6063 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6064 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6065 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6066 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
6067 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
6068}
6069
6070void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6071{
6072 struct intel_plane *plane;
6073 struct intel_crtc *crtc;
6074
6075 mutex_lock(&dev_priv->wm.wm_mutex);
6076
6077 for_each_intel_plane(&dev_priv->drm, plane) {
6078 struct intel_crtc *crtc =
6079 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6080 struct intel_crtc_state *crtc_state =
6081 to_intel_crtc_state(crtc->base.state);
6082 struct intel_plane_state *plane_state =
6083 to_intel_plane_state(plane->base.state);
6084 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6085 enum plane_id plane_id = plane->id;
6086 int level;
6087
6088 if (plane_state->base.visible)
6089 continue;
6090
6091 for (level = 0; level < 3; level++) {
6092 struct g4x_pipe_wm *raw =
6093 &crtc_state->wm.g4x.raw[level];
6094
6095 raw->plane[plane_id] = 0;
6096 wm_state->wm.plane[plane_id] = 0;
6097 }
6098
6099 if (plane_id == PLANE_PRIMARY) {
6100 for (level = 0; level < 3; level++) {
6101 struct g4x_pipe_wm *raw =
6102 &crtc_state->wm.g4x.raw[level];
6103 raw->fbc = 0;
6104 }
6105
6106 wm_state->sr.fbc = 0;
6107 wm_state->hpll.fbc = 0;
6108 wm_state->fbc_en = false;
6109 }
6110 }
6111
6112 for_each_intel_crtc(&dev_priv->drm, crtc) {
6113 struct intel_crtc_state *crtc_state =
6114 to_intel_crtc_state(crtc->base.state);
6115
6116 crtc_state->wm.g4x.intermediate =
6117 crtc_state->wm.g4x.optimal;
6118 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6119 }
6120
6121 g4x_program_watermarks(dev_priv);
6122
6123 mutex_unlock(&dev_priv->wm.wm_mutex);
6124}
6125
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006126void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006127{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006128 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006129 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006130 u32 val;
6131
6132 vlv_read_wm_values(dev_priv, wm);
6133
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006134 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6135 wm->level = VLV_WM_LEVEL_PM2;
6136
6137 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006138 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006139
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006140 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006141 if (val & DSP_MAXFIFO_PM5_ENABLE)
6142 wm->level = VLV_WM_LEVEL_PM5;
6143
Ville Syrjälä58590c12015-09-08 21:05:12 +03006144 /*
6145 * If DDR DVFS is disabled in the BIOS, Punit
6146 * will never ack the request. So if that happens
6147 * assume we don't have to enable/disable DDR DVFS
6148 * dynamically. To test that just set the REQ_ACK
6149 * bit to poke the Punit, but don't change the
6150 * HIGH/LOW bits so that we don't actually change
6151 * the current state.
6152 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006153 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006154 val |= FORCE_DDR_FREQ_REQ_ACK;
6155 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6156
6157 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6158 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6159 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
6160 "assuming DDR DVFS is disabled\n");
6161 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6162 } else {
6163 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6164 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6165 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6166 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006167
Chris Wilson337fa6e2019-04-26 09:17:20 +01006168 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006169 }
6170
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006171 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006172 struct intel_crtc_state *crtc_state =
6173 to_intel_crtc_state(crtc->base.state);
6174 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6175 const struct vlv_fifo_state *fifo_state =
6176 &crtc_state->wm.vlv.fifo_state;
6177 enum pipe pipe = crtc->pipe;
6178 enum plane_id plane_id;
6179 int level;
6180
6181 vlv_get_fifo_size(crtc_state);
6182
6183 active->num_levels = wm->level + 1;
6184 active->cxsr = wm->cxsr;
6185
Ville Syrjäläff32c542017-03-02 19:14:57 +02006186 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006187 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006188 &crtc_state->wm.vlv.raw[level];
6189
6190 active->sr[level].plane = wm->sr.plane;
6191 active->sr[level].cursor = wm->sr.cursor;
6192
6193 for_each_plane_id_on_crtc(crtc, plane_id) {
6194 active->wm[level].plane[plane_id] =
6195 wm->pipe[pipe].plane[plane_id];
6196
6197 raw->plane[plane_id] =
6198 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6199 fifo_state->plane[plane_id]);
6200 }
6201 }
6202
6203 for_each_plane_id_on_crtc(crtc, plane_id)
6204 vlv_raw_plane_wm_set(crtc_state, level,
6205 plane_id, USHRT_MAX);
6206 vlv_invalidate_wms(crtc, active, level);
6207
6208 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006209 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006210
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006211 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 +02006212 pipe_name(pipe),
6213 wm->pipe[pipe].plane[PLANE_PRIMARY],
6214 wm->pipe[pipe].plane[PLANE_CURSOR],
6215 wm->pipe[pipe].plane[PLANE_SPRITE0],
6216 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006217 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006218
6219 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6220 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6221}
6222
Ville Syrjälä602ae832017-03-02 19:15:02 +02006223void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6224{
6225 struct intel_plane *plane;
6226 struct intel_crtc *crtc;
6227
6228 mutex_lock(&dev_priv->wm.wm_mutex);
6229
6230 for_each_intel_plane(&dev_priv->drm, plane) {
6231 struct intel_crtc *crtc =
6232 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6233 struct intel_crtc_state *crtc_state =
6234 to_intel_crtc_state(crtc->base.state);
6235 struct intel_plane_state *plane_state =
6236 to_intel_plane_state(plane->base.state);
6237 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6238 const struct vlv_fifo_state *fifo_state =
6239 &crtc_state->wm.vlv.fifo_state;
6240 enum plane_id plane_id = plane->id;
6241 int level;
6242
6243 if (plane_state->base.visible)
6244 continue;
6245
6246 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006247 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006248 &crtc_state->wm.vlv.raw[level];
6249
6250 raw->plane[plane_id] = 0;
6251
6252 wm_state->wm[level].plane[plane_id] =
6253 vlv_invert_wm_value(raw->plane[plane_id],
6254 fifo_state->plane[plane_id]);
6255 }
6256 }
6257
6258 for_each_intel_crtc(&dev_priv->drm, crtc) {
6259 struct intel_crtc_state *crtc_state =
6260 to_intel_crtc_state(crtc->base.state);
6261
6262 crtc_state->wm.vlv.intermediate =
6263 crtc_state->wm.vlv.optimal;
6264 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6265 }
6266
6267 vlv_program_watermarks(dev_priv);
6268
6269 mutex_unlock(&dev_priv->wm.wm_mutex);
6270}
6271
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006272/*
6273 * FIXME should probably kill this and improve
6274 * the real watermark readout/sanitation instead
6275 */
6276static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6277{
6278 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6279 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6280 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6281
6282 /*
6283 * Don't touch WM1S_LP_EN here.
6284 * Doing so could cause underruns.
6285 */
6286}
6287
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006288void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006289{
Imre Deak820c1982013-12-17 14:46:36 +02006290 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006291 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006292
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006293 ilk_init_lp_watermarks(dev_priv);
6294
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006295 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006296 ilk_pipe_wm_get_hw_state(crtc);
6297
6298 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6299 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6300 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6301
6302 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006303 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006304 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6305 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6306 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006307
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006308 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006309 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6310 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006311 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006312 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6313 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006314
6315 hw->enable_fbc_wm =
6316 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6317}
6318
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006319/**
6320 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006321 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006322 *
6323 * Calculate watermark values for the various WM regs based on current mode
6324 * and plane configuration.
6325 *
6326 * There are several cases to deal with here:
6327 * - normal (i.e. non-self-refresh)
6328 * - self-refresh (SR) mode
6329 * - lines are large relative to FIFO size (buffer can hold up to 2)
6330 * - lines are small relative to FIFO size (buffer can hold more than 2
6331 * lines), so need to account for TLB latency
6332 *
6333 * The normal calculation is:
6334 * watermark = dotclock * bytes per pixel * latency
6335 * where latency is platform & configuration dependent (we assume pessimal
6336 * values here).
6337 *
6338 * The SR calculation is:
6339 * watermark = (trunc(latency/line time)+1) * surface width *
6340 * bytes per pixel
6341 * where
6342 * line time = htotal / dotclock
6343 * surface width = hdisplay for normal plane and 64 for cursor
6344 * and latency is assumed to be high, as above.
6345 *
6346 * The final value programmed to the register should always be rounded up,
6347 * and include an extra 2 entries to account for clock crossings.
6348 *
6349 * We don't use the sprite, so we can ignore that. And on Crestline we have
6350 * to set the non-SR watermarks to 8.
6351 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006352void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006353{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006354 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006355
6356 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006357 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006358}
6359
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306360void intel_enable_ipc(struct drm_i915_private *dev_priv)
6361{
6362 u32 val;
6363
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006364 if (!HAS_IPC(dev_priv))
6365 return;
6366
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306367 val = I915_READ(DISP_ARB_CTL2);
6368
6369 if (dev_priv->ipc_enabled)
6370 val |= DISP_IPC_ENABLE;
6371 else
6372 val &= ~DISP_IPC_ENABLE;
6373
6374 I915_WRITE(DISP_ARB_CTL2, val);
6375}
6376
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006377static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6378{
6379 /* Display WA #0477 WaDisableIPC: skl */
6380 if (IS_SKYLAKE(dev_priv))
6381 return false;
6382
6383 /* Display WA #1141: SKL:all KBL:all CFL */
6384 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
6385 return dev_priv->dram_info.symmetric_memory;
6386
6387 return true;
6388}
6389
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306390void intel_init_ipc(struct drm_i915_private *dev_priv)
6391{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306392 if (!HAS_IPC(dev_priv))
6393 return;
6394
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006395 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006396
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306397 intel_enable_ipc(dev_priv);
6398}
6399
Jani Nikulae2828912016-01-18 09:19:47 +02006400/*
Daniel Vetter92703882012-08-09 16:46:01 +02006401 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02006402 */
6403DEFINE_SPINLOCK(mchdev_lock);
6404
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006405bool ironlake_set_drps(struct drm_i915_private *i915, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006406{
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006407 struct intel_uncore *uncore = &i915->uncore;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006408 u16 rgvswctl;
6409
Chris Wilson67520412017-03-02 13:28:01 +00006410 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02006411
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006412 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006413 if (rgvswctl & MEMCTL_CMD_STS) {
6414 DRM_DEBUG("gpu busy, RCS change rejected\n");
6415 return false; /* still busy with another command */
6416 }
6417
6418 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6419 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006420 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
6421 intel_uncore_posting_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006422
6423 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006424 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006425
6426 return true;
6427}
6428
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006429static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006430{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006431 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006432 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006433 u8 fmax, fmin, fstart, vstart;
6434
Daniel Vetter92703882012-08-09 16:46:01 +02006435 spin_lock_irq(&mchdev_lock);
6436
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006437 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006438
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006439 /* Enable temp reporting */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006440 intel_uncore_write16(uncore, PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6441 intel_uncore_write16(uncore, TSC1, I915_READ(TSC1) | TSE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006442
6443 /* 100ms RC evaluation intervals */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006444 intel_uncore_write(uncore, RCUPEI, 100000);
6445 intel_uncore_write(uncore, RCDNEI, 100000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006446
6447 /* Set max/min thresholds to 90ms and 80ms respectively */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006448 intel_uncore_write(uncore, RCBMAXAVG, 90000);
6449 intel_uncore_write(uncore, RCBMINAVG, 80000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006450
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006451 intel_uncore_write(uncore, MEMIHYST, 1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006452
6453 /* Set up min, max, and cur for interrupt handling */
6454 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6455 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6456 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6457 MEMMODE_FSTART_SHIFT;
6458
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006459 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
6460 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006461
Daniel Vetter20e4d402012-08-08 23:35:39 +02006462 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
6463 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006464
Daniel Vetter20e4d402012-08-08 23:35:39 +02006465 dev_priv->ips.max_delay = fstart;
6466 dev_priv->ips.min_delay = fmin;
6467 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006468
6469 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6470 fmax, fmin, fstart);
6471
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006472 intel_uncore_write(uncore,
6473 MEMINTREN,
6474 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006475
6476 /*
6477 * Interrupts will be enabled in ironlake_irq_postinstall
6478 */
6479
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006480 intel_uncore_write(uncore, VIDSTART, vstart);
6481 intel_uncore_posting_read(uncore, VIDSTART);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006482
6483 rgvmodectl |= MEMMODE_SWMODE_EN;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006484 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006485
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006486 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
6487 MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006488 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006489 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006490
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006491 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006492
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006493 dev_priv->ips.last_count1 =
6494 intel_uncore_read(uncore, DMIEC) +
6495 intel_uncore_read(uncore, DDREC) +
6496 intel_uncore_read(uncore, CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006497 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006498 dev_priv->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006499 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02006500
6501 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006502}
6503
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006504static void ironlake_disable_drps(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006505{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006506 struct intel_uncore *uncore = &i915->uncore;
Daniel Vetter92703882012-08-09 16:46:01 +02006507 u16 rgvswctl;
6508
6509 spin_lock_irq(&mchdev_lock);
6510
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006511 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006512
6513 /* Ack interrupts, disable EFC interrupt */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006514 intel_uncore_write(uncore,
6515 MEMINTREN,
6516 intel_uncore_read(uncore, MEMINTREN) &
6517 ~MEMINT_EVAL_CHG_EN);
6518 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
6519 intel_uncore_write(uncore,
6520 DEIER,
6521 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
6522 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
6523 intel_uncore_write(uncore,
6524 DEIMR,
6525 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006526
6527 /* Go back to the starting frequency */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006528 ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006529 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006530 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006531 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006532 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006533
Daniel Vetter92703882012-08-09 16:46:01 +02006534 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006535}
6536
Daniel Vetteracbe9472012-07-26 11:50:05 +02006537/* There's a funny hw issue where the hw returns all 0 when reading from
6538 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6539 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6540 * all limits and the gpu stuck at whatever frequency it is at atm).
6541 */
Akash Goel74ef1172015-03-06 11:07:19 +05306542static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006543{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006544 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006545 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006546
Daniel Vetter20b46e52012-07-26 11:16:14 +02006547 /* Only set the down limit when we've reached the lowest level to avoid
6548 * getting more interrupts, otherwise leave this clear. This prevents a
6549 * race in the hw when coming out of rc6: There's a tiny window where
6550 * the hw runs at the minimal clock before selecting the desired
6551 * frequency, if the down threshold expires in that window we will not
6552 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006553 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006554 limits = (rps->max_freq_softlimit) << 23;
6555 if (val <= rps->min_freq_softlimit)
6556 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306557 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006558 limits = rps->max_freq_softlimit << 24;
6559 if (val <= rps->min_freq_softlimit)
6560 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306561 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006562
6563 return limits;
6564}
6565
Chris Wilson60548c52018-07-31 14:26:29 +01006566static void rps_set_power(struct drm_i915_private *dev_priv, int new_power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006567{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006568 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Akash Goel8a586432015-03-06 11:07:18 +05306569 u32 threshold_up = 0, threshold_down = 0; /* in % */
6570 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006571
Chris Wilson60548c52018-07-31 14:26:29 +01006572 lockdep_assert_held(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006573
Chris Wilson60548c52018-07-31 14:26:29 +01006574 if (new_power == rps->power.mode)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006575 return;
6576
6577 /* Note the units here are not exactly 1us, but 1280ns. */
6578 switch (new_power) {
6579 case LOW_POWER:
6580 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306581 ei_up = 16000;
6582 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006583
6584 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306585 ei_down = 32000;
6586 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006587 break;
6588
6589 case BETWEEN:
6590 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306591 ei_up = 13000;
6592 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006593
6594 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306595 ei_down = 32000;
6596 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006597 break;
6598
6599 case HIGH_POWER:
6600 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306601 ei_up = 10000;
6602 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006603
6604 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306605 ei_down = 32000;
6606 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006607 break;
6608 }
6609
Mika Kuoppala6067a272017-02-15 15:52:59 +02006610 /* When byt can survive without system hang with dynamic
6611 * sw freq adjustments, this restriction can be lifted.
6612 */
6613 if (IS_VALLEYVIEW(dev_priv))
6614 goto skip_hw_write;
6615
Akash Goel8a586432015-03-06 11:07:18 +05306616 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006617 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306618 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006619 GT_INTERVAL_FROM_US(dev_priv,
6620 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306621
6622 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006623 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306624 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006625 GT_INTERVAL_FROM_US(dev_priv,
6626 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306627
Chris Wilsona72b5622016-07-02 15:35:59 +01006628 I915_WRITE(GEN6_RP_CONTROL,
Mika Kuoppala1071d0f2019-04-10 16:24:36 +03006629 (INTEL_GEN(dev_priv) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
Chris Wilsona72b5622016-07-02 15:35:59 +01006630 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6631 GEN6_RP_MEDIA_IS_GFX |
6632 GEN6_RP_ENABLE |
6633 GEN6_RP_UP_BUSY_AVG |
6634 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306635
Mika Kuoppala6067a272017-02-15 15:52:59 +02006636skip_hw_write:
Chris Wilson60548c52018-07-31 14:26:29 +01006637 rps->power.mode = new_power;
6638 rps->power.up_threshold = threshold_up;
6639 rps->power.down_threshold = threshold_down;
6640}
6641
6642static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6643{
6644 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6645 int new_power;
6646
6647 new_power = rps->power.mode;
6648 switch (rps->power.mode) {
6649 case LOW_POWER:
6650 if (val > rps->efficient_freq + 1 &&
6651 val > rps->cur_freq)
6652 new_power = BETWEEN;
6653 break;
6654
6655 case BETWEEN:
6656 if (val <= rps->efficient_freq &&
6657 val < rps->cur_freq)
6658 new_power = LOW_POWER;
6659 else if (val >= rps->rp0_freq &&
6660 val > rps->cur_freq)
6661 new_power = HIGH_POWER;
6662 break;
6663
6664 case HIGH_POWER:
6665 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6666 val < rps->cur_freq)
6667 new_power = BETWEEN;
6668 break;
6669 }
6670 /* Max/min bins are special */
6671 if (val <= rps->min_freq_softlimit)
6672 new_power = LOW_POWER;
6673 if (val >= rps->max_freq_softlimit)
6674 new_power = HIGH_POWER;
6675
6676 mutex_lock(&rps->power.mutex);
6677 if (rps->power.interactive)
6678 new_power = HIGH_POWER;
6679 rps_set_power(dev_priv, new_power);
6680 mutex_unlock(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006681}
6682
Chris Wilson60548c52018-07-31 14:26:29 +01006683void intel_rps_mark_interactive(struct drm_i915_private *i915, bool interactive)
6684{
6685 struct intel_rps *rps = &i915->gt_pm.rps;
6686
6687 if (INTEL_GEN(i915) < 6)
6688 return;
6689
6690 mutex_lock(&rps->power.mutex);
6691 if (interactive) {
6692 if (!rps->power.interactive++ && READ_ONCE(i915->gt.awake))
6693 rps_set_power(i915, HIGH_POWER);
6694 } else {
6695 GEM_BUG_ON(!rps->power.interactive);
6696 rps->power.interactive--;
6697 }
6698 mutex_unlock(&rps->power.mutex);
6699}
6700
Chris Wilson2876ce72014-03-28 08:03:34 +00006701static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6702{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006703 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006704 u32 mask = 0;
6705
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006706 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006707 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006708 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 +01006709 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006710 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006711
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006712 mask &= dev_priv->pm_rps_events;
6713
Imre Deak59d02a12014-12-19 19:33:26 +02006714 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006715}
6716
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006717/* gen6_set_rps is called to update the frequency request, but should also be
6718 * called when the range (min_delay and max_delay) is modified so that we can
6719 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006720static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006721{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006722 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6723
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006724 /* min/max delay may still have been modified so be sure to
6725 * write the limits value.
6726 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006727 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006728 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006729
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006730 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306731 I915_WRITE(GEN6_RPNSWREQ,
6732 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006733 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006734 I915_WRITE(GEN6_RPNSWREQ,
6735 HSW_FREQUENCY(val));
6736 else
6737 I915_WRITE(GEN6_RPNSWREQ,
6738 GEN6_FREQUENCY(val) |
6739 GEN6_OFFSET(0) |
6740 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006741 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006742
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006743 /* Make sure we continue to get interrupts
6744 * until we hit the minimum or maximum frequencies.
6745 */
Akash Goel74ef1172015-03-06 11:07:19 +05306746 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006747 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006748
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006749 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006750 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006751
6752 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006753}
6754
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006755static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006756{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006757 int err;
6758
Chris Wilsondc979972016-05-10 14:10:04 +01006759 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006760 "Odd GPU freq value\n"))
6761 val &= ~1;
6762
Deepak Scd25dd52015-07-10 18:31:40 +05306763 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6764
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006765 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006766 vlv_punit_get(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006767 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson337fa6e2019-04-26 09:17:20 +01006768 vlv_punit_put(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006769 if (err)
6770 return err;
6771
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006772 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006773 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006774
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006775 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006776 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006777
6778 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006779}
6780
Deepak Sa7f6e232015-05-09 18:04:44 +05306781/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306782 *
6783 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306784 * 1. Forcewake Media well.
6785 * 2. Request idle freq.
6786 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306787*/
6788static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6789{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006790 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6791 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006792 int err;
Deepak S5549d252014-06-28 11:26:11 +05306793
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006794 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306795 return;
6796
Chris Wilsonc9efef72017-01-02 15:28:45 +00006797 /* The punit delays the write of the frequency and voltage until it
6798 * determines the GPU is awake. During normal usage we don't want to
6799 * waste power changing the frequency if the GPU is sleeping (rc6).
6800 * However, the GPU and driver is now idle and we do not want to delay
6801 * switching to minimum voltage (reducing power whilst idle) as we do
6802 * not expect to be woken in the near future and so must flush the
6803 * change by waking the device.
6804 *
6805 * We choose to take the media powerwell (either would do to trick the
6806 * punit into committing the voltage change) as that takes a lot less
6807 * power than the render powerwell.
6808 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006809 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006810 err = valleyview_set_rps(dev_priv, val);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006811 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006812
6813 if (err)
6814 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306815}
6816
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006817void gen6_rps_busy(struct drm_i915_private *dev_priv)
6818{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006819 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6820
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006821 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006822 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006823 u8 freq;
6824
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006825 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006826 gen6_rps_reset_ei(dev_priv);
6827 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006828 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006829
Chris Wilsonc33d2472016-07-04 08:08:36 +01006830 gen6_enable_rps_interrupts(dev_priv);
6831
Chris Wilsonbd648182017-02-10 15:03:48 +00006832 /* Use the user's desired frequency as a guide, but for better
6833 * performance, jump directly to RPe as our starting frequency.
6834 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006835 freq = max(rps->cur_freq,
6836 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006837
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006838 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006839 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006840 rps->min_freq_softlimit,
6841 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006842 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006843 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006844 mutex_unlock(&rps->lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006845}
6846
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006847void gen6_rps_idle(struct drm_i915_private *dev_priv)
6848{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006849 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6850
Chris Wilsonc33d2472016-07-04 08:08:36 +01006851 /* Flush our bottom-half so that it does not race with us
6852 * setting the idle frequency and so that it is bounded by
6853 * our rpm wakeref. And then disable the interrupts to stop any
6854 * futher RPS reclocking whilst we are asleep.
6855 */
6856 gen6_disable_rps_interrupts(dev_priv);
6857
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006858 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006859 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006860 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306861 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006862 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006863 gen6_set_rps(dev_priv, rps->idle_freq);
6864 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006865 I915_WRITE(GEN6_PMINTRMSK,
6866 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006867 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006868 mutex_unlock(&rps->lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006869}
6870
Chris Wilson62eb3c22019-02-13 09:25:04 +00006871void gen6_rps_boost(struct i915_request *rq)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006872{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006873 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006874 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006875 bool boost;
6876
Chris Wilson8d3afd72015-05-21 21:01:47 +01006877 /* This is intentionally racy! We peek at the state here, then
6878 * validate inside the RPS worker.
6879 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006880 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006881 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006882
Chris Wilson0e218342019-01-21 22:21:02 +00006883 if (i915_request_signaled(rq))
Chris Wilson253a2812018-02-06 14:31:37 +00006884 return;
6885
Chris Wilsone61e0f52018-02-21 09:56:36 +00006886 /* Serializes with i915_request_retire() */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006887 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006888 spin_lock_irqsave(&rq->lock, flags);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006889 if (!i915_request_has_waitboost(rq) &&
6890 !dma_fence_is_signaled_locked(&rq->fence)) {
Chris Wilson253a2812018-02-06 14:31:37 +00006891 boost = !atomic_fetch_inc(&rps->num_waiters);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006892 rq->flags |= I915_REQUEST_WAITBOOST;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006893 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006894 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006895 if (!boost)
6896 return;
6897
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006898 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6899 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006900
Chris Wilson62eb3c22019-02-13 09:25:04 +00006901 atomic_inc(&rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006902}
6903
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006904int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006905{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006906 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006907 int err;
6908
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006909 lockdep_assert_held(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006910 GEM_BUG_ON(val > rps->max_freq);
6911 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006912
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006913 if (!rps->enabled) {
6914 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006915 return 0;
6916 }
6917
Chris Wilsondc979972016-05-10 14:10:04 +01006918 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006919 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006920 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006921 err = gen6_set_rps(dev_priv, val);
6922
6923 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006924}
6925
Chris Wilsondc979972016-05-10 14:10:04 +01006926static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006927{
Zhe Wang20e49362014-11-04 17:07:05 +00006928 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006929 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006930}
6931
Chris Wilsondc979972016-05-10 14:10:04 +01006932static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306933{
Akash Goel2030d682016-04-23 00:05:45 +05306934 I915_WRITE(GEN6_RP_CONTROL, 0);
6935}
6936
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006937static void gen6_disable_rc6(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006938{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006939 I915_WRITE(GEN6_RC_CONTROL, 0);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006940}
6941
6942static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6943{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006944 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306945 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006946}
6947
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006948static void cherryview_disable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306949{
Deepak S38807742014-05-23 21:00:15 +05306950 I915_WRITE(GEN6_RC_CONTROL, 0);
6951}
6952
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006953static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6954{
6955 I915_WRITE(GEN6_RP_CONTROL, 0);
6956}
6957
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006958static void valleyview_disable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006959{
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006960 /* We're doing forcewake before Disabling RC6,
Deepak S98a2e5f2014-08-18 10:35:27 -07006961 * This what the BIOS expects when going into suspend */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006962 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006963
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006964 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006965
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006966 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006967}
6968
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006969static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6970{
6971 I915_WRITE(GEN6_RP_CONTROL, 0);
6972}
6973
Chris Wilsondc979972016-05-10 14:10:04 +01006974static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306975{
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306976 bool enable_rc6 = true;
6977 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006978 u32 rc_ctl;
6979 int rc_sw_target;
6980
6981 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6982 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6983 RC_SW_TARGET_STATE_SHIFT;
6984 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6985 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6986 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6987 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6988 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306989
6990 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006991 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306992 enable_rc6 = false;
6993 }
6994
6995 /*
6996 * The exact context size is not known for BXT, so assume a page size
6997 * for this check.
6998 */
6999 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Matthew Auld17a05342017-12-11 15:18:19 +00007000 if (!((rc6_ctx_base >= dev_priv->dsm_reserved.start) &&
7001 (rc6_ctx_base + PAGE_SIZE < dev_priv->dsm_reserved.end))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03007002 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307003 enable_rc6 = false;
7004 }
7005
7006 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
7007 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
7008 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
7009 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03007010 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307011 enable_rc6 = false;
7012 }
7013
Imre Deakfc619842016-06-29 19:13:55 +03007014 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
7015 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
7016 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
7017 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
7018 enable_rc6 = false;
7019 }
7020
7021 if (!I915_READ(GEN6_GFXPAUSE)) {
7022 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
7023 enable_rc6 = false;
7024 }
7025
7026 if (!I915_READ(GEN8_MISC_CTRL0)) {
7027 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307028 enable_rc6 = false;
7029 }
7030
7031 return enable_rc6;
7032}
7033
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007034static bool sanitize_rc6(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007035{
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007036 struct intel_device_info *info = mkwrite_device_info(i915);
Imre Deake6069ca2014-04-18 16:01:02 +03007037
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007038 /* Powersaving is controlled by the host when inside a VM */
Chris Wilson91cbdb82019-04-19 14:48:36 +01007039 if (intel_vgpu_active(i915)) {
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007040 info->has_rc6 = 0;
Chris Wilson91cbdb82019-04-19 14:48:36 +01007041 info->has_rps = false;
7042 }
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307043
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007044 if (info->has_rc6 &&
7045 IS_GEN9_LP(i915) && !bxt_check_bios_rc6_setup(i915)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307046 DRM_INFO("RC6 disabled by BIOS\n");
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007047 info->has_rc6 = 0;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05307048 }
7049
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007050 /*
7051 * We assume that we do not have any deep rc6 levels if we don't have
7052 * have the previous rc6 level supported, i.e. we use HAS_RC6()
7053 * as the initial coarse check for rc6 in general, moving on to
7054 * progressively finer/deeper levels.
7055 */
7056 if (!info->has_rc6 && info->has_rc6p)
7057 info->has_rc6p = 0;
Imre Deake6069ca2014-04-18 16:01:02 +03007058
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007059 return info->has_rc6;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007060}
7061
Chris Wilsondc979972016-05-10 14:10:04 +01007062static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03007063{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007064 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7065
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007066 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01007067
Tom O'Rourke93ee2922014-11-19 14:21:52 -08007068 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02007069 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01007070 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007071 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
7072 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
7073 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07007074 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01007075 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007076 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
7077 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
7078 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07007079 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007080 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007081 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007082
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007083 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01007084 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007085 IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01007086 u32 ddcc_status = 0;
7087
7088 if (sandybridge_pcode_read(dev_priv,
7089 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
Ville Syrjäläd284d512019-05-21 19:40:24 +03007090 &ddcc_status, NULL) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007091 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08007092 clamp_t(u8,
7093 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007094 rps->min_freq,
7095 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08007096 }
7097
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007098 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goelc5e06882015-06-29 14:50:19 +05307099 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01007100 * the natural hardware unit for SKL
7101 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007102 rps->rp0_freq *= GEN9_FREQ_SCALER;
7103 rps->rp1_freq *= GEN9_FREQ_SCALER;
7104 rps->min_freq *= GEN9_FREQ_SCALER;
7105 rps->max_freq *= GEN9_FREQ_SCALER;
7106 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05307107 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07007108}
7109
Chris Wilson3a45b052016-07-13 09:10:32 +01007110static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00007111 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01007112{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007113 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7114 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01007115
7116 /* force a reset */
Chris Wilson60548c52018-07-31 14:26:29 +01007117 rps->power.mode = -1;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007118 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01007119
Chris Wilson9fcee2f2017-01-26 10:19:19 +00007120 if (set(dev_priv, freq))
7121 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01007122}
7123
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007124/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01007125static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00007126{
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007127 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007128
David Weinehall36fe7782017-11-17 10:01:46 +02007129 /* Program defaults and thresholds for RPS */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007130 if (IS_GEN(dev_priv, 9))
David Weinehall36fe7782017-11-17 10:01:46 +02007131 I915_WRITE(GEN6_RC_VIDEO_FREQ,
7132 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007133
Akash Goel0beb0592015-03-06 11:07:20 +05307134 /* 1 second timeout*/
7135 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
7136 GT_INTERVAL_FROM_US(dev_priv, 1000000));
7137
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007138 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007139
Akash Goel0beb0592015-03-06 11:07:20 +05307140 /* Leaning on the below call to gen6_set_rps to program/setup the
7141 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
7142 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01007143 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007144
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007145 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007146}
7147
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007148static void gen11_enable_rc6(struct drm_i915_private *dev_priv)
7149{
7150 struct intel_engine_cs *engine;
7151 enum intel_engine_id id;
7152
7153 /* 1a: Software RC state - RC0 */
7154 I915_WRITE(GEN6_RC_STATE, 0);
7155
7156 /*
7157 * 1b: Get forcewake during program sequence. Although the driver
7158 * hasn't enabled a state yet where we need forcewake, BIOS may have.
7159 */
7160 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
7161
7162 /* 2a: Disable RC states. */
7163 I915_WRITE(GEN6_RC_CONTROL, 0);
7164
7165 /* 2b: Program RC6 thresholds.*/
7166 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
7167 I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
7168
7169 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7170 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7171 for_each_engine(engine, dev_priv, id)
7172 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
7173
Daniele Ceraolo Spurio702668e2019-07-24 17:18:06 -07007174 if (HAS_GT_UC(dev_priv))
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007175 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
7176
7177 I915_WRITE(GEN6_RC_SLEEP, 0);
7178
Mika Kuoppalad105e9a2019-04-10 13:59:18 +03007179 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
7180
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007181 /*
7182 * 2c: Program Coarse Power Gating Policies.
7183 *
7184 * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
7185 * use instead is a more conservative estimate for the maximum time
7186 * it takes us to service a CS interrupt and submit a new ELSP - that
7187 * is the time which the GPU is idle waiting for the CPU to select the
7188 * next request to execute. If the idle hysteresis is less than that
7189 * interrupt service latency, the hardware will automatically gate
7190 * the power well and we will then incur the wake up cost on top of
Maarten Lankhorstec193642019-06-28 10:55:17 +02007191 * the service latency. A similar guide from plane_state is that we
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007192 * do not want the enable hysteresis to less than the wakeup latency.
7193 *
7194 * igt/gem_exec_nop/sequential provides a rough estimate for the
7195 * service latency, and puts it around 10us for Broadwell (and other
7196 * big core) and around 40us for Broxton (and other low power cores).
7197 * [Note that for legacy ringbuffer submission, this is less than 1us!]
7198 * However, the wakeup latency on Broxton is closer to 100us. To be
7199 * conservative, we have to factor in a context switch on top (due
7200 * to ksoftirqd).
7201 */
7202 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 250);
7203 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 250);
7204
7205 /* 3a: Enable RC6 */
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007206 I915_WRITE(GEN6_RC_CONTROL,
7207 GEN6_RC_CTL_HW_ENABLE |
7208 GEN6_RC_CTL_RC6_ENABLE |
7209 GEN6_RC_CTL_EI_MODE(1));
7210
7211 /* 3b: Enable Coarse Power Gating only when RC6 is enabled. */
7212 I915_WRITE(GEN9_PG_ENABLE,
Mika Kuoppala2ea74142019-04-10 13:59:19 +03007213 GEN9_RENDER_PG_ENABLE |
7214 GEN9_MEDIA_PG_ENABLE |
7215 GEN11_MEDIA_SAMPLER_PG_ENABLE);
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03007216
7217 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
7218}
7219
Chris Wilsondc979972016-05-10 14:10:04 +01007220static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007221{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007222 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307223 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007224 u32 rc6_mode;
Zhe Wang20e49362014-11-04 17:07:05 +00007225
7226 /* 1a: Software RC state - RC0 */
7227 I915_WRITE(GEN6_RC_STATE, 0);
7228
7229 /* 1b: Get forcewake during program sequence. Although the driver
7230 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007231 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00007232
7233 /* 2a: Disable RC states. */
7234 I915_WRITE(GEN6_RC_CONTROL, 0);
7235
7236 /* 2b: Program RC6 thresholds.*/
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07007237 if (INTEL_GEN(dev_priv) >= 10) {
7238 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
7239 I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
7240 } else if (IS_SKYLAKE(dev_priv)) {
7241 /*
7242 * WaRsDoubleRc6WrlWithCoarsePowerGating:skl Doubling WRL only
7243 * when CPG is enabled
7244 */
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05307245 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07007246 } else {
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05307247 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07007248 }
7249
Zhe Wang20e49362014-11-04 17:07:05 +00007250 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7251 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05307252 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007253 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05307254
Daniele Ceraolo Spurio702668e2019-07-24 17:18:06 -07007255 if (HAS_GT_UC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05307256 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
7257
Zhe Wang20e49362014-11-04 17:07:05 +00007258 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00007259
Chris Wilsonc1beabc2018-01-22 13:55:41 +00007260 /*
7261 * 2c: Program Coarse Power Gating Policies.
7262 *
7263 * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
7264 * use instead is a more conservative estimate for the maximum time
7265 * it takes us to service a CS interrupt and submit a new ELSP - that
7266 * is the time which the GPU is idle waiting for the CPU to select the
7267 * next request to execute. If the idle hysteresis is less than that
7268 * interrupt service latency, the hardware will automatically gate
7269 * the power well and we will then incur the wake up cost on top of
Maarten Lankhorstec193642019-06-28 10:55:17 +02007270 * the service latency. A similar guide from plane_state is that we
Chris Wilsonc1beabc2018-01-22 13:55:41 +00007271 * do not want the enable hysteresis to less than the wakeup latency.
7272 *
7273 * igt/gem_exec_nop/sequential provides a rough estimate for the
7274 * service latency, and puts it around 10us for Broadwell (and other
7275 * big core) and around 40us for Broxton (and other low power cores).
7276 * [Note that for legacy ringbuffer submission, this is less than 1us!]
7277 * However, the wakeup latency on Broxton is closer to 100us. To be
7278 * conservative, we have to factor in a context switch on top (due
7279 * to ksoftirqd).
7280 */
7281 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 250);
7282 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 250);
Zhe Wang38c23522015-01-20 12:23:04 +00007283
Zhe Wang20e49362014-11-04 17:07:05 +00007284 /* 3a: Enable RC6 */
Chris Wilson1c044f92017-01-25 17:26:01 +00007285 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07007286
7287 /* WaRsUseTimeoutMode:cnl (pre-prod) */
7288 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_C0))
7289 rc6_mode = GEN7_RC_CTL_TO_MODE;
7290 else
7291 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
7292
Chris Wilson1c044f92017-01-25 17:26:01 +00007293 I915_WRITE(GEN6_RC_CONTROL,
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007294 GEN6_RC_CTL_HW_ENABLE |
7295 GEN6_RC_CTL_RC6_ENABLE |
7296 rc6_mode);
Zhe Wang20e49362014-11-04 17:07:05 +00007297
Sagar Kamblecb07bae2015-04-12 11:28:14 +05307298 /*
7299 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Rodrigo Vivid66047e42018-02-22 12:05:35 -08007300 * WaRsDisableCoarsePowerGating:skl,cnl - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05307301 */
Chris Wilsondc979972016-05-10 14:10:04 +01007302 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05307303 I915_WRITE(GEN9_PG_ENABLE, 0);
7304 else
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007305 I915_WRITE(GEN9_PG_ENABLE,
7306 GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE);
Zhe Wang38c23522015-01-20 12:23:04 +00007307
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007308 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00007309}
7310
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007311static void gen8_enable_rc6(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007312{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007313 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307314 enum intel_engine_id id;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007315
7316 /* 1a: Software RC state - RC0 */
7317 I915_WRITE(GEN6_RC_STATE, 0);
7318
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007319 /* 1b: Get forcewake during program sequence. Although the driver
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007320 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007321 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007322
7323 /* 2a: Disable RC states. */
7324 I915_WRITE(GEN6_RC_CONTROL, 0);
7325
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007326 /* 2b: Program RC6 thresholds.*/
7327 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7328 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7329 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05307330 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007331 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007332 I915_WRITE(GEN6_RC_SLEEP, 0);
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01007333 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007334
7335 /* 3: Enable RC6 */
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01007336
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007337 I915_WRITE(GEN6_RC_CONTROL,
7338 GEN6_RC_CTL_HW_ENABLE |
7339 GEN7_RC_CTL_TO_MODE |
7340 GEN6_RC_CTL_RC6_ENABLE);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007341
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007342 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007343}
7344
7345static void gen8_enable_rps(struct drm_i915_private *dev_priv)
7346{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007347 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7348
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007349 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007350
7351 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07007352 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007353 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07007354 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007355 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02007356 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
7357 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007358
Daniel Vetter7526ed72014-09-29 15:07:19 +02007359 /* Docs recommend 900MHz, and 300 MHz respectively */
7360 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007361 rps->max_freq_softlimit << 24 |
7362 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007363
Daniel Vetter7526ed72014-09-29 15:07:19 +02007364 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
7365 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
7366 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
7367 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007368
Daniel Vetter7526ed72014-09-29 15:07:19 +02007369 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007370
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007371 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02007372 I915_WRITE(GEN6_RP_CONTROL,
7373 GEN6_RP_MEDIA_TURBO |
7374 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7375 GEN6_RP_MEDIA_IS_GFX |
7376 GEN6_RP_ENABLE |
7377 GEN6_RP_UP_BUSY_AVG |
7378 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007379
Chris Wilson3a45b052016-07-13 09:10:32 +01007380 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02007381
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007382 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007383}
7384
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007385static void gen6_enable_rc6(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007386{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007387 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307388 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007389 u32 rc6vids, rc6_mask;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007390 u32 gtfifodbg;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00007391 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007392
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007393 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007394
7395 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007396 gtfifodbg = I915_READ(GTFIFODBG);
7397 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007398 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
7399 I915_WRITE(GTFIFODBG, gtfifodbg);
7400 }
7401
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007402 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007403
7404 /* disable the counters and set deterministic thresholds */
7405 I915_WRITE(GEN6_RC_CONTROL, 0);
7406
7407 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
7408 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
7409 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
7410 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7411 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7412
Akash Goel3b3f1652016-10-13 22:44:48 +05307413 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007414 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007415
7416 I915_WRITE(GEN6_RC_SLEEP, 0);
7417 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01007418 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07007419 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
7420 else
7421 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08007422 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007423 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
7424
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03007425 /* We don't use those on Haswell */
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007426 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
7427 if (HAS_RC6p(dev_priv))
7428 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
7429 if (HAS_RC6pp(dev_priv))
7430 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007431 I915_WRITE(GEN6_RC_CONTROL,
7432 rc6_mask |
7433 GEN6_RC_CTL_EI_MODE(1) |
7434 GEN6_RC_CTL_HW_ENABLE);
7435
Ben Widawsky31643d52012-09-26 10:34:01 -07007436 rc6vids = 0;
Ville Syrjäläd284d512019-05-21 19:40:24 +03007437 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS,
7438 &rc6vids, NULL);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007439 if (IS_GEN(dev_priv, 6) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07007440 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007441 } else if (IS_GEN(dev_priv, 6) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07007442 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
7443 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
7444 rc6vids &= 0xffff00;
7445 rc6vids |= GEN6_ENCODE_RC6_VID(450);
7446 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
7447 if (ret)
7448 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
7449 }
7450
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007451 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007452}
7453
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007454static void gen6_enable_rps(struct drm_i915_private *dev_priv)
7455{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007456 /* Here begins a magic sequence of register writes to enable
7457 * auto-downclocking.
7458 *
7459 * Perhaps there might be some value in exposing these to
7460 * userspace...
7461 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007462 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007463
7464 /* Power down if completely idle for over 50ms */
7465 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
7466 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7467
7468 reset_rps(dev_priv, gen6_set_rps);
7469
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007470 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007471}
7472
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007473static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007474{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007475 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007476 const int min_freq = 15;
7477 const int scaling_factor = 180;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007478 unsigned int gpu_freq;
7479 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05307480 unsigned int max_gpu_freq, min_gpu_freq;
Ben Widawskyeda79642013-10-07 17:15:48 -03007481 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007482
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007483 lockdep_assert_held(&rps->lock);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02007484
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007485 if (rps->max_freq <= rps->min_freq)
7486 return;
7487
Ben Widawskyeda79642013-10-07 17:15:48 -03007488 policy = cpufreq_cpu_get(0);
7489 if (policy) {
7490 max_ia_freq = policy->cpuinfo.max_freq;
7491 cpufreq_cpu_put(policy);
7492 } else {
7493 /*
7494 * Default to measured freq if none found, PCU will ensure we
7495 * don't go over
7496 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007497 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03007498 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007499
7500 /* Convert from kHz to MHz */
7501 max_ia_freq /= 1000;
7502
Ben Widawsky153b4b952013-10-22 22:05:09 -07007503 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07007504 /* convert DDR frequency from units of 266.6MHz to bandwidth */
7505 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007506
Chris Wilsond586b5f2018-03-08 14:26:48 +00007507 min_gpu_freq = rps->min_freq;
7508 max_gpu_freq = rps->max_freq;
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007509 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307510 /* Convert GT frequency to 50 HZ units */
Chris Wilsond586b5f2018-03-08 14:26:48 +00007511 min_gpu_freq /= GEN9_FREQ_SCALER;
7512 max_gpu_freq /= GEN9_FREQ_SCALER;
Akash Goel4c8c7742015-06-29 14:50:20 +05307513 }
7514
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007515 /*
7516 * For each potential GPU frequency, load a ring frequency we'd like
7517 * to use for memory access. We do this by specifying the IA frequency
7518 * the PCU should use as a reference to determine the ring frequency.
7519 */
Akash Goel4c8c7742015-06-29 14:50:20 +05307520 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007521 const int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007522 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007523
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007524 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307525 /*
7526 * ring_freq = 2 * GT. ring_freq is in 100MHz units
7527 * No floor required for ring frequency on SKL.
7528 */
7529 ring_freq = gpu_freq;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00007530 } else if (INTEL_GEN(dev_priv) >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07007531 /* max(2 * GT, DDR). NB: GT is 50MHz units */
7532 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01007533 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07007534 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007535 ring_freq = max(min_ring_freq, ring_freq);
7536 /* leave ia_freq as the default, chosen by cpufreq */
7537 } else {
7538 /* On older processors, there is no separate ring
7539 * clock domain, so in order to boost the bandwidth
7540 * of the ring, we need to upclock the CPU (ia_freq).
7541 *
7542 * For GPU frequencies less than 750MHz,
7543 * just use the lowest ring freq.
7544 */
7545 if (gpu_freq < min_freq)
7546 ia_freq = 800;
7547 else
7548 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
7549 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
7550 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007551
Ben Widawsky42c05262012-09-26 10:34:00 -07007552 sandybridge_pcode_write(dev_priv,
7553 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01007554 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
7555 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
7556 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007557 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007558}
7559
Ville Syrjälä03af2042014-06-28 02:03:53 +03007560static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05307561{
7562 u32 val, rp0;
7563
Jani Nikula5b5929c2015-10-07 11:17:46 +03007564 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05307565
Jani Nikula02584042018-12-31 16:56:41 +02007566 switch (RUNTIME_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03007567 case 8:
7568 /* (2 * 4) config */
7569 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
7570 break;
7571 case 12:
7572 /* (2 * 6) config */
7573 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
7574 break;
7575 case 16:
7576 /* (2 * 8) config */
7577 default:
7578 /* Setting (2 * 8) Min RP0 for any other combination */
7579 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
7580 break;
Deepak S095acd52015-01-17 11:05:59 +05307581 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03007582
7583 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
7584
Deepak S2b6b3a02014-05-27 15:59:30 +05307585 return rp0;
7586}
7587
7588static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7589{
7590 u32 val, rpe;
7591
7592 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
7593 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
7594
7595 return rpe;
7596}
7597
Deepak S7707df42014-07-12 18:46:14 +05307598static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
7599{
7600 u32 val, rp1;
7601
Jani Nikula5b5929c2015-10-07 11:17:46 +03007602 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
7603 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
7604
Deepak S7707df42014-07-12 18:46:14 +05307605 return rp1;
7606}
7607
Deepak S96676fe2016-08-12 18:46:41 +05307608static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
7609{
7610 u32 val, rpn;
7611
7612 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7613 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7614 FB_GFX_FREQ_FUSE_MASK);
7615
7616 return rpn;
7617}
7618
Deepak Sf8f2b002014-07-10 13:16:21 +05307619static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7620{
7621 u32 val, rp1;
7622
7623 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7624
7625 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7626
7627 return rp1;
7628}
7629
Ville Syrjälä03af2042014-06-28 02:03:53 +03007630static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007631{
7632 u32 val, rp0;
7633
Jani Nikula64936252013-05-22 15:36:20 +03007634 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007635
7636 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7637 /* Clamp to max */
7638 rp0 = min_t(u32, rp0, 0xea);
7639
7640 return rp0;
7641}
7642
7643static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7644{
7645 u32 val, rpe;
7646
Jani Nikula64936252013-05-22 15:36:20 +03007647 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007648 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007649 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007650 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7651
7652 return rpe;
7653}
7654
Ville Syrjälä03af2042014-06-28 02:03:53 +03007655static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007656{
Imre Deak36146032014-12-04 18:39:35 +02007657 u32 val;
7658
7659 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7660 /*
7661 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7662 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7663 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7664 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7665 * to make sure it matches what Punit accepts.
7666 */
7667 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007668}
7669
Imre Deakae484342014-03-31 15:10:44 +03007670/* Check that the pctx buffer wasn't move under us. */
7671static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
7672{
7673 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7674
Matthew Auld77894222017-12-11 15:18:18 +00007675 WARN_ON(pctx_addr != dev_priv->dsm.start +
Imre Deakae484342014-03-31 15:10:44 +03007676 dev_priv->vlv_pctx->stolen->start);
7677}
7678
Deepak S38807742014-05-23 21:00:15 +05307679
7680/* Check that the pcbr address is not empty. */
7681static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
7682{
7683 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7684
7685 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
7686}
7687
Chris Wilsondc979972016-05-10 14:10:04 +01007688static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307689{
Matthew Auldb7128ef2017-12-11 15:18:22 +00007690 resource_size_t pctx_paddr, paddr;
7691 resource_size_t pctx_size = 32*1024;
Deepak S38807742014-05-23 21:00:15 +05307692 u32 pcbr;
Deepak S38807742014-05-23 21:00:15 +05307693
Deepak S38807742014-05-23 21:00:15 +05307694 pcbr = I915_READ(VLV_PCBR);
7695 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007696 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Matthew Auld77894222017-12-11 15:18:18 +00007697 paddr = dev_priv->dsm.end + 1 - pctx_size;
7698 GEM_BUG_ON(paddr > U32_MAX);
Deepak S38807742014-05-23 21:00:15 +05307699
7700 pctx_paddr = (paddr & (~4095));
7701 I915_WRITE(VLV_PCBR, pctx_paddr);
7702 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007703
7704 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05307705}
7706
Chris Wilsondc979972016-05-10 14:10:04 +01007707static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007708{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007709 struct drm_i915_gem_object *pctx;
Matthew Auldb7128ef2017-12-11 15:18:22 +00007710 resource_size_t pctx_paddr;
7711 resource_size_t pctx_size = 24*1024;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007712 u32 pcbr;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007713
7714 pcbr = I915_READ(VLV_PCBR);
7715 if (pcbr) {
7716 /* BIOS set it up already, grab the pre-alloc'd space */
Matthew Auldb7128ef2017-12-11 15:18:22 +00007717 resource_size_t pcbr_offset;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007718
Matthew Auld77894222017-12-11 15:18:18 +00007719 pcbr_offset = (pcbr & (~4095)) - dev_priv->dsm.start;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007720 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007721 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02007722 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007723 pctx_size);
7724 goto out;
7725 }
7726
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007727 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
7728
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007729 /*
7730 * From the Gunit register HAS:
7731 * The Gfx driver is expected to program this register and ensure
7732 * proper allocation within Gfx stolen memory. For example, this
7733 * register should be programmed such than the PCBR range does not
7734 * overlap with other ranges, such as the frame buffer, protected
7735 * memory, or any other relevant ranges.
7736 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007737 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007738 if (!pctx) {
7739 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00007740 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007741 }
7742
Matthew Auld77894222017-12-11 15:18:18 +00007743 GEM_BUG_ON(range_overflows_t(u64,
7744 dev_priv->dsm.start,
7745 pctx->stolen->start,
7746 U32_MAX));
7747 pctx_paddr = dev_priv->dsm.start + pctx->stolen->start;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007748 I915_WRITE(VLV_PCBR, pctx_paddr);
7749
7750out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007751 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007752 dev_priv->vlv_pctx = pctx;
7753}
7754
Chris Wilsondc979972016-05-10 14:10:04 +01007755static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007756{
Chris Wilson818fed42018-07-12 11:54:54 +01007757 struct drm_i915_gem_object *pctx;
Imre Deakae484342014-03-31 15:10:44 +03007758
Chris Wilson818fed42018-07-12 11:54:54 +01007759 pctx = fetch_and_zero(&dev_priv->vlv_pctx);
7760 if (pctx)
7761 i915_gem_object_put(pctx);
Imre Deakae484342014-03-31 15:10:44 +03007762}
7763
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007764static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7765{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007766 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007767 vlv_get_cck_clock(dev_priv, "GPLL ref",
7768 CCK_GPLL_CLOCK_CONTROL,
7769 dev_priv->czclk_freq);
7770
7771 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007772 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007773}
7774
Chris Wilsondc979972016-05-10 14:10:04 +01007775static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007776{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007777 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007778 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007779
Chris Wilsondc979972016-05-10 14:10:04 +01007780 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007781
Chris Wilson337fa6e2019-04-26 09:17:20 +01007782 vlv_iosf_sb_get(dev_priv,
7783 BIT(VLV_IOSF_SB_PUNIT) |
7784 BIT(VLV_IOSF_SB_NC) |
7785 BIT(VLV_IOSF_SB_CCK));
7786
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007787 vlv_init_gpll_ref_freq(dev_priv);
7788
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007789 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7790 switch ((val >> 6) & 3) {
7791 case 0:
7792 case 1:
7793 dev_priv->mem_freq = 800;
7794 break;
7795 case 2:
7796 dev_priv->mem_freq = 1066;
7797 break;
7798 case 3:
7799 dev_priv->mem_freq = 1333;
7800 break;
7801 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007802 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007803
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007804 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7805 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007806 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007807 intel_gpu_freq(dev_priv, rps->max_freq),
7808 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007809
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007810 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007811 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007812 intel_gpu_freq(dev_priv, rps->efficient_freq),
7813 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007814
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007815 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307816 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007817 intel_gpu_freq(dev_priv, rps->rp1_freq),
7818 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307819
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007820 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007821 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007822 intel_gpu_freq(dev_priv, rps->min_freq),
7823 rps->min_freq);
Chris Wilson337fa6e2019-04-26 09:17:20 +01007824
7825 vlv_iosf_sb_put(dev_priv,
7826 BIT(VLV_IOSF_SB_PUNIT) |
7827 BIT(VLV_IOSF_SB_NC) |
7828 BIT(VLV_IOSF_SB_CCK));
Imre Deak4e805192014-04-14 20:24:41 +03007829}
7830
Chris Wilsondc979972016-05-10 14:10:04 +01007831static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307832{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007833 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007834 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307835
Chris Wilsondc979972016-05-10 14:10:04 +01007836 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307837
Chris Wilson337fa6e2019-04-26 09:17:20 +01007838 vlv_iosf_sb_get(dev_priv,
7839 BIT(VLV_IOSF_SB_PUNIT) |
7840 BIT(VLV_IOSF_SB_NC) |
7841 BIT(VLV_IOSF_SB_CCK));
7842
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007843 vlv_init_gpll_ref_freq(dev_priv);
7844
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007845 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007846
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007847 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007848 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007849 dev_priv->mem_freq = 2000;
7850 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007851 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007852 dev_priv->mem_freq = 1600;
7853 break;
7854 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007855 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007856
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007857 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7858 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307859 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007860 intel_gpu_freq(dev_priv, rps->max_freq),
7861 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307862
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007863 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307864 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007865 intel_gpu_freq(dev_priv, rps->efficient_freq),
7866 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307867
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007868 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307869 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007870 intel_gpu_freq(dev_priv, rps->rp1_freq),
7871 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307872
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007873 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307874 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007875 intel_gpu_freq(dev_priv, rps->min_freq),
7876 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307877
Chris Wilson337fa6e2019-04-26 09:17:20 +01007878 vlv_iosf_sb_put(dev_priv,
7879 BIT(VLV_IOSF_SB_PUNIT) |
7880 BIT(VLV_IOSF_SB_NC) |
7881 BIT(VLV_IOSF_SB_CCK));
7882
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007883 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7884 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007885 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307886}
7887
Chris Wilsondc979972016-05-10 14:10:04 +01007888static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007889{
Chris Wilsondc979972016-05-10 14:10:04 +01007890 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007891}
7892
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007893static void cherryview_enable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307894{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007895 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307896 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007897 u32 gtfifodbg, rc6_mode, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307898
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007899 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7900 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307901 if (gtfifodbg) {
7902 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7903 gtfifodbg);
7904 I915_WRITE(GTFIFODBG, gtfifodbg);
7905 }
7906
7907 cherryview_check_pctx(dev_priv);
7908
7909 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7910 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007911 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307912
Ville Syrjälä160614a2015-01-19 13:50:47 +02007913 /* Disable RC states. */
7914 I915_WRITE(GEN6_RC_CONTROL, 0);
7915
Deepak S38807742014-05-23 21:00:15 +05307916 /* 2a: Program RC6 thresholds.*/
7917 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7918 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7919 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7920
Akash Goel3b3f1652016-10-13 22:44:48 +05307921 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007922 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307923 I915_WRITE(GEN6_RC_SLEEP, 0);
7924
Deepak Sf4f71c72015-03-28 15:23:35 +05307925 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7926 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307927
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007928 /* Allows RC6 residency counter to work */
Deepak S38807742014-05-23 21:00:15 +05307929 I915_WRITE(VLV_COUNTER_CONTROL,
7930 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7931 VLV_MEDIA_RC6_COUNT_EN |
7932 VLV_RENDER_RC6_COUNT_EN));
7933
7934 /* For now we assume BIOS is allocating and populating the PCBR */
7935 pcbr = I915_READ(VLV_PCBR);
7936
Deepak S38807742014-05-23 21:00:15 +05307937 /* 3: Enable RC6 */
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007938 rc6_mode = 0;
7939 if (pcbr >> VLV_PCBR_ADDR_SHIFT)
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007940 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307941 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7942
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007943 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007944}
7945
7946static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7947{
7948 u32 val;
7949
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007950 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007951
7952 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007953 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307954 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7955 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7956 I915_WRITE(GEN6_RP_UP_EI, 66000);
7957 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7958
7959 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7960
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007961 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307962 I915_WRITE(GEN6_RP_CONTROL,
7963 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007964 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307965 GEN6_RP_ENABLE |
7966 GEN6_RP_UP_BUSY_AVG |
7967 GEN6_RP_DOWN_IDLE_AVG);
7968
Deepak S3ef62342015-04-29 08:36:24 +05307969 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007970 vlv_punit_get(dev_priv);
7971
7972 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
Deepak S3ef62342015-04-29 08:36:24 +05307973 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7974
Deepak S2b6b3a02014-05-27 15:59:30 +05307975 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7976
Chris Wilson337fa6e2019-04-26 09:17:20 +01007977 vlv_punit_put(dev_priv);
7978
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007979 /* RPS code assumes GPLL is used */
7980 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7981
Jani Nikula742f4912015-09-03 11:16:09 +03007982 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307983 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7984
Chris Wilson3a45b052016-07-13 09:10:32 +01007985 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307986
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007987 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307988}
7989
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007990static void valleyview_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007991{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007992 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307993 enum intel_engine_id id;
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00007994 u32 gtfifodbg;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007995
Imre Deakae484342014-03-31 15:10:44 +03007996 valleyview_check_pctx(dev_priv);
7997
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007998 gtfifodbg = I915_READ(GTFIFODBG);
7999 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07008000 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
8001 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008002 I915_WRITE(GTFIFODBG, gtfifodbg);
8003 }
8004
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008005 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008006
Ville Syrjälä160614a2015-01-19 13:50:47 +02008007 /* Disable RC states. */
8008 I915_WRITE(GEN6_RC_CONTROL, 0);
8009
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008010 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
8011 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
8012 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
8013
8014 for_each_engine(engine, dev_priv, id)
8015 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
8016
8017 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
8018
8019 /* Allows RC6 residency counter to work */
8020 I915_WRITE(VLV_COUNTER_CONTROL,
8021 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
8022 VLV_MEDIA_RC0_COUNT_EN |
8023 VLV_RENDER_RC0_COUNT_EN |
8024 VLV_MEDIA_RC6_COUNT_EN |
8025 VLV_RENDER_RC6_COUNT_EN));
8026
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008027 I915_WRITE(GEN6_RC_CONTROL,
8028 GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008029
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008030 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008031}
8032
8033static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
8034{
8035 u32 val;
8036
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008037 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01008038
Ville Syrjäläcad725f2015-01-19 13:50:48 +02008039 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008040 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
8041 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
8042 I915_WRITE(GEN6_RP_UP_EI, 66000);
8043 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
8044
8045 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
8046
8047 I915_WRITE(GEN6_RP_CONTROL,
8048 GEN6_RP_MEDIA_TURBO |
8049 GEN6_RP_MEDIA_HW_NORMAL_MODE |
8050 GEN6_RP_MEDIA_IS_GFX |
8051 GEN6_RP_ENABLE |
8052 GEN6_RP_UP_BUSY_AVG |
8053 GEN6_RP_DOWN_IDLE_CONT);
8054
Chris Wilson337fa6e2019-04-26 09:17:20 +01008055 vlv_punit_get(dev_priv);
8056
Deepak S3ef62342015-04-29 08:36:24 +05308057 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01008058 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
Deepak S3ef62342015-04-29 08:36:24 +05308059 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
8060
Jani Nikula64936252013-05-22 15:36:20 +03008061 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008062
Chris Wilson337fa6e2019-04-26 09:17:20 +01008063 vlv_punit_put(dev_priv);
8064
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02008065 /* RPS code assumes GPLL is used */
8066 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
8067
Jani Nikula742f4912015-09-03 11:16:09 +03008068 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07008069 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
8070
Chris Wilson3a45b052016-07-13 09:10:32 +01008071 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008072
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07008073 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008074}
8075
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008076static unsigned long intel_pxfreq(u32 vidfreq)
8077{
8078 unsigned long freq;
8079 int div = (vidfreq & 0x3f0000) >> 16;
8080 int post = (vidfreq & 0x3000) >> 12;
8081 int pre = (vidfreq & 0x7);
8082
8083 if (!pre)
8084 return 0;
8085
8086 freq = ((div * 133333) / ((1<<post) * pre));
8087
8088 return freq;
8089}
8090
Daniel Vettereb48eb02012-04-26 23:28:12 +02008091static const struct cparams {
8092 u16 i;
8093 u16 t;
8094 u16 m;
8095 u16 c;
8096} cparams[] = {
8097 { 1, 1333, 301, 28664 },
8098 { 1, 1066, 294, 24460 },
8099 { 1, 800, 294, 25192 },
8100 { 0, 1333, 276, 27605 },
8101 { 0, 1066, 276, 27605 },
8102 { 0, 800, 231, 23784 },
8103};
8104
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008105static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008106{
8107 u64 total_count, diff, ret;
8108 u32 count1, count2, count3, m = 0, c = 0;
8109 unsigned long now = jiffies_to_msecs(jiffies), diff1;
8110 int i;
8111
Chris Wilson67520412017-03-02 13:28:01 +00008112 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02008113
Daniel Vetter20e4d402012-08-08 23:35:39 +02008114 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008115
8116 /* Prevent division-by-zero if we are asking too fast.
8117 * Also, we don't get interesting results if we are polling
8118 * faster than once in 10ms, so just return the saved value
8119 * in such cases.
8120 */
8121 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02008122 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008123
8124 count1 = I915_READ(DMIEC);
8125 count2 = I915_READ(DDREC);
8126 count3 = I915_READ(CSIEC);
8127
8128 total_count = count1 + count2 + count3;
8129
8130 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02008131 if (total_count < dev_priv->ips.last_count1) {
8132 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008133 diff += total_count;
8134 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02008135 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008136 }
8137
8138 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02008139 if (cparams[i].i == dev_priv->ips.c_m &&
8140 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02008141 m = cparams[i].m;
8142 c = cparams[i].c;
8143 break;
8144 }
8145 }
8146
8147 diff = div_u64(diff, diff1);
8148 ret = ((m * diff) + c);
8149 ret = div_u64(ret, 10);
8150
Daniel Vetter20e4d402012-08-08 23:35:39 +02008151 dev_priv->ips.last_count1 = total_count;
8152 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008153
Daniel Vetter20e4d402012-08-08 23:35:39 +02008154 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008155
8156 return ret;
8157}
8158
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008159unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
8160{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008161 intel_wakeref_t wakeref;
8162 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008163
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008164 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008165 return 0;
8166
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008167 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008168 spin_lock_irq(&mchdev_lock);
8169 val = __i915_chipset_val(dev_priv);
8170 spin_unlock_irq(&mchdev_lock);
8171 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008172
8173 return val;
8174}
8175
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01008176unsigned long i915_mch_val(struct drm_i915_private *i915)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008177{
8178 unsigned long m, x, b;
8179 u32 tsfs;
8180
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01008181 tsfs = intel_uncore_read(&i915->uncore, TSFS);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008182
8183 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01008184 x = intel_uncore_read8(&i915->uncore, TR1);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008185
8186 b = tsfs & TSFS_INTR_MASK;
8187
8188 return ((m * x) / 127) - b;
8189}
8190
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02008191static int _pxvid_to_vd(u8 pxvid)
8192{
8193 if (pxvid == 0)
8194 return 0;
8195
8196 if (pxvid >= 8 && pxvid < 31)
8197 pxvid = 31;
8198
8199 return (pxvid + 2) * 125;
8200}
8201
8202static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008203{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02008204 const int vd = _pxvid_to_vd(pxvid);
8205 const int vm = vd - 1125;
8206
Chris Wilsondc979972016-05-10 14:10:04 +01008207 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02008208 return vm > 0 ? vm : 0;
8209
8210 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008211}
8212
Daniel Vetter02d71952012-08-09 16:44:54 +02008213static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008214{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00008215 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008216 u32 count;
8217
Chris Wilson67520412017-03-02 13:28:01 +00008218 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008219
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00008220 now = ktime_get_raw_ns();
8221 diffms = now - dev_priv->ips.last_time2;
8222 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008223
8224 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02008225 if (!diffms)
8226 return;
8227
8228 count = I915_READ(GFXEC);
8229
Daniel Vetter20e4d402012-08-08 23:35:39 +02008230 if (count < dev_priv->ips.last_count2) {
8231 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008232 diff += count;
8233 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02008234 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008235 }
8236
Daniel Vetter20e4d402012-08-08 23:35:39 +02008237 dev_priv->ips.last_count2 = count;
8238 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008239
8240 /* More magic constants... */
8241 diff = diff * 1181;
8242 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02008243 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008244}
8245
Daniel Vetter02d71952012-08-09 16:44:54 +02008246void i915_update_gfx_val(struct drm_i915_private *dev_priv)
8247{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008248 intel_wakeref_t wakeref;
8249
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008250 if (!IS_GEN(dev_priv, 5))
Daniel Vetter02d71952012-08-09 16:44:54 +02008251 return;
8252
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008253 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008254 spin_lock_irq(&mchdev_lock);
8255 __i915_update_gfx_val(dev_priv);
8256 spin_unlock_irq(&mchdev_lock);
8257 }
Daniel Vetter02d71952012-08-09 16:44:54 +02008258}
8259
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008260static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02008261{
8262 unsigned long t, corr, state1, corr2, state2;
8263 u32 pxvid, ext_v;
8264
Chris Wilson67520412017-03-02 13:28:01 +00008265 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02008266
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008267 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02008268 pxvid = (pxvid >> 24) & 0x7f;
8269 ext_v = pvid_to_extvid(dev_priv, pxvid);
8270
8271 state1 = ext_v;
8272
8273 t = i915_mch_val(dev_priv);
8274
8275 /* Revel in the empirically derived constants */
8276
8277 /* Correction factor in 1/100000 units */
8278 if (t > 80)
8279 corr = ((t * 2349) + 135940);
8280 else if (t >= 50)
8281 corr = ((t * 964) + 29317);
8282 else /* < 50 */
8283 corr = ((t * 301) + 1004);
8284
8285 corr = corr * ((150142 * state1) / 10000 - 78642);
8286 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02008287 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008288
8289 state2 = (corr2 * state1) / 10000;
8290 state2 /= 100; /* convert to mW */
8291
Daniel Vetter02d71952012-08-09 16:44:54 +02008292 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008293
Daniel Vetter20e4d402012-08-08 23:35:39 +02008294 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008295}
8296
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008297unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
8298{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008299 intel_wakeref_t wakeref;
8300 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008301
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008302 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008303 return 0;
8304
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008305 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008306 spin_lock_irq(&mchdev_lock);
8307 val = __i915_gfx_val(dev_priv);
8308 spin_unlock_irq(&mchdev_lock);
8309 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01008310
8311 return val;
8312}
8313
Chris Wilsonadc674c2019-04-12 09:53:22 +01008314static struct drm_i915_private __rcu *i915_mch_dev;
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008315
8316static struct drm_i915_private *mchdev_get(void)
8317{
8318 struct drm_i915_private *i915;
8319
8320 rcu_read_lock();
Chris Wilsonadc674c2019-04-12 09:53:22 +01008321 i915 = rcu_dereference(i915_mch_dev);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008322 if (!kref_get_unless_zero(&i915->drm.ref))
8323 i915 = NULL;
8324 rcu_read_unlock();
8325
8326 return i915;
8327}
8328
Daniel Vettereb48eb02012-04-26 23:28:12 +02008329/**
8330 * i915_read_mch_val - return value for IPS use
8331 *
8332 * Calculate and return a value for the IPS driver to use when deciding whether
8333 * we have thermal and power headroom to increase CPU or GPU power budget.
8334 */
8335unsigned long i915_read_mch_val(void)
8336{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008337 struct drm_i915_private *i915;
8338 unsigned long chipset_val = 0;
8339 unsigned long graphics_val = 0;
8340 intel_wakeref_t wakeref;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008341
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008342 i915 = mchdev_get();
8343 if (!i915)
8344 return 0;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008345
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07008346 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008347 spin_lock_irq(&mchdev_lock);
8348 chipset_val = __i915_chipset_val(i915);
8349 graphics_val = __i915_gfx_val(i915);
8350 spin_unlock_irq(&mchdev_lock);
8351 }
Daniel Vettereb48eb02012-04-26 23:28:12 +02008352
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008353 drm_dev_put(&i915->drm);
8354 return chipset_val + graphics_val;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008355}
8356EXPORT_SYMBOL_GPL(i915_read_mch_val);
8357
8358/**
8359 * i915_gpu_raise - raise GPU frequency limit
8360 *
8361 * Raise the limit; IPS indicates we have thermal headroom.
8362 */
8363bool i915_gpu_raise(void)
8364{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008365 struct drm_i915_private *i915;
8366
8367 i915 = mchdev_get();
8368 if (!i915)
8369 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008370
Daniel Vetter92703882012-08-09 16:46:01 +02008371 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008372 if (i915->ips.max_delay > i915->ips.fmax)
8373 i915->ips.max_delay--;
Daniel Vetter92703882012-08-09 16:46:01 +02008374 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008375
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008376 drm_dev_put(&i915->drm);
8377 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008378}
8379EXPORT_SYMBOL_GPL(i915_gpu_raise);
8380
8381/**
8382 * i915_gpu_lower - lower GPU frequency limit
8383 *
8384 * IPS indicates we're close to a thermal limit, so throttle back the GPU
8385 * frequency maximum.
8386 */
8387bool i915_gpu_lower(void)
8388{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008389 struct drm_i915_private *i915;
8390
8391 i915 = mchdev_get();
8392 if (!i915)
8393 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008394
Daniel Vetter92703882012-08-09 16:46:01 +02008395 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008396 if (i915->ips.max_delay < i915->ips.min_delay)
8397 i915->ips.max_delay++;
Daniel Vetter92703882012-08-09 16:46:01 +02008398 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008399
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008400 drm_dev_put(&i915->drm);
8401 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008402}
8403EXPORT_SYMBOL_GPL(i915_gpu_lower);
8404
8405/**
8406 * i915_gpu_busy - indicate GPU business to IPS
8407 *
8408 * Tell the IPS driver whether or not the GPU is busy.
8409 */
8410bool i915_gpu_busy(void)
8411{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008412 struct drm_i915_private *i915;
8413 bool ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008414
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008415 i915 = mchdev_get();
8416 if (!i915)
8417 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008418
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008419 ret = i915->gt.awake;
8420
8421 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008422 return ret;
8423}
8424EXPORT_SYMBOL_GPL(i915_gpu_busy);
8425
8426/**
8427 * i915_gpu_turbo_disable - disable graphics turbo
8428 *
8429 * Disable graphics turbo by resetting the max frequency and setting the
8430 * current frequency to the default.
8431 */
8432bool i915_gpu_turbo_disable(void)
8433{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008434 struct drm_i915_private *i915;
8435 bool ret;
8436
8437 i915 = mchdev_get();
8438 if (!i915)
8439 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02008440
Daniel Vetter92703882012-08-09 16:46:01 +02008441 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008442 i915->ips.max_delay = i915->ips.fstart;
8443 ret = ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02008444 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008445
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008446 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008447 return ret;
8448}
8449EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
8450
8451/**
8452 * Tells the intel_ips driver that the i915 driver is now loaded, if
8453 * IPS got loaded first.
8454 *
8455 * This awkward dance is so that neither module has to depend on the
8456 * other in order for IPS to do the appropriate communication of
8457 * GPU turbo limits to i915.
8458 */
8459static void
8460ips_ping_for_i915_load(void)
8461{
8462 void (*link)(void);
8463
8464 link = symbol_get(ips_link_to_i915_driver);
8465 if (link) {
8466 link();
8467 symbol_put(ips_link_to_i915_driver);
8468 }
8469}
8470
8471void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
8472{
Daniel Vetter02d71952012-08-09 16:44:54 +02008473 /* We only register the i915 ips part with intel-ips once everything is
8474 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008475 rcu_assign_pointer(i915_mch_dev, dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008476
8477 ips_ping_for_i915_load();
8478}
8479
8480void intel_gpu_ips_teardown(void)
8481{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00008482 rcu_assign_pointer(i915_mch_dev, NULL);
Daniel Vettereb48eb02012-04-26 23:28:12 +02008483}
Deepak S76c3552f2014-01-30 23:08:16 +05308484
Chris Wilsondc979972016-05-10 14:10:04 +01008485static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008486{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008487 u32 lcfuse;
8488 u8 pxw[16];
8489 int i;
8490
8491 /* Disable to program */
8492 I915_WRITE(ECR, 0);
8493 POSTING_READ(ECR);
8494
8495 /* Program energy weights for various events */
8496 I915_WRITE(SDEW, 0x15040d00);
8497 I915_WRITE(CSIEW0, 0x007f0000);
8498 I915_WRITE(CSIEW1, 0x1e220004);
8499 I915_WRITE(CSIEW2, 0x04000004);
8500
8501 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03008502 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008503 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03008504 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008505
8506 /* Program P-state weights to account for frequency power adjustment */
8507 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03008508 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008509 unsigned long freq = intel_pxfreq(pxvidfreq);
8510 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
8511 PXVFREQ_PX_SHIFT;
8512 unsigned long val;
8513
8514 val = vid * vid;
8515 val *= (freq / 1000);
8516 val *= 255;
8517 val /= (127*127*900);
8518 if (val > 0xff)
8519 DRM_ERROR("bad pxval: %ld\n", val);
8520 pxw[i] = val;
8521 }
8522 /* Render standby states get 0 weight */
8523 pxw[14] = 0;
8524 pxw[15] = 0;
8525
8526 for (i = 0; i < 4; i++) {
8527 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
8528 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03008529 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008530 }
8531
8532 /* Adjust magic regs to magic values (more experimental results) */
8533 I915_WRITE(OGW0, 0);
8534 I915_WRITE(OGW1, 0);
8535 I915_WRITE(EG0, 0x00007f00);
8536 I915_WRITE(EG1, 0x0000000e);
8537 I915_WRITE(EG2, 0x000e0000);
8538 I915_WRITE(EG3, 0x68000300);
8539 I915_WRITE(EG4, 0x42000000);
8540 I915_WRITE(EG5, 0x00140031);
8541 I915_WRITE(EG6, 0);
8542 I915_WRITE(EG7, 0);
8543
8544 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03008545 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008546
8547 /* Enable PMON + select events */
8548 I915_WRITE(ECR, 0x80000019);
8549
8550 lcfuse = I915_READ(LCFUSE02);
8551
Daniel Vetter20e4d402012-08-08 23:35:39 +02008552 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03008553}
8554
Chris Wilsondc979972016-05-10 14:10:04 +01008555void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03008556{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008557 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8558
Imre Deakb268c692015-12-15 20:10:31 +02008559 /*
8560 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
8561 * requirement.
8562 */
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008563 if (!sanitize_rc6(dev_priv)) {
Imre Deakb268c692015-12-15 20:10:31 +02008564 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
Chris Wilson08ea70a2018-08-12 23:36:31 +01008565 pm_runtime_get(&dev_priv->drm.pdev->dev);
Imre Deakb268c692015-12-15 20:10:31 +02008566 }
Imre Deake6069ca2014-04-18 16:01:02 +03008567
Chris Wilson773ea9a2016-07-13 09:10:33 +01008568 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01008569 if (IS_CHERRYVIEW(dev_priv))
8570 cherryview_init_gt_powersave(dev_priv);
8571 else if (IS_VALLEYVIEW(dev_priv))
8572 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01008573 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01008574 gen6_init_rps_frequencies(dev_priv);
8575
8576 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008577 rps->max_freq_softlimit = rps->max_freq;
8578 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01008579
Chris Wilson99ac9612016-07-13 09:10:34 +01008580 /* After setting max-softlimit, find the overclock max freq */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008581 if (IS_GEN(dev_priv, 6) ||
Chris Wilson99ac9612016-07-13 09:10:34 +01008582 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
8583 u32 params = 0;
8584
Ville Syrjäläd284d512019-05-21 19:40:24 +03008585 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS,
8586 &params, NULL);
Chris Wilson99ac9612016-07-13 09:10:34 +01008587 if (params & BIT(31)) { /* OC supported */
8588 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008589 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01008590 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008591 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01008592 }
8593 }
8594
Chris Wilson29ecd78d2016-07-13 09:10:35 +01008595 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008596 rps->boost_freq = rps->max_freq;
Chris Wilson844e3312019-04-18 21:53:58 +01008597 rps->idle_freq = rps->min_freq;
8598 rps->cur_freq = rps->idle_freq;
Imre Deakae484342014-03-31 15:10:44 +03008599}
8600
Chris Wilsondc979972016-05-10 14:10:04 +01008601void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03008602{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03008603 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01008604 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02008605
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008606 if (!HAS_RC6(dev_priv))
Chris Wilson08ea70a2018-08-12 23:36:31 +01008607 pm_runtime_put(&dev_priv->drm.pdev->dev);
Imre Deakae484342014-03-31 15:10:44 +03008608}
8609
Chris Wilsonb7137e02016-07-13 09:10:37 +01008610void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
8611{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008612 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
8613 dev_priv->gt_pm.rc6.enabled = true; /* force RC6 disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01008614 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008615
Oscar Mateod02b98b2018-04-05 17:00:50 +03008616 if (INTEL_GEN(dev_priv) >= 11)
8617 gen11_reset_rps_interrupts(dev_priv);
Chris Wilson61e1e372018-08-12 23:36:30 +01008618 else if (INTEL_GEN(dev_priv) >= 6)
Oscar Mateod02b98b2018-04-05 17:00:50 +03008619 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07008620}
8621
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008622static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
8623{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008624 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008625
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008626 if (!i915->gt_pm.llc_pstate.enabled)
8627 return;
8628
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008629 /* Currently there is no HW configuration to be done to disable. */
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008630
8631 i915->gt_pm.llc_pstate.enabled = false;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008632}
8633
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008634static void intel_disable_rc6(struct drm_i915_private *dev_priv)
8635{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008636 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008637
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008638 if (!dev_priv->gt_pm.rc6.enabled)
8639 return;
8640
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008641 if (INTEL_GEN(dev_priv) >= 9)
8642 gen9_disable_rc6(dev_priv);
8643 else if (IS_CHERRYVIEW(dev_priv))
8644 cherryview_disable_rc6(dev_priv);
8645 else if (IS_VALLEYVIEW(dev_priv))
8646 valleyview_disable_rc6(dev_priv);
8647 else if (INTEL_GEN(dev_priv) >= 6)
8648 gen6_disable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008649
8650 dev_priv->gt_pm.rc6.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008651}
8652
8653static void intel_disable_rps(struct drm_i915_private *dev_priv)
8654{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008655 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008656
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008657 if (!dev_priv->gt_pm.rps.enabled)
8658 return;
8659
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008660 if (INTEL_GEN(dev_priv) >= 9)
8661 gen9_disable_rps(dev_priv);
8662 else if (IS_CHERRYVIEW(dev_priv))
8663 cherryview_disable_rps(dev_priv);
8664 else if (IS_VALLEYVIEW(dev_priv))
8665 valleyview_disable_rps(dev_priv);
8666 else if (INTEL_GEN(dev_priv) >= 6)
8667 gen6_disable_rps(dev_priv);
8668 else if (IS_IRONLAKE_M(dev_priv))
8669 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008670
8671 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008672}
8673
Chris Wilsondc979972016-05-10 14:10:04 +01008674void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008675{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008676 mutex_lock(&dev_priv->gt_pm.rps.lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008677
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008678 intel_disable_rc6(dev_priv);
8679 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008680 if (HAS_LLC(dev_priv))
8681 intel_disable_llc_pstate(dev_priv);
8682
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008683 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008684}
8685
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008686static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
8687{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008688 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008689
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008690 if (i915->gt_pm.llc_pstate.enabled)
8691 return;
8692
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008693 gen6_update_ring_freq(i915);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008694
8695 i915->gt_pm.llc_pstate.enabled = true;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008696}
8697
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008698static void intel_enable_rc6(struct drm_i915_private *dev_priv)
8699{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008700 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008701
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008702 if (dev_priv->gt_pm.rc6.enabled)
8703 return;
8704
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008705 if (IS_CHERRYVIEW(dev_priv))
8706 cherryview_enable_rc6(dev_priv);
8707 else if (IS_VALLEYVIEW(dev_priv))
8708 valleyview_enable_rc6(dev_priv);
Mika Kuoppalaa79208d2019-04-10 13:59:17 +03008709 else if (INTEL_GEN(dev_priv) >= 11)
8710 gen11_enable_rc6(dev_priv);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008711 else if (INTEL_GEN(dev_priv) >= 9)
8712 gen9_enable_rc6(dev_priv);
8713 else if (IS_BROADWELL(dev_priv))
8714 gen8_enable_rc6(dev_priv);
8715 else if (INTEL_GEN(dev_priv) >= 6)
8716 gen6_enable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008717
8718 dev_priv->gt_pm.rc6.enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008719}
8720
8721static void intel_enable_rps(struct drm_i915_private *dev_priv)
8722{
8723 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8724
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008725 lockdep_assert_held(&rps->lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008726
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008727 if (rps->enabled)
8728 return;
8729
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008730 if (IS_CHERRYVIEW(dev_priv)) {
8731 cherryview_enable_rps(dev_priv);
8732 } else if (IS_VALLEYVIEW(dev_priv)) {
8733 valleyview_enable_rps(dev_priv);
8734 } else if (INTEL_GEN(dev_priv) >= 9) {
8735 gen9_enable_rps(dev_priv);
8736 } else if (IS_BROADWELL(dev_priv)) {
8737 gen8_enable_rps(dev_priv);
8738 } else if (INTEL_GEN(dev_priv) >= 6) {
8739 gen6_enable_rps(dev_priv);
8740 } else if (IS_IRONLAKE_M(dev_priv)) {
8741 ironlake_enable_drps(dev_priv);
8742 intel_init_emon(dev_priv);
8743 }
8744
8745 WARN_ON(rps->max_freq < rps->min_freq);
8746 WARN_ON(rps->idle_freq > rps->max_freq);
8747
8748 WARN_ON(rps->efficient_freq < rps->min_freq);
8749 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008750
8751 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008752}
8753
Chris Wilsonb7137e02016-07-13 09:10:37 +01008754void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
8755{
Chris Wilsonb7137e02016-07-13 09:10:37 +01008756 /* Powersaving is controlled by the host when inside a VM */
8757 if (intel_vgpu_active(dev_priv))
8758 return;
8759
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008760 mutex_lock(&dev_priv->gt_pm.rps.lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02008761
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00008762 if (HAS_RC6(dev_priv))
8763 intel_enable_rc6(dev_priv);
Chris Wilson91cbdb82019-04-19 14:48:36 +01008764 if (HAS_RPS(dev_priv))
8765 intel_enable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008766 if (HAS_LLC(dev_priv))
8767 intel_enable_llc_pstate(dev_priv);
8768
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008769 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008770}
Imre Deakc6df39b2014-04-14 20:24:29 +03008771
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008772static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008773{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008774 /*
8775 * On Ibex Peak and Cougar Point, we need to disable clock
8776 * gating for the panel power sequencer or it will fail to
8777 * start up when no ports are active.
8778 */
8779 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8780}
8781
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008782static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008783{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008784 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008785
Damien Lespiau055e3932014-08-18 13:49:10 +01008786 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008787 I915_WRITE(DSPCNTR(pipe),
8788 I915_READ(DSPCNTR(pipe)) |
8789 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008790
8791 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8792 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008793 }
8794}
8795
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008796static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008797{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008798 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008799
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008800 /*
8801 * Required for FBC
8802 * WaFbcDisableDpfcClockGating:ilk
8803 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008804 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8805 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8806 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008807
8808 I915_WRITE(PCH_3DCGDIS0,
8809 MARIUNIT_CLOCK_GATE_DISABLE |
8810 SVSMUNIT_CLOCK_GATE_DISABLE);
8811 I915_WRITE(PCH_3DCGDIS1,
8812 VFMUNIT_CLOCK_GATE_DISABLE);
8813
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008814 /*
8815 * According to the spec the following bits should be set in
8816 * order to enable memory self-refresh
8817 * The bit 22/21 of 0x42004
8818 * The bit 5 of 0x42020
8819 * The bit 15 of 0x45000
8820 */
8821 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8822 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8823 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008824 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008825 I915_WRITE(DISP_ARB_CTL,
8826 (I915_READ(DISP_ARB_CTL) |
8827 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008828
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008829 /*
8830 * Based on the document from hardware guys the following bits
8831 * should be set unconditionally in order to enable FBC.
8832 * The bit 22 of 0x42000
8833 * The bit 22 of 0x42004
8834 * The bit 7,8,9 of 0x42020.
8835 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008836 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008837 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008838 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8839 I915_READ(ILK_DISPLAY_CHICKEN1) |
8840 ILK_FBCQ_DIS);
8841 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8842 I915_READ(ILK_DISPLAY_CHICKEN2) |
8843 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008844 }
8845
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008846 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8847
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008848 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8849 I915_READ(ILK_DISPLAY_CHICKEN2) |
8850 ILK_ELPIN_409_SELECT);
8851 I915_WRITE(_3D_CHICKEN2,
8852 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8853 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008854
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008855 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008856 I915_WRITE(CACHE_MODE_0,
8857 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008858
Akash Goel4e046322014-04-04 17:14:38 +05308859 /* WaDisable_RenderCache_OperationalFlush:ilk */
8860 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8861
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008862 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008863
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008864 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008865}
8866
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008867static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008868{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008869 int pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008870 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008871
8872 /*
8873 * On Ibex Peak and Cougar Point, we need to disable clock
8874 * gating for the panel power sequencer or it will fail to
8875 * start up when no ports are active.
8876 */
Jesse Barnescd664072013-10-02 10:34:19 -07008877 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8878 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8879 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008880 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8881 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008882 /* The below fixes the weird display corruption, a few pixels shifted
8883 * downward, on (only) LVDS of some HP laptops with IVY.
8884 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008885 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008886 val = I915_READ(TRANS_CHICKEN2(pipe));
8887 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8888 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008889 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008890 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008891 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8892 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8893 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008894 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8895 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008896 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008897 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008898 I915_WRITE(TRANS_CHICKEN1(pipe),
8899 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8900 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008901}
8902
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008903static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008904{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008905 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008906
8907 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008908 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8909 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8910 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008911}
8912
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008913static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008914{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008915 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008916
Damien Lespiau231e54f2012-10-19 17:55:41 +01008917 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008918
8919 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8920 I915_READ(ILK_DISPLAY_CHICKEN2) |
8921 ILK_ELPIN_409_SELECT);
8922
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008923 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008924 I915_WRITE(_3D_CHICKEN,
8925 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8926
Akash Goel4e046322014-04-04 17:14:38 +05308927 /* WaDisable_RenderCache_OperationalFlush:snb */
8928 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8929
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008930 /*
8931 * BSpec recoomends 8x4 when MSAA is used,
8932 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008933 *
8934 * Note that PS/WM thread counts depend on the WIZ hashing
8935 * disable bit, which we don't touch here, but it's good
8936 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008937 */
8938 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008939 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008940
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008941 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008942 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008943
8944 I915_WRITE(GEN6_UCGCTL1,
8945 I915_READ(GEN6_UCGCTL1) |
8946 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8947 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8948
8949 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8950 * gating disable must be set. Failure to set it results in
8951 * flickering pixels due to Z write ordering failures after
8952 * some amount of runtime in the Mesa "fire" demo, and Unigine
8953 * Sanctuary and Tropics, and apparently anything else with
8954 * alpha test or pixel discard.
8955 *
8956 * According to the spec, bit 11 (RCCUNIT) must also be set,
8957 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008958 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008959 * WaDisableRCCUnitClockGating:snb
8960 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008961 */
8962 I915_WRITE(GEN6_UCGCTL2,
8963 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8964 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8965
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008966 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008967 I915_WRITE(_3D_CHICKEN3,
8968 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008969
8970 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008971 * Bspec says:
8972 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8973 * 3DSTATE_SF number of SF output attributes is more than 16."
8974 */
8975 I915_WRITE(_3D_CHICKEN3,
8976 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8977
8978 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008979 * According to the spec the following bits should be
8980 * set in order to enable memory self-refresh and fbc:
8981 * The bit21 and bit22 of 0x42000
8982 * The bit21 and bit22 of 0x42004
8983 * The bit5 and bit7 of 0x42020
8984 * The bit14 of 0x70180
8985 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008986 *
8987 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008988 */
8989 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8990 I915_READ(ILK_DISPLAY_CHICKEN1) |
8991 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8992 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8993 I915_READ(ILK_DISPLAY_CHICKEN2) |
8994 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008995 I915_WRITE(ILK_DSPCLK_GATE_D,
8996 I915_READ(ILK_DSPCLK_GATE_D) |
8997 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8998 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008999
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009000 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07009001
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009002 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01009003
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009004 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009005}
9006
9007static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
9008{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009009 u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009010
Ville Syrjälä3aad9052014-01-22 21:32:59 +02009011 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02009012 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02009013 *
9014 * This actually overrides the dispatch
9015 * mode for all thread types.
9016 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009017 reg &= ~GEN7_FF_SCHED_MASK;
9018 reg |= GEN7_FF_TS_SCHED_HW;
9019 reg |= GEN7_FF_VS_SCHED_HW;
9020 reg |= GEN7_FF_DS_SCHED_HW;
9021
9022 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
9023}
9024
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009025static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009026{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009027 /*
9028 * TODO: this bit should only be enabled when really needed, then
9029 * disabled when not needed anymore in order to save power.
9030 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009031 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009032 I915_WRITE(SOUTH_DSPCLK_GATE_D,
9033 I915_READ(SOUTH_DSPCLK_GATE_D) |
9034 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03009035
9036 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03009037 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
9038 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03009039 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02009040}
9041
Ville Syrjälä712bf362016-10-31 22:37:23 +02009042static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03009043{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009044 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009045 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03009046
9047 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9048 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9049 }
9050}
9051
Imre Deak450174f2016-05-03 15:54:21 +03009052static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
9053 int general_prio_credits,
9054 int high_prio_credits)
9055{
9056 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07009057 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03009058
9059 /* WaTempDisableDOPClkGating:bdw */
9060 misccpctl = I915_READ(GEN7_MISCCPCTL);
9061 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
9062
Oscar Mateo930a7842017-10-17 13:25:45 -07009063 val = I915_READ(GEN8_L3SQCREG1);
9064 val &= ~L3_PRIO_CREDITS_MASK;
9065 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
9066 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
9067 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03009068
9069 /*
9070 * Wait at least 100 clocks before re-enabling clock gating.
9071 * See the definition of L3SQCREG1 in BSpec.
9072 */
9073 POSTING_READ(GEN8_L3SQCREG1);
9074 udelay(1);
9075 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
9076}
9077
Oscar Mateod65dc3e2018-05-08 14:29:24 -07009078static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
9079{
9080 /* This is not an Wa. Enable to reduce Sampler power */
9081 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
9082 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07009083
9084 /* WaEnable32PlaneMode:icl */
9085 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
9086 _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
Oscar Mateod65dc3e2018-05-08 14:29:24 -07009087}
9088
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009089static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
9090{
9091 if (!HAS_PCH_CNP(dev_priv))
9092 return;
9093
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08009094 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07009095 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
9096 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009097}
9098
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009099static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009100{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07009101 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009102 cnp_init_clock_gating(dev_priv);
9103
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07009104 /* This is not an Wa. Enable for better image quality */
9105 I915_WRITE(_3D_CHICKEN3,
9106 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
9107
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009108 /* WaEnableChickenDCPR:cnl */
9109 I915_WRITE(GEN8_CHICKEN_DCPR_1,
9110 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
9111
9112 /* WaFbcWakeMemOn:cnl */
9113 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
9114 DISP_FBC_MEMORY_WAKE);
9115
Chris Wilson34991bd2017-11-11 10:03:36 +00009116 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
9117 /* ReadHitWriteOnlyDisable:cnl */
9118 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009119 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
9120 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00009121 val |= SARBUNIT_CLKGATE_DIS;
9122 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08009123
Rodrigo Vivia4713c52018-03-07 14:09:12 -08009124 /* Wa_2201832410:cnl */
9125 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
9126 val |= GWUNIT_CLKGATE_DIS;
9127 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
9128
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08009129 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08009130 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08009131 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
9132 val |= VFUNIT_CLKGATE_DIS;
9133 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009134}
9135
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009136static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
9137{
9138 cnp_init_clock_gating(dev_priv);
9139 gen9_init_clock_gating(dev_priv);
9140
9141 /* WaFbcNukeOnHostModify:cfl */
9142 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
9143 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
9144}
9145
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009146static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03009147{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009148 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03009149
9150 /* WaDisableSDEUnitClockGating:kbl */
9151 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
9152 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
9153 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03009154
9155 /* WaDisableGamClockGating:kbl */
9156 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
9157 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
9158 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03009159
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009160 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03009161 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
9162 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03009163}
9164
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009165static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02009166{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009167 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03009168
9169 /* WAC6entrylatency:skl */
9170 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
9171 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03009172
9173 /* WaFbcNukeOnHostModify:skl */
9174 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
9175 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02009176}
9177
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009178static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07009179{
Damien Lespiau07d27e22014-03-03 17:31:46 +00009180 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07009181
Ben Widawskyab57fff2013-12-12 15:28:04 -08009182 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07009183 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07009184
Ben Widawskyab57fff2013-12-12 15:28:04 -08009185 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07009186 I915_WRITE(CHICKEN_PAR1_1,
9187 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
9188
Ben Widawskyab57fff2013-12-12 15:28:04 -08009189 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01009190 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00009191 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02009192 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02009193 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07009194 }
Ben Widawsky63801f22013-12-12 17:26:03 -08009195
Ben Widawskyab57fff2013-12-12 15:28:04 -08009196 /* WaVSRefCountFullforceMissDisable:bdw */
9197 /* WaDSRefCountFullforceMissDisable:bdw */
9198 I915_WRITE(GEN7_FF_THREAD_MODE,
9199 I915_READ(GEN7_FF_THREAD_MODE) &
9200 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02009201
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02009202 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
9203 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02009204
9205 /* WaDisableSDEUnitClockGating:bdw */
9206 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
9207 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00009208
Imre Deak450174f2016-05-03 15:54:21 +03009209 /* WaProgramL3SqcReg1Default:bdw */
9210 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03009211
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03009212 /* WaKVMNotificationOnConfigChange:bdw */
9213 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
9214 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
9215
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009216 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00009217
9218 /* WaDisableDopClockGating:bdw
9219 *
9220 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
9221 * clock gating.
9222 */
9223 I915_WRITE(GEN6_UCGCTL1,
9224 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07009225}
9226
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009227static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009228{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07009229 /* L3 caching of data atomics doesn't work -- disable it. */
9230 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
9231 I915_WRITE(HSW_ROW_CHICKEN3,
9232 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
9233
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009234 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009235 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
9236 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
9237 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
9238
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02009239 /* WaVSRefCountFullforceMissDisable:hsw */
9240 I915_WRITE(GEN7_FF_THREAD_MODE,
9241 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009242
Akash Goel4e046322014-04-04 17:14:38 +05309243 /* WaDisable_RenderCache_OperationalFlush:hsw */
9244 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9245
Chia-I Wufe27c602014-01-28 13:29:33 +08009246 /* enable HiZ Raw Stall Optimization */
9247 I915_WRITE(CACHE_MODE_0_GEN7,
9248 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
9249
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009250 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009251 I915_WRITE(CACHE_MODE_1,
9252 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03009253
Ville Syrjäläa12c4962014-02-04 21:59:20 +02009254 /*
9255 * BSpec recommends 8x4 when MSAA is used,
9256 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02009257 *
9258 * Note that PS/WM thread counts depend on the WIZ hashing
9259 * disable bit, which we don't touch here, but it's good
9260 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02009261 */
9262 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00009263 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02009264
Kenneth Graunke94411592014-12-31 16:23:00 -08009265 /* WaSampleCChickenBitEnable:hsw */
9266 I915_WRITE(HALF_SLICE_CHICKEN3,
9267 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
9268
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009269 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07009270 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
9271
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009272 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03009273}
9274
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009275static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009276{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009277 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009278
Damien Lespiau231e54f2012-10-19 17:55:41 +01009279 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009280
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009281 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05009282 I915_WRITE(_3D_CHICKEN3,
9283 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
9284
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009285 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009286 I915_WRITE(IVB_CHICKEN3,
9287 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
9288 CHICKEN3_DGMG_DONE_FIX_DISABLE);
9289
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009290 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009291 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07009292 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
9293 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07009294
Akash Goel4e046322014-04-04 17:14:38 +05309295 /* WaDisable_RenderCache_OperationalFlush:ivb */
9296 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9297
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009298 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009299 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
9300 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
9301
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009302 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009303 I915_WRITE(GEN7_L3CNTLREG1,
9304 GEN7_WA_FOR_GEN7_L3_CONTROL);
9305 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07009306 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009307 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07009308 I915_WRITE(GEN7_ROW_CHICKEN2,
9309 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02009310 else {
9311 /* must write both registers */
9312 I915_WRITE(GEN7_ROW_CHICKEN2,
9313 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07009314 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
9315 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02009316 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009317
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009318 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05009319 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
9320 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
9321
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02009322 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07009323 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009324 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07009325 */
9326 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02009327 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07009328
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009329 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009330 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
9331 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
9332 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
9333
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009334 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009335
9336 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02009337
Chris Wilson22721342014-03-04 09:41:43 +00009338 if (0) { /* causes HiZ corruption on ivb:gt1 */
9339 /* enable HiZ Raw Stall Optimization */
9340 I915_WRITE(CACHE_MODE_0_GEN7,
9341 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
9342 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08009343
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009344 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02009345 I915_WRITE(CACHE_MODE_1,
9346 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07009347
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02009348 /*
9349 * BSpec recommends 8x4 when MSAA is used,
9350 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02009351 *
9352 * Note that PS/WM thread counts depend on the WIZ hashing
9353 * disable bit, which we don't touch here, but it's good
9354 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02009355 */
9356 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00009357 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02009358
Ben Widawsky20848222012-05-04 18:58:59 -07009359 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
9360 snpcr &= ~GEN6_MBC_SNPCR_MASK;
9361 snpcr |= GEN6_MBC_SNPCR_MED;
9362 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01009363
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009364 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009365 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01009366
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009367 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009368}
9369
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009370static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009371{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009372 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05009373 I915_WRITE(_3D_CHICKEN3,
9374 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
9375
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009376 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009377 I915_WRITE(IVB_CHICKEN3,
9378 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
9379 CHICKEN3_DGMG_DONE_FIX_DISABLE);
9380
Ville Syrjäläfad7d362014-01-22 21:32:39 +02009381 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009382 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07009383 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08009384 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
9385 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07009386
Akash Goel4e046322014-04-04 17:14:38 +05309387 /* WaDisable_RenderCache_OperationalFlush:vlv */
9388 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9389
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009390 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05009391 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
9392 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
9393
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009394 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07009395 I915_WRITE(GEN7_ROW_CHICKEN2,
9396 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
9397
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009398 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009399 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
9400 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
9401 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
9402
Ville Syrjälä46680e02014-01-22 21:33:01 +02009403 gen7_setup_fixed_func_scheduler(dev_priv);
9404
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02009405 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07009406 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009407 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07009408 */
9409 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02009410 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07009411
Akash Goelc98f5062014-03-24 23:00:07 +05309412 /* WaDisableL3Bank2xClockGate:vlv
9413 * Disabling L3 clock gating- MMIO 940c[25] = 1
9414 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
9415 I915_WRITE(GEN7_UCGCTL4,
9416 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07009417
Ville Syrjäläafd58e72014-01-22 21:33:03 +02009418 /*
9419 * BSpec says this must be set, even though
9420 * WaDisable4x2SubspanOptimization isn't listed for VLV.
9421 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02009422 I915_WRITE(CACHE_MODE_1,
9423 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07009424
9425 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02009426 * BSpec recommends 8x4 when MSAA is used,
9427 * however in practice 16x4 seems fastest.
9428 *
9429 * Note that PS/WM thread counts depend on the WIZ hashing
9430 * disable bit, which we don't touch here, but it's good
9431 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
9432 */
9433 I915_WRITE(GEN7_GT_MODE,
9434 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
9435
9436 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02009437 * WaIncreaseL3CreditsForVLVB0:vlv
9438 * This is the hardware default actually.
9439 */
9440 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
9441
9442 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01009443 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07009444 * Disable clock gating on th GCFG unit to prevent a delay
9445 * in the reporting of vblank events.
9446 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02009447 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009448}
9449
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009450static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03009451{
Ville Syrjälä232ce332014-04-09 13:28:35 +03009452 /* WaVSRefCountFullforceMissDisable:chv */
9453 /* WaDSRefCountFullforceMissDisable:chv */
9454 I915_WRITE(GEN7_FF_THREAD_MODE,
9455 I915_READ(GEN7_FF_THREAD_MODE) &
9456 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03009457
9458 /* WaDisableSemaphoreAndSyncFlipWait:chv */
9459 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
9460 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03009461
9462 /* WaDisableCSUnitClockGating:chv */
9463 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
9464 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03009465
9466 /* WaDisableSDEUnitClockGating:chv */
9467 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
9468 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03009469
9470 /*
Imre Deak450174f2016-05-03 15:54:21 +03009471 * WaProgramL3SqcReg1Default:chv
9472 * See gfxspecs/Related Documents/Performance Guide/
9473 * LSQC Setting Recommendations.
9474 */
9475 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03009476}
9477
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009478static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009479{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02009480 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009481
9482 I915_WRITE(RENCLK_GATE_D1, 0);
9483 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
9484 GS_UNIT_CLOCK_GATE_DISABLE |
9485 CL_UNIT_CLOCK_GATE_DISABLE);
9486 I915_WRITE(RAMCLK_GATE_D, 0);
9487 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
9488 OVRUNIT_CLOCK_GATE_DISABLE |
9489 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009490 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009491 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
9492 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02009493
9494 /* WaDisableRenderCachePipelinedFlush */
9495 I915_WRITE(CACHE_MODE_0,
9496 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03009497
Akash Goel4e046322014-04-04 17:14:38 +05309498 /* WaDisable_RenderCache_OperationalFlush:g4x */
9499 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
9500
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009501 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009502}
9503
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009504static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009505{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01009506 struct intel_uncore *uncore = &dev_priv->uncore;
9507
9508 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
9509 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
9510 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
9511 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
9512 intel_uncore_write16(uncore, DEUC, 0);
9513 intel_uncore_write(uncore,
9514 MI_ARB_STATE,
9515 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05309516
9517 /* WaDisable_RenderCache_OperationalFlush:gen4 */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01009518 intel_uncore_write(uncore,
9519 CACHE_MODE_0,
9520 _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009521}
9522
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009523static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009524{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009525 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
9526 I965_RCC_CLOCK_GATE_DISABLE |
9527 I965_RCPB_CLOCK_GATE_DISABLE |
9528 I965_ISC_CLOCK_GATE_DISABLE |
9529 I965_FBC_CLOCK_GATE_DISABLE);
9530 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03009531 I915_WRITE(MI_ARB_STATE,
9532 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05309533
9534 /* WaDisable_RenderCache_OperationalFlush:gen4 */
9535 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009536}
9537
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009538static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009539{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009540 u32 dstate = I915_READ(D_STATE);
9541
9542 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
9543 DSTATE_DOT_CLOCK_GATING;
9544 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01009545
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009546 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01009547 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02009548
9549 /* IIR "flip pending" means done if this bit is set */
9550 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02009551
9552 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02009553 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02009554
9555 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
9556 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03009557
9558 I915_WRITE(MI_ARB_STATE,
9559 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009560}
9561
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009562static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009563{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009564 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02009565
9566 /* interrupts should cause a wake up from C3 */
9567 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
9568 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03009569
9570 I915_WRITE(MEM_MODE,
9571 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009572}
9573
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009574static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009575{
Ville Syrjälä10383922014-08-15 01:21:54 +03009576 I915_WRITE(MEM_MODE,
9577 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
9578 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009579}
9580
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009581void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009582{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009583 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03009584}
9585
Ville Syrjälä712bf362016-10-31 22:37:23 +02009586void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03009587{
Ville Syrjälä712bf362016-10-31 22:37:23 +02009588 if (HAS_PCH_LPT(dev_priv))
9589 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03009590}
9591
Ville Syrjälä46f16e62016-10-31 22:37:22 +02009592static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02009593{
9594 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
9595}
9596
9597/**
9598 * intel_init_clock_gating_hooks - setup the clock gating hooks
9599 * @dev_priv: device private
9600 *
9601 * Setup the hooks that configure which clocks of a given platform can be
9602 * gated and also apply various GT and display specific workarounds for these
9603 * platforms. Note that some GT specific workarounds are applied separately
9604 * when GPU contexts or batchbuffers start their execution.
9605 */
9606void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
9607{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07009608 if (IS_GEN(dev_priv, 12))
9609 dev_priv->display.init_clock_gating = nop_init_clock_gating;
9610 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07009611 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07009612 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009613 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009614 else if (IS_COFFEELAKE(dev_priv))
9615 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07009616 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009617 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07009618 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009619 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02009620 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02009621 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02009622 else if (IS_GEMINILAKE(dev_priv))
9623 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009624 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009625 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009626 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009627 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009628 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009629 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009630 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009631 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009632 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009633 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009634 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02009635 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009636 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009637 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009638 else if (IS_G4X(dev_priv))
9639 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009640 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009641 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009642 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009643 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009644 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02009645 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
9646 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
9647 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009648 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02009649 dev_priv->display.init_clock_gating = i830_init_clock_gating;
9650 else {
9651 MISSING_CASE(INTEL_DEVID(dev_priv));
9652 dev_priv->display.init_clock_gating = nop_init_clock_gating;
9653 }
9654}
9655
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009656/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009657void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009658{
Daniel Vetterc921aba2012-04-26 23:28:17 +02009659 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009660 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009661 i915_pineview_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009662 else if (IS_GEN(dev_priv, 5))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009663 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02009664
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009665 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009666 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009667 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01009668 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01009669 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07009670 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009671 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009672 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03009673
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009674 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009675 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009676 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009677 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07009678 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08009679 dev_priv->display.compute_intermediate_wm =
9680 ilk_compute_intermediate_wm;
9681 dev_priv->display.initial_watermarks =
9682 ilk_initial_watermarks;
9683 dev_priv->display.optimize_watermarks =
9684 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02009685 } else {
9686 DRM_DEBUG_KMS("Failed to read display plane latency. "
9687 "Disable CxSR\n");
9688 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02009689 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009690 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02009691 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009692 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009693 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009694 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009695 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03009696 } else if (IS_G4X(dev_priv)) {
9697 g4x_setup_wm_latency(dev_priv);
9698 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
9699 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
9700 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
9701 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009702 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00009703 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009704 dev_priv->is_ddr3,
9705 dev_priv->fsb_freq,
9706 dev_priv->mem_freq)) {
9707 DRM_INFO("failed to find known CxSR latency "
9708 "(found ddr%s fsb freq %d, mem freq %d), "
9709 "disabling CxSR\n",
9710 (dev_priv->is_ddr3 == 1) ? "3" : "2",
9711 dev_priv->fsb_freq, dev_priv->mem_freq);
9712 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03009713 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009714 dev_priv->display.update_wm = NULL;
9715 } else
9716 dev_priv->display.update_wm = pineview_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009717 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009718 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009719 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009720 dev_priv->display.update_wm = i9xx_update_wm;
9721 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009722 } else if (IS_GEN(dev_priv, 2)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009723 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009724 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009725 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009726 } else {
9727 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009728 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009729 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009730 } else {
9731 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009732 }
9733}
9734
Ville Syrjälädd06f882014-11-10 22:55:12 +02009735static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9736{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009737 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9738
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009739 /*
9740 * N = val - 0xb7
9741 * Slow = Fast = GPLL ref * N
9742 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009743 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009744}
9745
Fengguang Wub55dd642014-07-12 11:21:39 +02009746static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009747{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009748 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9749
9750 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009751}
9752
Fengguang Wub55dd642014-07-12 11:21:39 +02009753static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309754{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009755 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9756
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009757 /*
9758 * N = val / 2
9759 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9760 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009761 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309762}
9763
Fengguang Wub55dd642014-07-12 11:21:39 +02009764static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309765{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009766 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9767
Ville Syrjälä1c147622014-08-18 14:42:43 +03009768 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009769 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309770}
9771
Ville Syrjälä616bc822015-01-23 21:04:25 +02009772int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9773{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009774 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009775 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9776 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009777 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009778 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009779 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009780 return byt_gpu_freq(dev_priv, val);
9781 else
9782 return val * GT_FREQUENCY_MULTIPLIER;
9783}
9784
Ville Syrjälä616bc822015-01-23 21:04:25 +02009785int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9786{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009787 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009788 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9789 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009790 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009791 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009792 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009793 return byt_freq_opcode(dev_priv, val);
9794 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009795 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309796}
9797
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009798void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009799{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01009800 mutex_init(&dev_priv->gt_pm.rps.lock);
Chris Wilson60548c52018-07-31 14:26:29 +01009801 mutex_init(&dev_priv->gt_pm.rps.power.mutex);
Daniel Vetterf742a552013-12-06 10:17:53 +01009802
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009803 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009804
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009805 dev_priv->runtime_pm.suspended = false;
9806 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009807}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009808
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009809static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9810 const i915_reg_t reg)
9811{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009812 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009813 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009814
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009815 /*
9816 * The register accessed do not need forcewake. We borrow
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009817 * uncore lock to prevent concurrent access to range reg.
9818 */
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009819 lockdep_assert_held(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009820
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009821 /*
9822 * vlv and chv residency counters are 40 bits in width.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009823 * With a control bit, we can choose between upper or lower
9824 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009825 *
9826 * Although we always use the counter in high-range mode elsewhere,
9827 * userspace may attempt to read the value before rc6 is initialised,
9828 * before we have set the default VLV_COUNTER_CONTROL value. So always
9829 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009830 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009831 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9832 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009833 upper = I915_READ_FW(reg);
9834 do {
9835 tmp = upper;
9836
9837 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9838 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9839 lower = I915_READ_FW(reg);
9840
9841 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9842 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9843 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009844 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009845
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009846 /*
9847 * Everywhere else we always use VLV_COUNTER_CONTROL with the
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009848 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9849 * now.
9850 */
9851
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009852 return lower | (u64)upper << 8;
9853}
9854
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009855u64 intel_rc6_residency_ns(struct drm_i915_private *dev_priv,
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009856 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009857{
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009858 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009859 u64 time_hw, prev_hw, overflow_hw;
9860 unsigned int fw_domains;
9861 unsigned long flags;
9862 unsigned int i;
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009863 u32 mul, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009864
Chris Wilsonfb6db0f2017-12-01 11:30:30 +00009865 if (!HAS_RC6(dev_priv))
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009866 return 0;
9867
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009868 /*
9869 * Store previous hw counter values for counter wrap-around handling.
9870 *
9871 * There are only four interesting registers and they live next to each
9872 * other so we can use the relative address, compared to the smallest
9873 * one as the index into driver storage.
9874 */
9875 i = (i915_mmio_reg_offset(reg) -
9876 i915_mmio_reg_offset(GEN6_GT_GFX_RC6_LOCKED)) / sizeof(u32);
9877 if (WARN_ON_ONCE(i >= ARRAY_SIZE(dev_priv->gt_pm.rc6.cur_residency)))
9878 return 0;
9879
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009880 fw_domains = intel_uncore_forcewake_for_reg(uncore, reg, FW_REG_READ);
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009881
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009882 spin_lock_irqsave(&uncore->lock, flags);
9883 intel_uncore_forcewake_get__locked(uncore, fw_domains);
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009884
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009885 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9886 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009887 mul = 1000000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009888 div = dev_priv->czclk_freq;
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009889 overflow_hw = BIT_ULL(40);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009890 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009891 } else {
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009892 /* 833.33ns units on Gen9LP, 1.28us elsewhere. */
9893 if (IS_GEN9_LP(dev_priv)) {
9894 mul = 10000;
9895 div = 12;
9896 } else {
9897 mul = 1280;
9898 div = 1;
9899 }
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009900
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009901 overflow_hw = BIT_ULL(32);
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009902 time_hw = intel_uncore_read_fw(uncore, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009903 }
9904
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009905 /*
9906 * Counter wrap handling.
9907 *
9908 * But relying on a sufficient frequency of queries otherwise counters
9909 * can still wrap.
9910 */
9911 prev_hw = dev_priv->gt_pm.rc6.prev_hw_residency[i];
9912 dev_priv->gt_pm.rc6.prev_hw_residency[i] = time_hw;
9913
9914 /* RC6 delta from last sample. */
9915 if (time_hw >= prev_hw)
9916 time_hw -= prev_hw;
9917 else
9918 time_hw += overflow_hw - prev_hw;
9919
9920 /* Add delta to RC6 extended raw driver copy. */
9921 time_hw += dev_priv->gt_pm.rc6.cur_residency[i];
9922 dev_priv->gt_pm.rc6.cur_residency[i] = time_hw;
9923
Daniele Ceraolo Spurio43193822019-03-25 14:49:37 -07009924 intel_uncore_forcewake_put__locked(uncore, fw_domains);
9925 spin_unlock_irqrestore(&uncore->lock, flags);
Tvrtko Ursulin817cc0792018-02-08 16:00:36 +00009926
9927 return mul_u64_u32_div(time_hw, mul, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009928}
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009929
Jani Nikulaecbb5fb2019-04-29 15:29:37 +03009930u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9931 i915_reg_t reg)
9932{
9933 return DIV_ROUND_UP_ULL(intel_rc6_residency_ns(dev_priv, reg), 1000);
9934}
9935
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009936u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
9937{
9938 u32 cagf;
9939
9940 if (INTEL_GEN(dev_priv) >= 9)
9941 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
9942 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9943 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
9944 else
9945 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
9946
9947 return cagf;
9948}