blob: f54375b11964acbd716ff320159c38d0f23c87c5 [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
Sam Ravnborgd0e93592019-01-26 13:25:24 +010028#include <linux/module.h>
Chris Wilson08ea70a2018-08-12 23:36:31 +010029#include <linux/pm_runtime.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010030
31#include <drm/drm_atomic_helper.h>
32#include <drm/drm_fourcc.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070033#include <drm/drm_plane_helper.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010034
Jani Nikuladf0566a2019-06-13 11:44:16 +030035#include "display/intel_atomic.h"
Stanislav Lisovskiycac91e62020-05-22 16:18:43 +030036#include "display/intel_bw.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
Andi Shyti0dc3c562019-10-20 19:41:39 +010041#include "gt/intel_llc.h"
42
Eugeni Dodonov85208be2012-04-16 22:20:34 -030043#include "i915_drv.h"
Jani Nikulaa10510a2020-02-27 19:00:47 +020044#include "i915_fixed.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030045#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030046#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030047#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010048#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020049#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030050
Jani Nikulaa10510a2020-02-27 19:00:47 +020051/* Stores plane specific WM parameters */
52struct skl_wm_params {
53 bool x_tiled, y_tiled;
54 bool rc_surface;
55 bool is_planar;
56 u32 width;
57 u8 cpp;
58 u32 plane_pixel_rate;
59 u32 y_min_scanlines;
60 u32 plane_bytes_per_line;
61 uint_fixed_16_16_t plane_blocks_per_line;
62 uint_fixed_16_16_t y_tile_minimum;
63 u32 linetime_us;
64 u32 dbuf_block_size;
65};
66
67/* used in computing the new watermarks state */
68struct intel_wm_config {
69 unsigned int num_pipes_active;
70 bool sprites_enabled;
71 bool sprites_scaled;
72};
73
Ville Syrjälä46f16e62016-10-31 22:37:22 +020074static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030075{
Ville Syrjälä93564042017-08-24 22:10:51 +030076 if (HAS_LLC(dev_priv)) {
77 /*
78 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080079 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030080 *
81 * Must match Sampler, Pixel Back End, and Media. See
82 * WaCompressedResourceSamplerPbeMediaNewHashMode.
83 */
84 I915_WRITE(CHICKEN_PAR1_1,
85 I915_READ(CHICKEN_PAR1_1) |
86 SKL_DE_COMPRESSED_HASH_MODE);
87 }
88
Rodrigo Vivi82525c12017-06-08 08:50:00 -070089 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030090 I915_WRITE(CHICKEN_PAR1_1,
91 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
92
Rodrigo Vivi82525c12017-06-08 08:50:00 -070093 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030094 I915_WRITE(GEN8_CHICKEN_DCPR_1,
95 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030096
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +030097 /*
98 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
99 * Display WA #0859: skl,bxt,kbl,glk,cfl
100 */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +0300101 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
Mika Kuoppala303d4ea2016-06-07 17:19:17 +0300102 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300103}
104
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200105static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200106{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200107 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200108
Nick Hoatha7546152015-06-29 14:07:32 +0100109 /* WaDisableSDEUnitClockGating:bxt */
110 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
111 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
112
Imre Deak32608ca2015-03-11 11:10:27 +0200113 /*
114 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200115 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200116 */
Imre Deak32608ca2015-03-11 11:10:27 +0200117 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200118 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200119
120 /*
121 * Wa: Backlight PWM may stop in the asserted state, causing backlight
122 * to stay fully on.
123 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200124 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
125 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530126
127 /*
128 * Lower the display internal timeout.
129 * This is needed to avoid any hard hangs when DSI port PLL
130 * is off and a MMIO access is attempted by any privilege
131 * application, using batch buffers or any other means.
132 */
133 I915_WRITE(RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300134
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300135 /*
136 * WaFbcTurnOffFbcWatermark:bxt
137 * Display WA #0562: bxt
138 */
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300139 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
140 DISP_FBC_WM_DIS);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300141
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300142 /*
143 * WaFbcHighMemBwCorruptionAvoidance:bxt
144 * Display WA #0883: bxt
145 */
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300146 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
147 ILK_DPFC_DISABLE_DUMMY0);
Imre Deaka82abe42015-03-27 14:00:04 +0200148}
149
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200150static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
151{
152 gen9_init_clock_gating(dev_priv);
153
154 /*
155 * WaDisablePWMClockGating:glk
156 * Backlight PWM may stop in the asserted state, causing backlight
157 * to stay fully on.
158 */
159 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
160 PWM1_GATING_DIS | PWM2_GATING_DIS);
161}
162
Lucas De Marchi1d218222019-12-24 00:40:04 -0800163static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200164{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200165 u32 tmp;
166
167 tmp = I915_READ(CLKCFG);
168
169 switch (tmp & CLKCFG_FSB_MASK) {
170 case CLKCFG_FSB_533:
171 dev_priv->fsb_freq = 533; /* 133*4 */
172 break;
173 case CLKCFG_FSB_800:
174 dev_priv->fsb_freq = 800; /* 200*4 */
175 break;
176 case CLKCFG_FSB_667:
177 dev_priv->fsb_freq = 667; /* 167*4 */
178 break;
179 case CLKCFG_FSB_400:
180 dev_priv->fsb_freq = 400; /* 100*4 */
181 break;
182 }
183
184 switch (tmp & CLKCFG_MEM_MASK) {
185 case CLKCFG_MEM_533:
186 dev_priv->mem_freq = 533;
187 break;
188 case CLKCFG_MEM_667:
189 dev_priv->mem_freq = 667;
190 break;
191 case CLKCFG_MEM_800:
192 dev_priv->mem_freq = 800;
193 break;
194 }
195
196 /* detect pineview DDR3 setting */
197 tmp = I915_READ(CSHRDDR3CTL);
198 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
199}
200
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800201static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200202{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200203 u16 ddrpll, csipll;
204
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100205 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
206 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200207
208 switch (ddrpll & 0xff) {
209 case 0xc:
210 dev_priv->mem_freq = 800;
211 break;
212 case 0x10:
213 dev_priv->mem_freq = 1066;
214 break;
215 case 0x14:
216 dev_priv->mem_freq = 1333;
217 break;
218 case 0x18:
219 dev_priv->mem_freq = 1600;
220 break;
221 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300222 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
223 ddrpll & 0xff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200224 dev_priv->mem_freq = 0;
225 break;
226 }
227
Daniel Vetterc921aba2012-04-26 23:28:17 +0200228 switch (csipll & 0x3ff) {
229 case 0x00c:
230 dev_priv->fsb_freq = 3200;
231 break;
232 case 0x00e:
233 dev_priv->fsb_freq = 3733;
234 break;
235 case 0x010:
236 dev_priv->fsb_freq = 4266;
237 break;
238 case 0x012:
239 dev_priv->fsb_freq = 4800;
240 break;
241 case 0x014:
242 dev_priv->fsb_freq = 5333;
243 break;
244 case 0x016:
245 dev_priv->fsb_freq = 5866;
246 break;
247 case 0x018:
248 dev_priv->fsb_freq = 6400;
249 break;
250 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300251 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
252 csipll & 0x3ff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200253 dev_priv->fsb_freq = 0;
254 break;
255 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200256}
257
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300258static const struct cxsr_latency cxsr_latency_table[] = {
259 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
260 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
261 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
262 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
263 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
264
265 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
266 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
267 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
268 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
269 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
270
271 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
272 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
273 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
274 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
275 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
276
277 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
278 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
279 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
280 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
281 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
282
283 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
284 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
285 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
286 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
287 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
288
289 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
290 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
291 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
292 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
293 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
294};
295
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100296static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
297 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300298 int fsb,
299 int mem)
300{
301 const struct cxsr_latency *latency;
302 int i;
303
304 if (fsb == 0 || mem == 0)
305 return NULL;
306
307 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
308 latency = &cxsr_latency_table[i];
309 if (is_desktop == latency->is_desktop &&
310 is_ddr3 == latency->is_ddr3 &&
311 fsb == latency->fsb_freq && mem == latency->mem_freq)
312 return latency;
313 }
314
315 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
316
317 return NULL;
318}
319
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200320static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
321{
322 u32 val;
323
Chris Wilson337fa6e2019-04-26 09:17:20 +0100324 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200325
326 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
327 if (enable)
328 val &= ~FORCE_DDR_HIGH_FREQ;
329 else
330 val |= FORCE_DDR_HIGH_FREQ;
331 val &= ~FORCE_DDR_LOW_FREQ;
332 val |= FORCE_DDR_FREQ_REQ_ACK;
333 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
334
335 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
336 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300337 drm_err(&dev_priv->drm,
338 "timed out waiting for Punit DDR DVFS request\n");
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200339
Chris Wilson337fa6e2019-04-26 09:17:20 +0100340 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200341}
342
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200343static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
344{
345 u32 val;
346
Chris Wilson337fa6e2019-04-26 09:17:20 +0100347 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200348
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200349 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200350 if (enable)
351 val |= DSP_MAXFIFO_PM5_ENABLE;
352 else
353 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200354 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200355
Chris Wilson337fa6e2019-04-26 09:17:20 +0100356 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200357}
358
Ville Syrjäläf4998962015-03-10 17:02:21 +0200359#define FW_WM(value, plane) \
360 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
361
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200362static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300363{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200364 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300365 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300366
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100367 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200368 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300369 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300370 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200371 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200372 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300373 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300374 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200375 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200376 val = I915_READ(DSPFW3);
377 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
378 if (enable)
379 val |= PINEVIEW_SELF_REFRESH_EN;
380 else
381 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300382 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300383 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100384 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200385 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300386 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
387 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
388 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300389 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100390 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300391 /*
392 * FIXME can't find a bit like this for 915G, and
393 * and yet it does have the related watermark in
394 * FW_BLC_SELF. What's going on?
395 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200396 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300397 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
398 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
399 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300400 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300401 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200402 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300403 }
404
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200405 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
406
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300407 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
408 enableddisabled(enable),
409 enableddisabled(was_enabled));
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200410
411 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300412}
413
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300414/**
415 * intel_set_memory_cxsr - Configure CxSR state
416 * @dev_priv: i915 device
417 * @enable: Allow vs. disallow CxSR
418 *
419 * Allow or disallow the system to enter a special CxSR
420 * (C-state self refresh) state. What typically happens in CxSR mode
421 * is that several display FIFOs may get combined into a single larger
422 * FIFO for a particular plane (so called max FIFO mode) to allow the
423 * system to defer memory fetches longer, and the memory will enter
424 * self refresh.
425 *
426 * Note that enabling CxSR does not guarantee that the system enter
427 * this special mode, nor does it guarantee that the system stays
428 * in that mode once entered. So this just allows/disallows the system
429 * to autonomously utilize the CxSR mode. Other factors such as core
430 * C-states will affect when/if the system actually enters/exits the
431 * CxSR mode.
432 *
433 * Note that on VLV/CHV this actually only controls the max FIFO mode,
434 * and the system is free to enter/exit memory self refresh at any time
435 * even when the use of CxSR has been disallowed.
436 *
437 * While the system is actually in the CxSR/max FIFO mode, some plane
438 * control registers will not get latched on vblank. Thus in order to
439 * guarantee the system will respond to changes in the plane registers
440 * we must always disallow CxSR prior to making changes to those registers.
441 * Unfortunately the system will re-evaluate the CxSR conditions at
442 * frame start which happens after vblank start (which is when the plane
443 * registers would get latched), so we can't proceed with the plane update
444 * during the same frame where we disallowed CxSR.
445 *
446 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
447 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
448 * the hardware w.r.t. HPLL SR when writing to plane registers.
449 * Disallowing just CxSR is sufficient.
450 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200451bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200452{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200453 bool ret;
454
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200455 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200456 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300457 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
458 dev_priv->wm.vlv.cxsr = enable;
459 else if (IS_G4X(dev_priv))
460 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200461 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200462
463 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200464}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200465
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300466/*
467 * Latency for FIFO fetches is dependent on several factors:
468 * - memory configuration (speed, channels)
469 * - chipset
470 * - current MCH state
471 * It can be fairly high in some situations, so here we assume a fairly
472 * pessimal value. It's a tradeoff between extra memory fetches (if we
473 * set this value too high, the FIFO will fetch frequently to stay full)
474 * and power consumption (set it too low to save power and we might see
475 * FIFO underruns and display "flicker").
476 *
477 * A value of 5us seems to be a good balance; safe for very low end
478 * platforms but not overly aggressive on lower latency configs.
479 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100480static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300481
Ville Syrjäläb5004722015-03-05 21:19:47 +0200482#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
483 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
484
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200485static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200486{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100487 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200488 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200489 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200490 enum pipe pipe = crtc->pipe;
491 int sprite0_start, sprite1_start;
Kees Cook2713eb42020-02-20 16:05:17 -0800492 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200493
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200494 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200495 case PIPE_A:
496 dsparb = I915_READ(DSPARB);
497 dsparb2 = I915_READ(DSPARB2);
498 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
499 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
500 break;
501 case PIPE_B:
502 dsparb = I915_READ(DSPARB);
503 dsparb2 = I915_READ(DSPARB2);
504 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
505 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
506 break;
507 case PIPE_C:
508 dsparb2 = I915_READ(DSPARB2);
509 dsparb3 = I915_READ(DSPARB3);
510 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
511 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
512 break;
513 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200514 MISSING_CASE(pipe);
515 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200516 }
517
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200518 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
519 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
520 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
521 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200522}
523
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200524static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
525 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200527 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300528 int size;
529
530 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200531 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300532 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
533
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300534 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
535 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300536
537 return size;
538}
539
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200540static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
541 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200543 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300544 int size;
545
546 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200547 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300548 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
549 size >>= 1; /* Convert to cachelines */
550
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300551 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
552 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300553
554 return size;
555}
556
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200557static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
558 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200560 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300561 int size;
562
563 size = dsparb & 0x7f;
564 size >>= 2; /* Convert to cachelines */
565
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300566 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
567 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300568
569 return size;
570}
571
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300572/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800573static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300574 .fifo_size = PINEVIEW_DISPLAY_FIFO,
575 .max_wm = PINEVIEW_MAX_WM,
576 .default_wm = PINEVIEW_DFT_WM,
577 .guard_size = PINEVIEW_GUARD_WM,
578 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300579};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800580
581static const struct intel_watermark_params pnv_display_hplloff_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_HPLLOFF_WM,
585 .guard_size = PINEVIEW_GUARD_WM,
586 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300587};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800588
589static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300590 .fifo_size = PINEVIEW_CURSOR_FIFO,
591 .max_wm = PINEVIEW_CURSOR_MAX_WM,
592 .default_wm = PINEVIEW_CURSOR_DFT_WM,
593 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
594 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300595};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800596
597static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300598 .fifo_size = PINEVIEW_CURSOR_FIFO,
599 .max_wm = PINEVIEW_CURSOR_MAX_WM,
600 .default_wm = PINEVIEW_CURSOR_DFT_WM,
601 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
602 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300603};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800604
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300605static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300606 .fifo_size = I965_CURSOR_FIFO,
607 .max_wm = I965_CURSOR_MAX_WM,
608 .default_wm = I965_CURSOR_DFT_WM,
609 .guard_size = 2,
610 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300611};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800612
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300613static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300614 .fifo_size = I945_FIFO_SIZE,
615 .max_wm = I915_MAX_WM,
616 .default_wm = 1,
617 .guard_size = 2,
618 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300619};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800620
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300621static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300622 .fifo_size = I915_FIFO_SIZE,
623 .max_wm = I915_MAX_WM,
624 .default_wm = 1,
625 .guard_size = 2,
626 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300627};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800628
Ville Syrjälä9d539102014-08-15 01:21:53 +0300629static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300630 .fifo_size = I855GM_FIFO_SIZE,
631 .max_wm = I915_MAX_WM,
632 .default_wm = 1,
633 .guard_size = 2,
634 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300635};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800636
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};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800644
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200645static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300646 .fifo_size = I830_FIFO_SIZE,
647 .max_wm = I915_MAX_WM,
648 .default_wm = 1,
649 .guard_size = 2,
650 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300651};
652
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300653/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300654 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
655 * @pixel_rate: Pipe pixel rate in kHz
656 * @cpp: Plane bytes per pixel
657 * @latency: Memory wakeup latency in 0.1us units
658 *
659 * Compute the watermark using the method 1 or "small buffer"
660 * formula. The caller may additonally add extra cachelines
661 * to account for TLB misses and clock crossings.
662 *
663 * This method is concerned with the short term drain rate
664 * of the FIFO, ie. it does not account for blanking periods
665 * which would effectively reduce the average drain rate across
666 * a longer period. The name "small" refers to the fact the
667 * FIFO is relatively small compared to the amount of data
668 * fetched.
669 *
670 * The FIFO level vs. time graph might look something like:
671 *
672 * |\ |\
673 * | \ | \
674 * __---__---__ (- plane active, _ blanking)
675 * -> time
676 *
677 * or perhaps like this:
678 *
679 * |\|\ |\|\
680 * __----__----__ (- plane active, _ blanking)
681 * -> time
682 *
683 * Returns:
684 * The watermark in bytes
685 */
686static unsigned int intel_wm_method1(unsigned int pixel_rate,
687 unsigned int cpp,
688 unsigned int latency)
689{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200690 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300691
Ville Syrjäläd492a292019-04-08 18:27:01 +0300692 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300693 ret = DIV_ROUND_UP_ULL(ret, 10000);
694
695 return ret;
696}
697
698/**
699 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
700 * @pixel_rate: Pipe pixel rate in kHz
701 * @htotal: Pipe horizontal total
702 * @width: Plane width in pixels
703 * @cpp: Plane bytes per pixel
704 * @latency: Memory wakeup latency in 0.1us units
705 *
706 * Compute the watermark using the method 2 or "large buffer"
707 * formula. The caller may additonally add extra cachelines
708 * to account for TLB misses and clock crossings.
709 *
710 * This method is concerned with the long term drain rate
711 * of the FIFO, ie. it does account for blanking periods
712 * which effectively reduce the average drain rate across
713 * a longer period. The name "large" refers to the fact the
714 * FIFO is relatively large compared to the amount of data
715 * fetched.
716 *
717 * The FIFO level vs. time graph might look something like:
718 *
719 * |\___ |\___
720 * | \___ | \___
721 * | \ | \
722 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
723 * -> time
724 *
725 * Returns:
726 * The watermark in bytes
727 */
728static unsigned int intel_wm_method2(unsigned int pixel_rate,
729 unsigned int htotal,
730 unsigned int width,
731 unsigned int cpp,
732 unsigned int latency)
733{
734 unsigned int ret;
735
736 /*
737 * FIXME remove once all users are computing
738 * watermarks in the correct place.
739 */
740 if (WARN_ON_ONCE(htotal == 0))
741 htotal = 1;
742
743 ret = (latency * pixel_rate) / (htotal * 10000);
744 ret = (ret + 1) * width * cpp;
745
746 return ret;
747}
748
749/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300750 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300751 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300752 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000753 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200754 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755 * @latency_ns: memory latency for the platform
756 *
757 * Calculate the watermark level (the level at which the display plane will
758 * start fetching from memory again). Each chip has a different display
759 * FIFO size and allocation, so the caller needs to figure that out and pass
760 * in the correct intel_watermark_params structure.
761 *
762 * As the pixel clock runs, the FIFO will be drained at a rate that depends
763 * on the pixel size. When it reaches the watermark level, it'll start
764 * fetching FIFO line sized based chunks from memory until the FIFO fills
765 * past the watermark point. If the FIFO drains completely, a FIFO underrun
766 * will occur, and a display engine hang could result.
767 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300768static unsigned int intel_calculate_wm(int pixel_rate,
769 const struct intel_watermark_params *wm,
770 int fifo_size, int cpp,
771 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300772{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300773 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300774
775 /*
776 * Note: we need to make sure we don't overflow for various clock &
777 * latency values.
778 * clocks go from a few thousand to several hundred thousand.
779 * latency is usually a few thousand
780 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300781 entries = intel_wm_method1(pixel_rate, cpp,
782 latency_ns / 100);
783 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
784 wm->guard_size;
785 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300786
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300787 wm_size = fifo_size - entries;
788 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300789
790 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300791 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300792 wm_size = wm->max_wm;
793 if (wm_size <= 0)
794 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300795
796 /*
797 * Bspec seems to indicate that the value shouldn't be lower than
798 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
799 * Lets go for 8 which is the burst size since certain platforms
800 * already use a hardcoded 8 (which is what the spec says should be
801 * done).
802 */
803 if (wm_size <= 8)
804 wm_size = 8;
805
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300806 return wm_size;
807}
808
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300809static bool is_disabling(int old, int new, int threshold)
810{
811 return old >= threshold && new < threshold;
812}
813
814static bool is_enabling(int old, int new, int threshold)
815{
816 return old < threshold && new >= threshold;
817}
818
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300819static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
820{
821 return dev_priv->wm.max_level + 1;
822}
823
Ville Syrjälä24304d812017-03-14 17:10:49 +0200824static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
825 const struct intel_plane_state *plane_state)
826{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100827 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200828
829 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100830 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200831 return false;
832
833 /*
834 * Treat cursor with fb as always visible since cursor updates
835 * can happen faster than the vrefresh rate, and the current
836 * watermark code doesn't handle that correctly. Cursor updates
837 * which set/clear the fb or change the cursor size are going
838 * to get throttled by intel_legacy_cursor_update() to work
839 * around this problem with the watermark code.
840 */
841 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100842 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200843 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100844 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200845}
846
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200847static bool intel_crtc_active(struct intel_crtc *crtc)
848{
849 /* Be paranoid as we can arrive here with only partial
850 * state retrieved from the hardware during setup.
851 *
852 * We can ditch the adjusted_mode.crtc_clock check as soon
853 * as Haswell has gained clock readout/fastboot support.
854 *
855 * We can ditch the crtc->primary->state->fb check as soon as we can
856 * properly reconstruct framebuffers.
857 *
858 * FIXME: The intel_crtc->active here should be switched to
859 * crtc->state->active once we have proper CRTC states wired up
860 * for atomic.
861 */
862 return crtc->active && crtc->base.primary->state->fb &&
863 crtc->config->hw.adjusted_mode.crtc_clock;
864}
865
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200866static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300867{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200868 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300869
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200870 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200871 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300872 if (enabled)
873 return NULL;
874 enabled = crtc;
875 }
876 }
877
878 return enabled;
879}
880
Lucas De Marchi1d218222019-12-24 00:40:04 -0800881static void pnv_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300882{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200883 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200884 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885 const struct cxsr_latency *latency;
886 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300887 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300888
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000889 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100890 dev_priv->is_ddr3,
891 dev_priv->fsb_freq,
892 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300893 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300894 drm_dbg_kms(&dev_priv->drm,
895 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300896 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300897 return;
898 }
899
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200900 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300901 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200902 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100903 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200904 const struct drm_framebuffer *fb =
905 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200906 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300907 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300908
909 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800910 wm = intel_calculate_wm(clock, &pnv_display_wm,
911 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200912 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913 reg = I915_READ(DSPFW1);
914 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200915 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300916 I915_WRITE(DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300917 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300918
919 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800920 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
921 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300922 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300923 reg = I915_READ(DSPFW3);
924 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200925 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300926 I915_WRITE(DSPFW3, reg);
927
928 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800929 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
930 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200931 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300932 reg = I915_READ(DSPFW3);
933 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200934 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300935 I915_WRITE(DSPFW3, reg);
936
937 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800938 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
939 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300940 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300941 reg = I915_READ(DSPFW3);
942 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200943 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300944 I915_WRITE(DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300945 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300946
Imre Deak5209b1f2014-07-01 12:36:17 +0300947 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300948 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300949 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300950 }
951}
952
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300953/*
954 * Documentation says:
955 * "If the line size is small, the TLB fetches can get in the way of the
956 * data fetches, causing some lag in the pixel data return which is not
957 * accounted for in the above formulas. The following adjustment only
958 * needs to be applied if eight whole lines fit in the buffer at once.
959 * The WM is adjusted upwards by the difference between the FIFO size
960 * and the size of 8 whole lines. This adjustment is always performed
961 * in the actual pixel depth regardless of whether FBC is enabled or not."
962 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000963static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300964{
965 int tlb_miss = fifo_size * 64 - width * cpp * 8;
966
967 return max(0, tlb_miss);
968}
969
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300970static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
971 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300972{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300973 enum pipe pipe;
974
975 for_each_pipe(dev_priv, pipe)
976 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
977
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300978 I915_WRITE(DSPFW1,
979 FW_WM(wm->sr.plane, SR) |
980 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
981 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
982 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
983 I915_WRITE(DSPFW2,
984 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
985 FW_WM(wm->sr.fbc, FBC_SR) |
986 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
987 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
988 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
989 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
990 I915_WRITE(DSPFW3,
991 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
992 FW_WM(wm->sr.cursor, CURSOR_SR) |
993 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
994 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300995
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300996 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300997}
998
Ville Syrjälä15665972015-03-10 16:16:28 +0200999#define FW_WM_VLV(value, plane) \
1000 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1001
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001002static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001003 const struct vlv_wm_values *wm)
1004{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001005 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001006
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001007 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001008 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1009
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001010 I915_WRITE(VLV_DDL(pipe),
1011 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1012 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1013 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1014 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1015 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001016
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001017 /*
1018 * Zero the (unused) WM1 watermarks, and also clear all the
1019 * high order bits so that there are no out of bounds values
1020 * present in the registers during the reprogramming.
1021 */
1022 I915_WRITE(DSPHOWM, 0);
1023 I915_WRITE(DSPHOWM1, 0);
1024 I915_WRITE(DSPFW4, 0);
1025 I915_WRITE(DSPFW5, 0);
1026 I915_WRITE(DSPFW6, 0);
1027
Ville Syrjäläae801522015-03-05 21:19:49 +02001028 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001029 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001030 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1031 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1032 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001033 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001034 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1035 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1036 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001037 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001038 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001039
1040 if (IS_CHERRYVIEW(dev_priv)) {
1041 I915_WRITE(DSPFW7_CHV,
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(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001045 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1046 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001047 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001048 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1049 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001050 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001051 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001052 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1053 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1054 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1055 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1056 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1057 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1058 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1059 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1060 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001061 } else {
1062 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001063 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1064 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001065 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001066 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001067 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1068 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1069 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1070 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1071 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1072 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001073 }
1074
1075 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001076}
1077
Ville Syrjälä15665972015-03-10 16:16:28 +02001078#undef FW_WM_VLV
1079
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001080static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1081{
1082 /* all latencies in usec */
1083 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1084 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001085 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001086
Ville Syrjälä79d94302017-04-21 21:14:30 +03001087 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001088}
1089
1090static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1091{
1092 /*
1093 * DSPCNTR[13] supposedly controls whether the
1094 * primary plane can use the FIFO space otherwise
1095 * reserved for the sprite plane. It's not 100% clear
1096 * what the actual FIFO size is, but it looks like we
1097 * can happily set both primary and sprite watermarks
1098 * up to 127 cachelines. So that would seem to mean
1099 * that either DSPCNTR[13] doesn't do anything, or that
1100 * the total FIFO is >= 256 cachelines in size. Either
1101 * way, we don't seem to have to worry about this
1102 * repartitioning as the maximum watermark value the
1103 * register can hold for each plane is lower than the
1104 * minimum FIFO size.
1105 */
1106 switch (plane_id) {
1107 case PLANE_CURSOR:
1108 return 63;
1109 case PLANE_PRIMARY:
1110 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1111 case PLANE_SPRITE0:
1112 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1113 default:
1114 MISSING_CASE(plane_id);
1115 return 0;
1116 }
1117}
1118
1119static int g4x_fbc_fifo_size(int level)
1120{
1121 switch (level) {
1122 case G4X_WM_LEVEL_SR:
1123 return 7;
1124 case G4X_WM_LEVEL_HPLL:
1125 return 15;
1126 default:
1127 MISSING_CASE(level);
1128 return 0;
1129 }
1130}
1131
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001132static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1133 const struct intel_plane_state *plane_state,
1134 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001135{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001136 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001137 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1138 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01001139 &crtc_state->hw.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001140 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1141 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001142
1143 if (latency == 0)
1144 return USHRT_MAX;
1145
1146 if (!intel_wm_plane_visible(crtc_state, plane_state))
1147 return 0;
1148
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001149 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001150
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001151 /*
1152 * Not 100% sure which way ELK should go here as the
1153 * spec only says CL/CTG should assume 32bpp and BW
1154 * doesn't need to. But as these things followed the
1155 * mobile vs. desktop lines on gen3 as well, let's
1156 * assume ELK doesn't need this.
1157 *
1158 * The spec also fails to list such a restriction for
1159 * the HPLL watermark, which seems a little strange.
1160 * Let's use 32bpp for the HPLL watermark as well.
1161 */
1162 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1163 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001164 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001165
1166 clock = adjusted_mode->crtc_clock;
1167 htotal = adjusted_mode->crtc_htotal;
1168
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001169 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001170
1171 if (plane->id == PLANE_CURSOR) {
1172 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1173 } else if (plane->id == PLANE_PRIMARY &&
1174 level == G4X_WM_LEVEL_NORMAL) {
1175 wm = intel_wm_method1(clock, cpp, latency);
1176 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001177 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001178
1179 small = intel_wm_method1(clock, cpp, latency);
1180 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1181
1182 wm = min(small, large);
1183 }
1184
1185 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1186 width, cpp);
1187
1188 wm = DIV_ROUND_UP(wm, 64) + 2;
1189
Chris Wilson1a1f1282017-11-07 14:03:38 +00001190 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001191}
1192
1193static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1194 int level, enum plane_id plane_id, u16 value)
1195{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001196 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001197 bool dirty = false;
1198
1199 for (; level < intel_wm_num_levels(dev_priv); level++) {
1200 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1201
1202 dirty |= raw->plane[plane_id] != value;
1203 raw->plane[plane_id] = value;
1204 }
1205
1206 return dirty;
1207}
1208
1209static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1210 int level, u16 value)
1211{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001212 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001213 bool dirty = false;
1214
1215 /* NORMAL level doesn't have an FBC watermark */
1216 level = max(level, G4X_WM_LEVEL_SR);
1217
1218 for (; level < intel_wm_num_levels(dev_priv); level++) {
1219 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1220
1221 dirty |= raw->fbc != value;
1222 raw->fbc = value;
1223 }
1224
1225 return dirty;
1226}
1227
Maarten Lankhorstec193642019-06-28 10:55:17 +02001228static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1229 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001230 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001231
1232static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1233 const struct intel_plane_state *plane_state)
1234{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001235 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001236 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001237 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1238 enum plane_id plane_id = plane->id;
1239 bool dirty = false;
1240 int level;
1241
1242 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1243 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1244 if (plane_id == PLANE_PRIMARY)
1245 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1246 goto out;
1247 }
1248
1249 for (level = 0; level < num_levels; level++) {
1250 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1251 int wm, max_wm;
1252
1253 wm = g4x_compute_wm(crtc_state, plane_state, level);
1254 max_wm = g4x_plane_fifo_size(plane_id, level);
1255
1256 if (wm > max_wm)
1257 break;
1258
1259 dirty |= raw->plane[plane_id] != wm;
1260 raw->plane[plane_id] = wm;
1261
1262 if (plane_id != PLANE_PRIMARY ||
1263 level == G4X_WM_LEVEL_NORMAL)
1264 continue;
1265
1266 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1267 raw->plane[plane_id]);
1268 max_wm = g4x_fbc_fifo_size(level);
1269
1270 /*
1271 * FBC wm is not mandatory as we
1272 * can always just disable its use.
1273 */
1274 if (wm > max_wm)
1275 wm = USHRT_MAX;
1276
1277 dirty |= raw->fbc != wm;
1278 raw->fbc = wm;
1279 }
1280
1281 /* mark watermarks as invalid */
1282 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1283
1284 if (plane_id == PLANE_PRIMARY)
1285 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1286
1287 out:
1288 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001289 drm_dbg_kms(&dev_priv->drm,
1290 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1291 plane->base.name,
1292 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1293 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1294 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001295
1296 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001297 drm_dbg_kms(&dev_priv->drm,
1298 "FBC watermarks: SR=%d, HPLL=%d\n",
1299 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1300 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001301 }
1302
1303 return dirty;
1304}
1305
1306static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1307 enum plane_id plane_id, int level)
1308{
1309 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1310
1311 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1312}
1313
1314static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1315 int level)
1316{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001317 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001318
1319 if (level > dev_priv->wm.max_level)
1320 return false;
1321
1322 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1323 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1324 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1325}
1326
1327/* mark all levels starting from 'level' as invalid */
1328static void g4x_invalidate_wms(struct intel_crtc *crtc,
1329 struct g4x_wm_state *wm_state, int level)
1330{
1331 if (level <= G4X_WM_LEVEL_NORMAL) {
1332 enum plane_id plane_id;
1333
1334 for_each_plane_id_on_crtc(crtc, plane_id)
1335 wm_state->wm.plane[plane_id] = USHRT_MAX;
1336 }
1337
1338 if (level <= G4X_WM_LEVEL_SR) {
1339 wm_state->cxsr = false;
1340 wm_state->sr.cursor = USHRT_MAX;
1341 wm_state->sr.plane = USHRT_MAX;
1342 wm_state->sr.fbc = USHRT_MAX;
1343 }
1344
1345 if (level <= G4X_WM_LEVEL_HPLL) {
1346 wm_state->hpll_en = false;
1347 wm_state->hpll.cursor = USHRT_MAX;
1348 wm_state->hpll.plane = USHRT_MAX;
1349 wm_state->hpll.fbc = USHRT_MAX;
1350 }
1351}
1352
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001353static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1354 int level)
1355{
1356 if (level < G4X_WM_LEVEL_SR)
1357 return false;
1358
1359 if (level >= G4X_WM_LEVEL_SR &&
1360 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1361 return false;
1362
1363 if (level >= G4X_WM_LEVEL_HPLL &&
1364 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1365 return false;
1366
1367 return true;
1368}
1369
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001370static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1371{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001372 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001373 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001374 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001375 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001376 int num_active_planes = hweight8(crtc_state->active_planes &
1377 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001378 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001379 const struct intel_plane_state *old_plane_state;
1380 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001381 struct intel_plane *plane;
1382 enum plane_id plane_id;
1383 int i, level;
1384 unsigned int dirty = 0;
1385
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001386 for_each_oldnew_intel_plane_in_state(state, plane,
1387 old_plane_state,
1388 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001389 if (new_plane_state->hw.crtc != &crtc->base &&
1390 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001391 continue;
1392
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001393 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001394 dirty |= BIT(plane->id);
1395 }
1396
1397 if (!dirty)
1398 return 0;
1399
1400 level = G4X_WM_LEVEL_NORMAL;
1401 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1402 goto out;
1403
1404 raw = &crtc_state->wm.g4x.raw[level];
1405 for_each_plane_id_on_crtc(crtc, plane_id)
1406 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1407
1408 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001409 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1410 goto out;
1411
1412 raw = &crtc_state->wm.g4x.raw[level];
1413 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1414 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1415 wm_state->sr.fbc = raw->fbc;
1416
1417 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1418
1419 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001420 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1421 goto out;
1422
1423 raw = &crtc_state->wm.g4x.raw[level];
1424 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1425 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1426 wm_state->hpll.fbc = raw->fbc;
1427
1428 wm_state->hpll_en = wm_state->cxsr;
1429
1430 level++;
1431
1432 out:
1433 if (level == G4X_WM_LEVEL_NORMAL)
1434 return -EINVAL;
1435
1436 /* invalidate the higher levels */
1437 g4x_invalidate_wms(crtc, wm_state, level);
1438
1439 /*
1440 * Determine if the FBC watermark(s) can be used. IF
1441 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001442 * watermark(s) rather than disable the SR/HPLL
1443 * level(s) entirely. 'level-1' is the highest valid
1444 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001445 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001446 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001447
1448 return 0;
1449}
1450
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001451static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001452{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001453 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301454 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001455 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1456 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1457 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001458 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001459 const struct intel_crtc_state *old_crtc_state =
1460 intel_atomic_get_old_crtc_state(intel_state, crtc);
1461 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001462 enum plane_id plane_id;
1463
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001464 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001465 *intermediate = *optimal;
1466
1467 intermediate->cxsr = false;
1468 intermediate->hpll_en = false;
1469 goto out;
1470 }
1471
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001472 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001473 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001474 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001475 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001476 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1477
1478 for_each_plane_id_on_crtc(crtc, plane_id) {
1479 intermediate->wm.plane[plane_id] =
1480 max(optimal->wm.plane[plane_id],
1481 active->wm.plane[plane_id]);
1482
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301483 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1484 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001485 }
1486
1487 intermediate->sr.plane = max(optimal->sr.plane,
1488 active->sr.plane);
1489 intermediate->sr.cursor = max(optimal->sr.cursor,
1490 active->sr.cursor);
1491 intermediate->sr.fbc = max(optimal->sr.fbc,
1492 active->sr.fbc);
1493
1494 intermediate->hpll.plane = max(optimal->hpll.plane,
1495 active->hpll.plane);
1496 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1497 active->hpll.cursor);
1498 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1499 active->hpll.fbc);
1500
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301501 drm_WARN_ON(&dev_priv->drm,
1502 (intermediate->sr.plane >
1503 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1504 intermediate->sr.cursor >
1505 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1506 intermediate->cxsr);
1507 drm_WARN_ON(&dev_priv->drm,
1508 (intermediate->sr.plane >
1509 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1510 intermediate->sr.cursor >
1511 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1512 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001513
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301514 drm_WARN_ON(&dev_priv->drm,
1515 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1516 intermediate->fbc_en && intermediate->cxsr);
1517 drm_WARN_ON(&dev_priv->drm,
1518 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1519 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001520
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001521out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001522 /*
1523 * If our intermediate WM are identical to the final WM, then we can
1524 * omit the post-vblank programming; only update if it's different.
1525 */
1526 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001527 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001528
1529 return 0;
1530}
1531
1532static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1533 struct g4x_wm_values *wm)
1534{
1535 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001536 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001537
1538 wm->cxsr = true;
1539 wm->hpll_en = true;
1540 wm->fbc_en = true;
1541
1542 for_each_intel_crtc(&dev_priv->drm, crtc) {
1543 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1544
1545 if (!crtc->active)
1546 continue;
1547
1548 if (!wm_state->cxsr)
1549 wm->cxsr = false;
1550 if (!wm_state->hpll_en)
1551 wm->hpll_en = false;
1552 if (!wm_state->fbc_en)
1553 wm->fbc_en = false;
1554
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001555 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001556 }
1557
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001558 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001559 wm->cxsr = false;
1560 wm->hpll_en = false;
1561 wm->fbc_en = false;
1562 }
1563
1564 for_each_intel_crtc(&dev_priv->drm, crtc) {
1565 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1566 enum pipe pipe = crtc->pipe;
1567
1568 wm->pipe[pipe] = wm_state->wm;
1569 if (crtc->active && wm->cxsr)
1570 wm->sr = wm_state->sr;
1571 if (crtc->active && wm->hpll_en)
1572 wm->hpll = wm_state->hpll;
1573 }
1574}
1575
1576static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1577{
1578 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1579 struct g4x_wm_values new_wm = {};
1580
1581 g4x_merge_wm(dev_priv, &new_wm);
1582
1583 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1584 return;
1585
1586 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1587 _intel_set_memory_cxsr(dev_priv, false);
1588
1589 g4x_write_wm_values(dev_priv, &new_wm);
1590
1591 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1592 _intel_set_memory_cxsr(dev_priv, true);
1593
1594 *old_wm = new_wm;
1595}
1596
1597static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001598 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001599{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001600 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1601 const struct intel_crtc_state *crtc_state =
1602 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001603
1604 mutex_lock(&dev_priv->wm.wm_mutex);
1605 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1606 g4x_program_watermarks(dev_priv);
1607 mutex_unlock(&dev_priv->wm.wm_mutex);
1608}
1609
1610static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001611 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001612{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001613 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1614 const struct intel_crtc_state *crtc_state =
1615 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001616
1617 if (!crtc_state->wm.need_postvbl_update)
1618 return;
1619
1620 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001621 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001622 g4x_program_watermarks(dev_priv);
1623 mutex_unlock(&dev_priv->wm.wm_mutex);
1624}
1625
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001626/* latency must be in 0.1us units. */
1627static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001628 unsigned int htotal,
1629 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001630 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001631 unsigned int latency)
1632{
1633 unsigned int ret;
1634
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001635 ret = intel_wm_method2(pixel_rate, htotal,
1636 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001637 ret = DIV_ROUND_UP(ret, 64);
1638
1639 return ret;
1640}
1641
Ville Syrjäläbb726512016-10-31 22:37:24 +02001642static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001643{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001644 /* all latencies in usec */
1645 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1646
Ville Syrjälä58590c12015-09-08 21:05:12 +03001647 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1648
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001649 if (IS_CHERRYVIEW(dev_priv)) {
1650 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1651 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001652
1653 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001654 }
1655}
1656
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001657static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1658 const struct intel_plane_state *plane_state,
1659 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001660{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001661 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001662 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001663 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01001664 &crtc_state->hw.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001665 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001666
1667 if (dev_priv->wm.pri_latency[level] == 0)
1668 return USHRT_MAX;
1669
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001670 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001671 return 0;
1672
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001673 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001674 clock = adjusted_mode->crtc_clock;
1675 htotal = adjusted_mode->crtc_htotal;
1676 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001677
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001678 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001679 /*
1680 * FIXME the formula gives values that are
1681 * too big for the cursor FIFO, and hence we
1682 * would never be able to use cursors. For
1683 * now just hardcode the watermark.
1684 */
1685 wm = 63;
1686 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001687 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001688 dev_priv->wm.pri_latency[level] * 10);
1689 }
1690
Chris Wilson1a1f1282017-11-07 14:03:38 +00001691 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001692}
1693
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001694static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1695{
1696 return (active_planes & (BIT(PLANE_SPRITE0) |
1697 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1698}
1699
Ville Syrjälä5012e602017-03-02 19:14:56 +02001700static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001701{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001702 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301703 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001704 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001705 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001706 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001707 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001708 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001709 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001710 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001711 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001712 unsigned int total_rate;
1713 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001714
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001715 /*
1716 * When enabling sprite0 after sprite1 has already been enabled
1717 * we tend to get an underrun unless sprite0 already has some
1718 * FIFO space allcoated. Hence we always allocate at least one
1719 * cacheline for sprite0 whenever sprite1 is enabled.
1720 *
1721 * All other plane enable sequences appear immune to this problem.
1722 */
1723 if (vlv_need_sprite0_fifo_workaround(active_planes))
1724 sprite0_fifo_extra = 1;
1725
Ville Syrjälä5012e602017-03-02 19:14:56 +02001726 total_rate = raw->plane[PLANE_PRIMARY] +
1727 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001728 raw->plane[PLANE_SPRITE1] +
1729 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001730
Ville Syrjälä5012e602017-03-02 19:14:56 +02001731 if (total_rate > fifo_size)
1732 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001733
Ville Syrjälä5012e602017-03-02 19:14:56 +02001734 if (total_rate == 0)
1735 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001736
Ville Syrjälä5012e602017-03-02 19:14:56 +02001737 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001738 unsigned int rate;
1739
Ville Syrjälä5012e602017-03-02 19:14:56 +02001740 if ((active_planes & BIT(plane_id)) == 0) {
1741 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001742 continue;
1743 }
1744
Ville Syrjälä5012e602017-03-02 19:14:56 +02001745 rate = raw->plane[plane_id];
1746 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1747 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001748 }
1749
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001750 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1751 fifo_left -= sprite0_fifo_extra;
1752
Ville Syrjälä5012e602017-03-02 19:14:56 +02001753 fifo_state->plane[PLANE_CURSOR] = 63;
1754
1755 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001756
1757 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001758 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001759 int plane_extra;
1760
1761 if (fifo_left == 0)
1762 break;
1763
Ville Syrjälä5012e602017-03-02 19:14:56 +02001764 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001765 continue;
1766
1767 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001768 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001769 fifo_left -= plane_extra;
1770 }
1771
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301772 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001773
1774 /* give it all to the first plane if none are active */
1775 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301776 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001777 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1778 }
1779
1780 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001781}
1782
Ville Syrjäläff32c542017-03-02 19:14:57 +02001783/* mark all levels starting from 'level' as invalid */
1784static void vlv_invalidate_wms(struct intel_crtc *crtc,
1785 struct vlv_wm_state *wm_state, int level)
1786{
1787 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1788
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001789 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001790 enum plane_id plane_id;
1791
1792 for_each_plane_id_on_crtc(crtc, plane_id)
1793 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1794
1795 wm_state->sr[level].cursor = USHRT_MAX;
1796 wm_state->sr[level].plane = USHRT_MAX;
1797 }
1798}
1799
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001800static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1801{
1802 if (wm > fifo_size)
1803 return USHRT_MAX;
1804 else
1805 return fifo_size - wm;
1806}
1807
Ville Syrjäläff32c542017-03-02 19:14:57 +02001808/*
1809 * Starting from 'level' set all higher
1810 * levels to 'value' in the "raw" watermarks.
1811 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001812static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001813 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001814{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001815 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001816 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001817 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001818
Ville Syrjäläff32c542017-03-02 19:14:57 +02001819 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001820 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001821
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001822 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001823 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001824 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001825
1826 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001827}
1828
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001829static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1830 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001831{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001832 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001833 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001834 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001835 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001836 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001837 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001838
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001839 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001840 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1841 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001842 }
1843
1844 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001845 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001846 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1847 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1848
Ville Syrjäläff32c542017-03-02 19:14:57 +02001849 if (wm > max_wm)
1850 break;
1851
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001852 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001853 raw->plane[plane_id] = wm;
1854 }
1855
1856 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001857 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001858
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001859out:
1860 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001861 drm_dbg_kms(&dev_priv->drm,
1862 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1863 plane->base.name,
1864 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1865 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1866 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001867
1868 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001869}
1870
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001871static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1872 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001873{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001874 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001875 &crtc_state->wm.vlv.raw[level];
1876 const struct vlv_fifo_state *fifo_state =
1877 &crtc_state->wm.vlv.fifo_state;
1878
1879 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1880}
1881
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001882static 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 +02001883{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001884 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1885 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1886 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1887 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001888}
1889
1890static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001891{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001892 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001893 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001894 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001895 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001896 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001897 const struct vlv_fifo_state *fifo_state =
1898 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001899 int num_active_planes = hweight8(crtc_state->active_planes &
1900 ~BIT(PLANE_CURSOR));
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001901 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001902 const struct intel_plane_state *old_plane_state;
1903 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001904 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001905 enum plane_id plane_id;
1906 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001907 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001908
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001909 for_each_oldnew_intel_plane_in_state(state, plane,
1910 old_plane_state,
1911 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001912 if (new_plane_state->hw.crtc != &crtc->base &&
1913 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001914 continue;
1915
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001916 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001917 dirty |= BIT(plane->id);
1918 }
1919
1920 /*
1921 * DSPARB registers may have been reset due to the
1922 * power well being turned off. Make sure we restore
1923 * them to a consistent state even if no primary/sprite
1924 * planes are initially active.
1925 */
1926 if (needs_modeset)
1927 crtc_state->fifo_changed = true;
1928
1929 if (!dirty)
1930 return 0;
1931
1932 /* cursor changes don't warrant a FIFO recompute */
1933 if (dirty & ~BIT(PLANE_CURSOR)) {
1934 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001935 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001936 const struct vlv_fifo_state *old_fifo_state =
1937 &old_crtc_state->wm.vlv.fifo_state;
1938
1939 ret = vlv_compute_fifo(crtc_state);
1940 if (ret)
1941 return ret;
1942
1943 if (needs_modeset ||
1944 memcmp(old_fifo_state, fifo_state,
1945 sizeof(*fifo_state)) != 0)
1946 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001947 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001948
Ville Syrjäläff32c542017-03-02 19:14:57 +02001949 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001950 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001951 /*
1952 * Note that enabling cxsr with no primary/sprite planes
1953 * enabled can wedge the pipe. Hence we only allow cxsr
1954 * with exactly one enabled primary/sprite plane.
1955 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001956 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001957
Ville Syrjälä5012e602017-03-02 19:14:56 +02001958 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001959 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001960 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001961
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001962 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001963 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001964
Ville Syrjäläff32c542017-03-02 19:14:57 +02001965 for_each_plane_id_on_crtc(crtc, plane_id) {
1966 wm_state->wm[level].plane[plane_id] =
1967 vlv_invert_wm_value(raw->plane[plane_id],
1968 fifo_state->plane[plane_id]);
1969 }
1970
1971 wm_state->sr[level].plane =
1972 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001973 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001974 raw->plane[PLANE_SPRITE1]),
1975 sr_fifo_size);
1976
1977 wm_state->sr[level].cursor =
1978 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1979 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001980 }
1981
Ville Syrjäläff32c542017-03-02 19:14:57 +02001982 if (level == 0)
1983 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001984
Ville Syrjäläff32c542017-03-02 19:14:57 +02001985 /* limit to only levels we can actually handle */
1986 wm_state->num_levels = level;
1987
1988 /* invalidate the higher levels */
1989 vlv_invalidate_wms(crtc, wm_state, level);
1990
1991 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001992}
1993
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001994#define VLV_FIFO(plane, value) \
1995 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1996
Ville Syrjäläff32c542017-03-02 19:14:57 +02001997static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001998 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001999{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002000 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002001 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002002 const struct intel_crtc_state *crtc_state =
2003 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002004 const struct vlv_fifo_state *fifo_state =
2005 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002006 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002007 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002008
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002009 if (!crtc_state->fifo_changed)
2010 return;
2011
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002012 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2013 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2014 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002015
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302016 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2017 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002018
Ville Syrjäläc137d662017-03-02 19:15:06 +02002019 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2020
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002021 /*
2022 * uncore.lock serves a double purpose here. It allows us to
2023 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2024 * it protects the DSPARB registers from getting clobbered by
2025 * parallel updates from multiple pipes.
2026 *
2027 * intel_pipe_update_start() has already disabled interrupts
2028 * for us, so a plain spin_lock() is sufficient here.
2029 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002030 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002031
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002032 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002033 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002034 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2035 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002036
2037 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2038 VLV_FIFO(SPRITEB, 0xff));
2039 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2040 VLV_FIFO(SPRITEB, sprite1_start));
2041
2042 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2043 VLV_FIFO(SPRITEB_HI, 0x1));
2044 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2045 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2046
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002047 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2048 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002049 break;
2050 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002051 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2052 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002053
2054 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2055 VLV_FIFO(SPRITED, 0xff));
2056 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2057 VLV_FIFO(SPRITED, sprite1_start));
2058
2059 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2060 VLV_FIFO(SPRITED_HI, 0xff));
2061 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2062 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2063
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002064 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2065 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002066 break;
2067 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002068 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2069 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002070
2071 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2072 VLV_FIFO(SPRITEF, 0xff));
2073 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2074 VLV_FIFO(SPRITEF, sprite1_start));
2075
2076 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2077 VLV_FIFO(SPRITEF_HI, 0xff));
2078 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2079 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2080
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002081 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2082 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002083 break;
2084 default:
2085 break;
2086 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002087
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002088 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002089
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002090 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002091}
2092
2093#undef VLV_FIFO
2094
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002095static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002096{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002097 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002098 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2099 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2100 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002101 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002102 const struct intel_crtc_state *old_crtc_state =
2103 intel_atomic_get_old_crtc_state(intel_state, crtc);
2104 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002105 int level;
2106
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002107 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002108 *intermediate = *optimal;
2109
2110 intermediate->cxsr = false;
2111 goto out;
2112 }
2113
Ville Syrjälä4841da52017-03-02 19:14:59 +02002114 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002115 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002116 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002117
2118 for (level = 0; level < intermediate->num_levels; level++) {
2119 enum plane_id plane_id;
2120
2121 for_each_plane_id_on_crtc(crtc, plane_id) {
2122 intermediate->wm[level].plane[plane_id] =
2123 min(optimal->wm[level].plane[plane_id],
2124 active->wm[level].plane[plane_id]);
2125 }
2126
2127 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2128 active->sr[level].plane);
2129 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2130 active->sr[level].cursor);
2131 }
2132
2133 vlv_invalidate_wms(crtc, intermediate, level);
2134
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002135out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002136 /*
2137 * If our intermediate WM are identical to the final WM, then we can
2138 * omit the post-vblank programming; only update if it's different.
2139 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002140 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002141 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002142
2143 return 0;
2144}
2145
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002146static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002147 struct vlv_wm_values *wm)
2148{
2149 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002150 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002151
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002152 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002153 wm->cxsr = true;
2154
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002155 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002156 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002157
2158 if (!crtc->active)
2159 continue;
2160
2161 if (!wm_state->cxsr)
2162 wm->cxsr = false;
2163
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002164 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002165 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2166 }
2167
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002168 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002169 wm->cxsr = false;
2170
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002171 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002172 wm->level = VLV_WM_LEVEL_PM2;
2173
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002174 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002175 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002176 enum pipe pipe = crtc->pipe;
2177
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002178 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002179 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002180 wm->sr = wm_state->sr[wm->level];
2181
Ville Syrjälä1b313892016-11-28 19:37:08 +02002182 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2183 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2184 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2185 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002186 }
2187}
2188
Ville Syrjäläff32c542017-03-02 19:14:57 +02002189static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002190{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002191 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2192 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002193
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002194 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002195
Ville Syrjäläff32c542017-03-02 19:14:57 +02002196 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002197 return;
2198
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002199 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002200 chv_set_memory_dvfs(dev_priv, false);
2201
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002202 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002203 chv_set_memory_pm5(dev_priv, false);
2204
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002205 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002206 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002207
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002208 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002209
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002210 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002211 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002212
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002213 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002214 chv_set_memory_pm5(dev_priv, true);
2215
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002216 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002217 chv_set_memory_dvfs(dev_priv, true);
2218
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002219 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002220}
2221
Ville Syrjäläff32c542017-03-02 19:14:57 +02002222static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002223 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002224{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002225 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2226 const struct intel_crtc_state *crtc_state =
2227 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002228
2229 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002230 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2231 vlv_program_watermarks(dev_priv);
2232 mutex_unlock(&dev_priv->wm.wm_mutex);
2233}
2234
2235static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002236 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002237{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002238 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2239 const struct intel_crtc_state *crtc_state =
2240 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002241
2242 if (!crtc_state->wm.need_postvbl_update)
2243 return;
2244
2245 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002246 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002247 vlv_program_watermarks(dev_priv);
2248 mutex_unlock(&dev_priv->wm.wm_mutex);
2249}
2250
Ville Syrjälä432081b2016-10-31 22:37:03 +02002251static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002252{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002253 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002254 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002255 int srwm = 1;
2256 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002257 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002258
2259 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002260 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002261 if (crtc) {
2262 /* self-refresh has much higher latency */
2263 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002264 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002265 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002266 const struct drm_framebuffer *fb =
2267 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002268 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002269 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002270 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002271 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002272 int entries;
2273
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002274 entries = intel_wm_method2(clock, htotal,
2275 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002276 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2277 srwm = I965_FIFO_SIZE - entries;
2278 if (srwm < 0)
2279 srwm = 1;
2280 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002281 drm_dbg_kms(&dev_priv->drm,
2282 "self-refresh entries: %d, wm: %d\n",
2283 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002284
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002285 entries = intel_wm_method2(clock, htotal,
2286 crtc->base.cursor->state->crtc_w, 4,
2287 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002288 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002289 i965_cursor_wm_info.cacheline_size) +
2290 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002291
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002292 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002293 if (cursor_sr > i965_cursor_wm_info.max_wm)
2294 cursor_sr = i965_cursor_wm_info.max_wm;
2295
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002296 drm_dbg_kms(&dev_priv->drm,
2297 "self-refresh watermark: display plane %d "
2298 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002299
Imre Deak98584252014-06-13 14:54:20 +03002300 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002301 } else {
Imre Deak98584252014-06-13 14:54:20 +03002302 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002303 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002304 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002305 }
2306
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002307 drm_dbg_kms(&dev_priv->drm,
2308 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2309 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002310
2311 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002312 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2313 FW_WM(8, CURSORB) |
2314 FW_WM(8, PLANEB) |
2315 FW_WM(8, PLANEA));
2316 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2317 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002318 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002319 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002320
2321 if (cxsr_enabled)
2322 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002323}
2324
Ville Syrjäläf4998962015-03-10 17:02:21 +02002325#undef FW_WM
2326
Ville Syrjälä432081b2016-10-31 22:37:03 +02002327static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002328{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002329 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002330 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002331 u32 fwater_lo;
2332 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002333 int cwm, srwm = 1;
2334 int fifo_size;
2335 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002336 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002337
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002338 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002339 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002340 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341 wm_info = &i915_wm_info;
2342 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002343 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002345 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2346 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002347 if (intel_crtc_active(crtc)) {
2348 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002349 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002350 const struct drm_framebuffer *fb =
2351 crtc->base.primary->state->fb;
2352 int cpp;
2353
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002354 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002355 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002356 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002357 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002358
Damien Lespiau241bfc32013-09-25 16:45:37 +01002359 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002360 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002361 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002362 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002363 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002364 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002365 if (planea_wm > (long)wm_info->max_wm)
2366 planea_wm = wm_info->max_wm;
2367 }
2368
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002369 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002370 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002371
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002372 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2373 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002374 if (intel_crtc_active(crtc)) {
2375 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002376 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002377 const struct drm_framebuffer *fb =
2378 crtc->base.primary->state->fb;
2379 int cpp;
2380
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002381 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002382 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002383 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002384 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002385
Damien Lespiau241bfc32013-09-25 16:45:37 +01002386 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002387 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002388 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002389 if (enabled == NULL)
2390 enabled = crtc;
2391 else
2392 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002393 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002394 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002395 if (planeb_wm > (long)wm_info->max_wm)
2396 planeb_wm = wm_info->max_wm;
2397 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002398
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002399 drm_dbg_kms(&dev_priv->drm,
2400 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002401
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002402 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002403 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002404
Ville Syrjäläefc26112016-10-31 22:37:04 +02002405 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002406
2407 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002408 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002409 enabled = NULL;
2410 }
2411
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002412 /*
2413 * Overlay gets an aggressive default since video jitter is bad.
2414 */
2415 cwm = 2;
2416
2417 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002418 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002419
2420 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002421 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002422 /* self-refresh has much higher latency */
2423 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002424 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002425 &enabled->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002426 const struct drm_framebuffer *fb =
2427 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002428 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002429 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002430 int hdisplay = enabled->config->pipe_src_w;
2431 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002432 int entries;
2433
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002434 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002435 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002436 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002437 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002438
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002439 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2440 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002441 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002442 drm_dbg_kms(&dev_priv->drm,
2443 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002444 srwm = wm_info->fifo_size - entries;
2445 if (srwm < 0)
2446 srwm = 1;
2447
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002448 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002449 I915_WRITE(FW_BLC_SELF,
2450 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002451 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002452 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2453 }
2454
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002455 drm_dbg_kms(&dev_priv->drm,
2456 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2457 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002458
2459 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2460 fwater_hi = (cwm & 0x1f);
2461
2462 /* Set request length to 8 cachelines per fetch */
2463 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2464 fwater_hi = fwater_hi | (1 << 8);
2465
2466 I915_WRITE(FW_BLC, fwater_lo);
2467 I915_WRITE(FW_BLC2, fwater_hi);
2468
Imre Deak5209b1f2014-07-01 12:36:17 +03002469 if (enabled)
2470 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002471}
2472
Ville Syrjälä432081b2016-10-31 22:37:03 +02002473static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002474{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002475 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002476 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002477 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002478 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002479 int planea_wm;
2480
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002481 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002482 if (crtc == NULL)
2483 return;
2484
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002485 adjusted_mode = &crtc->config->hw.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002486 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002487 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002488 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002489 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002490 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2491 fwater_lo |= (3<<8) | planea_wm;
2492
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002493 drm_dbg_kms(&dev_priv->drm,
2494 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002495
2496 I915_WRITE(FW_BLC, fwater_lo);
2497}
2498
Ville Syrjälä37126462013-08-01 16:18:55 +03002499/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002500static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2501 unsigned int cpp,
2502 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002503{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002504 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002505
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002506 ret = intel_wm_method1(pixel_rate, cpp, latency);
2507 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508
2509 return ret;
2510}
2511
Ville Syrjälä37126462013-08-01 16:18:55 +03002512/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002513static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2514 unsigned int htotal,
2515 unsigned int width,
2516 unsigned int cpp,
2517 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002518{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002519 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002520
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002521 ret = intel_wm_method2(pixel_rate, htotal,
2522 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002523 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002524
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002525 return ret;
2526}
2527
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002528static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002529{
Matt Roper15126882015-12-03 11:37:40 -08002530 /*
2531 * Neither of these should be possible since this function shouldn't be
2532 * called if the CRTC is off or the plane is invisible. But let's be
2533 * extra paranoid to avoid a potential divide-by-zero if we screw up
2534 * elsewhere in the driver.
2535 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002536 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002537 return 0;
2538 if (WARN_ON(!horiz_pixels))
2539 return 0;
2540
Ville Syrjäläac484962016-01-20 21:05:26 +02002541 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002542}
2543
Imre Deak820c1982013-12-17 14:46:36 +02002544struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002545 u16 pri;
2546 u16 spr;
2547 u16 cur;
2548 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002549};
2550
Ville Syrjälä37126462013-08-01 16:18:55 +03002551/*
2552 * For both WM_PIPE and WM_LP.
2553 * mem_value must be in 0.1us units.
2554 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002555static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2556 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002557 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002558{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002559 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002560 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002561
Ville Syrjälä03981c62018-11-14 19:34:40 +02002562 if (mem_value == 0)
2563 return U32_MAX;
2564
Maarten Lankhorstec193642019-06-28 10:55:17 +02002565 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002566 return 0;
2567
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002568 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002569
Maarten Lankhorstec193642019-06-28 10:55:17 +02002570 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002571
2572 if (!is_lp)
2573 return method1;
2574
Maarten Lankhorstec193642019-06-28 10:55:17 +02002575 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002576 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002577 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002578 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002579
2580 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002581}
2582
Ville Syrjälä37126462013-08-01 16:18:55 +03002583/*
2584 * For both WM_PIPE and WM_LP.
2585 * mem_value must be in 0.1us units.
2586 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002587static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2588 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002589 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002590{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002591 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002592 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002593
Ville Syrjälä03981c62018-11-14 19:34:40 +02002594 if (mem_value == 0)
2595 return U32_MAX;
2596
Maarten Lankhorstec193642019-06-28 10:55:17 +02002597 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002598 return 0;
2599
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002600 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002601
Maarten Lankhorstec193642019-06-28 10:55:17 +02002602 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2603 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002604 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002605 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002606 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002607 return min(method1, method2);
2608}
2609
Ville Syrjälä37126462013-08-01 16:18:55 +03002610/*
2611 * For both WM_PIPE and WM_LP.
2612 * mem_value must be in 0.1us units.
2613 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002614static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2615 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002616 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002617{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002618 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002619
Ville Syrjälä03981c62018-11-14 19:34:40 +02002620 if (mem_value == 0)
2621 return U32_MAX;
2622
Maarten Lankhorstec193642019-06-28 10:55:17 +02002623 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002624 return 0;
2625
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002626 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002627
Maarten Lankhorstec193642019-06-28 10:55:17 +02002628 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002629 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002630 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002631 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002632}
2633
Paulo Zanonicca32e92013-05-31 11:45:06 -03002634/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002635static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2636 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002637 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002638{
Ville Syrjälä83054942016-11-18 21:53:00 +02002639 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002640
Maarten Lankhorstec193642019-06-28 10:55:17 +02002641 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002642 return 0;
2643
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002644 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002645
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002646 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2647 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002648}
2649
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002650static unsigned int
2651ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002652{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002653 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002654 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002655 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002656 return 768;
2657 else
2658 return 512;
2659}
2660
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002661static unsigned int
2662ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2663 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002664{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002665 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002666 /* BDW primary/sprite plane watermarks */
2667 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002668 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002669 /* IVB/HSW primary/sprite plane watermarks */
2670 return level == 0 ? 127 : 1023;
2671 else if (!is_sprite)
2672 /* ILK/SNB primary plane watermarks */
2673 return level == 0 ? 127 : 511;
2674 else
2675 /* ILK/SNB sprite plane watermarks */
2676 return level == 0 ? 63 : 255;
2677}
2678
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002679static unsigned int
2680ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002681{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002682 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002683 return level == 0 ? 63 : 255;
2684 else
2685 return level == 0 ? 31 : 63;
2686}
2687
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002688static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002689{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002690 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002691 return 31;
2692 else
2693 return 15;
2694}
2695
Ville Syrjälä158ae642013-08-07 13:28:19 +03002696/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002697static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002698 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002699 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002700 enum intel_ddb_partitioning ddb_partitioning,
2701 bool is_sprite)
2702{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002703 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002704
2705 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002706 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002707 return 0;
2708
2709 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002710 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002711 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002712
2713 /*
2714 * For some reason the non self refresh
2715 * FIFO size is only half of the self
2716 * refresh FIFO size on ILK/SNB.
2717 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002718 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002719 fifo_size /= 2;
2720 }
2721
Ville Syrjälä240264f2013-08-07 13:29:12 +03002722 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002723 /* level 0 is always calculated with 1:1 split */
2724 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2725 if (is_sprite)
2726 fifo_size *= 5;
2727 fifo_size /= 6;
2728 } else {
2729 fifo_size /= 2;
2730 }
2731 }
2732
2733 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002734 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002735}
2736
2737/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002738static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002739 int level,
2740 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002741{
2742 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002743 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002744 return 64;
2745
2746 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002747 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002748}
2749
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002750static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002751 int level,
2752 const struct intel_wm_config *config,
2753 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002754 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002755{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002756 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2757 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2758 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2759 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002760}
2761
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002762static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002763 int level,
2764 struct ilk_wm_maximums *max)
2765{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002766 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2767 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2768 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2769 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002770}
2771
Ville Syrjäläd9395652013-10-09 19:18:10 +03002772static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002773 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002774 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002775{
2776 bool ret;
2777
2778 /* already determined to be invalid? */
2779 if (!result->enable)
2780 return false;
2781
2782 result->enable = result->pri_val <= max->pri &&
2783 result->spr_val <= max->spr &&
2784 result->cur_val <= max->cur;
2785
2786 ret = result->enable;
2787
2788 /*
2789 * HACK until we can pre-compute everything,
2790 * and thus fail gracefully if LP0 watermarks
2791 * are exceeded...
2792 */
2793 if (level == 0 && !result->enable) {
2794 if (result->pri_val > max->pri)
2795 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2796 level, result->pri_val, max->pri);
2797 if (result->spr_val > max->spr)
2798 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2799 level, result->spr_val, max->spr);
2800 if (result->cur_val > max->cur)
2801 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2802 level, result->cur_val, max->cur);
2803
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002804 result->pri_val = min_t(u32, result->pri_val, max->pri);
2805 result->spr_val = min_t(u32, result->spr_val, max->spr);
2806 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002807 result->enable = true;
2808 }
2809
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002810 return ret;
2811}
2812
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002813static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002814 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002815 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002816 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002817 const struct intel_plane_state *pristate,
2818 const struct intel_plane_state *sprstate,
2819 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002820 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002821{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002822 u16 pri_latency = dev_priv->wm.pri_latency[level];
2823 u16 spr_latency = dev_priv->wm.spr_latency[level];
2824 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002825
2826 /* WM1+ latency values stored in 0.5us units */
2827 if (level > 0) {
2828 pri_latency *= 5;
2829 spr_latency *= 5;
2830 cur_latency *= 5;
2831 }
2832
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002833 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002834 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002835 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002836 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002837 }
2838
2839 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002840 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002841
2842 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002843 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002844
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002845 result->enable = true;
2846}
2847
Ville Syrjäläbb726512016-10-31 22:37:24 +02002848static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002849 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002850{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002851 struct intel_uncore *uncore = &dev_priv->uncore;
2852
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002853 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002854 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002855 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002856 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002857
2858 /* read the first set of memory latencies[0:3] */
2859 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002860 ret = sandybridge_pcode_read(dev_priv,
2861 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002862 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002863
2864 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002865 drm_err(&dev_priv->drm,
2866 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002867 return;
2868 }
2869
2870 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2871 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2872 GEN9_MEM_LATENCY_LEVEL_MASK;
2873 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2874 GEN9_MEM_LATENCY_LEVEL_MASK;
2875 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2876 GEN9_MEM_LATENCY_LEVEL_MASK;
2877
2878 /* read the second set of memory latencies[4:7] */
2879 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002880 ret = sandybridge_pcode_read(dev_priv,
2881 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002882 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002883 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002884 drm_err(&dev_priv->drm,
2885 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002886 return;
2887 }
2888
2889 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2890 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2891 GEN9_MEM_LATENCY_LEVEL_MASK;
2892 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2893 GEN9_MEM_LATENCY_LEVEL_MASK;
2894 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2895 GEN9_MEM_LATENCY_LEVEL_MASK;
2896
Vandana Kannan367294b2014-11-04 17:06:46 +00002897 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002898 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2899 * need to be disabled. We make sure to sanitize the values out
2900 * of the punit to satisfy this requirement.
2901 */
2902 for (level = 1; level <= max_level; level++) {
2903 if (wm[level] == 0) {
2904 for (i = level + 1; i <= max_level; i++)
2905 wm[i] = 0;
2906 break;
2907 }
2908 }
2909
2910 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002911 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002912 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002913 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002914 * to add 2us to the various latency levels we retrieve from the
2915 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002916 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002917 if (wm[0] == 0) {
2918 wm[0] += 2;
2919 for (level = 1; level <= max_level; level++) {
2920 if (wm[level] == 0)
2921 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002922 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002923 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002924 }
2925
Mahesh Kumar86b59282018-08-31 16:39:42 +05302926 /*
2927 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2928 * If we could not get dimm info enable this WA to prevent from
2929 * any underrun. If not able to get Dimm info assume 16GB dimm
2930 * to avoid any underrun.
2931 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002932 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302933 wm[0] += 1;
2934
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002935 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002936 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002937
2938 wm[0] = (sskpd >> 56) & 0xFF;
2939 if (wm[0] == 0)
2940 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002941 wm[1] = (sskpd >> 4) & 0xFF;
2942 wm[2] = (sskpd >> 12) & 0xFF;
2943 wm[3] = (sskpd >> 20) & 0x1FF;
2944 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002945 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002946 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002947
2948 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2949 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2950 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2951 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002952 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002953 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002954
2955 /* ILK primary LP0 latency is 700 ns */
2956 wm[0] = 7;
2957 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2958 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002959 } else {
2960 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002961 }
2962}
2963
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002964static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002965 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002966{
2967 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002968 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002969 wm[0] = 13;
2970}
2971
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002972static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002973 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002974{
2975 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002976 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002977 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002978}
2979
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002980int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002981{
2982 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002983 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002984 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002985 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002986 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002987 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002988 return 3;
2989 else
2990 return 2;
2991}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002992
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002993static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002994 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002995 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002996{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002997 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002998
2999 for (level = 0; level <= max_level; level++) {
3000 unsigned int latency = wm[level];
3001
3002 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003003 drm_dbg_kms(&dev_priv->drm,
3004 "%s WM%d latency not provided\n",
3005 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003006 continue;
3007 }
3008
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003009 /*
3010 * - latencies are in us on gen9.
3011 * - before then, WM1+ latency values are in 0.5us units
3012 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07003013 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003014 latency *= 10;
3015 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003016 latency *= 5;
3017
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003018 drm_dbg_kms(&dev_priv->drm,
3019 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3020 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003021 }
3022}
3023
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003024static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003025 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003026{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003027 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003028
3029 if (wm[0] >= min)
3030 return false;
3031
3032 wm[0] = max(wm[0], min);
3033 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003034 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003035
3036 return true;
3037}
3038
Ville Syrjäläbb726512016-10-31 22:37:24 +02003039static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003040{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003041 bool changed;
3042
3043 /*
3044 * The BIOS provided WM memory latency values are often
3045 * inadequate for high resolution displays. Adjust them.
3046 */
3047 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3048 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3049 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3050
3051 if (!changed)
3052 return;
3053
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003054 drm_dbg_kms(&dev_priv->drm,
3055 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003056 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);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003059}
3060
Ville Syrjälä03981c62018-11-14 19:34:40 +02003061static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3062{
3063 /*
3064 * On some SNB machines (Thinkpad X220 Tablet at least)
3065 * LP3 usage can cause vblank interrupts to be lost.
3066 * The DEIIR bit will go high but it looks like the CPU
3067 * never gets interrupted.
3068 *
3069 * It's not clear whether other interrupt source could
3070 * be affected or if this is somehow limited to vblank
3071 * interrupts only. To play it safe we disable LP3
3072 * watermarks entirely.
3073 */
3074 if (dev_priv->wm.pri_latency[3] == 0 &&
3075 dev_priv->wm.spr_latency[3] == 0 &&
3076 dev_priv->wm.cur_latency[3] == 0)
3077 return;
3078
3079 dev_priv->wm.pri_latency[3] = 0;
3080 dev_priv->wm.spr_latency[3] = 0;
3081 dev_priv->wm.cur_latency[3] = 0;
3082
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003083 drm_dbg_kms(&dev_priv->drm,
3084 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003085 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3086 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3087 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3088}
3089
Ville Syrjäläbb726512016-10-31 22:37:24 +02003090static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003091{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003092 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003093
3094 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3095 sizeof(dev_priv->wm.pri_latency));
3096 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3097 sizeof(dev_priv->wm.pri_latency));
3098
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003099 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003100 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003101
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003102 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3103 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3104 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003105
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003106 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003107 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003108 snb_wm_lp3_irq_quirk(dev_priv);
3109 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003110}
3111
Ville Syrjäläbb726512016-10-31 22:37:24 +02003112static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003113{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003114 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003115 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003116}
3117
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003118static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003119 struct intel_pipe_wm *pipe_wm)
3120{
3121 /* LP0 watermark maximums depend on this pipe alone */
3122 const struct intel_wm_config config = {
3123 .num_pipes_active = 1,
3124 .sprites_enabled = pipe_wm->sprites_enabled,
3125 .sprites_scaled = pipe_wm->sprites_scaled,
3126 };
3127 struct ilk_wm_maximums max;
3128
3129 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003130 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003131
3132 /* At least LP0 must be valid */
3133 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003134 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003135 return false;
3136 }
3137
3138 return true;
3139}
3140
Matt Roper261a27d2015-10-08 15:28:25 -07003141/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003142static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003143{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003144 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003145 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003146 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003147 struct intel_plane *plane;
3148 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003149 const struct intel_plane_state *pristate = NULL;
3150 const struct intel_plane_state *sprstate = NULL;
3151 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003152 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003153 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003154
Maarten Lankhorstec193642019-06-28 10:55:17 +02003155 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003156
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003157 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3158 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3159 pristate = plane_state;
3160 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3161 sprstate = plane_state;
3162 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3163 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003164 }
3165
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003166 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003167 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003168 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3169 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3170 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3171 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003172 }
3173
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003174 usable_level = max_level;
3175
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003176 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003177 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003178 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003179
3180 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003181 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003182 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003183
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003184 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003185 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003186 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003187
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003188 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003189 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003190
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003191 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003192
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003193 for (level = 1; level <= usable_level; level++) {
3194 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003195
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003196 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003197 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003198
3199 /*
3200 * Disable any watermark level that exceeds the
3201 * register maximums since such watermarks are
3202 * always invalid.
3203 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003204 if (!ilk_validate_wm_level(level, &max, wm)) {
3205 memset(wm, 0, sizeof(*wm));
3206 break;
3207 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003208 }
3209
Matt Roper86c8bbb2015-09-24 15:53:16 -07003210 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003211}
3212
3213/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003214 * Build a set of 'intermediate' watermark values that satisfy both the old
3215 * state and the new state. These can be programmed to the hardware
3216 * immediately.
3217 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003218static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003219{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003220 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->uapi.crtc);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003221 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003222 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003223 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003224 to_intel_atomic_state(newstate->uapi.state);
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003225 const struct intel_crtc_state *oldstate =
3226 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3227 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003228 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003229
3230 /*
3231 * Start with the final, target watermarks, then combine with the
3232 * currently active watermarks to get values that are safe both before
3233 * and after the vblank.
3234 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003235 *a = newstate->wm.ilk.optimal;
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003236 if (!newstate->hw.active || drm_atomic_crtc_needs_modeset(&newstate->uapi) ||
Ville Syrjäläf255c622018-11-08 17:10:13 +02003237 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003238 return 0;
3239
Matt Ropered4a6a72016-02-23 17:20:13 -08003240 a->pipe_enabled |= b->pipe_enabled;
3241 a->sprites_enabled |= b->sprites_enabled;
3242 a->sprites_scaled |= b->sprites_scaled;
3243
3244 for (level = 0; level <= max_level; level++) {
3245 struct intel_wm_level *a_wm = &a->wm[level];
3246 const struct intel_wm_level *b_wm = &b->wm[level];
3247
3248 a_wm->enable &= b_wm->enable;
3249 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3250 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3251 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3252 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3253 }
3254
3255 /*
3256 * We need to make sure that these merged watermark values are
3257 * actually a valid configuration themselves. If they're not,
3258 * there's no safe way to transition from the old state to
3259 * the new state, so we need to fail the atomic transaction.
3260 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003261 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003262 return -EINVAL;
3263
3264 /*
3265 * If our intermediate WM are identical to the final WM, then we can
3266 * omit the post-vblank programming; only update if it's different.
3267 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003268 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3269 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003270
3271 return 0;
3272}
3273
3274/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003275 * Merge the watermarks from all active pipes for a specific level.
3276 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003277static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003278 int level,
3279 struct intel_wm_level *ret_wm)
3280{
3281 const struct intel_crtc *intel_crtc;
3282
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003283 ret_wm->enable = true;
3284
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003285 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003286 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003287 const struct intel_wm_level *wm = &active->wm[level];
3288
3289 if (!active->pipe_enabled)
3290 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003291
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003292 /*
3293 * The watermark values may have been used in the past,
3294 * so we must maintain them in the registers for some
3295 * time even if the level is now disabled.
3296 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003297 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003298 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003299
3300 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3301 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3302 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3303 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3304 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003305}
3306
3307/*
3308 * Merge all low power watermarks for all active pipes.
3309 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003310static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003311 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003312 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003313 struct intel_pipe_wm *merged)
3314{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003315 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003316 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003317
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003318 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003319 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003320 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003321 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003322
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003323 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003324 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003325
3326 /* merge each WM1+ level */
3327 for (level = 1; level <= max_level; level++) {
3328 struct intel_wm_level *wm = &merged->wm[level];
3329
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003330 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003331
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003332 if (level > last_enabled_level)
3333 wm->enable = false;
3334 else if (!ilk_validate_wm_level(level, max, wm))
3335 /* make sure all following levels get disabled */
3336 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003337
3338 /*
3339 * The spec says it is preferred to disable
3340 * FBC WMs instead of disabling a WM level.
3341 */
3342 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003343 if (wm->enable)
3344 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003345 wm->fbc_val = 0;
3346 }
3347 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003348
3349 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3350 /*
3351 * FIXME this is racy. FBC might get enabled later.
3352 * What we should check here is whether FBC can be
3353 * enabled sometime later.
3354 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003355 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003356 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003357 for (level = 2; level <= max_level; level++) {
3358 struct intel_wm_level *wm = &merged->wm[level];
3359
3360 wm->enable = false;
3361 }
3362 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003363}
3364
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003365static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3366{
3367 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3368 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3369}
3370
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003371/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003372static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3373 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003374{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003375 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003376 return 2 * level;
3377 else
3378 return dev_priv->wm.pri_latency[level];
3379}
3380
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003381static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003382 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003383 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003384 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003385{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003386 struct intel_crtc *intel_crtc;
3387 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003388
Ville Syrjälä0362c782013-10-09 19:17:57 +03003389 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003390 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003391
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003392 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003393 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003394 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003395
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003396 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003397
Ville Syrjälä0362c782013-10-09 19:17:57 +03003398 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003399
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003400 /*
3401 * Maintain the watermark values even if the level is
3402 * disabled. Doing otherwise could cause underruns.
3403 */
3404 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003405 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003406 (r->pri_val << WM1_LP_SR_SHIFT) |
3407 r->cur_val;
3408
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003409 if (r->enable)
3410 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3411
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003412 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003413 results->wm_lp[wm_lp - 1] |=
3414 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3415 else
3416 results->wm_lp[wm_lp - 1] |=
3417 r->fbc_val << WM1_LP_FBC_SHIFT;
3418
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003419 /*
3420 * Always set WM1S_LP_EN when spr_val != 0, even if the
3421 * level is disabled. Doing otherwise could cause underruns.
3422 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003423 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303424 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003425 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3426 } else
3427 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003428 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003429
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003430 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003431 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003432 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003433 const struct intel_pipe_wm *pipe_wm = &intel_crtc->wm.active.ilk;
3434 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003435
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303436 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003437 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003438
3439 results->wm_pipe[pipe] =
3440 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3441 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3442 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003443 }
3444}
3445
Paulo Zanoni861f3382013-05-31 10:19:21 -03003446/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3447 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003448static struct intel_pipe_wm *
3449ilk_find_best_result(struct drm_i915_private *dev_priv,
3450 struct intel_pipe_wm *r1,
3451 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003452{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003453 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003454 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003455
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003456 for (level = 1; level <= max_level; level++) {
3457 if (r1->wm[level].enable)
3458 level1 = level;
3459 if (r2->wm[level].enable)
3460 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003461 }
3462
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003463 if (level1 == level2) {
3464 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003465 return r2;
3466 else
3467 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003468 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003469 return r1;
3470 } else {
3471 return r2;
3472 }
3473}
3474
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003475/* dirty bits used to track which watermarks need changes */
3476#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003477#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3478#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3479#define WM_DIRTY_FBC (1 << 24)
3480#define WM_DIRTY_DDB (1 << 25)
3481
Damien Lespiau055e3932014-08-18 13:49:10 +01003482static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003483 const struct ilk_wm_values *old,
3484 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003485{
3486 unsigned int dirty = 0;
3487 enum pipe pipe;
3488 int wm_lp;
3489
Damien Lespiau055e3932014-08-18 13:49:10 +01003490 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003491 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3492 dirty |= WM_DIRTY_PIPE(pipe);
3493 /* Must disable LP1+ watermarks too */
3494 dirty |= WM_DIRTY_LP_ALL;
3495 }
3496 }
3497
3498 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3499 dirty |= WM_DIRTY_FBC;
3500 /* Must disable LP1+ watermarks too */
3501 dirty |= WM_DIRTY_LP_ALL;
3502 }
3503
3504 if (old->partitioning != new->partitioning) {
3505 dirty |= WM_DIRTY_DDB;
3506 /* Must disable LP1+ watermarks too */
3507 dirty |= WM_DIRTY_LP_ALL;
3508 }
3509
3510 /* LP1+ watermarks already deemed dirty, no need to continue */
3511 if (dirty & WM_DIRTY_LP_ALL)
3512 return dirty;
3513
3514 /* Find the lowest numbered LP1+ watermark in need of an update... */
3515 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3516 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3517 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3518 break;
3519 }
3520
3521 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3522 for (; wm_lp <= 3; wm_lp++)
3523 dirty |= WM_DIRTY_LP(wm_lp);
3524
3525 return dirty;
3526}
3527
Ville Syrjälä8553c182013-12-05 15:51:39 +02003528static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3529 unsigned int dirty)
3530{
Imre Deak820c1982013-12-17 14:46:36 +02003531 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003532 bool changed = false;
3533
3534 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3535 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3536 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3537 changed = true;
3538 }
3539 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3540 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3541 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3542 changed = true;
3543 }
3544 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3545 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3546 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3547 changed = true;
3548 }
3549
3550 /*
3551 * Don't touch WM1S_LP_EN here.
3552 * Doing so could cause underruns.
3553 */
3554
3555 return changed;
3556}
3557
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003558/*
3559 * The spec says we shouldn't write when we don't need, because every write
3560 * causes WMs to be re-evaluated, expending some power.
3561 */
Imre Deak820c1982013-12-17 14:46:36 +02003562static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3563 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003564{
Imre Deak820c1982013-12-17 14:46:36 +02003565 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003566 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003567 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003568
Damien Lespiau055e3932014-08-18 13:49:10 +01003569 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003570 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003571 return;
3572
Ville Syrjälä8553c182013-12-05 15:51:39 +02003573 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003574
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003575 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Ville Syrjälä96eaeb3d2018-12-12 23:17:38 +02003576 I915_WRITE(WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003577 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Ville Syrjälä96eaeb3d2018-12-12 23:17:38 +02003578 I915_WRITE(WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003579 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Ville Syrjälä96eaeb3d2018-12-12 23:17:38 +02003580 I915_WRITE(WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003581
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003582 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003583 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003584 val = I915_READ(WM_MISC);
3585 if (results->partitioning == INTEL_DDB_PART_1_2)
3586 val &= ~WM_MISC_DATA_PARTITION_5_6;
3587 else
3588 val |= WM_MISC_DATA_PARTITION_5_6;
3589 I915_WRITE(WM_MISC, val);
3590 } else {
3591 val = I915_READ(DISP_ARB_CTL2);
3592 if (results->partitioning == INTEL_DDB_PART_1_2)
3593 val &= ~DISP_DATA_PARTITION_5_6;
3594 else
3595 val |= DISP_DATA_PARTITION_5_6;
3596 I915_WRITE(DISP_ARB_CTL2, val);
3597 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003598 }
3599
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003600 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003601 val = I915_READ(DISP_ARB_CTL);
3602 if (results->enable_fbc_wm)
3603 val &= ~DISP_FBC_WM_DIS;
3604 else
3605 val |= DISP_FBC_WM_DIS;
3606 I915_WRITE(DISP_ARB_CTL, val);
3607 }
3608
Imre Deak954911e2013-12-17 14:46:34 +02003609 if (dirty & WM_DIRTY_LP(1) &&
3610 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3611 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3612
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003613 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003614 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3615 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3616 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3617 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3618 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003619
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003620 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003621 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003622 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003623 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003624 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003625 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003626
3627 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003628}
3629
Ville Syrjälä60aca572019-11-27 21:05:51 +02003630bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003631{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003632 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3633}
3634
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003635u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303636{
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003637 int i;
3638 int max_slices = INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
3639 u8 enabled_slices_mask = 0;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303640
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003641 for (i = 0; i < max_slices; i++) {
3642 if (I915_READ(DBUF_CTL_S(i)) & DBUF_POWER_STATE)
3643 enabled_slices_mask |= BIT(i);
3644 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303645
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003646 return enabled_slices_mask;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303647}
3648
Matt Roper024c9042015-09-24 15:53:11 -07003649/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003650 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3651 * so assume we'll always need it in order to avoid underruns.
3652 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003653static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003654{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003655 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003656}
3657
Paulo Zanoni56feca92016-09-22 18:00:28 -03003658static bool
3659intel_has_sagv(struct drm_i915_private *dev_priv)
3660{
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003661 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3662 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003663}
3664
James Ausmusb068a862019-10-09 10:23:14 -07003665static void
3666skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3667{
James Ausmusda80f042019-10-09 10:23:15 -07003668 if (INTEL_GEN(dev_priv) >= 12) {
3669 u32 val = 0;
3670 int ret;
3671
3672 ret = sandybridge_pcode_read(dev_priv,
3673 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3674 &val, NULL);
3675 if (!ret) {
3676 dev_priv->sagv_block_time_us = val;
3677 return;
3678 }
3679
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003680 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
James Ausmusda80f042019-10-09 10:23:15 -07003681 } else if (IS_GEN(dev_priv, 11)) {
James Ausmusb068a862019-10-09 10:23:14 -07003682 dev_priv->sagv_block_time_us = 10;
3683 return;
3684 } else if (IS_GEN(dev_priv, 10)) {
3685 dev_priv->sagv_block_time_us = 20;
3686 return;
3687 } else if (IS_GEN(dev_priv, 9)) {
3688 dev_priv->sagv_block_time_us = 30;
3689 return;
3690 } else {
3691 MISSING_CASE(INTEL_GEN(dev_priv));
3692 }
3693
3694 /* Default to an unusable block time */
3695 dev_priv->sagv_block_time_us = -1;
3696}
3697
Lyude656d1b82016-08-17 15:55:54 -04003698/*
3699 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3700 * depending on power and performance requirements. The display engine access
3701 * to system memory is blocked during the adjustment time. Because of the
3702 * blocking time, having this enabled can cause full system hangs and/or pipe
3703 * underruns if we don't meet all of the following requirements:
3704 *
3705 * - <= 1 pipe enabled
3706 * - All planes can enable watermarks for latencies >= SAGV engine block time
3707 * - We're not using an interlaced display configuration
3708 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003709static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003710intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003711{
3712 int ret;
3713
Paulo Zanoni56feca92016-09-22 18:00:28 -03003714 if (!intel_has_sagv(dev_priv))
3715 return 0;
3716
3717 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003718 return 0;
3719
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003720 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003721 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3722 GEN9_SAGV_ENABLE);
3723
Ville Syrjäläff61a972018-12-21 19:14:34 +02003724 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003725
3726 /*
3727 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003728 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003729 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003730 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003731 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003732 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003733 return 0;
3734 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003735 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003736 return ret;
3737 }
3738
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003739 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003740 return 0;
3741}
3742
Ville Syrjälä71024042020-09-25 15:17:48 +03003743static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003744intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003745{
Imre Deakb3b8e992016-12-05 18:27:38 +02003746 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003747
Paulo Zanoni56feca92016-09-22 18:00:28 -03003748 if (!intel_has_sagv(dev_priv))
3749 return 0;
3750
3751 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003752 return 0;
3753
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003754 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003755 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003756 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3757 GEN9_SAGV_DISABLE,
3758 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3759 1);
Lyude656d1b82016-08-17 15:55:54 -04003760 /*
3761 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003762 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003763 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003764 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003765 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003766 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003767 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003768 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003769 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003770 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003771 }
3772
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003773 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003774 return 0;
3775}
3776
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003777void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3778{
3779 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003780 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003781 const struct intel_bw_state *old_bw_state;
3782 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003783
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003784 /*
3785 * Just return if we can't control SAGV or don't have it.
3786 * This is different from situation when we have SAGV but just can't
3787 * afford it due to DBuf limitation - in case if SAGV is completely
3788 * disabled in a BIOS, we are not even allowed to send a PCode request,
3789 * as it will throw an error. So have to check it here.
3790 */
3791 if (!intel_has_sagv(dev_priv))
3792 return;
3793
3794 new_bw_state = intel_atomic_get_new_bw_state(state);
3795 if (!new_bw_state)
3796 return;
3797
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003798 if (INTEL_GEN(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003799 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003800 return;
3801 }
3802
3803 old_bw_state = intel_atomic_get_old_bw_state(state);
3804 /*
3805 * Nothing to mask
3806 */
3807 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3808 return;
3809
3810 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3811
3812 /*
3813 * If new mask is zero - means there is nothing to mask,
3814 * we can only unmask, which should be done in unmask.
3815 */
3816 if (!new_mask)
3817 return;
3818
3819 /*
3820 * Restrict required qgv points before updating the configuration.
3821 * According to BSpec we can't mask and unmask qgv points at the same
3822 * time. Also masking should be done before updating the configuration
3823 * and unmasking afterwards.
3824 */
3825 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003826}
3827
3828void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3829{
3830 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003831 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003832 const struct intel_bw_state *old_bw_state;
3833 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003834
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003835 /*
3836 * Just return if we can't control SAGV or don't have it.
3837 * This is different from situation when we have SAGV but just can't
3838 * afford it due to DBuf limitation - in case if SAGV is completely
3839 * disabled in a BIOS, we are not even allowed to send a PCode request,
3840 * as it will throw an error. So have to check it here.
3841 */
3842 if (!intel_has_sagv(dev_priv))
3843 return;
3844
3845 new_bw_state = intel_atomic_get_new_bw_state(state);
3846 if (!new_bw_state)
3847 return;
3848
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003849 if (INTEL_GEN(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003850 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003851 return;
3852 }
3853
3854 old_bw_state = intel_atomic_get_old_bw_state(state);
3855 /*
3856 * Nothing to unmask
3857 */
3858 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3859 return;
3860
3861 new_mask = new_bw_state->qgv_points_mask;
3862
3863 /*
3864 * Allow required qgv points after updating the configuration.
3865 * According to BSpec we can't mask and unmask qgv points at the same
3866 * time. Also masking should be done before updating the configuration
3867 * and unmasking afterwards.
3868 */
3869 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003870}
3871
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003872static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003873{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003874 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003875 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003876 struct intel_plane *plane;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003877 const struct intel_plane_state *plane_state;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003878 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003879
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003880 if (!intel_has_sagv(dev_priv))
3881 return false;
3882
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003883 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003884 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003885
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003886 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003887 return false;
3888
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003889 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003890 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003891 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003892
Lyude656d1b82016-08-17 15:55:54 -04003893 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003894 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003895 continue;
3896
3897 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003898 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003899 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003900 { }
3901
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003902 latency = dev_priv->wm.skl_latency[level];
3903
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003904 if (skl_needs_memory_bw_wa(dev_priv) &&
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003905 plane_state->uapi.fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003906 I915_FORMAT_MOD_X_TILED)
3907 latency += 15;
3908
Lyude656d1b82016-08-17 15:55:54 -04003909 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003910 * If any of the planes on this pipe don't enable wm levels that
3911 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003912 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003913 */
James Ausmusb068a862019-10-09 10:23:14 -07003914 if (latency < dev_priv->sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003915 return false;
3916 }
3917
3918 return true;
3919}
3920
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003921static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3922{
3923 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3924 enum plane_id plane_id;
3925
3926 if (!crtc_state->hw.active)
3927 return true;
3928
3929 for_each_plane_id_on_crtc(crtc, plane_id) {
3930 const struct skl_ddb_entry *plane_alloc =
3931 &crtc_state->wm.skl.plane_ddb_y[plane_id];
3932 const struct skl_plane_wm *wm =
3933 &crtc_state->wm.skl.optimal.planes[plane_id];
3934
3935 if (skl_ddb_entry_size(plane_alloc) < wm->sagv_wm0.min_ddb_alloc)
3936 return false;
3937 }
3938
3939 return true;
3940}
3941
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003942static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3943{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3945 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3946
3947 if (INTEL_GEN(dev_priv) >= 12)
3948 return tgl_crtc_can_enable_sagv(crtc_state);
3949 else
3950 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003951}
3952
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003953bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3954 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003955{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003956 if (INTEL_GEN(dev_priv) < 11 &&
3957 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003958 return false;
3959
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003960 return bw_state->pipe_sagv_reject == 0;
3961}
3962
3963static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3964{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003965 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003966 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003967 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003968 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003969 struct intel_bw_state *new_bw_state = NULL;
3970 const struct intel_bw_state *old_bw_state = NULL;
3971 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003972
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003973 for_each_new_intel_crtc_in_state(state, crtc,
3974 new_crtc_state, i) {
3975 new_bw_state = intel_atomic_get_bw_state(state);
3976 if (IS_ERR(new_bw_state))
3977 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003978
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003979 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003980
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003981 if (intel_crtc_can_enable_sagv(new_crtc_state))
3982 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
3983 else
3984 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
3985 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003986
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003987 if (!new_bw_state)
3988 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003989
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003990 new_bw_state->active_pipes =
3991 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003992
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003993 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
3994 ret = intel_atomic_lock_global_state(&new_bw_state->base);
3995 if (ret)
3996 return ret;
3997 }
3998
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003999 for_each_new_intel_crtc_in_state(state, crtc,
4000 new_crtc_state, i) {
4001 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4002
4003 /*
4004 * We store use_sagv_wm in the crtc state rather than relying on
4005 * that bw state since we have no convenient way to get at the
4006 * latter from the plane commit hooks (especially in the legacy
4007 * cursor case)
4008 */
4009 pipe_wm->use_sagv_wm = INTEL_GEN(dev_priv) >= 12 &&
4010 intel_can_enable_sagv(dev_priv, new_bw_state);
4011 }
4012
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004013 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4014 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004015 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4016 if (ret)
4017 return ret;
4018 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4019 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4020 if (ret)
4021 return ret;
4022 }
4023
4024 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004025}
4026
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004027/*
4028 * Calculate initial DBuf slice offset, based on slice size
4029 * and mask(i.e if slice size is 1024 and second slice is enabled
4030 * offset would be 1024)
4031 */
4032static unsigned int
4033icl_get_first_dbuf_slice_offset(u32 dbuf_slice_mask,
4034 u32 slice_size,
4035 u32 ddb_size)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304036{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004037 unsigned int offset = 0;
4038
4039 if (!dbuf_slice_mask)
4040 return 0;
4041
4042 offset = (ffs(dbuf_slice_mask) - 1) * slice_size;
4043
4044 WARN_ON(offset >= ddb_size);
4045 return offset;
4046}
4047
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004048u16 intel_get_ddb_size(struct drm_i915_private *dev_priv)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004049{
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304050 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304051 drm_WARN_ON(&dev_priv->drm, ddb_size == 0);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304052
4053 if (INTEL_GEN(dev_priv) < 11)
4054 return ddb_size - 4; /* 4 blocks for bypass path allocation */
4055
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304056 return ddb_size;
4057}
4058
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004059u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4060 const struct skl_ddb_entry *entry)
4061{
4062 u32 slice_mask = 0;
4063 u16 ddb_size = intel_get_ddb_size(dev_priv);
4064 u16 num_supported_slices = INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
4065 u16 slice_size = ddb_size / num_supported_slices;
4066 u16 start_slice;
4067 u16 end_slice;
4068
4069 if (!skl_ddb_entry_size(entry))
4070 return 0;
4071
4072 start_slice = entry->start / slice_size;
4073 end_slice = (entry->end - 1) / slice_size;
4074
4075 /*
4076 * Per plane DDB entry can in a really worst case be on multiple slices
4077 * but single entry is anyway contigious.
4078 */
4079 while (start_slice <= end_slice) {
4080 slice_mask |= BIT(start_slice);
4081 start_slice++;
4082 }
4083
4084 return slice_mask;
4085}
4086
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004087static u8 skl_compute_dbuf_slices(const struct intel_crtc_state *crtc_state,
Ville Syrjälä05e81552020-02-25 19:11:09 +02004088 u8 active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004089
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004090static int
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004091skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004092 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004093 const u64 total_data_rate,
Matt Roperc107acf2016-05-12 07:06:01 -07004094 struct skl_ddb_entry *alloc, /* out */
4095 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004096{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004097 struct drm_atomic_state *state = crtc_state->uapi.state;
Matt Roperc107acf2016-05-12 07:06:01 -07004098 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004099 struct drm_crtc *for_crtc = crtc_state->uapi.crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004100 const struct intel_crtc *crtc;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004101 u32 pipe_width = 0, total_width_in_range = 0, width_before_pipe_in_range = 0;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304102 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004103 struct intel_dbuf_state *new_dbuf_state =
4104 intel_atomic_get_new_dbuf_state(intel_state);
4105 const struct intel_dbuf_state *old_dbuf_state =
4106 intel_atomic_get_old_dbuf_state(intel_state);
4107 u8 active_pipes = new_dbuf_state->active_pipes;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304108 u16 ddb_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004109 u32 ddb_range_size;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304110 u32 i;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004111 u32 dbuf_slice_mask;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004112 u32 offset;
4113 u32 slice_size;
4114 u32 total_slice_mask;
4115 u32 start, end;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004116 int ret;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004117
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004118 *num_active = hweight8(active_pipes);
4119
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004120 if (!crtc_state->hw.active) {
4121 alloc->start = 0;
4122 alloc->end = 0;
4123 return 0;
4124 }
4125
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004126 ddb_size = intel_get_ddb_size(dev_priv);
4127
4128 slice_size = ddb_size / INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004129
Matt Roperc107acf2016-05-12 07:06:01 -07004130 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304131 * If the state doesn't change the active CRTC's or there is no
4132 * modeset request, then there's no need to recalculate;
4133 * the existing pipe allocation limits should remain unchanged.
4134 * Note that we're safe from racing commits since any racing commit
4135 * that changes the active CRTC list or do modeset would need to
4136 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07004137 */
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004138 if (old_dbuf_state->active_pipes == new_dbuf_state->active_pipes &&
4139 !dev_priv->wm.distrust_bios_wm) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01004140 /*
4141 * alloc may be cleared by clear_intel_crtc_state,
4142 * copy from old state to be sure
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004143 *
4144 * FIXME get rid of this mess
Maarten Lankhorst512b5522016-11-08 13:55:34 +01004145 */
4146 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004147 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004148 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07004149
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304150 /*
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004151 * Get allowed DBuf slices for correspondent pipe and platform.
4152 */
4153 dbuf_slice_mask = skl_compute_dbuf_slices(crtc_state, active_pipes);
4154
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004155 /*
4156 * Figure out at which DBuf slice we start, i.e if we start at Dbuf S2
4157 * and slice size is 1024, the offset would be 1024
4158 */
4159 offset = icl_get_first_dbuf_slice_offset(dbuf_slice_mask,
4160 slice_size, ddb_size);
4161
4162 /*
4163 * Figure out total size of allowed DBuf slices, which is basically
4164 * a number of allowed slices for that pipe multiplied by slice size.
4165 * Inside of this
4166 * range ddb entries are still allocated in proportion to display width.
4167 */
4168 ddb_range_size = hweight8(dbuf_slice_mask) * slice_size;
4169
4170 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304171 * Watermark/ddb requirement highly depends upon width of the
4172 * framebuffer, So instead of allocating DDB equally among pipes
4173 * distribute DDB based on resolution/width of the display.
4174 */
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004175 total_slice_mask = dbuf_slice_mask;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004176 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
4177 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004178 &crtc_state->hw.adjusted_mode;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004179 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304180 int hdisplay, vdisplay;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004181 u32 pipe_dbuf_slice_mask;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304182
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004183 if (!crtc_state->hw.active)
4184 continue;
4185
4186 pipe_dbuf_slice_mask = skl_compute_dbuf_slices(crtc_state,
4187 active_pipes);
4188
4189 /*
4190 * According to BSpec pipe can share one dbuf slice with another
4191 * pipes or pipe can use multiple dbufs, in both cases we
4192 * account for other pipes only if they have exactly same mask.
4193 * However we need to account how many slices we should enable
4194 * in total.
4195 */
4196 total_slice_mask |= pipe_dbuf_slice_mask;
4197
4198 /*
4199 * Do not account pipes using other slice sets
4200 * luckily as of current BSpec slice sets do not partially
4201 * intersect(pipes share either same one slice or same slice set
4202 * i.e no partial intersection), so it is enough to check for
4203 * equality for now.
4204 */
4205 if (dbuf_slice_mask != pipe_dbuf_slice_mask)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304206 continue;
4207
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304208 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004209
4210 total_width_in_range += hdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304211
4212 if (pipe < for_pipe)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004213 width_before_pipe_in_range += hdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304214 else if (pipe == for_pipe)
4215 pipe_width = hdisplay;
4216 }
4217
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004218 /*
4219 * FIXME: For now we always enable slice S1 as per
4220 * the Bspec display initialization sequence.
4221 */
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004222 new_dbuf_state->enabled_slices = total_slice_mask | BIT(DBUF_S1);
4223
4224 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices) {
4225 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
4226 if (ret)
4227 return ret;
4228 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004229
4230 start = ddb_range_size * width_before_pipe_in_range / total_width_in_range;
4231 end = ddb_range_size *
4232 (width_before_pipe_in_range + pipe_width) / total_width_in_range;
4233
4234 alloc->start = offset + start;
4235 alloc->end = offset + end;
4236
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004237 drm_dbg_kms(&dev_priv->drm,
4238 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x\n",
4239 for_crtc->base.id, for_crtc->name,
4240 dbuf_slice_mask, alloc->start, alloc->end, active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004241
4242 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004243}
4244
Ville Syrjälädf331de2019-03-19 18:03:11 +02004245static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4246 int width, const struct drm_format_info *format,
4247 u64 modifier, unsigned int rotation,
4248 u32 plane_pixel_rate, struct skl_wm_params *wp,
4249 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004250static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004251 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004252 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004253 const struct skl_wm_params *wp,
4254 const struct skl_wm_level *result_prev,
4255 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004256
Ville Syrjälädf331de2019-03-19 18:03:11 +02004257static unsigned int
4258skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4259 int num_active)
4260{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004261 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004262 int level, max_level = ilk_wm_max_level(dev_priv);
4263 struct skl_wm_level wm = {};
4264 int ret, min_ddb_alloc = 0;
4265 struct skl_wm_params wp;
4266
4267 ret = skl_compute_wm_params(crtc_state, 256,
4268 drm_format_info(DRM_FORMAT_ARGB8888),
4269 DRM_FORMAT_MOD_LINEAR,
4270 DRM_MODE_ROTATE_0,
4271 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304272 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004273
4274 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004275 unsigned int latency = dev_priv->wm.skl_latency[level];
4276
4277 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004278 if (wm.min_ddb_alloc == U16_MAX)
4279 break;
4280
4281 min_ddb_alloc = wm.min_ddb_alloc;
4282 }
4283
4284 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004285}
4286
Mahesh Kumar37cde112018-04-26 19:55:17 +05304287static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4288 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004289{
Mahesh Kumar37cde112018-04-26 19:55:17 +05304290
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004291 entry->start = reg & DDB_ENTRY_MASK;
4292 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304293
Damien Lespiau16160e32014-11-04 17:06:53 +00004294 if (entry->end)
4295 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004296}
4297
Mahesh Kumarddf34312018-04-09 09:11:03 +05304298static void
4299skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4300 const enum pipe pipe,
4301 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004302 struct skl_ddb_entry *ddb_y,
4303 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304304{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004305 u32 val, val2;
4306 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304307
4308 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4309 if (plane_id == PLANE_CURSOR) {
4310 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004311 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304312 return;
4313 }
4314
4315 val = I915_READ(PLANE_CTL(pipe, plane_id));
4316
4317 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004318 if (val & PLANE_CTL_ENABLE)
4319 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4320 val & PLANE_CTL_ORDER_RGBX,
4321 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304322
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004323 if (INTEL_GEN(dev_priv) >= 11) {
4324 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
4325 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4326 } else {
4327 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07004328 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304329
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004330 if (fourcc &&
4331 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004332 swap(val, val2);
4333
4334 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4335 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304336 }
4337}
4338
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004339void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4340 struct skl_ddb_entry *ddb_y,
4341 struct skl_ddb_entry *ddb_uv)
4342{
4343 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4344 enum intel_display_power_domain power_domain;
4345 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004346 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004347 enum plane_id plane_id;
4348
4349 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004350 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4351 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004352 return;
4353
4354 for_each_plane_id_on_crtc(crtc, plane_id)
4355 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4356 plane_id,
4357 &ddb_y[plane_id],
4358 &ddb_uv[plane_id]);
4359
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004360 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004361}
4362
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004363/*
4364 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4365 * The bspec defines downscale amount as:
4366 *
4367 * """
4368 * Horizontal down scale amount = maximum[1, Horizontal source size /
4369 * Horizontal destination size]
4370 * Vertical down scale amount = maximum[1, Vertical source size /
4371 * Vertical destination size]
4372 * Total down scale amount = Horizontal down scale amount *
4373 * Vertical down scale amount
4374 * """
4375 *
4376 * Return value is provided in 16.16 fixed point form to retain fractional part.
4377 * Caller should take care of dividing & rounding off the value.
4378 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304379static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004380skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4381 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004382{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304383 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004384 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304385 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4386 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004387
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304388 if (drm_WARN_ON(&dev_priv->drm,
4389 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304390 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004391
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004392 /*
4393 * Src coordinates are already rotated by 270 degrees for
4394 * the 90/270 degree plane rotation cases (to match the
4395 * GTT mapping), hence no need to account for rotation here.
4396 *
4397 * n.b., src is 16.16 fixed point, dst is whole integer.
4398 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004399 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4400 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4401 dst_w = drm_rect_width(&plane_state->uapi.dst);
4402 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004403
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304404 fp_w_ratio = div_fixed16(src_w, dst_w);
4405 fp_h_ratio = div_fixed16(src_h, dst_h);
4406 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4407 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004408
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304409 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004410}
4411
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004412struct dbuf_slice_conf_entry {
4413 u8 active_pipes;
4414 u8 dbuf_mask[I915_MAX_PIPES];
4415};
4416
4417/*
4418 * Table taken from Bspec 12716
4419 * Pipes do have some preferred DBuf slice affinity,
4420 * plus there are some hardcoded requirements on how
4421 * those should be distributed for multipipe scenarios.
4422 * For more DBuf slices algorithm can get even more messy
4423 * and less readable, so decided to use a table almost
4424 * as is from BSpec itself - that way it is at least easier
4425 * to compare, change and check.
4426 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004427static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004428/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4429{
4430 {
4431 .active_pipes = BIT(PIPE_A),
4432 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004433 [PIPE_A] = BIT(DBUF_S1),
4434 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004435 },
4436 {
4437 .active_pipes = BIT(PIPE_B),
4438 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004439 [PIPE_B] = BIT(DBUF_S1),
4440 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004441 },
4442 {
4443 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4444 .dbuf_mask = {
4445 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004446 [PIPE_B] = BIT(DBUF_S2),
4447 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004448 },
4449 {
4450 .active_pipes = BIT(PIPE_C),
4451 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004452 [PIPE_C] = BIT(DBUF_S2),
4453 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004454 },
4455 {
4456 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4457 .dbuf_mask = {
4458 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004459 [PIPE_C] = BIT(DBUF_S2),
4460 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004461 },
4462 {
4463 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4464 .dbuf_mask = {
4465 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004466 [PIPE_C] = BIT(DBUF_S2),
4467 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004468 },
4469 {
4470 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4471 .dbuf_mask = {
4472 [PIPE_A] = BIT(DBUF_S1),
4473 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004474 [PIPE_C] = BIT(DBUF_S2),
4475 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004476 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004477 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004478};
4479
4480/*
4481 * Table taken from Bspec 49255
4482 * Pipes do have some preferred DBuf slice affinity,
4483 * plus there are some hardcoded requirements on how
4484 * those should be distributed for multipipe scenarios.
4485 * For more DBuf slices algorithm can get even more messy
4486 * and less readable, so decided to use a table almost
4487 * as is from BSpec itself - that way it is at least easier
4488 * to compare, change and check.
4489 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004490static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004491/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4492{
4493 {
4494 .active_pipes = BIT(PIPE_A),
4495 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004496 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4497 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004498 },
4499 {
4500 .active_pipes = BIT(PIPE_B),
4501 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004502 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4503 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004504 },
4505 {
4506 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4507 .dbuf_mask = {
4508 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004509 [PIPE_B] = BIT(DBUF_S1),
4510 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004511 },
4512 {
4513 .active_pipes = BIT(PIPE_C),
4514 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004515 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4516 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004517 },
4518 {
4519 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4520 .dbuf_mask = {
4521 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004522 [PIPE_C] = BIT(DBUF_S2),
4523 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004524 },
4525 {
4526 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4527 .dbuf_mask = {
4528 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004529 [PIPE_C] = BIT(DBUF_S2),
4530 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004531 },
4532 {
4533 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4534 .dbuf_mask = {
4535 [PIPE_A] = BIT(DBUF_S1),
4536 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004537 [PIPE_C] = BIT(DBUF_S2),
4538 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004539 },
4540 {
4541 .active_pipes = BIT(PIPE_D),
4542 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004543 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4544 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004545 },
4546 {
4547 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4548 .dbuf_mask = {
4549 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004550 [PIPE_D] = BIT(DBUF_S2),
4551 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004552 },
4553 {
4554 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4555 .dbuf_mask = {
4556 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004557 [PIPE_D] = BIT(DBUF_S2),
4558 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004559 },
4560 {
4561 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4562 .dbuf_mask = {
4563 [PIPE_A] = BIT(DBUF_S1),
4564 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004565 [PIPE_D] = BIT(DBUF_S2),
4566 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004567 },
4568 {
4569 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4570 .dbuf_mask = {
4571 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004572 [PIPE_D] = BIT(DBUF_S2),
4573 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004574 },
4575 {
4576 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4577 .dbuf_mask = {
4578 [PIPE_A] = BIT(DBUF_S1),
4579 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004580 [PIPE_D] = BIT(DBUF_S2),
4581 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004582 },
4583 {
4584 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4585 .dbuf_mask = {
4586 [PIPE_B] = BIT(DBUF_S1),
4587 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004588 [PIPE_D] = BIT(DBUF_S2),
4589 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004590 },
4591 {
4592 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4593 .dbuf_mask = {
4594 [PIPE_A] = BIT(DBUF_S1),
4595 [PIPE_B] = BIT(DBUF_S1),
4596 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004597 [PIPE_D] = BIT(DBUF_S2),
4598 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004599 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004600 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004601};
4602
Ville Syrjälä05e81552020-02-25 19:11:09 +02004603static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4604 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004605{
4606 int i;
4607
Ville Syrjälä05e81552020-02-25 19:11:09 +02004608 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004609 if (dbuf_slices[i].active_pipes == active_pipes)
4610 return dbuf_slices[i].dbuf_mask[pipe];
4611 }
4612 return 0;
4613}
4614
4615/*
4616 * This function finds an entry with same enabled pipe configuration and
4617 * returns correspondent DBuf slice mask as stated in BSpec for particular
4618 * platform.
4619 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004620static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004621{
4622 /*
4623 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4624 * required calculating "pipe ratio" in order to determine
4625 * if one or two slices can be used for single pipe configurations
4626 * as additional constraint to the existing table.
4627 * However based on recent info, it should be not "pipe ratio"
4628 * but rather ratio between pixel_rate and cdclk with additional
4629 * constants, so for now we are using only table until this is
4630 * clarified. Also this is the reason why crtc_state param is
4631 * still here - we will need it once those additional constraints
4632 * pop up.
4633 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004634 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004635}
4636
Ville Syrjälä05e81552020-02-25 19:11:09 +02004637static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004638{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004639 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004640}
4641
4642static u8 skl_compute_dbuf_slices(const struct intel_crtc_state *crtc_state,
Ville Syrjälä05e81552020-02-25 19:11:09 +02004643 u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004644{
4645 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4646 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4647 enum pipe pipe = crtc->pipe;
4648
4649 if (IS_GEN(dev_priv, 12))
Ville Syrjälä05e81552020-02-25 19:11:09 +02004650 return tgl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004651 else if (IS_GEN(dev_priv, 11))
Ville Syrjälä05e81552020-02-25 19:11:09 +02004652 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004653 /*
4654 * For anything else just return one slice yet.
4655 * Should be extended for other platforms.
4656 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004657 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004658}
4659
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004660static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004661skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4662 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004663 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004664{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004665 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004666 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004667 u32 data_rate;
4668 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304669 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004670 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004671
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004672 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004673 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004674
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004675 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004676 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004677
4678 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004679 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004680 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004681
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004682 /*
4683 * Src coordinates are already rotated by 270 degrees for
4684 * the 90/270 degree plane rotation cases (to match the
4685 * GTT mapping), hence no need to account for rotation here.
4686 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004687 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4688 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004689
Mahesh Kumarb879d582018-04-09 09:11:01 +05304690 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004691 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304692 width /= 2;
4693 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004694 }
4695
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004696 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304697
Maarten Lankhorstec193642019-06-28 10:55:17 +02004698 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004699
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004700 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4701
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004702 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004703 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004704}
4705
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004706static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004707skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004708 u64 *plane_data_rate,
4709 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004710{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004711 struct intel_plane *plane;
4712 const struct intel_plane_state *plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004713 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004714
Matt Ropera1de91e2016-05-12 07:05:57 -07004715 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004716 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4717 enum plane_id plane_id = plane->id;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004718 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004719
Mahesh Kumarb879d582018-04-09 09:11:01 +05304720 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004721 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004722 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004723 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004724
Mahesh Kumarb879d582018-04-09 09:11:01 +05304725 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004726 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304727 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004728 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004729 }
4730
4731 return total_data_rate;
4732}
4733
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004734static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004735icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004736 u64 *plane_data_rate)
4737{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004738 struct intel_plane *plane;
4739 const struct intel_plane_state *plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004740 u64 total_data_rate = 0;
4741
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004742 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004743 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4744 enum plane_id plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004745 u64 rate;
4746
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004747 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004748 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004749 plane_data_rate[plane_id] = rate;
4750 total_data_rate += rate;
4751 } else {
4752 enum plane_id y_plane_id;
4753
4754 /*
4755 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004756 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004757 * and needs the master plane state which may be
4758 * NULL if we try get_new_plane_state(), so we
4759 * always calculate from the master.
4760 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004761 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004762 continue;
4763
4764 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004765 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004766 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004767 plane_data_rate[y_plane_id] = rate;
4768 total_data_rate += rate;
4769
Maarten Lankhorstec193642019-06-28 10:55:17 +02004770 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004771 plane_data_rate[plane_id] = rate;
4772 total_data_rate += rate;
4773 }
4774 }
4775
4776 return total_data_rate;
4777}
4778
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004779static const struct skl_wm_level *
4780skl_plane_wm_level(const struct intel_crtc_state *crtc_state,
4781 enum plane_id plane_id,
4782 int level)
4783{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004784 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
4785 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4786
4787 if (level == 0 && pipe_wm->use_sagv_wm)
4788 return &wm->sagv_wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004789
4790 return &wm->wm[level];
4791}
4792
Matt Roperc107acf2016-05-12 07:06:01 -07004793static int
Stanislav Lisovskiy072fcc32020-02-03 01:06:25 +02004794skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004795{
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004796 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4797 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004798 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004799 u16 alloc_size, start = 0;
4800 u16 total[I915_MAX_PLANES] = {};
4801 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004802 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004803 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004804 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004805 u64 plane_data_rate[I915_MAX_PLANES] = {};
4806 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004807 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004808 int level;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004809 int ret;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004810
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004811 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004812 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4813 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004814
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004815 if (!crtc_state->hw.active) {
Ville Syrjäläb6a13a32020-05-18 15:13:54 +03004816 struct intel_atomic_state *state =
4817 to_intel_atomic_state(crtc_state->uapi.state);
4818 struct intel_dbuf_state *new_dbuf_state =
4819 intel_atomic_get_new_dbuf_state(state);
4820 const struct intel_dbuf_state *old_dbuf_state =
4821 intel_atomic_get_old_dbuf_state(state);
4822
4823 /*
4824 * FIXME hack to make sure we compute this sensibly when
4825 * turning off all the pipes. Otherwise we leave it at
4826 * whatever we had previously, and then runtime PM will
4827 * mess it up by turning off all but S1. Remove this
4828 * once the dbuf state computation flow becomes sane.
4829 */
4830 if (new_dbuf_state->active_pipes == 0) {
4831 new_dbuf_state->enabled_slices = BIT(DBUF_S1);
4832
4833 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices) {
4834 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
4835 if (ret)
4836 return ret;
4837 }
4838 }
4839
Lyudece0ba282016-09-15 10:46:35 -04004840 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004841 return 0;
4842 }
4843
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004844 if (INTEL_GEN(dev_priv) >= 11)
4845 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004846 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004847 plane_data_rate);
4848 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004849 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004850 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004851 plane_data_rate,
4852 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004853
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004854 ret = skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state,
4855 total_data_rate,
4856 alloc, &num_active);
4857 if (ret)
4858 return ret;
4859
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004860 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304861 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004862 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004863
Matt Roperd8e87492018-12-11 09:31:07 -08004864 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004865 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004866 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004867 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004868 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004869 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004870
Matt Ropera1de91e2016-05-12 07:05:57 -07004871 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004872 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004873
Matt Roperd8e87492018-12-11 09:31:07 -08004874 /*
4875 * Find the highest watermark level for which we can satisfy the block
4876 * requirement of active planes.
4877 */
4878 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004879 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004880 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004881 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004882 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004883
4884 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05304885 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304886 drm_WARN_ON(&dev_priv->drm,
4887 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004888 blocks = U32_MAX;
4889 break;
4890 }
4891 continue;
4892 }
4893
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004894 blocks += wm->wm[level].min_ddb_alloc;
4895 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004896 }
4897
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004898 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004899 alloc_size -= blocks;
4900 break;
4901 }
4902 }
4903
4904 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03004905 drm_dbg_kms(&dev_priv->drm,
4906 "Requested display configuration exceeds system DDB limitations");
4907 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
4908 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08004909 return -EINVAL;
4910 }
4911
4912 /*
4913 * Grant each plane the blocks it requires at the highest achievable
4914 * watermark level, plus an extra share of the leftover blocks
4915 * proportional to its relative data rate.
4916 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004917 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004918 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004919 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004920 u64 rate;
4921 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004922
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004923 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004924 continue;
4925
Damien Lespiaub9cec072014-11-04 17:06:43 +00004926 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004927 * We've accounted for all active planes; remaining planes are
4928 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004929 */
Matt Roperd8e87492018-12-11 09:31:07 -08004930 if (total_data_rate == 0)
4931 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004932
Matt Roperd8e87492018-12-11 09:31:07 -08004933 rate = plane_data_rate[plane_id];
4934 extra = min_t(u16, alloc_size,
4935 DIV64_U64_ROUND_UP(alloc_size * rate,
4936 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004937 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004938 alloc_size -= extra;
4939 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004940
Matt Roperd8e87492018-12-11 09:31:07 -08004941 if (total_data_rate == 0)
4942 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004943
Matt Roperd8e87492018-12-11 09:31:07 -08004944 rate = uv_plane_data_rate[plane_id];
4945 extra = min_t(u16, alloc_size,
4946 DIV64_U64_ROUND_UP(alloc_size * rate,
4947 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004948 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004949 alloc_size -= extra;
4950 total_data_rate -= rate;
4951 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304952 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08004953
4954 /* Set the actual DDB start/end points for each plane */
4955 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004956 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004957 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004958 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004959 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004960 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004961
4962 if (plane_id == PLANE_CURSOR)
4963 continue;
4964
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004965 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304966 drm_WARN_ON(&dev_priv->drm,
4967 INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004968
Matt Roperd8e87492018-12-11 09:31:07 -08004969 /* Leave disabled planes at (0,0) */
4970 if (total[plane_id]) {
4971 plane_alloc->start = start;
4972 start += total[plane_id];
4973 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004974 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004975
Matt Roperd8e87492018-12-11 09:31:07 -08004976 if (uv_total[plane_id]) {
4977 uv_plane_alloc->start = start;
4978 start += uv_total[plane_id];
4979 uv_plane_alloc->end = start;
4980 }
4981 }
4982
4983 /*
4984 * When we calculated watermark values we didn't know how high
4985 * of a level we'd actually be able to hit, so we just marked
4986 * all levels as "enabled." Go back now and disable the ones
4987 * that aren't actually possible.
4988 */
4989 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004990 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004991 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004992 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004993
4994 /*
4995 * We only disable the watermarks for each plane if
4996 * they exceed the ddb allocation of said plane. This
4997 * is done so that we don't end up touching cursor
4998 * watermarks needlessly when some other plane reduces
4999 * our max possible watermark level.
5000 *
5001 * Bspec has this to say about the PLANE_WM enable bit:
5002 * "All the watermarks at this level for all enabled
5003 * planes must be enabled before the level will be used."
5004 * So this is actually safe to do.
5005 */
5006 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
5007 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
5008 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02005009
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005010 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005011 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005012 * Underruns with WM1+ disabled
5013 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07005014 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02005015 level == 1 && wm->wm[0].plane_en) {
5016 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005017 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
5018 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005019 }
Matt Roperd8e87492018-12-11 09:31:07 -08005020 }
5021 }
5022
5023 /*
5024 * Go back and disable the transition watermark if it turns out we
5025 * don't have enough DDB blocks for it.
5026 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005027 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005028 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005029 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005030
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02005031 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08005032 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00005033 }
5034
Matt Roperc107acf2016-05-12 07:06:01 -07005035 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005036}
5037
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005038/*
5039 * 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 +02005040 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005041 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5042 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5043*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005044static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005045skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5046 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005047{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005048 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305049 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005050
5051 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305052 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005053
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305054 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005055 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005056
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005057 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005058 ret = add_fixed16_u32(ret, 1);
5059
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005060 return ret;
5061}
5062
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005063static uint_fixed_16_16_t
5064skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5065 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005066{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005067 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305068 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005069
5070 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305071 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005072
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005073 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305074 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5075 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305076 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005077 return ret;
5078}
5079
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305080static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005081intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305082{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305083 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005084 u32 pixel_rate;
5085 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305086 uint_fixed_16_16_t linetime_us;
5087
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005088 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305089 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305090
Maarten Lankhorstec193642019-06-28 10:55:17 +02005091 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305092
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305093 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305094 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305095
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005096 crtc_htotal = crtc_state->hw.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305097 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305098
5099 return linetime_us;
5100}
5101
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005102static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02005103skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
5104 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005105{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305106 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005107 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05305108 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005109
5110 /* Shouldn't reach here on disabled planes... */
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305111 if (drm_WARN_ON(&dev_priv->drm,
5112 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005113 return 0;
5114
5115 /*
5116 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
5117 * with additional adjustments for plane-specific scaling.
5118 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005119 adjusted_pixel_rate = crtc_state->pixel_rate;
5120 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005121
Kumar, Mahesh7084b502017-05-17 17:28:23 +05305122 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
5123 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005124}
5125
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305126static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005127skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5128 int width, const struct drm_format_info *format,
5129 u64 modifier, unsigned int rotation,
5130 u32 plane_pixel_rate, struct skl_wm_params *wp,
5131 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305132{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005133 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005134 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005135 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305136
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305137 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005138 if (color_plane == 1 &&
5139 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005140 drm_dbg_kms(&dev_priv->drm,
5141 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305142 return -EINVAL;
5143 }
5144
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005145 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5146 modifier == I915_FORMAT_MOD_Yf_TILED ||
5147 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5148 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5149 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5150 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5151 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005152 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305153
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005154 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005155 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305156 wp->width /= 2;
5157
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005158 wp->cpp = format->cpp[color_plane];
5159 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305160
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005161 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005162 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005163 wp->dbuf_block_size = 256;
5164 else
5165 wp->dbuf_block_size = 512;
5166
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005167 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305168 switch (wp->cpp) {
5169 case 1:
5170 wp->y_min_scanlines = 16;
5171 break;
5172 case 2:
5173 wp->y_min_scanlines = 8;
5174 break;
5175 case 4:
5176 wp->y_min_scanlines = 4;
5177 break;
5178 default:
5179 MISSING_CASE(wp->cpp);
5180 return -EINVAL;
5181 }
5182 } else {
5183 wp->y_min_scanlines = 4;
5184 }
5185
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005186 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305187 wp->y_min_scanlines *= 2;
5188
5189 wp->plane_bytes_per_line = wp->width * wp->cpp;
5190 if (wp->y_tiled) {
5191 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005192 wp->y_min_scanlines,
5193 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305194
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005195 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305196 interm_pbpl++;
5197
5198 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5199 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305200 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005201 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005202 wp->dbuf_block_size);
5203
5204 if (!wp->x_tiled ||
5205 INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
5206 interm_pbpl++;
5207
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305208 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5209 }
5210
5211 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5212 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005213
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305214 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005215 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305216
5217 return 0;
5218}
5219
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005220static int
5221skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5222 const struct intel_plane_state *plane_state,
5223 struct skl_wm_params *wp, int color_plane)
5224{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005225 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005226 int width;
5227
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005228 /*
5229 * Src coordinates are already rotated by 270 degrees for
5230 * the 90/270 degree plane rotation cases (to match the
5231 * GTT mapping), hence no need to account for rotation here.
5232 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005233 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005234
5235 return skl_compute_wm_params(crtc_state, width,
5236 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005237 plane_state->hw.rotation,
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005238 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
5239 wp, color_plane);
5240}
5241
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005242static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5243{
5244 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
5245 return true;
5246
5247 /* The number of lines are ignored for the level 0 watermark. */
5248 return level > 0;
5249}
5250
Maarten Lankhorstec193642019-06-28 10:55:17 +02005251static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005252 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005253 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005254 const struct skl_wm_params *wp,
5255 const struct skl_wm_level *result_prev,
5256 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005257{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005258 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305259 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305260 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005261 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005262
Ville Syrjälä0aded172019-02-05 17:50:53 +02005263 if (latency == 0) {
5264 /* reject it */
5265 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005266 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005267 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005268
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005269 /*
5270 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5271 * Display WA #1141: kbl,cfl
5272 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005273 if ((IS_KABYLAKE(dev_priv) ||
5274 IS_COFFEELAKE(dev_priv) ||
5275 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005276 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305277 latency += 4;
5278
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005279 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005280 latency += 15;
5281
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305282 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005283 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305284 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005285 crtc_state->hw.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005286 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305287 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005288
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305289 if (wp->y_tiled) {
5290 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005291 } else {
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005292 if ((wp->cpp * crtc_state->hw.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005293 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005294 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005295 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005296 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005297 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005298 !IS_GEMINILAKE(dev_priv))
5299 selected_result = min_fixed16(method1, method2);
5300 else
5301 selected_result = method2;
5302 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005303 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005304 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005305 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005306
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305307 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05305308 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305309 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005310
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005311 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
5312 /* Display WA #1125: skl,bxt,kbl */
5313 if (level == 0 && wp->rc_surface)
5314 res_blocks +=
5315 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005316
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005317 /* Display WA #1126: skl,bxt,kbl */
5318 if (level >= 1 && level <= 7) {
5319 if (wp->y_tiled) {
5320 res_blocks +=
5321 fixed16_to_u32_round_up(wp->y_tile_minimum);
5322 res_lines += wp->y_min_scanlines;
5323 } else {
5324 res_blocks++;
5325 }
5326
5327 /*
5328 * Make sure result blocks for higher latency levels are
5329 * atleast as high as level below the current level.
5330 * Assumption in DDB algorithm optimization for special
5331 * cases. Also covers Display WA #1125 for RC.
5332 */
5333 if (result_prev->plane_res_b > res_blocks)
5334 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005335 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005336 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005337
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005338 if (INTEL_GEN(dev_priv) >= 11) {
5339 if (wp->y_tiled) {
5340 int extra_lines;
5341
5342 if (res_lines % wp->y_min_scanlines == 0)
5343 extra_lines = wp->y_min_scanlines;
5344 else
5345 extra_lines = wp->y_min_scanlines * 2 -
5346 res_lines % wp->y_min_scanlines;
5347
5348 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
5349 wp->plane_blocks_per_line);
5350 } else {
5351 min_ddb_alloc = res_blocks +
5352 DIV_ROUND_UP(res_blocks, 10);
5353 }
5354 }
5355
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005356 if (!skl_wm_has_lines(dev_priv, level))
5357 res_lines = 0;
5358
Ville Syrjälä0aded172019-02-05 17:50:53 +02005359 if (res_lines > 31) {
5360 /* reject it */
5361 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005362 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005363 }
Matt Roperd8e87492018-12-11 09:31:07 -08005364
5365 /*
5366 * If res_lines is valid, assume we can use this watermark level
5367 * for now. We'll come back and disable it after we calculate the
5368 * DDB allocation if it turns out we don't actually have enough
5369 * blocks to satisfy it.
5370 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05305371 result->plane_res_b = res_blocks;
5372 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005373 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
5374 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05305375 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005376}
5377
Matt Roperd8e87492018-12-11 09:31:07 -08005378static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005379skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305380 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005381 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005382{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005383 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305384 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005385 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005386
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305387 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005388 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005389 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305390
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005391 skl_compute_plane_wm(crtc_state, level, latency,
5392 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005393
5394 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305395 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005396}
5397
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005398static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5399 const struct skl_wm_params *wm_params,
5400 struct skl_plane_wm *plane_wm)
5401{
5402 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5403 struct skl_wm_level *sagv_wm = &plane_wm->sagv_wm0;
5404 struct skl_wm_level *levels = plane_wm->wm;
5405 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5406
5407 skl_compute_plane_wm(crtc_state, 0, latency,
5408 wm_params, &levels[0],
5409 sagv_wm);
5410}
5411
Maarten Lankhorstec193642019-06-28 10:55:17 +02005412static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02005413 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08005414 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005415{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005416 struct drm_device *dev = crtc_state->uapi.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05305417 const struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc834d032020-02-28 22:35:52 +02005418 u16 trans_min, trans_amount, trans_y_tile_min;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005419 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005420
Kumar, Maheshca476672017-08-17 19:15:24 +05305421 /* Transition WM don't make any sense if ipc is disabled */
5422 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005423 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305424
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005425 /*
5426 * WaDisableTWM:skl,kbl,cfl,bxt
5427 * Transition WM are not recommended by HW team for GEN9
5428 */
5429 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
5430 return;
5431
Paulo Zanoni91961a82018-10-04 16:15:56 -07005432 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305433 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005434 else
5435 trans_min = 14;
5436
5437 /* Display WA #1140: glk,cnl */
5438 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
5439 trans_amount = 0;
5440 else
5441 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305442
5443 trans_offset_b = trans_min + trans_amount;
5444
Paulo Zanonicbacc792018-10-04 16:15:58 -07005445 /*
5446 * The spec asks for Selected Result Blocks for wm0 (the real value),
5447 * not Result Blocks (the integer value). Pay attention to the capital
5448 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
5449 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5450 * and since we later will have to get the ceiling of the sum in the
5451 * transition watermarks calculation, we can just pretend Selected
5452 * Result Blocks is Result Blocks minus 1 and it should work for the
5453 * current platforms.
5454 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02005455 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005456
Kumar, Maheshca476672017-08-17 19:15:24 +05305457 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005458 trans_y_tile_min =
5459 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07005460 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05305461 trans_offset_b;
5462 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07005463 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05305464 }
5465
Matt Roperd8e87492018-12-11 09:31:07 -08005466 /*
5467 * Just assume we can enable the transition watermark. After
5468 * computing the DDB we'll come back and disable it if that
5469 * assumption turns out to be false.
5470 */
5471 wm->trans_wm.plane_res_b = res_blocks + 1;
5472 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005473}
5474
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005475static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005476 const struct intel_plane_state *plane_state,
5477 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005478{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005479 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5480 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä83158472018-11-27 18:57:26 +02005481 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005482 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005483 int ret;
5484
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005485 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005486 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005487 if (ret)
5488 return ret;
5489
Ville Syrjälä67155a62019-03-12 22:58:37 +02005490 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005491
5492 if (INTEL_GEN(dev_priv) >= 12)
5493 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5494
Matt Roperd8e87492018-12-11 09:31:07 -08005495 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005496
5497 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005498}
5499
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005500static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005501 const struct intel_plane_state *plane_state,
5502 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005503{
Ville Syrjälä83158472018-11-27 18:57:26 +02005504 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
5505 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005506 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005507
Ville Syrjälä83158472018-11-27 18:57:26 +02005508 wm->is_planar = true;
5509
5510 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005511 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005512 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005513 if (ret)
5514 return ret;
5515
Ville Syrjälä67155a62019-03-12 22:58:37 +02005516 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005517
5518 return 0;
5519}
5520
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005521static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005522 const struct intel_plane_state *plane_state)
5523{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005524 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005525 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005526 enum plane_id plane_id = plane->id;
5527 int ret;
5528
5529 if (!intel_wm_plane_visible(crtc_state, plane_state))
5530 return 0;
5531
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005532 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005533 plane_id, 0);
5534 if (ret)
5535 return ret;
5536
5537 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005538 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005539 plane_id);
5540 if (ret)
5541 return ret;
5542 }
5543
5544 return 0;
5545}
5546
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005547static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005548 const struct intel_plane_state *plane_state)
5549{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305550 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005551 enum plane_id plane_id = to_intel_plane(plane_state->uapi.plane)->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005552 int ret;
5553
5554 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005555 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005556 return 0;
5557
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005558 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005559 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005560 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005561
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305562 drm_WARN_ON(&dev_priv->drm,
5563 !intel_wm_plane_visible(crtc_state, plane_state));
5564 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5565 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005566
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005567 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005568 y_plane_id, 0);
5569 if (ret)
5570 return ret;
5571
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005572 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005573 plane_id, 1);
5574 if (ret)
5575 return ret;
5576 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005577 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005578 plane_id, 0);
5579 if (ret)
5580 return ret;
5581 }
5582
5583 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005584}
5585
Maarten Lankhorstec193642019-06-28 10:55:17 +02005586static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005587{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005588 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005589 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005590 struct intel_plane *plane;
5591 const struct intel_plane_state *plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005592 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005593
Lyudea62163e2016-10-04 14:28:20 -04005594 /*
5595 * We'll only calculate watermarks for planes that are actually
5596 * enabled, so make sure all other planes are set as disabled.
5597 */
5598 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5599
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005600 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state,
5601 crtc_state) {
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305602
Ville Syrjälä83158472018-11-27 18:57:26 +02005603 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005604 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005605 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005606 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305607 if (ret)
5608 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005609 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305610
Matt Roper55994c22016-05-12 07:06:08 -07005611 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005612}
5613
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005614static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5615 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005616 const struct skl_ddb_entry *entry)
5617{
5618 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005619 intel_de_write_fw(dev_priv, reg,
5620 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005621 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005622 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005623}
5624
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005625static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5626 i915_reg_t reg,
5627 const struct skl_wm_level *level)
5628{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005629 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005630
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005631 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005632 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005633 if (level->ignore_lines)
5634 val |= PLANE_WM_IGNORE_LINES;
5635 val |= level->plane_res_b;
5636 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005637
Jani Nikula9b6320a2020-01-23 16:00:04 +02005638 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005639}
5640
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005641void skl_write_plane_wm(struct intel_plane *plane,
5642 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005643{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005644 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005645 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005646 enum plane_id plane_id = plane->id;
5647 enum pipe pipe = plane->pipe;
5648 const struct skl_plane_wm *wm =
5649 &crtc_state->wm.skl.optimal.planes[plane_id];
5650 const struct skl_ddb_entry *ddb_y =
5651 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5652 const struct skl_ddb_entry *ddb_uv =
5653 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005654
5655 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005656 const struct skl_wm_level *wm_level;
5657
5658 wm_level = skl_plane_wm_level(crtc_state, plane_id, level);
5659
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005660 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005661 wm_level);
Lyude62e0fb82016-08-22 12:50:08 -04005662 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005663 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005664 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005665
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005666 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005667 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005668 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5669 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305670 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005671
5672 if (wm->is_planar)
5673 swap(ddb_y, ddb_uv);
5674
5675 skl_ddb_entry_write(dev_priv,
5676 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5677 skl_ddb_entry_write(dev_priv,
5678 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005679}
5680
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005681void skl_write_cursor_wm(struct intel_plane *plane,
5682 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005683{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005684 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005685 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005686 enum plane_id plane_id = plane->id;
5687 enum pipe pipe = plane->pipe;
5688 const struct skl_plane_wm *wm =
5689 &crtc_state->wm.skl.optimal.planes[plane_id];
5690 const struct skl_ddb_entry *ddb =
5691 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005692
5693 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005694 const struct skl_wm_level *wm_level;
5695
5696 wm_level = skl_plane_wm_level(crtc_state, plane_id, level);
5697
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005698 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005699 wm_level);
Lyude62e0fb82016-08-22 12:50:08 -04005700 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005701 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005702
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005703 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005704}
5705
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005706bool skl_wm_level_equals(const struct skl_wm_level *l1,
5707 const struct skl_wm_level *l2)
5708{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005709 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005710 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005711 l1->plane_res_l == l2->plane_res_l &&
5712 l1->plane_res_b == l2->plane_res_b;
5713}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005714
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005715static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5716 const struct skl_plane_wm *wm1,
5717 const struct skl_plane_wm *wm2)
5718{
5719 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005720
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005721 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005722 /*
5723 * We don't check uv_wm as the hardware doesn't actually
5724 * use it. It only gets used for calculating the required
5725 * ddb allocation.
5726 */
5727 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005728 return false;
5729 }
5730
5731 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005732}
5733
Jani Nikula81b55ef2020-04-20 17:04:38 +03005734static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5735 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005736{
Lyude27082492016-08-24 07:48:10 +02005737 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005738}
5739
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005740bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005741 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005742 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005743{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005744 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005745
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005746 for (i = 0; i < num_entries; i++) {
5747 if (i != ignore_idx &&
5748 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005749 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005750 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005751
Lyude27082492016-08-24 07:48:10 +02005752 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005753}
5754
Jani Nikulabb7791b2016-10-04 12:29:17 +03005755static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005756skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5757 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005758{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005759 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
5760 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005761 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5762 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005763
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005764 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5765 struct intel_plane_state *plane_state;
5766 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005767
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005768 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5769 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5770 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5771 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005772 continue;
5773
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005774 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005775 if (IS_ERR(plane_state))
5776 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005777
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005778 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005779 }
5780
5781 return 0;
5782}
5783
5784static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005785skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005786{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005787 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5788 const struct intel_dbuf_state *old_dbuf_state;
5789 const struct intel_dbuf_state *new_dbuf_state;
5790 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005791 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305792 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305793 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005794
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005795 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005796 new_crtc_state, i) {
Stanislav Lisovskiy072fcc32020-02-03 01:06:25 +02005797 ret = skl_allocate_pipe_ddb(new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005798 if (ret)
5799 return ret;
5800
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005801 ret = skl_ddb_add_affected_planes(old_crtc_state,
5802 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005803 if (ret)
5804 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005805 }
5806
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005807 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
5808 new_dbuf_state = intel_atomic_get_new_dbuf_state(state);
5809
5810 if (new_dbuf_state &&
5811 new_dbuf_state->enabled_slices != old_dbuf_state->enabled_slices)
5812 drm_dbg_kms(&dev_priv->drm,
5813 "Enabled dbuf slices 0x%x -> 0x%x (out of %d dbuf slices)\n",
5814 old_dbuf_state->enabled_slices,
5815 new_dbuf_state->enabled_slices,
5816 INTEL_INFO(dev_priv)->num_supported_dbuf_slices);
5817
Matt Roper98d39492016-05-12 07:06:03 -07005818 return 0;
5819}
5820
Ville Syrjäläab98e942019-02-08 22:05:27 +02005821static char enast(bool enable)
5822{
5823 return enable ? '*' : ' ';
5824}
5825
Matt Roper2722efb2016-08-17 15:55:55 -04005826static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005827skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005828{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005829 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5830 const struct intel_crtc_state *old_crtc_state;
5831 const struct intel_crtc_state *new_crtc_state;
5832 struct intel_plane *plane;
5833 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005834 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005835
Jani Nikulabdbf43d2019-10-28 12:38:15 +02005836 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02005837 return;
5838
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005839 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5840 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005841 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5842
5843 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5844 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5845
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005846 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5847 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005848 const struct skl_ddb_entry *old, *new;
5849
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005850 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5851 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005852
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005853 if (skl_ddb_entry_equal(old, new))
5854 continue;
5855
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005856 drm_dbg_kms(&dev_priv->drm,
5857 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
5858 plane->base.base.id, plane->base.name,
5859 old->start, old->end, new->start, new->end,
5860 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005861 }
5862
5863 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5864 enum plane_id plane_id = plane->id;
5865 const struct skl_plane_wm *old_wm, *new_wm;
5866
5867 old_wm = &old_pipe_wm->planes[plane_id];
5868 new_wm = &new_pipe_wm->planes[plane_id];
5869
5870 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5871 continue;
5872
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005873 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005874 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm"
5875 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005876 plane->base.base.id, plane->base.name,
5877 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5878 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5879 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5880 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5881 enast(old_wm->trans_wm.plane_en),
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005882 enast(old_wm->sagv_wm0.plane_en),
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005883 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5884 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5885 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5886 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005887 enast(new_wm->trans_wm.plane_en),
5888 enast(new_wm->sagv_wm0.plane_en));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005889
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005890 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005891 "[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d"
5892 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005893 plane->base.base.id, plane->base.name,
5894 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5895 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5896 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5897 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5898 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5899 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5900 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5901 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5902 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005903 enast(old_wm->sagv_wm0.ignore_lines), old_wm->sagv_wm0.plane_res_l,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005904
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005905 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5906 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5907 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5908 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5909 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5910 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5911 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5912 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005913 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l,
5914 enast(new_wm->sagv_wm0.ignore_lines), new_wm->sagv_wm0.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005915
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005916 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005917 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5918 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005919 plane->base.base.id, plane->base.name,
5920 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5921 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5922 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5923 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5924 old_wm->trans_wm.plane_res_b,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005925 old_wm->sagv_wm0.plane_res_b,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005926 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5927 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5928 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5929 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005930 new_wm->trans_wm.plane_res_b,
5931 new_wm->sagv_wm0.plane_res_b);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005932
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005933 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005934 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5935 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005936 plane->base.base.id, plane->base.name,
5937 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5938 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5939 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5940 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5941 old_wm->trans_wm.min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005942 old_wm->sagv_wm0.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005943 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5944 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5945 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5946 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005947 new_wm->trans_wm.min_ddb_alloc,
5948 new_wm->sagv_wm0.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005949 }
5950 }
5951}
5952
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005953static int intel_add_affected_pipes(struct intel_atomic_state *state,
5954 u8 pipe_mask)
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005955{
5956 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5957 struct intel_crtc *crtc;
5958
5959 for_each_intel_crtc(&dev_priv->drm, crtc) {
5960 struct intel_crtc_state *crtc_state;
5961
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005962 if ((pipe_mask & BIT(crtc->pipe)) == 0)
5963 continue;
5964
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005965 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5966 if (IS_ERR(crtc_state))
5967 return PTR_ERR(crtc_state);
5968 }
5969
5970 return 0;
5971}
5972
Matt Roper98d39492016-05-12 07:06:03 -07005973static int
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005974skl_ddb_add_affected_pipes(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005975{
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005976 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005977 struct intel_crtc_state *crtc_state;
5978 struct intel_crtc *crtc;
5979 int i, ret;
Matt Roper98d39492016-05-12 07:06:03 -07005980
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305981 if (dev_priv->wm.distrust_bios_wm) {
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005982 /*
5983 * skl_ddb_get_pipe_allocation_limits() currently requires
5984 * all active pipes to be included in the state so that
5985 * it can redistribute the dbuf among them, and it really
5986 * wants to recompute things when distrust_bios_wm is set
5987 * so we add all the pipes to the state.
5988 */
5989 ret = intel_add_affected_pipes(state, ~0);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305990 if (ret)
5991 return ret;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305992 }
5993
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005994 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5995 struct intel_dbuf_state *new_dbuf_state;
5996 const struct intel_dbuf_state *old_dbuf_state;
5997
5998 new_dbuf_state = intel_atomic_get_dbuf_state(state);
5999 if (IS_ERR(new_dbuf_state))
Chris Wilsoncba597a2020-05-16 20:09:40 +01006000 return PTR_ERR(new_dbuf_state);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02006001
6002 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6003
6004 new_dbuf_state->active_pipes =
6005 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6006
6007 if (old_dbuf_state->active_pipes == new_dbuf_state->active_pipes)
6008 break;
6009
6010 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03006011 if (ret)
6012 return ret;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02006013
6014 /*
6015 * skl_ddb_get_pipe_allocation_limits() currently requires
6016 * all active pipes to be included in the state so that
6017 * it can redistribute the dbuf among them.
6018 */
6019 ret = intel_add_affected_pipes(state,
6020 new_dbuf_state->active_pipes);
6021 if (ret)
6022 return ret;
6023
6024 break;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306025 }
6026
6027 return 0;
6028}
6029
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006030/*
6031 * To make sure the cursor watermark registers are always consistent
6032 * with our computed state the following scenario needs special
6033 * treatment:
6034 *
6035 * 1. enable cursor
6036 * 2. move cursor entirely offscreen
6037 * 3. disable cursor
6038 *
6039 * Step 2. does call .disable_plane() but does not zero the watermarks
6040 * (since we consider an offscreen cursor still active for the purposes
6041 * of watermarks). Step 3. would not normally call .disable_plane()
6042 * because the actual plane visibility isn't changing, and we don't
6043 * deallocate the cursor ddb until the pipe gets disabled. So we must
6044 * force step 3. to call .disable_plane() to update the watermark
6045 * registers properly.
6046 *
6047 * Other planes do not suffer from this issues as their watermarks are
6048 * calculated based on the actual plane visibility. The only time this
6049 * can trigger for the other planes is during the initial readout as the
6050 * default value of the watermarks registers is not zero.
6051 */
6052static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6053 struct intel_crtc *crtc)
6054{
6055 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6056 const struct intel_crtc_state *old_crtc_state =
6057 intel_atomic_get_old_crtc_state(state, crtc);
6058 struct intel_crtc_state *new_crtc_state =
6059 intel_atomic_get_new_crtc_state(state, crtc);
6060 struct intel_plane *plane;
6061
6062 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6063 struct intel_plane_state *plane_state;
6064 enum plane_id plane_id = plane->id;
6065
6066 /*
6067 * Force a full wm update for every plane on modeset.
6068 * Required because the reset value of the wm registers
6069 * is non-zero, whereas we want all disabled planes to
6070 * have zero watermarks. So if we turn off the relevant
6071 * power well the hardware state will go out of sync
6072 * with the software state.
6073 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006074 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006075 skl_plane_wm_equals(dev_priv,
6076 &old_crtc_state->wm.skl.optimal.planes[plane_id],
6077 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
6078 continue;
6079
6080 plane_state = intel_atomic_get_plane_state(state, plane);
6081 if (IS_ERR(plane_state))
6082 return PTR_ERR(plane_state);
6083
6084 new_crtc_state->update_planes |= BIT(plane_id);
6085 }
6086
6087 return 0;
6088}
6089
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306090static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006091skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306092{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006093 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006094 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006095 struct intel_crtc_state *old_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306096 int ret, i;
6097
Ville Syrjäläd7a14582019-10-11 23:09:42 +03006098 ret = skl_ddb_add_affected_pipes(state);
6099 if (ret)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306100 return ret;
6101
Matt Roper734fa012016-05-12 15:11:40 -07006102 /*
6103 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08006104 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Ville Syrjäläf119a5e2020-01-20 19:47:13 +02006105 * weren't otherwise being modified if pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07006106 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006107 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006108 new_crtc_state, i) {
6109 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006110 if (ret)
6111 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006112 }
6113
Matt Roperd8e87492018-12-11 09:31:07 -08006114 ret = skl_compute_ddb(state);
6115 if (ret)
6116 return ret;
6117
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006118 ret = intel_compute_sagv_mask(state);
6119 if (ret)
6120 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006121
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006122 /*
6123 * skl_compute_ddb() will have adjusted the final watermarks
6124 * based on how much ddb is available. Now we can actually
6125 * check if the final watermarks changed.
6126 */
6127 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6128 new_crtc_state, i) {
6129 ret = skl_wm_add_affected_planes(state, crtc);
6130 if (ret)
6131 return ret;
6132 }
6133
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006134 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006135
Matt Roper98d39492016-05-12 07:06:03 -07006136 return 0;
6137}
6138
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006139static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006140 struct intel_wm_config *config)
6141{
6142 struct intel_crtc *crtc;
6143
6144 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006145 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006146 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6147
6148 if (!wm->pipe_enabled)
6149 continue;
6150
6151 config->sprites_enabled |= wm->sprites_enabled;
6152 config->sprites_scaled |= wm->sprites_scaled;
6153 config->num_pipes_active++;
6154 }
6155}
6156
Matt Ropered4a6a72016-02-23 17:20:13 -08006157static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006158{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006159 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006160 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006161 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006162 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006163 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006164
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006165 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006166
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006167 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6168 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006169
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006170 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006171 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006172 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006173 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6174 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006175
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006176 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006177 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006178 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006179 }
6180
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006181 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006182 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006183
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006184 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006185
Imre Deak820c1982013-12-17 14:46:36 +02006186 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006187}
6188
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006189static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006190 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006191{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006192 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6193 const struct intel_crtc_state *crtc_state =
6194 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006195
Matt Ropered4a6a72016-02-23 17:20:13 -08006196 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006197 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006198 ilk_program_watermarks(dev_priv);
6199 mutex_unlock(&dev_priv->wm.wm_mutex);
6200}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006201
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006202static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006203 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006204{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006205 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6206 const struct intel_crtc_state *crtc_state =
6207 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006208
6209 if (!crtc_state->wm.need_postvbl_update)
6210 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006211
6212 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006213 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6214 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006215 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006216}
6217
Jani Nikula81b55ef2020-04-20 17:04:38 +03006218static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006219{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006220 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006221 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006222 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
6223 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
6224 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00006225}
6226
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006227void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006228 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006229{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006230 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6231 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006232 int level, max_level;
6233 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006234 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006235
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006236 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006237
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006238 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006239 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006240
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006241 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006242 if (plane_id != PLANE_CURSOR)
6243 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006244 else
6245 val = I915_READ(CUR_WM(pipe, level));
6246
6247 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6248 }
6249
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006250 if (INTEL_GEN(dev_priv) >= 12)
6251 wm->sagv_wm0 = wm->wm[0];
6252
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006253 if (plane_id != PLANE_CURSOR)
6254 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006255 else
6256 val = I915_READ(CUR_WM_TRANS(pipe));
6257
6258 skl_wm_level_from_reg_val(val, &wm->trans_wm);
6259 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006260
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006261 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00006262 return;
Pradeep Bhat30789992014-11-04 17:06:45 +00006263}
6264
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006265void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006266{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006267 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006268 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00006269
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006270 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02006271 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006272
Maarten Lankhorstec193642019-06-28 10:55:17 +02006273 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006274 }
Matt Ropera1de91e2016-05-12 07:05:57 -07006275
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03006276 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07006277 /* Fully recompute DDB on first atomic commit */
6278 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07006279 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006280}
6281
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006282static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006283{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006284 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006285 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006286 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006287 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6288 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006289 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006290
Ville Syrjälä96eaeb3d2018-12-12 23:17:38 +02006291 hw->wm_pipe[pipe] = I915_READ(WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006292
Ville Syrjälä15606532016-05-13 17:55:17 +03006293 memset(active, 0, sizeof(*active));
6294
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006295 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006296
6297 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006298 u32 tmp = hw->wm_pipe[pipe];
6299
6300 /*
6301 * For active pipes LP0 watermark is marked as
6302 * enabled, and LP1+ watermaks as disabled since
6303 * we can't really reverse compute them in case
6304 * multiple pipes are active.
6305 */
6306 active->wm[0].enable = true;
6307 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6308 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6309 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006310 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006311 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006312
6313 /*
6314 * For inactive pipes, all watermark levels
6315 * should be marked as enabled but zeroed,
6316 * which is what we'd compute them to.
6317 */
6318 for (level = 0; level <= max_level; level++)
6319 active->wm[level].enable = true;
6320 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006321
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006322 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006323}
6324
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006325#define _FW_WM(value, plane) \
6326 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6327#define _FW_WM_VLV(value, plane) \
6328 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6329
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006330static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6331 struct g4x_wm_values *wm)
6332{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006333 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006334
6335 tmp = I915_READ(DSPFW1);
6336 wm->sr.plane = _FW_WM(tmp, SR);
6337 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6338 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6339 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6340
6341 tmp = I915_READ(DSPFW2);
6342 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6343 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6344 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6345 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6346 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6347 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6348
6349 tmp = I915_READ(DSPFW3);
6350 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6351 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6352 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6353 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6354}
6355
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006356static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6357 struct vlv_wm_values *wm)
6358{
6359 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006360 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006361
6362 for_each_pipe(dev_priv, pipe) {
6363 tmp = I915_READ(VLV_DDL(pipe));
6364
Ville Syrjälä1b313892016-11-28 19:37:08 +02006365 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006366 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006367 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006368 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006369 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006370 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006371 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006372 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6373 }
6374
6375 tmp = I915_READ(DSPFW1);
6376 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006377 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6378 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6379 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006380
6381 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006382 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6383 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6384 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006385
6386 tmp = I915_READ(DSPFW3);
6387 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6388
6389 if (IS_CHERRYVIEW(dev_priv)) {
6390 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006391 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6392 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006393
6394 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006395 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6396 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006397
6398 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006399 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6400 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006401
6402 tmp = I915_READ(DSPHOWM);
6403 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006404 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6405 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6406 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6407 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6408 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6409 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6410 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6411 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6412 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006413 } else {
6414 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006415 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6416 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006417
6418 tmp = I915_READ(DSPHOWM);
6419 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006420 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6421 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6422 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6423 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6424 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6425 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006426 }
6427}
6428
6429#undef _FW_WM
6430#undef _FW_WM_VLV
6431
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006432void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006433{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006434 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6435 struct intel_crtc *crtc;
6436
6437 g4x_read_wm_values(dev_priv, wm);
6438
6439 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
6440
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006441 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006442 struct intel_crtc_state *crtc_state =
6443 to_intel_crtc_state(crtc->base.state);
6444 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6445 struct g4x_pipe_wm *raw;
6446 enum pipe pipe = crtc->pipe;
6447 enum plane_id plane_id;
6448 int level, max_level;
6449
6450 active->cxsr = wm->cxsr;
6451 active->hpll_en = wm->hpll_en;
6452 active->fbc_en = wm->fbc_en;
6453
6454 active->sr = wm->sr;
6455 active->hpll = wm->hpll;
6456
6457 for_each_plane_id_on_crtc(crtc, plane_id) {
6458 active->wm.plane[plane_id] =
6459 wm->pipe[pipe].plane[plane_id];
6460 }
6461
6462 if (wm->cxsr && wm->hpll_en)
6463 max_level = G4X_WM_LEVEL_HPLL;
6464 else if (wm->cxsr)
6465 max_level = G4X_WM_LEVEL_SR;
6466 else
6467 max_level = G4X_WM_LEVEL_NORMAL;
6468
6469 level = G4X_WM_LEVEL_NORMAL;
6470 raw = &crtc_state->wm.g4x.raw[level];
6471 for_each_plane_id_on_crtc(crtc, plane_id)
6472 raw->plane[plane_id] = active->wm.plane[plane_id];
6473
6474 if (++level > max_level)
6475 goto out;
6476
6477 raw = &crtc_state->wm.g4x.raw[level];
6478 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6479 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6480 raw->plane[PLANE_SPRITE0] = 0;
6481 raw->fbc = active->sr.fbc;
6482
6483 if (++level > max_level)
6484 goto out;
6485
6486 raw = &crtc_state->wm.g4x.raw[level];
6487 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6488 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6489 raw->plane[PLANE_SPRITE0] = 0;
6490 raw->fbc = active->hpll.fbc;
6491
6492 out:
6493 for_each_plane_id_on_crtc(crtc, plane_id)
6494 g4x_raw_plane_wm_set(crtc_state, level,
6495 plane_id, USHRT_MAX);
6496 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6497
6498 crtc_state->wm.g4x.optimal = *active;
6499 crtc_state->wm.g4x.intermediate = *active;
6500
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006501 drm_dbg_kms(&dev_priv->drm,
6502 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6503 pipe_name(pipe),
6504 wm->pipe[pipe].plane[PLANE_PRIMARY],
6505 wm->pipe[pipe].plane[PLANE_CURSOR],
6506 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006507 }
6508
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006509 drm_dbg_kms(&dev_priv->drm,
6510 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6511 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6512 drm_dbg_kms(&dev_priv->drm,
6513 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6514 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6515 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6516 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006517}
6518
6519void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6520{
6521 struct intel_plane *plane;
6522 struct intel_crtc *crtc;
6523
6524 mutex_lock(&dev_priv->wm.wm_mutex);
6525
6526 for_each_intel_plane(&dev_priv->drm, plane) {
6527 struct intel_crtc *crtc =
6528 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6529 struct intel_crtc_state *crtc_state =
6530 to_intel_crtc_state(crtc->base.state);
6531 struct intel_plane_state *plane_state =
6532 to_intel_plane_state(plane->base.state);
6533 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6534 enum plane_id plane_id = plane->id;
6535 int level;
6536
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006537 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006538 continue;
6539
6540 for (level = 0; level < 3; level++) {
6541 struct g4x_pipe_wm *raw =
6542 &crtc_state->wm.g4x.raw[level];
6543
6544 raw->plane[plane_id] = 0;
6545 wm_state->wm.plane[plane_id] = 0;
6546 }
6547
6548 if (plane_id == PLANE_PRIMARY) {
6549 for (level = 0; level < 3; level++) {
6550 struct g4x_pipe_wm *raw =
6551 &crtc_state->wm.g4x.raw[level];
6552 raw->fbc = 0;
6553 }
6554
6555 wm_state->sr.fbc = 0;
6556 wm_state->hpll.fbc = 0;
6557 wm_state->fbc_en = false;
6558 }
6559 }
6560
6561 for_each_intel_crtc(&dev_priv->drm, crtc) {
6562 struct intel_crtc_state *crtc_state =
6563 to_intel_crtc_state(crtc->base.state);
6564
6565 crtc_state->wm.g4x.intermediate =
6566 crtc_state->wm.g4x.optimal;
6567 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6568 }
6569
6570 g4x_program_watermarks(dev_priv);
6571
6572 mutex_unlock(&dev_priv->wm.wm_mutex);
6573}
6574
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006575void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006576{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006577 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006578 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006579 u32 val;
6580
6581 vlv_read_wm_values(dev_priv, wm);
6582
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006583 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6584 wm->level = VLV_WM_LEVEL_PM2;
6585
6586 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006587 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006588
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006589 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006590 if (val & DSP_MAXFIFO_PM5_ENABLE)
6591 wm->level = VLV_WM_LEVEL_PM5;
6592
Ville Syrjälä58590c12015-09-08 21:05:12 +03006593 /*
6594 * If DDR DVFS is disabled in the BIOS, Punit
6595 * will never ack the request. So if that happens
6596 * assume we don't have to enable/disable DDR DVFS
6597 * dynamically. To test that just set the REQ_ACK
6598 * bit to poke the Punit, but don't change the
6599 * HIGH/LOW bits so that we don't actually change
6600 * the current state.
6601 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006602 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006603 val |= FORCE_DDR_FREQ_REQ_ACK;
6604 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6605
6606 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6607 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006608 drm_dbg_kms(&dev_priv->drm,
6609 "Punit not acking DDR DVFS request, "
6610 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006611 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6612 } else {
6613 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6614 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6615 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6616 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006617
Chris Wilson337fa6e2019-04-26 09:17:20 +01006618 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006619 }
6620
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006621 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006622 struct intel_crtc_state *crtc_state =
6623 to_intel_crtc_state(crtc->base.state);
6624 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6625 const struct vlv_fifo_state *fifo_state =
6626 &crtc_state->wm.vlv.fifo_state;
6627 enum pipe pipe = crtc->pipe;
6628 enum plane_id plane_id;
6629 int level;
6630
6631 vlv_get_fifo_size(crtc_state);
6632
6633 active->num_levels = wm->level + 1;
6634 active->cxsr = wm->cxsr;
6635
Ville Syrjäläff32c542017-03-02 19:14:57 +02006636 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006637 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006638 &crtc_state->wm.vlv.raw[level];
6639
6640 active->sr[level].plane = wm->sr.plane;
6641 active->sr[level].cursor = wm->sr.cursor;
6642
6643 for_each_plane_id_on_crtc(crtc, plane_id) {
6644 active->wm[level].plane[plane_id] =
6645 wm->pipe[pipe].plane[plane_id];
6646
6647 raw->plane[plane_id] =
6648 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6649 fifo_state->plane[plane_id]);
6650 }
6651 }
6652
6653 for_each_plane_id_on_crtc(crtc, plane_id)
6654 vlv_raw_plane_wm_set(crtc_state, level,
6655 plane_id, USHRT_MAX);
6656 vlv_invalidate_wms(crtc, active, level);
6657
6658 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006659 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006660
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006661 drm_dbg_kms(&dev_priv->drm,
6662 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
6663 pipe_name(pipe),
6664 wm->pipe[pipe].plane[PLANE_PRIMARY],
6665 wm->pipe[pipe].plane[PLANE_CURSOR],
6666 wm->pipe[pipe].plane[PLANE_SPRITE0],
6667 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006668 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006669
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006670 drm_dbg_kms(&dev_priv->drm,
6671 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6672 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006673}
6674
Ville Syrjälä602ae832017-03-02 19:15:02 +02006675void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6676{
6677 struct intel_plane *plane;
6678 struct intel_crtc *crtc;
6679
6680 mutex_lock(&dev_priv->wm.wm_mutex);
6681
6682 for_each_intel_plane(&dev_priv->drm, plane) {
6683 struct intel_crtc *crtc =
6684 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6685 struct intel_crtc_state *crtc_state =
6686 to_intel_crtc_state(crtc->base.state);
6687 struct intel_plane_state *plane_state =
6688 to_intel_plane_state(plane->base.state);
6689 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6690 const struct vlv_fifo_state *fifo_state =
6691 &crtc_state->wm.vlv.fifo_state;
6692 enum plane_id plane_id = plane->id;
6693 int level;
6694
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006695 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02006696 continue;
6697
6698 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006699 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006700 &crtc_state->wm.vlv.raw[level];
6701
6702 raw->plane[plane_id] = 0;
6703
6704 wm_state->wm[level].plane[plane_id] =
6705 vlv_invert_wm_value(raw->plane[plane_id],
6706 fifo_state->plane[plane_id]);
6707 }
6708 }
6709
6710 for_each_intel_crtc(&dev_priv->drm, crtc) {
6711 struct intel_crtc_state *crtc_state =
6712 to_intel_crtc_state(crtc->base.state);
6713
6714 crtc_state->wm.vlv.intermediate =
6715 crtc_state->wm.vlv.optimal;
6716 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6717 }
6718
6719 vlv_program_watermarks(dev_priv);
6720
6721 mutex_unlock(&dev_priv->wm.wm_mutex);
6722}
6723
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006724/*
6725 * FIXME should probably kill this and improve
6726 * the real watermark readout/sanitation instead
6727 */
6728static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6729{
6730 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6731 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6732 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6733
6734 /*
6735 * Don't touch WM1S_LP_EN here.
6736 * Doing so could cause underruns.
6737 */
6738}
6739
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006740void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006741{
Imre Deak820c1982013-12-17 14:46:36 +02006742 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006743 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006744
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006745 ilk_init_lp_watermarks(dev_priv);
6746
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006747 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006748 ilk_pipe_wm_get_hw_state(crtc);
6749
6750 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6751 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6752 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6753
6754 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006755 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006756 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6757 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6758 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006759
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006760 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006761 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6762 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006763 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006764 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6765 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006766
6767 hw->enable_fbc_wm =
6768 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6769}
6770
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006771/**
6772 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006773 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006774 *
6775 * Calculate watermark values for the various WM regs based on current mode
6776 * and plane configuration.
6777 *
6778 * There are several cases to deal with here:
6779 * - normal (i.e. non-self-refresh)
6780 * - self-refresh (SR) mode
6781 * - lines are large relative to FIFO size (buffer can hold up to 2)
6782 * - lines are small relative to FIFO size (buffer can hold more than 2
6783 * lines), so need to account for TLB latency
6784 *
6785 * The normal calculation is:
6786 * watermark = dotclock * bytes per pixel * latency
6787 * where latency is platform & configuration dependent (we assume pessimal
6788 * values here).
6789 *
6790 * The SR calculation is:
6791 * watermark = (trunc(latency/line time)+1) * surface width *
6792 * bytes per pixel
6793 * where
6794 * line time = htotal / dotclock
6795 * surface width = hdisplay for normal plane and 64 for cursor
6796 * and latency is assumed to be high, as above.
6797 *
6798 * The final value programmed to the register should always be rounded up,
6799 * and include an extra 2 entries to account for clock crossings.
6800 *
6801 * We don't use the sprite, so we can ignore that. And on Crestline we have
6802 * to set the non-SR watermarks to 8.
6803 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006804void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006805{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006806 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006807
6808 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006809 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006810}
6811
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306812void intel_enable_ipc(struct drm_i915_private *dev_priv)
6813{
6814 u32 val;
6815
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006816 if (!HAS_IPC(dev_priv))
6817 return;
6818
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306819 val = I915_READ(DISP_ARB_CTL2);
6820
6821 if (dev_priv->ipc_enabled)
6822 val |= DISP_IPC_ENABLE;
6823 else
6824 val &= ~DISP_IPC_ENABLE;
6825
6826 I915_WRITE(DISP_ARB_CTL2, val);
6827}
6828
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006829static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6830{
6831 /* Display WA #0477 WaDisableIPC: skl */
6832 if (IS_SKYLAKE(dev_priv))
6833 return false;
6834
6835 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01006836 if (IS_KABYLAKE(dev_priv) ||
6837 IS_COFFEELAKE(dev_priv) ||
6838 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006839 return dev_priv->dram_info.symmetric_memory;
6840
6841 return true;
6842}
6843
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306844void intel_init_ipc(struct drm_i915_private *dev_priv)
6845{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306846 if (!HAS_IPC(dev_priv))
6847 return;
6848
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006849 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006850
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306851 intel_enable_ipc(dev_priv);
6852}
6853
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006854static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006855{
Daniel Vetter3107bd42012-10-31 22:52:31 +01006856 /*
6857 * On Ibex Peak and Cougar Point, we need to disable clock
6858 * gating for the panel power sequencer or it will fail to
6859 * start up when no ports are active.
6860 */
6861 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6862}
6863
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006864static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006865{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006866 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006867
Damien Lespiau055e3932014-08-18 13:49:10 +01006868 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006869 I915_WRITE(DSPCNTR(pipe),
6870 I915_READ(DSPCNTR(pipe)) |
6871 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006872
6873 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6874 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006875 }
6876}
6877
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006878static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006879{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006880 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006881
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006882 /*
6883 * Required for FBC
6884 * WaFbcDisableDpfcClockGating:ilk
6885 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006886 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6887 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6888 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006889
6890 I915_WRITE(PCH_3DCGDIS0,
6891 MARIUNIT_CLOCK_GATE_DISABLE |
6892 SVSMUNIT_CLOCK_GATE_DISABLE);
6893 I915_WRITE(PCH_3DCGDIS1,
6894 VFMUNIT_CLOCK_GATE_DISABLE);
6895
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006896 /*
6897 * According to the spec the following bits should be set in
6898 * order to enable memory self-refresh
6899 * The bit 22/21 of 0x42004
6900 * The bit 5 of 0x42020
6901 * The bit 15 of 0x45000
6902 */
6903 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6904 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6905 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006906 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006907 I915_WRITE(DISP_ARB_CTL,
6908 (I915_READ(DISP_ARB_CTL) |
6909 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006910
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006911 /*
6912 * Based on the document from hardware guys the following bits
6913 * should be set unconditionally in order to enable FBC.
6914 * The bit 22 of 0x42000
6915 * The bit 22 of 0x42004
6916 * The bit 7,8,9 of 0x42020.
6917 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006918 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006919 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006920 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6921 I915_READ(ILK_DISPLAY_CHICKEN1) |
6922 ILK_FBCQ_DIS);
6923 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6924 I915_READ(ILK_DISPLAY_CHICKEN2) |
6925 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006926 }
6927
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006928 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6929
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006930 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6931 I915_READ(ILK_DISPLAY_CHICKEN2) |
6932 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05306933
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006934 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006935
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006936 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006937}
6938
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006939static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006940{
Ville Syrjäläd048a262019-08-21 20:30:31 +03006941 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006942 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006943
6944 /*
6945 * On Ibex Peak and Cougar Point, we need to disable clock
6946 * gating for the panel power sequencer or it will fail to
6947 * start up when no ports are active.
6948 */
Jesse Barnescd664072013-10-02 10:34:19 -07006949 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6950 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6951 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006952 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6953 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006954 /* The below fixes the weird display corruption, a few pixels shifted
6955 * downward, on (only) LVDS of some HP laptops with IVY.
6956 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006957 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006958 val = I915_READ(TRANS_CHICKEN2(pipe));
6959 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6960 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006961 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006962 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006963 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6964 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006965 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6966 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006967 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006968 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006969 I915_WRITE(TRANS_CHICKEN1(pipe),
6970 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6971 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006972}
6973
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006974static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006975{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006976 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006977
6978 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006979 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006980 drm_dbg_kms(&dev_priv->drm,
6981 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6982 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006983}
6984
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006985static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006986{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006987 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006988
Damien Lespiau231e54f2012-10-19 17:55:41 +01006989 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006990
6991 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6992 I915_READ(ILK_DISPLAY_CHICKEN2) |
6993 ILK_ELPIN_409_SELECT);
6994
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006995 I915_WRITE(GEN6_UCGCTL1,
6996 I915_READ(GEN6_UCGCTL1) |
6997 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6998 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6999
7000 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7001 * gating disable must be set. Failure to set it results in
7002 * flickering pixels due to Z write ordering failures after
7003 * some amount of runtime in the Mesa "fire" demo, and Unigine
7004 * Sanctuary and Tropics, and apparently anything else with
7005 * alpha test or pixel discard.
7006 *
7007 * According to the spec, bit 11 (RCCUNIT) must also be set,
7008 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007009 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007010 * WaDisableRCCUnitClockGating:snb
7011 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007012 */
7013 I915_WRITE(GEN6_UCGCTL2,
7014 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7015 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7016
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007017 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007018 * According to the spec the following bits should be
7019 * set in order to enable memory self-refresh and fbc:
7020 * The bit21 and bit22 of 0x42000
7021 * The bit21 and bit22 of 0x42004
7022 * The bit5 and bit7 of 0x42020
7023 * The bit14 of 0x70180
7024 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007025 *
7026 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007027 */
7028 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7029 I915_READ(ILK_DISPLAY_CHICKEN1) |
7030 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7031 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7032 I915_READ(ILK_DISPLAY_CHICKEN2) |
7033 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007034 I915_WRITE(ILK_DSPCLK_GATE_D,
7035 I915_READ(ILK_DSPCLK_GATE_D) |
7036 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7037 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007038
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007039 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007040
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007041 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007042
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007043 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007044}
7045
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007046static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007047{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007048 /*
7049 * TODO: this bit should only be enabled when really needed, then
7050 * disabled when not needed anymore in order to save power.
7051 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007052 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007053 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7054 I915_READ(SOUTH_DSPCLK_GATE_D) |
7055 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007056
7057 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007058 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7059 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007060 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007061}
7062
Ville Syrjälä712bf362016-10-31 22:37:23 +02007063static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007064{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007065 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007066 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007067
7068 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7069 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7070 }
7071}
7072
Imre Deak450174f2016-05-03 15:54:21 +03007073static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7074 int general_prio_credits,
7075 int high_prio_credits)
7076{
7077 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007078 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007079
7080 /* WaTempDisableDOPClkGating:bdw */
7081 misccpctl = I915_READ(GEN7_MISCCPCTL);
7082 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7083
Oscar Mateo930a7842017-10-17 13:25:45 -07007084 val = I915_READ(GEN8_L3SQCREG1);
7085 val &= ~L3_PRIO_CREDITS_MASK;
7086 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7087 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
7088 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007089
7090 /*
7091 * Wait at least 100 clocks before re-enabling clock gating.
7092 * See the definition of L3SQCREG1 in BSpec.
7093 */
7094 POSTING_READ(GEN8_L3SQCREG1);
7095 udelay(1);
7096 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7097}
7098
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007099static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7100{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007101 /* Wa_1409120013:icl,ehl */
7102 I915_WRITE(ILK_DPFC_CHICKEN,
7103 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7104
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007105 /* This is not an Wa. Enable to reduce Sampler power */
7106 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
7107 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07007108
Matt Atwood6f4194c2020-01-13 23:11:28 -05007109 /*Wa_14010594013:icl, ehl */
7110 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7111 0, CNL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007112}
7113
Stuart Summersda9427502020-10-14 12:19:34 -07007114static void gen12_init_clock_gating(struct drm_i915_private *i915)
7115{
7116 unsigned int i;
7117
7118 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
7119 for (i = 0; i < I915_MAX_VCS; i++)
7120 if (HAS_ENGINE(&i915->gt, _VCS(i)))
7121 intel_uncore_rmw(&i915->uncore, POWERGATE_ENABLE, 0,
7122 VDN_HCP_POWERGATE_ENABLE(i) |
7123 VDN_MFX_POWERGATE_ENABLE(i));
7124}
7125
Michel Thierry5d869232019-08-23 01:20:34 -07007126static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
7127{
Stuart Summersda9427502020-10-14 12:19:34 -07007128 gen12_init_clock_gating(dev_priv);
Michel Thierry5d869232019-08-23 01:20:34 -07007129
Ville Syrjälä885f1822020-07-08 16:12:20 +03007130 /* Wa_1409120013:tgl */
7131 I915_WRITE(ILK_DPFC_CHICKEN,
7132 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7133
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007134 /* Wa_1409825376:tgl (pre-prod)*/
José Roberto de Souzac33298c2020-08-27 16:39:43 -07007135 if (IS_TGL_DISP_REVID(dev_priv, TGL_REVID_A0, TGL_REVID_B1))
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007136 I915_WRITE(GEN9_CLKGATE_DIS_3, I915_READ(GEN9_CLKGATE_DIS_3) |
7137 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007138
7139 /* Wa_14011059788:tgl */
7140 intel_uncore_rmw(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7141 0, DFR_DISABLE);
Michel Thierry5d869232019-08-23 01:20:34 -07007142}
7143
Stuart Summersda9427502020-10-14 12:19:34 -07007144static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7145{
7146 gen12_init_clock_gating(dev_priv);
7147
7148 /* Wa_1409836686:dg1[a0] */
7149 if (IS_DG1_REVID(dev_priv, DG1_REVID_A0, DG1_REVID_A0))
7150 I915_WRITE(GEN9_CLKGATE_DIS_3, I915_READ(GEN9_CLKGATE_DIS_3) |
7151 DPT_GATING_DIS);
7152}
7153
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007154static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7155{
7156 if (!HAS_PCH_CNP(dev_priv))
7157 return;
7158
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007159 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07007160 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
7161 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007162}
7163
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007164static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007165{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07007166 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007167 cnp_init_clock_gating(dev_priv);
7168
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07007169 /* This is not an Wa. Enable for better image quality */
7170 I915_WRITE(_3D_CHICKEN3,
7171 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
7172
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007173 /* WaEnableChickenDCPR:cnl */
7174 I915_WRITE(GEN8_CHICKEN_DCPR_1,
7175 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
7176
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007177 /*
7178 * WaFbcWakeMemOn:cnl
7179 * Display WA #0859: cnl
7180 */
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007181 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7182 DISP_FBC_MEMORY_WAKE);
7183
Chris Wilson34991bd2017-11-11 10:03:36 +00007184 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
7185 /* ReadHitWriteOnlyDisable:cnl */
7186 val |= RCCUNIT_CLKGATE_DIS;
Chris Wilson34991bd2017-11-11 10:03:36 +00007187 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007188
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007189 /* Wa_2201832410:cnl */
7190 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
7191 val |= GWUNIT_CLKGATE_DIS;
7192 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
7193
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007194 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08007195 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007196 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
7197 val |= VFUNIT_CLKGATE_DIS;
7198 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007199}
7200
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007201static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7202{
7203 cnp_init_clock_gating(dev_priv);
7204 gen9_init_clock_gating(dev_priv);
7205
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007206 /* WAC6entrylatency:cfl */
7207 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7208 FBC_LLC_FULLY_OPEN);
7209
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007210 /*
7211 * WaFbcTurnOffFbcWatermark:cfl
7212 * Display WA #0562: cfl
7213 */
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007214 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7215 DISP_FBC_WM_DIS);
7216
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007217 /*
7218 * WaFbcNukeOnHostModify:cfl
7219 * Display WA #0873: cfl
7220 */
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007221 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7222 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7223}
7224
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007225static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007226{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007227 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007228
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007229 /* WAC6entrylatency:kbl */
7230 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7231 FBC_LLC_FULLY_OPEN);
7232
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007233 /* WaDisableSDEUnitClockGating:kbl */
Matt Roper96c5a152020-08-10 20:21:05 -07007234 if (IS_KBL_GT_REVID(dev_priv, 0, KBL_REVID_B0))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007235 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7236 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007237
7238 /* WaDisableGamClockGating:kbl */
Matt Roper96c5a152020-08-10 20:21:05 -07007239 if (IS_KBL_GT_REVID(dev_priv, 0, KBL_REVID_B0))
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007240 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7241 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007242
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007243 /*
7244 * WaFbcTurnOffFbcWatermark:kbl
7245 * Display WA #0562: kbl
7246 */
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007247 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7248 DISP_FBC_WM_DIS);
7249
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007250 /*
7251 * WaFbcNukeOnHostModify:kbl
7252 * Display WA #0873: kbl
7253 */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007254 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7255 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007256}
7257
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007258static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007259{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007260 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007261
Ville Syrjäläf1421192020-07-16 22:04:25 +03007262 /* WaDisableDopClockGating:skl */
7263 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL) &
7264 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7265
Mika Kuoppala44fff992016-06-07 17:19:09 +03007266 /* WAC6entrylatency:skl */
7267 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7268 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007269
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007270 /*
7271 * WaFbcTurnOffFbcWatermark:skl
7272 * Display WA #0562: skl
7273 */
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007274 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7275 DISP_FBC_WM_DIS);
7276
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007277 /*
7278 * WaFbcNukeOnHostModify:skl
7279 * Display WA #0873: skl
7280 */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007281 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7282 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007283
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007284 /*
7285 * WaFbcHighMemBwCorruptionAvoidance:skl
7286 * Display WA #0883: skl
7287 */
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007288 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7289 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007290}
7291
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007292static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007293{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007294 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007295
Ville Syrjälä885f1822020-07-08 16:12:20 +03007296 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7297 I915_WRITE(CHICKEN_PIPESL_1(PIPE_A),
7298 I915_READ(CHICKEN_PIPESL_1(PIPE_A)) |
7299 HSW_FBCQ_DIS);
7300
Ben Widawskyab57fff2013-12-12 15:28:04 -08007301 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007302 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007303
Ben Widawskyab57fff2013-12-12 15:28:04 -08007304 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007305 I915_WRITE(CHICKEN_PAR1_1,
7306 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7307
Ben Widawskyab57fff2013-12-12 15:28:04 -08007308 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007309 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007310 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007311 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007312 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007313 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007314
Ben Widawskyab57fff2013-12-12 15:28:04 -08007315 /* WaVSRefCountFullforceMissDisable:bdw */
7316 /* WaDSRefCountFullforceMissDisable:bdw */
7317 I915_WRITE(GEN7_FF_THREAD_MODE,
7318 I915_READ(GEN7_FF_THREAD_MODE) &
7319 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007320
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007321 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7322 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007323
7324 /* WaDisableSDEUnitClockGating:bdw */
7325 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7326 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007327
Imre Deak450174f2016-05-03 15:54:21 +03007328 /* WaProgramL3SqcReg1Default:bdw */
7329 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007330
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007331 /* WaKVMNotificationOnConfigChange:bdw */
7332 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7333 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7334
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007335 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007336
7337 /* WaDisableDopClockGating:bdw
7338 *
7339 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7340 * clock gating.
7341 */
7342 I915_WRITE(GEN6_UCGCTL1,
7343 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007344}
7345
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007346static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007347{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007348 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7349 I915_WRITE(CHICKEN_PIPESL_1(PIPE_A),
7350 I915_READ(CHICKEN_PIPESL_1(PIPE_A)) |
7351 HSW_FBCQ_DIS);
7352
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007353 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007354 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007355 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7356 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007357
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007358 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007359 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7360
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007361 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007362}
7363
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007364static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007365{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007366 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007367
Damien Lespiau231e54f2012-10-19 17:55:41 +01007368 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007369
Ville Syrjälä885f1822020-07-08 16:12:20 +03007370 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
7371 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7372 I915_READ(ILK_DISPLAY_CHICKEN1) |
7373 ILK_FBCQ_DIS);
7374
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007375 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007376 I915_WRITE(IVB_CHICKEN3,
7377 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7378 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7379
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007380 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007381 I915_WRITE(GEN7_ROW_CHICKEN2,
7382 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007383 else {
7384 /* must write both registers */
7385 I915_WRITE(GEN7_ROW_CHICKEN2,
7386 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007387 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7388 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007389 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007390
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007391 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007392 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007393 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007394 */
7395 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007396 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007397
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007398 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007399 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7400 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7401 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7402
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007403 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007404
Ben Widawsky20848222012-05-04 18:58:59 -07007405 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7406 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7407 snpcr |= GEN6_MBC_SNPCR_MED;
7408 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007409
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007410 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007411 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007412
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007413 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007414}
7415
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007416static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007417{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007418 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007419 I915_WRITE(IVB_CHICKEN3,
7420 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7421 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7422
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007423 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007424 I915_WRITE(GEN7_ROW_CHICKEN2,
7425 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7426
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007427 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007428 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7429 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7430 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7431
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007432 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007433 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007434 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007435 */
7436 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007437 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007438
Akash Goelc98f5062014-03-24 23:00:07 +05307439 /* WaDisableL3Bank2xClockGate:vlv
7440 * Disabling L3 clock gating- MMIO 940c[25] = 1
7441 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7442 I915_WRITE(GEN7_UCGCTL4,
7443 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007444
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007445 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007446 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007447 * Disable clock gating on th GCFG unit to prevent a delay
7448 * in the reporting of vblank events.
7449 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007450 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007451}
7452
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007453static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007454{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007455 /* WaVSRefCountFullforceMissDisable:chv */
7456 /* WaDSRefCountFullforceMissDisable:chv */
7457 I915_WRITE(GEN7_FF_THREAD_MODE,
7458 I915_READ(GEN7_FF_THREAD_MODE) &
7459 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007460
7461 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7462 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7463 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007464
7465 /* WaDisableCSUnitClockGating:chv */
7466 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7467 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007468
7469 /* WaDisableSDEUnitClockGating:chv */
7470 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7471 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007472
7473 /*
Imre Deak450174f2016-05-03 15:54:21 +03007474 * WaProgramL3SqcReg1Default:chv
7475 * See gfxspecs/Related Documents/Performance Guide/
7476 * LSQC Setting Recommendations.
7477 */
7478 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007479}
7480
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007481static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007482{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007483 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007484
7485 I915_WRITE(RENCLK_GATE_D1, 0);
7486 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7487 GS_UNIT_CLOCK_GATE_DISABLE |
7488 CL_UNIT_CLOCK_GATE_DISABLE);
7489 I915_WRITE(RAMCLK_GATE_D, 0);
7490 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7491 OVRUNIT_CLOCK_GATE_DISABLE |
7492 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007493 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007494 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7495 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007496
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007497 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007498}
7499
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007500static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007501{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007502 struct intel_uncore *uncore = &dev_priv->uncore;
7503
7504 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7505 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7506 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7507 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7508 intel_uncore_write16(uncore, DEUC, 0);
7509 intel_uncore_write(uncore,
7510 MI_ARB_STATE,
7511 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007512}
7513
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007514static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007515{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007516 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7517 I965_RCC_CLOCK_GATE_DISABLE |
7518 I965_RCPB_CLOCK_GATE_DISABLE |
7519 I965_ISC_CLOCK_GATE_DISABLE |
7520 I965_FBC_CLOCK_GATE_DISABLE);
7521 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007522 I915_WRITE(MI_ARB_STATE,
7523 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007524}
7525
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007526static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007527{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007528 u32 dstate = I915_READ(D_STATE);
7529
7530 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7531 DSTATE_DOT_CLOCK_GATING;
7532 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007533
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007534 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007535 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007536
7537 /* IIR "flip pending" means done if this bit is set */
7538 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007539
7540 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007541 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007542
7543 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7544 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007545
7546 I915_WRITE(MI_ARB_STATE,
7547 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007548}
7549
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007550static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007551{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007552 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007553
7554 /* interrupts should cause a wake up from C3 */
7555 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7556 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007557
7558 I915_WRITE(MEM_MODE,
7559 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007560
7561 /*
7562 * Have FBC ignore 3D activity since we use software
7563 * render tracking, and otherwise a pure 3D workload
7564 * (even if it just renders a single frame and then does
7565 * abosultely nothing) would not allow FBC to recompress
7566 * until a 2D blit occurs.
7567 */
7568 I915_WRITE(SCPD0,
7569 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007570}
7571
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007572static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007573{
Ville Syrjälä10383922014-08-15 01:21:54 +03007574 I915_WRITE(MEM_MODE,
7575 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7576 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007577}
7578
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007579void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007580{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007581 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007582}
7583
Ville Syrjälä712bf362016-10-31 22:37:23 +02007584void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007585{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007586 if (HAS_PCH_LPT(dev_priv))
7587 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007588}
7589
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007590static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007591{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007592 drm_dbg_kms(&dev_priv->drm,
7593 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007594}
7595
7596/**
7597 * intel_init_clock_gating_hooks - setup the clock gating hooks
7598 * @dev_priv: device private
7599 *
7600 * Setup the hooks that configure which clocks of a given platform can be
7601 * gated and also apply various GT and display specific workarounds for these
7602 * platforms. Note that some GT specific workarounds are applied separately
7603 * when GPU contexts or batchbuffers start their execution.
7604 */
7605void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7606{
Stuart Summersda9427502020-10-14 12:19:34 -07007607 if (IS_DG1(dev_priv))
7608 dev_priv->display.init_clock_gating = dg1_init_clock_gating;
7609 else if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07007610 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07007611 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007612 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07007613 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007614 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007615 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007616 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007617 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007618 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007619 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007620 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007621 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007622 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007623 else if (IS_GEMINILAKE(dev_priv))
7624 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007625 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007626 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007627 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007628 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007629 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007630 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007631 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007632 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007633 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007634 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007635 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02007636 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007637 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007638 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007639 else if (IS_G4X(dev_priv))
7640 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007641 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007642 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007643 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007644 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007645 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02007646 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7647 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7648 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007649 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02007650 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7651 else {
7652 MISSING_CASE(INTEL_DEVID(dev_priv));
7653 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7654 }
7655}
7656
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007657/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007658void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007659{
Daniel Vetterc921aba2012-04-26 23:28:17 +02007660 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007661 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08007662 pnv_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007663 else if (IS_GEN(dev_priv, 5))
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08007664 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007665
James Ausmusb068a862019-10-09 10:23:14 -07007666 if (intel_has_sagv(dev_priv))
7667 skl_setup_sagv_block_time(dev_priv);
7668
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007669 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007670 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007671 skl_setup_wm_latency(dev_priv);
Matt Roper98d39492016-05-12 07:06:03 -07007672 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007673 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007674 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007675
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007676 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007677 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007678 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007679 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007680 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007681 dev_priv->display.compute_intermediate_wm =
7682 ilk_compute_intermediate_wm;
7683 dev_priv->display.initial_watermarks =
7684 ilk_initial_watermarks;
7685 dev_priv->display.optimize_watermarks =
7686 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007687 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007688 drm_dbg_kms(&dev_priv->drm,
7689 "Failed to read display plane latency. "
7690 "Disable CxSR\n");
Ville Syrjäläbd602542014-01-07 16:14:10 +02007691 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007692 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007693 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007694 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007695 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007696 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007697 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007698 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03007699 } else if (IS_G4X(dev_priv)) {
7700 g4x_setup_wm_latency(dev_priv);
7701 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
7702 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
7703 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
7704 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007705 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00007706 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007707 dev_priv->is_ddr3,
7708 dev_priv->fsb_freq,
7709 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007710 drm_info(&dev_priv->drm,
7711 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007712 "(found ddr%s fsb freq %d, mem freq %d), "
7713 "disabling CxSR\n",
7714 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7715 dev_priv->fsb_freq, dev_priv->mem_freq);
7716 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007717 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007718 dev_priv->display.update_wm = NULL;
7719 } else
Lucas De Marchi1d218222019-12-24 00:40:04 -08007720 dev_priv->display.update_wm = pnv_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007721 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007722 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007723 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007724 dev_priv->display.update_wm = i9xx_update_wm;
7725 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007726 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03007727 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007728 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007729 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007730 } else {
7731 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007732 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007733 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007734 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007735 drm_err(&dev_priv->drm,
7736 "unexpected fall-through in %s\n", __func__);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007737 }
7738}
7739
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00007740void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01007741{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01007742 dev_priv->runtime_pm.suspended = false;
7743 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007744}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02007745
7746static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
7747{
7748 struct intel_dbuf_state *dbuf_state;
7749
7750 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
7751 if (!dbuf_state)
7752 return NULL;
7753
7754 return &dbuf_state->base;
7755}
7756
7757static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
7758 struct intel_global_state *state)
7759{
7760 kfree(state);
7761}
7762
7763static const struct intel_global_state_funcs intel_dbuf_funcs = {
7764 .atomic_duplicate_state = intel_dbuf_duplicate_state,
7765 .atomic_destroy_state = intel_dbuf_destroy_state,
7766};
7767
7768struct intel_dbuf_state *
7769intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
7770{
7771 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7772 struct intel_global_state *dbuf_state;
7773
7774 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
7775 if (IS_ERR(dbuf_state))
7776 return ERR_CAST(dbuf_state);
7777
7778 return to_intel_dbuf_state(dbuf_state);
7779}
7780
7781int intel_dbuf_init(struct drm_i915_private *dev_priv)
7782{
7783 struct intel_dbuf_state *dbuf_state;
7784
7785 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
7786 if (!dbuf_state)
7787 return -ENOMEM;
7788
7789 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
7790 &dbuf_state->base, &intel_dbuf_funcs);
7791
7792 return 0;
7793}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02007794
7795void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
7796{
7797 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7798 const struct intel_dbuf_state *new_dbuf_state =
7799 intel_atomic_get_new_dbuf_state(state);
7800 const struct intel_dbuf_state *old_dbuf_state =
7801 intel_atomic_get_old_dbuf_state(state);
7802
7803 if (!new_dbuf_state ||
7804 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7805 return;
7806
7807 WARN_ON(!new_dbuf_state->base.changed);
7808
7809 gen9_dbuf_slices_update(dev_priv,
7810 old_dbuf_state->enabled_slices |
7811 new_dbuf_state->enabled_slices);
7812}
7813
7814void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
7815{
7816 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7817 const struct intel_dbuf_state *new_dbuf_state =
7818 intel_atomic_get_new_dbuf_state(state);
7819 const struct intel_dbuf_state *old_dbuf_state =
7820 intel_atomic_get_old_dbuf_state(state);
7821
7822 if (!new_dbuf_state ||
7823 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7824 return;
7825
7826 WARN_ON(!new_dbuf_state->base.changed);
7827
7828 gen9_dbuf_slices_update(dev_priv,
7829 new_dbuf_state->enabled_slices);
7830}