blob: 08e0195edeb7e2cec1e251b053cdf121e66fb268 [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"
Ville Syrjälä3df3fe22020-11-06 19:30:42 +020036#include "display/intel_atomic_plane.h"
Stanislav Lisovskiycac91e62020-05-22 16:18:43 +030037#include "display/intel_bw.h"
Ville Syrjälä7785ae02021-04-30 17:39:44 +030038#include "display/intel_de.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030039#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030040#include "display/intel_fbc.h"
41#include "display/intel_sprite.h"
Dave Airlie46d12f92021-02-05 16:48:36 +020042#include "display/skl_universal_plane.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030043
Andi Shyti0dc3c562019-10-20 19:41:39 +010044#include "gt/intel_llc.h"
45
Eugeni Dodonov85208be2012-04-16 22:20:34 -030046#include "i915_drv.h"
Jani Nikulaa10510a2020-02-27 19:00:47 +020047#include "i915_fixed.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030048#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030049#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030050#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010051#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020052#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030053
Jani Nikulaa10510a2020-02-27 19:00:47 +020054/* Stores plane specific WM parameters */
55struct skl_wm_params {
56 bool x_tiled, y_tiled;
57 bool rc_surface;
58 bool is_planar;
59 u32 width;
60 u8 cpp;
61 u32 plane_pixel_rate;
62 u32 y_min_scanlines;
63 u32 plane_bytes_per_line;
64 uint_fixed_16_16_t plane_blocks_per_line;
65 uint_fixed_16_16_t y_tile_minimum;
66 u32 linetime_us;
67 u32 dbuf_block_size;
68};
69
70/* used in computing the new watermarks state */
71struct intel_wm_config {
72 unsigned int num_pipes_active;
73 bool sprites_enabled;
74 bool sprites_scaled;
75};
76
Ville Syrjälä46f16e62016-10-31 22:37:22 +020077static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030078{
Ville Syrjälä93564042017-08-24 22:10:51 +030079 if (HAS_LLC(dev_priv)) {
80 /*
81 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080082 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030083 *
84 * Must match Sampler, Pixel Back End, and Media. See
85 * WaCompressedResourceSamplerPbeMediaNewHashMode.
86 */
Jani Nikula5f461662020-11-30 13:15:58 +020087 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
88 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
Ville Syrjälä93564042017-08-24 22:10:51 +030089 SKL_DE_COMPRESSED_HASH_MODE);
90 }
91
Rodrigo Vivi82525c12017-06-08 08:50:00 -070092 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +020093 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
94 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030095
Rodrigo Vivi82525c12017-06-08 08:50:00 -070096 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +020097 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
98 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030099
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300100 /*
101 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
102 * Display WA #0859: skl,bxt,kbl,glk,cfl
103 */
Jani Nikula5f461662020-11-30 13:15:58 +0200104 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Mika Kuoppala303d4ea2016-06-07 17:19:17 +0300105 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300106}
107
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200108static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200109{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200110 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200111
Nick Hoatha7546152015-06-29 14:07:32 +0100112 /* WaDisableSDEUnitClockGating:bxt */
Jani Nikula5f461662020-11-30 13:15:58 +0200113 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Nick Hoatha7546152015-06-29 14:07:32 +0100114 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
115
Imre Deak32608ca2015-03-11 11:10:27 +0200116 /*
117 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200118 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200119 */
Jani Nikula5f461662020-11-30 13:15:58 +0200120 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200121 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200122
123 /*
124 * Wa: Backlight PWM may stop in the asserted state, causing backlight
125 * to stay fully on.
126 */
Jani Nikula5f461662020-11-30 13:15:58 +0200127 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
Jani Nikula8aeaf642017-02-15 17:21:37 +0200128 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530129
130 /*
131 * Lower the display internal timeout.
132 * This is needed to avoid any hard hangs when DSI port PLL
133 * is off and a MMIO access is attempted by any privilege
134 * application, using batch buffers or any other means.
135 */
Jani Nikula5f461662020-11-30 13:15:58 +0200136 intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300137
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300138 /*
139 * WaFbcTurnOffFbcWatermark:bxt
140 * Display WA #0562: bxt
141 */
Jani Nikula5f461662020-11-30 13:15:58 +0200142 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300143 DISP_FBC_WM_DIS);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300144
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300145 /*
146 * WaFbcHighMemBwCorruptionAvoidance:bxt
147 * Display WA #0883: bxt
148 */
Jani Nikula5f461662020-11-30 13:15:58 +0200149 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300150 ILK_DPFC_DISABLE_DUMMY0);
Imre Deaka82abe42015-03-27 14:00:04 +0200151}
152
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200153static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
154{
155 gen9_init_clock_gating(dev_priv);
156
157 /*
158 * WaDisablePWMClockGating:glk
159 * Backlight PWM may stop in the asserted state, causing backlight
160 * to stay fully on.
161 */
Jani Nikula5f461662020-11-30 13:15:58 +0200162 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200163 PWM1_GATING_DIS | PWM2_GATING_DIS);
164}
165
Lucas De Marchi1d218222019-12-24 00:40:04 -0800166static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200167{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200168 u32 tmp;
169
Jani Nikula5f461662020-11-30 13:15:58 +0200170 tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200171
172 switch (tmp & CLKCFG_FSB_MASK) {
173 case CLKCFG_FSB_533:
174 dev_priv->fsb_freq = 533; /* 133*4 */
175 break;
176 case CLKCFG_FSB_800:
177 dev_priv->fsb_freq = 800; /* 200*4 */
178 break;
179 case CLKCFG_FSB_667:
180 dev_priv->fsb_freq = 667; /* 167*4 */
181 break;
182 case CLKCFG_FSB_400:
183 dev_priv->fsb_freq = 400; /* 100*4 */
184 break;
185 }
186
187 switch (tmp & CLKCFG_MEM_MASK) {
188 case CLKCFG_MEM_533:
189 dev_priv->mem_freq = 533;
190 break;
191 case CLKCFG_MEM_667:
192 dev_priv->mem_freq = 667;
193 break;
194 case CLKCFG_MEM_800:
195 dev_priv->mem_freq = 800;
196 break;
197 }
198
199 /* detect pineview DDR3 setting */
Jani Nikula5f461662020-11-30 13:15:58 +0200200 tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200201 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
202}
203
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800204static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200205{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200206 u16 ddrpll, csipll;
207
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100208 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
209 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200210
211 switch (ddrpll & 0xff) {
212 case 0xc:
213 dev_priv->mem_freq = 800;
214 break;
215 case 0x10:
216 dev_priv->mem_freq = 1066;
217 break;
218 case 0x14:
219 dev_priv->mem_freq = 1333;
220 break;
221 case 0x18:
222 dev_priv->mem_freq = 1600;
223 break;
224 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300225 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
226 ddrpll & 0xff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200227 dev_priv->mem_freq = 0;
228 break;
229 }
230
Daniel Vetterc921aba2012-04-26 23:28:17 +0200231 switch (csipll & 0x3ff) {
232 case 0x00c:
233 dev_priv->fsb_freq = 3200;
234 break;
235 case 0x00e:
236 dev_priv->fsb_freq = 3733;
237 break;
238 case 0x010:
239 dev_priv->fsb_freq = 4266;
240 break;
241 case 0x012:
242 dev_priv->fsb_freq = 4800;
243 break;
244 case 0x014:
245 dev_priv->fsb_freq = 5333;
246 break;
247 case 0x016:
248 dev_priv->fsb_freq = 5866;
249 break;
250 case 0x018:
251 dev_priv->fsb_freq = 6400;
252 break;
253 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300254 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
255 csipll & 0x3ff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200256 dev_priv->fsb_freq = 0;
257 break;
258 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200259}
260
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300261static const struct cxsr_latency cxsr_latency_table[] = {
262 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
263 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
264 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
265 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
266 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
267
268 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
269 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
270 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
271 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
272 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
273
274 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
275 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
276 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
277 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
278 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
279
280 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
281 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
282 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
283 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
284 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
285
286 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
287 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
288 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
289 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
290 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
291
292 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
293 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
294 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
295 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
296 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
297};
298
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100299static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
300 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300301 int fsb,
302 int mem)
303{
304 const struct cxsr_latency *latency;
305 int i;
306
307 if (fsb == 0 || mem == 0)
308 return NULL;
309
310 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
311 latency = &cxsr_latency_table[i];
312 if (is_desktop == latency->is_desktop &&
313 is_ddr3 == latency->is_ddr3 &&
314 fsb == latency->fsb_freq && mem == latency->mem_freq)
315 return latency;
316 }
317
318 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
319
320 return NULL;
321}
322
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200323static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
324{
325 u32 val;
326
Chris Wilson337fa6e2019-04-26 09:17:20 +0100327 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200328
329 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
330 if (enable)
331 val &= ~FORCE_DDR_HIGH_FREQ;
332 else
333 val |= FORCE_DDR_HIGH_FREQ;
334 val &= ~FORCE_DDR_LOW_FREQ;
335 val |= FORCE_DDR_FREQ_REQ_ACK;
336 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
337
338 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
339 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300340 drm_err(&dev_priv->drm,
341 "timed out waiting for Punit DDR DVFS request\n");
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200342
Chris Wilson337fa6e2019-04-26 09:17:20 +0100343 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200344}
345
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200346static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
347{
348 u32 val;
349
Chris Wilson337fa6e2019-04-26 09:17:20 +0100350 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200351
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200352 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200353 if (enable)
354 val |= DSP_MAXFIFO_PM5_ENABLE;
355 else
356 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200357 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200358
Chris Wilson337fa6e2019-04-26 09:17:20 +0100359 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200360}
361
Ville Syrjäläf4998962015-03-10 17:02:21 +0200362#define FW_WM(value, plane) \
363 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
364
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200365static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300366{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200367 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300368 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300369
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100370 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200371 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
372 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
373 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200374 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200375 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
376 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
377 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200378 } else if (IS_PINEVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200379 val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200380 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
381 if (enable)
382 val |= PINEVIEW_SELF_REFRESH_EN;
383 else
384 val &= ~PINEVIEW_SELF_REFRESH_EN;
Jani Nikula5f461662020-11-30 13:15:58 +0200385 intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
386 intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100387 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200388 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300389 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
390 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200391 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
392 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100393 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300394 /*
395 * FIXME can't find a bit like this for 915G, and
396 * and yet it does have the related watermark in
397 * FW_BLC_SELF. What's going on?
398 */
Jani Nikula5f461662020-11-30 13:15:58 +0200399 was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300400 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
401 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200402 intel_uncore_write(&dev_priv->uncore, INSTPM, val);
403 intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300404 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200405 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300406 }
407
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200408 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
409
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300410 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
411 enableddisabled(enable),
412 enableddisabled(was_enabled));
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200413
414 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300415}
416
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300417/**
418 * intel_set_memory_cxsr - Configure CxSR state
419 * @dev_priv: i915 device
420 * @enable: Allow vs. disallow CxSR
421 *
422 * Allow or disallow the system to enter a special CxSR
423 * (C-state self refresh) state. What typically happens in CxSR mode
424 * is that several display FIFOs may get combined into a single larger
425 * FIFO for a particular plane (so called max FIFO mode) to allow the
426 * system to defer memory fetches longer, and the memory will enter
427 * self refresh.
428 *
429 * Note that enabling CxSR does not guarantee that the system enter
430 * this special mode, nor does it guarantee that the system stays
431 * in that mode once entered. So this just allows/disallows the system
432 * to autonomously utilize the CxSR mode. Other factors such as core
433 * C-states will affect when/if the system actually enters/exits the
434 * CxSR mode.
435 *
436 * Note that on VLV/CHV this actually only controls the max FIFO mode,
437 * and the system is free to enter/exit memory self refresh at any time
438 * even when the use of CxSR has been disallowed.
439 *
440 * While the system is actually in the CxSR/max FIFO mode, some plane
441 * control registers will not get latched on vblank. Thus in order to
442 * guarantee the system will respond to changes in the plane registers
443 * we must always disallow CxSR prior to making changes to those registers.
444 * Unfortunately the system will re-evaluate the CxSR conditions at
445 * frame start which happens after vblank start (which is when the plane
446 * registers would get latched), so we can't proceed with the plane update
447 * during the same frame where we disallowed CxSR.
448 *
449 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
450 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
451 * the hardware w.r.t. HPLL SR when writing to plane registers.
452 * Disallowing just CxSR is sufficient.
453 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200454bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200455{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200456 bool ret;
457
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200458 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200459 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300460 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
461 dev_priv->wm.vlv.cxsr = enable;
462 else if (IS_G4X(dev_priv))
463 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200464 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200465
466 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200467}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200468
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300469/*
470 * Latency for FIFO fetches is dependent on several factors:
471 * - memory configuration (speed, channels)
472 * - chipset
473 * - current MCH state
474 * It can be fairly high in some situations, so here we assume a fairly
475 * pessimal value. It's a tradeoff between extra memory fetches (if we
476 * set this value too high, the FIFO will fetch frequently to stay full)
477 * and power consumption (set it too low to save power and we might see
478 * FIFO underruns and display "flicker").
479 *
480 * A value of 5us seems to be a good balance; safe for very low end
481 * platforms but not overly aggressive on lower latency configs.
482 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100483static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300484
Ville Syrjäläb5004722015-03-05 21:19:47 +0200485#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
486 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
487
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200488static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200489{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100490 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200491 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200492 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200493 enum pipe pipe = crtc->pipe;
494 int sprite0_start, sprite1_start;
Kees Cook2713eb42020-02-20 16:05:17 -0800495 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200496
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200497 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200498 case PIPE_A:
Jani Nikula5f461662020-11-30 13:15:58 +0200499 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
500 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200501 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
502 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
503 break;
504 case PIPE_B:
Jani Nikula5f461662020-11-30 13:15:58 +0200505 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
506 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200507 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
508 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
509 break;
510 case PIPE_C:
Jani Nikula5f461662020-11-30 13:15:58 +0200511 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
512 dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200513 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
514 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
515 break;
516 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200517 MISSING_CASE(pipe);
518 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200519 }
520
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200521 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
522 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
523 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
524 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200525}
526
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200527static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
528 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300529{
Jani Nikula5f461662020-11-30 13:15:58 +0200530 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531 int size;
532
533 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200534 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300535 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
536
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300537 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
538 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300539
540 return size;
541}
542
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200543static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
544 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545{
Jani Nikula5f461662020-11-30 13:15:58 +0200546 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300547 int size;
548
549 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200550 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300551 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
552 size >>= 1; /* Convert to cachelines */
553
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300554 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
555 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300556
557 return size;
558}
559
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200560static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
561 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300562{
Jani Nikula5f461662020-11-30 13:15:58 +0200563 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300564 int size;
565
566 size = dsparb & 0x7f;
567 size >>= 2; /* Convert to cachelines */
568
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300569 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
570 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300571
572 return size;
573}
574
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300575/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800576static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300577 .fifo_size = PINEVIEW_DISPLAY_FIFO,
578 .max_wm = PINEVIEW_MAX_WM,
579 .default_wm = PINEVIEW_DFT_WM,
580 .guard_size = PINEVIEW_GUARD_WM,
581 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300582};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800583
584static const struct intel_watermark_params pnv_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300585 .fifo_size = PINEVIEW_DISPLAY_FIFO,
586 .max_wm = PINEVIEW_MAX_WM,
587 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
588 .guard_size = PINEVIEW_GUARD_WM,
589 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300590};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800591
592static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300593 .fifo_size = PINEVIEW_CURSOR_FIFO,
594 .max_wm = PINEVIEW_CURSOR_MAX_WM,
595 .default_wm = PINEVIEW_CURSOR_DFT_WM,
596 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
597 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300598};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800599
600static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300601 .fifo_size = PINEVIEW_CURSOR_FIFO,
602 .max_wm = PINEVIEW_CURSOR_MAX_WM,
603 .default_wm = PINEVIEW_CURSOR_DFT_WM,
604 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
605 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300606};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800607
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300608static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300609 .fifo_size = I965_CURSOR_FIFO,
610 .max_wm = I965_CURSOR_MAX_WM,
611 .default_wm = I965_CURSOR_DFT_WM,
612 .guard_size = 2,
613 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300614};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800615
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300616static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300617 .fifo_size = I945_FIFO_SIZE,
618 .max_wm = I915_MAX_WM,
619 .default_wm = 1,
620 .guard_size = 2,
621 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300622};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800623
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300624static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300625 .fifo_size = I915_FIFO_SIZE,
626 .max_wm = I915_MAX_WM,
627 .default_wm = 1,
628 .guard_size = 2,
629 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300630};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800631
Ville Syrjälä9d539102014-08-15 01:21:53 +0300632static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300633 .fifo_size = I855GM_FIFO_SIZE,
634 .max_wm = I915_MAX_WM,
635 .default_wm = 1,
636 .guard_size = 2,
637 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300638};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800639
Ville Syrjälä9d539102014-08-15 01:21:53 +0300640static const struct intel_watermark_params i830_bc_wm_info = {
641 .fifo_size = I855GM_FIFO_SIZE,
642 .max_wm = I915_MAX_WM/2,
643 .default_wm = 1,
644 .guard_size = 2,
645 .cacheline_size = I830_FIFO_LINE_SIZE,
646};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800647
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200648static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300649 .fifo_size = I830_FIFO_SIZE,
650 .max_wm = I915_MAX_WM,
651 .default_wm = 1,
652 .guard_size = 2,
653 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300654};
655
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300656/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300657 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
658 * @pixel_rate: Pipe pixel rate in kHz
659 * @cpp: Plane bytes per pixel
660 * @latency: Memory wakeup latency in 0.1us units
661 *
662 * Compute the watermark using the method 1 or "small buffer"
663 * formula. The caller may additonally add extra cachelines
664 * to account for TLB misses and clock crossings.
665 *
666 * This method is concerned with the short term drain rate
667 * of the FIFO, ie. it does not account for blanking periods
668 * which would effectively reduce the average drain rate across
669 * a longer period. The name "small" refers to the fact the
670 * FIFO is relatively small compared to the amount of data
671 * fetched.
672 *
673 * The FIFO level vs. time graph might look something like:
674 *
675 * |\ |\
676 * | \ | \
677 * __---__---__ (- plane active, _ blanking)
678 * -> time
679 *
680 * or perhaps like this:
681 *
682 * |\|\ |\|\
683 * __----__----__ (- plane active, _ blanking)
684 * -> time
685 *
686 * Returns:
687 * The watermark in bytes
688 */
689static unsigned int intel_wm_method1(unsigned int pixel_rate,
690 unsigned int cpp,
691 unsigned int latency)
692{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200693 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300694
Ville Syrjäläd492a292019-04-08 18:27:01 +0300695 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300696 ret = DIV_ROUND_UP_ULL(ret, 10000);
697
698 return ret;
699}
700
701/**
702 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
703 * @pixel_rate: Pipe pixel rate in kHz
704 * @htotal: Pipe horizontal total
705 * @width: Plane width in pixels
706 * @cpp: Plane bytes per pixel
707 * @latency: Memory wakeup latency in 0.1us units
708 *
709 * Compute the watermark using the method 2 or "large buffer"
710 * formula. The caller may additonally add extra cachelines
711 * to account for TLB misses and clock crossings.
712 *
713 * This method is concerned with the long term drain rate
714 * of the FIFO, ie. it does account for blanking periods
715 * which effectively reduce the average drain rate across
716 * a longer period. The name "large" refers to the fact the
717 * FIFO is relatively large compared to the amount of data
718 * fetched.
719 *
720 * The FIFO level vs. time graph might look something like:
721 *
722 * |\___ |\___
723 * | \___ | \___
724 * | \ | \
725 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
726 * -> time
727 *
728 * Returns:
729 * The watermark in bytes
730 */
731static unsigned int intel_wm_method2(unsigned int pixel_rate,
732 unsigned int htotal,
733 unsigned int width,
734 unsigned int cpp,
735 unsigned int latency)
736{
737 unsigned int ret;
738
739 /*
740 * FIXME remove once all users are computing
741 * watermarks in the correct place.
742 */
743 if (WARN_ON_ONCE(htotal == 0))
744 htotal = 1;
745
746 ret = (latency * pixel_rate) / (htotal * 10000);
747 ret = (ret + 1) * width * cpp;
748
749 return ret;
750}
751
752/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300753 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300754 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000756 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200757 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758 * @latency_ns: memory latency for the platform
759 *
760 * Calculate the watermark level (the level at which the display plane will
761 * start fetching from memory again). Each chip has a different display
762 * FIFO size and allocation, so the caller needs to figure that out and pass
763 * in the correct intel_watermark_params structure.
764 *
765 * As the pixel clock runs, the FIFO will be drained at a rate that depends
766 * on the pixel size. When it reaches the watermark level, it'll start
767 * fetching FIFO line sized based chunks from memory until the FIFO fills
768 * past the watermark point. If the FIFO drains completely, a FIFO underrun
769 * will occur, and a display engine hang could result.
770 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300771static unsigned int intel_calculate_wm(int pixel_rate,
772 const struct intel_watermark_params *wm,
773 int fifo_size, int cpp,
774 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300776 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300777
778 /*
779 * Note: we need to make sure we don't overflow for various clock &
780 * latency values.
781 * clocks go from a few thousand to several hundred thousand.
782 * latency is usually a few thousand
783 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300784 entries = intel_wm_method1(pixel_rate, cpp,
785 latency_ns / 100);
786 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
787 wm->guard_size;
788 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300789
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300790 wm_size = fifo_size - entries;
791 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300792
793 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300794 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300795 wm_size = wm->max_wm;
796 if (wm_size <= 0)
797 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300798
799 /*
800 * Bspec seems to indicate that the value shouldn't be lower than
801 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
802 * Lets go for 8 which is the burst size since certain platforms
803 * already use a hardcoded 8 (which is what the spec says should be
804 * done).
805 */
806 if (wm_size <= 8)
807 wm_size = 8;
808
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300809 return wm_size;
810}
811
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300812static bool is_disabling(int old, int new, int threshold)
813{
814 return old >= threshold && new < threshold;
815}
816
817static bool is_enabling(int old, int new, int threshold)
818{
819 return old < threshold && new >= threshold;
820}
821
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300822static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
823{
824 return dev_priv->wm.max_level + 1;
825}
826
Ville Syrjälä24304d812017-03-14 17:10:49 +0200827static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
828 const struct intel_plane_state *plane_state)
829{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100830 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200831
832 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100833 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200834 return false;
835
836 /*
837 * Treat cursor with fb as always visible since cursor updates
838 * can happen faster than the vrefresh rate, and the current
839 * watermark code doesn't handle that correctly. Cursor updates
840 * which set/clear the fb or change the cursor size are going
841 * to get throttled by intel_legacy_cursor_update() to work
842 * around this problem with the watermark code.
843 */
844 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100845 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200846 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100847 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200848}
849
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200850static bool intel_crtc_active(struct intel_crtc *crtc)
851{
852 /* Be paranoid as we can arrive here with only partial
853 * state retrieved from the hardware during setup.
854 *
855 * We can ditch the adjusted_mode.crtc_clock check as soon
856 * as Haswell has gained clock readout/fastboot support.
857 *
858 * We can ditch the crtc->primary->state->fb check as soon as we can
859 * properly reconstruct framebuffers.
860 *
861 * FIXME: The intel_crtc->active here should be switched to
862 * crtc->state->active once we have proper CRTC states wired up
863 * for atomic.
864 */
865 return crtc->active && crtc->base.primary->state->fb &&
866 crtc->config->hw.adjusted_mode.crtc_clock;
867}
868
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200869static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300870{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200871 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300872
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200873 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200874 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300875 if (enabled)
876 return NULL;
877 enabled = crtc;
878 }
879 }
880
881 return enabled;
882}
883
Dave Airlieef9c66a2021-09-29 01:57:47 +0300884static void pnv_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200886 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887 const struct cxsr_latency *latency;
888 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300889 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300890
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000891 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100892 dev_priv->is_ddr3,
893 dev_priv->fsb_freq,
894 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300895 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300896 drm_dbg_kms(&dev_priv->drm,
897 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300898 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300899 return;
900 }
901
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200902 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300903 if (crtc) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200904 const struct drm_display_mode *pipe_mode =
905 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200906 const struct drm_framebuffer *fb =
907 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200908 int cpp = fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200909 int clock = pipe_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300910
911 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800912 wm = intel_calculate_wm(clock, &pnv_display_wm,
913 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200914 cpp, latency->display_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200915 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300916 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200917 reg |= FW_WM(wm, SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200918 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300919 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300920
921 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800922 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
923 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300924 4, latency->cursor_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200925 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300926 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200927 reg |= FW_WM(wm, CURSOR_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200928 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300929
930 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800931 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
932 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200933 cpp, latency->display_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200934 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300935 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200936 reg |= FW_WM(wm, HPLL_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200937 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938
939 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800940 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
941 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300942 4, latency->cursor_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200943 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300944 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200945 reg |= FW_WM(wm, HPLL_CURSOR);
Jani Nikula5f461662020-11-30 13:15:58 +0200946 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300947 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300948
Imre Deak5209b1f2014-07-01 12:36:17 +0300949 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300950 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300951 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300952 }
953}
954
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300955/*
956 * Documentation says:
957 * "If the line size is small, the TLB fetches can get in the way of the
958 * data fetches, causing some lag in the pixel data return which is not
959 * accounted for in the above formulas. The following adjustment only
960 * needs to be applied if eight whole lines fit in the buffer at once.
961 * The WM is adjusted upwards by the difference between the FIFO size
962 * and the size of 8 whole lines. This adjustment is always performed
963 * in the actual pixel depth regardless of whether FBC is enabled or not."
964 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000965static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300966{
967 int tlb_miss = fifo_size * 64 - width * cpp * 8;
968
969 return max(0, tlb_miss);
970}
971
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300972static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
973 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300974{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300975 enum pipe pipe;
976
977 for_each_pipe(dev_priv, pipe)
978 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
979
Jani Nikula5f461662020-11-30 13:15:58 +0200980 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300981 FW_WM(wm->sr.plane, SR) |
982 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
983 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
984 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200985 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300986 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
987 FW_WM(wm->sr.fbc, FBC_SR) |
988 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
989 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
990 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
991 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200992 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300993 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
994 FW_WM(wm->sr.cursor, CURSOR_SR) |
995 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
996 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300997
Jani Nikula5f461662020-11-30 13:15:58 +0200998 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300999}
1000
Ville Syrjälä15665972015-03-10 16:16:28 +02001001#define FW_WM_VLV(value, plane) \
1002 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1003
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001004static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001005 const struct vlv_wm_values *wm)
1006{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001007 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001008
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001009 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001010 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1011
Jani Nikula5f461662020-11-30 13:15:58 +02001012 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001013 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1014 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1015 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1016 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1017 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001018
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001019 /*
1020 * Zero the (unused) WM1 watermarks, and also clear all the
1021 * high order bits so that there are no out of bounds values
1022 * present in the registers during the reprogramming.
1023 */
Jani Nikula5f461662020-11-30 13:15:58 +02001024 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1025 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1026 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1027 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1028 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001029
Jani Nikula5f461662020-11-30 13:15:58 +02001030 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001031 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001032 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1033 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1034 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001035 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001036 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1037 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1038 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001039 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001040 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001041
1042 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02001043 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001044 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1045 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001046 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001047 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1048 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Jani Nikula5f461662020-11-30 13:15:58 +02001049 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001050 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1051 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Jani Nikula5f461662020-11-30 13:15:58 +02001052 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001053 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001054 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1055 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1056 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1057 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1058 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1059 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1060 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1061 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1062 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001063 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02001064 intel_uncore_write(&dev_priv->uncore, DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001065 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1066 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001067 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001068 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001069 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1070 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1071 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1072 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1073 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1074 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001075 }
1076
Jani Nikula5f461662020-11-30 13:15:58 +02001077 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001078}
1079
Ville Syrjälä15665972015-03-10 16:16:28 +02001080#undef FW_WM_VLV
1081
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001082static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1083{
1084 /* all latencies in usec */
1085 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1086 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001087 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001088
Ville Syrjälä79d94302017-04-21 21:14:30 +03001089 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001090}
1091
1092static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1093{
1094 /*
1095 * DSPCNTR[13] supposedly controls whether the
1096 * primary plane can use the FIFO space otherwise
1097 * reserved for the sprite plane. It's not 100% clear
1098 * what the actual FIFO size is, but it looks like we
1099 * can happily set both primary and sprite watermarks
1100 * up to 127 cachelines. So that would seem to mean
1101 * that either DSPCNTR[13] doesn't do anything, or that
1102 * the total FIFO is >= 256 cachelines in size. Either
1103 * way, we don't seem to have to worry about this
1104 * repartitioning as the maximum watermark value the
1105 * register can hold for each plane is lower than the
1106 * minimum FIFO size.
1107 */
1108 switch (plane_id) {
1109 case PLANE_CURSOR:
1110 return 63;
1111 case PLANE_PRIMARY:
1112 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1113 case PLANE_SPRITE0:
1114 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1115 default:
1116 MISSING_CASE(plane_id);
1117 return 0;
1118 }
1119}
1120
1121static int g4x_fbc_fifo_size(int level)
1122{
1123 switch (level) {
1124 case G4X_WM_LEVEL_SR:
1125 return 7;
1126 case G4X_WM_LEVEL_HPLL:
1127 return 15;
1128 default:
1129 MISSING_CASE(level);
1130 return 0;
1131 }
1132}
1133
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001134static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1135 const struct intel_plane_state *plane_state,
1136 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001137{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001138 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001139 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001140 const struct drm_display_mode *pipe_mode =
1141 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001142 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1143 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001144
1145 if (latency == 0)
1146 return USHRT_MAX;
1147
1148 if (!intel_wm_plane_visible(crtc_state, plane_state))
1149 return 0;
1150
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001151 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001152
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001153 /*
Ville Syrjälä52913622021-05-14 15:57:41 +03001154 * WaUse32BppForSRWM:ctg,elk
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001155 *
Ville Syrjälä52913622021-05-14 15:57:41 +03001156 * The spec fails to list this restriction for the
1157 * HPLL watermark, which seems a little strange.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001158 * Let's use 32bpp for the HPLL watermark as well.
1159 */
Ville Syrjälä52913622021-05-14 15:57:41 +03001160 if (plane->id == PLANE_PRIMARY &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001161 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001162 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001163
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001164 clock = pipe_mode->crtc_clock;
1165 htotal = pipe_mode->crtc_htotal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001166
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001167 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001168
1169 if (plane->id == PLANE_CURSOR) {
1170 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1171 } else if (plane->id == PLANE_PRIMARY &&
1172 level == G4X_WM_LEVEL_NORMAL) {
1173 wm = intel_wm_method1(clock, cpp, latency);
1174 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001175 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001176
1177 small = intel_wm_method1(clock, cpp, latency);
1178 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1179
1180 wm = min(small, large);
1181 }
1182
1183 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1184 width, cpp);
1185
1186 wm = DIV_ROUND_UP(wm, 64) + 2;
1187
Chris Wilson1a1f1282017-11-07 14:03:38 +00001188 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001189}
1190
1191static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1192 int level, enum plane_id plane_id, u16 value)
1193{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001194 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001195 bool dirty = false;
1196
1197 for (; level < intel_wm_num_levels(dev_priv); level++) {
1198 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1199
1200 dirty |= raw->plane[plane_id] != value;
1201 raw->plane[plane_id] = value;
1202 }
1203
1204 return dirty;
1205}
1206
1207static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1208 int level, u16 value)
1209{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001210 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001211 bool dirty = false;
1212
1213 /* NORMAL level doesn't have an FBC watermark */
1214 level = max(level, G4X_WM_LEVEL_SR);
1215
1216 for (; level < intel_wm_num_levels(dev_priv); level++) {
1217 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1218
1219 dirty |= raw->fbc != value;
1220 raw->fbc = value;
1221 }
1222
1223 return dirty;
1224}
1225
Maarten Lankhorstec193642019-06-28 10:55:17 +02001226static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1227 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001228 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001229
1230static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1231 const struct intel_plane_state *plane_state)
1232{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001233 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001234 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001235 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1236 enum plane_id plane_id = plane->id;
1237 bool dirty = false;
1238 int level;
1239
1240 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1241 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1242 if (plane_id == PLANE_PRIMARY)
1243 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1244 goto out;
1245 }
1246
1247 for (level = 0; level < num_levels; level++) {
1248 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1249 int wm, max_wm;
1250
1251 wm = g4x_compute_wm(crtc_state, plane_state, level);
1252 max_wm = g4x_plane_fifo_size(plane_id, level);
1253
1254 if (wm > max_wm)
1255 break;
1256
1257 dirty |= raw->plane[plane_id] != wm;
1258 raw->plane[plane_id] = wm;
1259
1260 if (plane_id != PLANE_PRIMARY ||
1261 level == G4X_WM_LEVEL_NORMAL)
1262 continue;
1263
1264 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1265 raw->plane[plane_id]);
1266 max_wm = g4x_fbc_fifo_size(level);
1267
1268 /*
1269 * FBC wm is not mandatory as we
1270 * can always just disable its use.
1271 */
1272 if (wm > max_wm)
1273 wm = USHRT_MAX;
1274
1275 dirty |= raw->fbc != wm;
1276 raw->fbc = wm;
1277 }
1278
1279 /* mark watermarks as invalid */
1280 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1281
1282 if (plane_id == PLANE_PRIMARY)
1283 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1284
1285 out:
1286 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001287 drm_dbg_kms(&dev_priv->drm,
1288 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1289 plane->base.name,
1290 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1291 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1292 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001293
1294 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001295 drm_dbg_kms(&dev_priv->drm,
1296 "FBC watermarks: SR=%d, HPLL=%d\n",
1297 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1298 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001299 }
1300
1301 return dirty;
1302}
1303
1304static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1305 enum plane_id plane_id, int level)
1306{
1307 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1308
1309 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1310}
1311
1312static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1313 int level)
1314{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001315 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001316
1317 if (level > dev_priv->wm.max_level)
1318 return false;
1319
1320 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1321 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1322 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1323}
1324
1325/* mark all levels starting from 'level' as invalid */
1326static void g4x_invalidate_wms(struct intel_crtc *crtc,
1327 struct g4x_wm_state *wm_state, int level)
1328{
1329 if (level <= G4X_WM_LEVEL_NORMAL) {
1330 enum plane_id plane_id;
1331
1332 for_each_plane_id_on_crtc(crtc, plane_id)
1333 wm_state->wm.plane[plane_id] = USHRT_MAX;
1334 }
1335
1336 if (level <= G4X_WM_LEVEL_SR) {
1337 wm_state->cxsr = false;
1338 wm_state->sr.cursor = USHRT_MAX;
1339 wm_state->sr.plane = USHRT_MAX;
1340 wm_state->sr.fbc = USHRT_MAX;
1341 }
1342
1343 if (level <= G4X_WM_LEVEL_HPLL) {
1344 wm_state->hpll_en = false;
1345 wm_state->hpll.cursor = USHRT_MAX;
1346 wm_state->hpll.plane = USHRT_MAX;
1347 wm_state->hpll.fbc = USHRT_MAX;
1348 }
1349}
1350
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001351static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1352 int level)
1353{
1354 if (level < G4X_WM_LEVEL_SR)
1355 return false;
1356
1357 if (level >= G4X_WM_LEVEL_SR &&
1358 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1359 return false;
1360
1361 if (level >= G4X_WM_LEVEL_HPLL &&
1362 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1363 return false;
1364
1365 return true;
1366}
1367
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001368static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1369 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001370{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001371 struct intel_crtc_state *crtc_state =
1372 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001373 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001374 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001375 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001376 const struct intel_plane_state *old_plane_state;
1377 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001378 struct intel_plane *plane;
1379 enum plane_id plane_id;
1380 int i, level;
1381 unsigned int dirty = 0;
1382
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001383 for_each_oldnew_intel_plane_in_state(state, plane,
1384 old_plane_state,
1385 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001386 if (new_plane_state->hw.crtc != &crtc->base &&
1387 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001388 continue;
1389
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001390 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001391 dirty |= BIT(plane->id);
1392 }
1393
1394 if (!dirty)
1395 return 0;
1396
1397 level = G4X_WM_LEVEL_NORMAL;
1398 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1399 goto out;
1400
1401 raw = &crtc_state->wm.g4x.raw[level];
1402 for_each_plane_id_on_crtc(crtc, plane_id)
1403 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1404
1405 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001406 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1407 goto out;
1408
1409 raw = &crtc_state->wm.g4x.raw[level];
1410 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1411 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1412 wm_state->sr.fbc = raw->fbc;
1413
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001414 wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001415
1416 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001417 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1418 goto out;
1419
1420 raw = &crtc_state->wm.g4x.raw[level];
1421 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1422 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1423 wm_state->hpll.fbc = raw->fbc;
1424
1425 wm_state->hpll_en = wm_state->cxsr;
1426
1427 level++;
1428
1429 out:
1430 if (level == G4X_WM_LEVEL_NORMAL)
1431 return -EINVAL;
1432
1433 /* invalidate the higher levels */
1434 g4x_invalidate_wms(crtc, wm_state, level);
1435
1436 /*
1437 * Determine if the FBC watermark(s) can be used. IF
1438 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001439 * watermark(s) rather than disable the SR/HPLL
1440 * level(s) entirely. 'level-1' is the highest valid
1441 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001442 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001443 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001444
1445 return 0;
1446}
1447
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001448static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1449 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001450{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301451 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001452 struct intel_crtc_state *new_crtc_state =
1453 intel_atomic_get_new_crtc_state(state, crtc);
1454 const struct intel_crtc_state *old_crtc_state =
1455 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001456 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1457 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001458 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001459 enum plane_id plane_id;
1460
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001461 if (!new_crtc_state->hw.active ||
1462 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001463 *intermediate = *optimal;
1464
1465 intermediate->cxsr = false;
1466 intermediate->hpll_en = false;
1467 goto out;
1468 }
1469
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001470 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001471 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001472 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001473 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001474 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1475
1476 for_each_plane_id_on_crtc(crtc, plane_id) {
1477 intermediate->wm.plane[plane_id] =
1478 max(optimal->wm.plane[plane_id],
1479 active->wm.plane[plane_id]);
1480
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301481 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1482 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001483 }
1484
1485 intermediate->sr.plane = max(optimal->sr.plane,
1486 active->sr.plane);
1487 intermediate->sr.cursor = max(optimal->sr.cursor,
1488 active->sr.cursor);
1489 intermediate->sr.fbc = max(optimal->sr.fbc,
1490 active->sr.fbc);
1491
1492 intermediate->hpll.plane = max(optimal->hpll.plane,
1493 active->hpll.plane);
1494 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1495 active->hpll.cursor);
1496 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1497 active->hpll.fbc);
1498
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301499 drm_WARN_ON(&dev_priv->drm,
1500 (intermediate->sr.plane >
1501 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1502 intermediate->sr.cursor >
1503 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1504 intermediate->cxsr);
1505 drm_WARN_ON(&dev_priv->drm,
1506 (intermediate->sr.plane >
1507 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1508 intermediate->sr.cursor >
1509 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1510 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001511
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301512 drm_WARN_ON(&dev_priv->drm,
1513 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1514 intermediate->fbc_en && intermediate->cxsr);
1515 drm_WARN_ON(&dev_priv->drm,
1516 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1517 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001518
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001519out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001520 /*
1521 * If our intermediate WM are identical to the final WM, then we can
1522 * omit the post-vblank programming; only update if it's different.
1523 */
1524 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001525 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001526
1527 return 0;
1528}
1529
1530static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1531 struct g4x_wm_values *wm)
1532{
1533 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001534 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001535
1536 wm->cxsr = true;
1537 wm->hpll_en = true;
1538 wm->fbc_en = true;
1539
1540 for_each_intel_crtc(&dev_priv->drm, crtc) {
1541 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1542
1543 if (!crtc->active)
1544 continue;
1545
1546 if (!wm_state->cxsr)
1547 wm->cxsr = false;
1548 if (!wm_state->hpll_en)
1549 wm->hpll_en = false;
1550 if (!wm_state->fbc_en)
1551 wm->fbc_en = false;
1552
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001553 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001554 }
1555
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001556 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001557 wm->cxsr = false;
1558 wm->hpll_en = false;
1559 wm->fbc_en = false;
1560 }
1561
1562 for_each_intel_crtc(&dev_priv->drm, crtc) {
1563 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1564 enum pipe pipe = crtc->pipe;
1565
1566 wm->pipe[pipe] = wm_state->wm;
1567 if (crtc->active && wm->cxsr)
1568 wm->sr = wm_state->sr;
1569 if (crtc->active && wm->hpll_en)
1570 wm->hpll = wm_state->hpll;
1571 }
1572}
1573
1574static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1575{
1576 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1577 struct g4x_wm_values new_wm = {};
1578
1579 g4x_merge_wm(dev_priv, &new_wm);
1580
1581 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1582 return;
1583
1584 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1585 _intel_set_memory_cxsr(dev_priv, false);
1586
1587 g4x_write_wm_values(dev_priv, &new_wm);
1588
1589 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1590 _intel_set_memory_cxsr(dev_priv, true);
1591
1592 *old_wm = new_wm;
1593}
1594
1595static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001596 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001597{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001598 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1599 const struct intel_crtc_state *crtc_state =
1600 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001601
1602 mutex_lock(&dev_priv->wm.wm_mutex);
1603 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1604 g4x_program_watermarks(dev_priv);
1605 mutex_unlock(&dev_priv->wm.wm_mutex);
1606}
1607
1608static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001609 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001610{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001611 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1612 const struct intel_crtc_state *crtc_state =
1613 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001614
1615 if (!crtc_state->wm.need_postvbl_update)
1616 return;
1617
1618 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001619 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001620 g4x_program_watermarks(dev_priv);
1621 mutex_unlock(&dev_priv->wm.wm_mutex);
1622}
1623
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001624/* latency must be in 0.1us units. */
1625static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001626 unsigned int htotal,
1627 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001628 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001629 unsigned int latency)
1630{
1631 unsigned int ret;
1632
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001633 ret = intel_wm_method2(pixel_rate, htotal,
1634 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001635 ret = DIV_ROUND_UP(ret, 64);
1636
1637 return ret;
1638}
1639
Ville Syrjäläbb726512016-10-31 22:37:24 +02001640static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001641{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001642 /* all latencies in usec */
1643 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1644
Ville Syrjälä58590c12015-09-08 21:05:12 +03001645 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1646
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001647 if (IS_CHERRYVIEW(dev_priv)) {
1648 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1649 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001650
1651 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001652 }
1653}
1654
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001655static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1656 const struct intel_plane_state *plane_state,
1657 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001658{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001659 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001660 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001661 const struct drm_display_mode *pipe_mode =
1662 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001663 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001664
1665 if (dev_priv->wm.pri_latency[level] == 0)
1666 return USHRT_MAX;
1667
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001668 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001669 return 0;
1670
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001671 cpp = plane_state->hw.fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001672 clock = pipe_mode->crtc_clock;
1673 htotal = pipe_mode->crtc_htotal;
Ville Syrjäläe339d672016-11-28 19:37:17 +02001674 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001675
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001676 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001677 /*
1678 * FIXME the formula gives values that are
1679 * too big for the cursor FIFO, and hence we
1680 * would never be able to use cursors. For
1681 * now just hardcode the watermark.
1682 */
1683 wm = 63;
1684 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001685 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001686 dev_priv->wm.pri_latency[level] * 10);
1687 }
1688
Chris Wilson1a1f1282017-11-07 14:03:38 +00001689 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001690}
1691
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001692static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1693{
1694 return (active_planes & (BIT(PLANE_SPRITE0) |
1695 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1696}
1697
Ville Syrjälä5012e602017-03-02 19:14:56 +02001698static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001699{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001700 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301701 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001702 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001703 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001704 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001705 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001706 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001707 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001708 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001709 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001710 unsigned int total_rate;
1711 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001712
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001713 /*
1714 * When enabling sprite0 after sprite1 has already been enabled
1715 * we tend to get an underrun unless sprite0 already has some
1716 * FIFO space allcoated. Hence we always allocate at least one
1717 * cacheline for sprite0 whenever sprite1 is enabled.
1718 *
1719 * All other plane enable sequences appear immune to this problem.
1720 */
1721 if (vlv_need_sprite0_fifo_workaround(active_planes))
1722 sprite0_fifo_extra = 1;
1723
Ville Syrjälä5012e602017-03-02 19:14:56 +02001724 total_rate = raw->plane[PLANE_PRIMARY] +
1725 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001726 raw->plane[PLANE_SPRITE1] +
1727 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001728
Ville Syrjälä5012e602017-03-02 19:14:56 +02001729 if (total_rate > fifo_size)
1730 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001731
Ville Syrjälä5012e602017-03-02 19:14:56 +02001732 if (total_rate == 0)
1733 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001734
Ville Syrjälä5012e602017-03-02 19:14:56 +02001735 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001736 unsigned int rate;
1737
Ville Syrjälä5012e602017-03-02 19:14:56 +02001738 if ((active_planes & BIT(plane_id)) == 0) {
1739 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001740 continue;
1741 }
1742
Ville Syrjälä5012e602017-03-02 19:14:56 +02001743 rate = raw->plane[plane_id];
1744 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1745 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001746 }
1747
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001748 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1749 fifo_left -= sprite0_fifo_extra;
1750
Ville Syrjälä5012e602017-03-02 19:14:56 +02001751 fifo_state->plane[PLANE_CURSOR] = 63;
1752
1753 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001754
1755 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001756 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001757 int plane_extra;
1758
1759 if (fifo_left == 0)
1760 break;
1761
Ville Syrjälä5012e602017-03-02 19:14:56 +02001762 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001763 continue;
1764
1765 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001766 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001767 fifo_left -= plane_extra;
1768 }
1769
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301770 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001771
1772 /* give it all to the first plane if none are active */
1773 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301774 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001775 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1776 }
1777
1778 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001779}
1780
Ville Syrjäläff32c542017-03-02 19:14:57 +02001781/* mark all levels starting from 'level' as invalid */
1782static void vlv_invalidate_wms(struct intel_crtc *crtc,
1783 struct vlv_wm_state *wm_state, int level)
1784{
1785 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1786
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001787 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001788 enum plane_id plane_id;
1789
1790 for_each_plane_id_on_crtc(crtc, plane_id)
1791 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1792
1793 wm_state->sr[level].cursor = USHRT_MAX;
1794 wm_state->sr[level].plane = USHRT_MAX;
1795 }
1796}
1797
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001798static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1799{
1800 if (wm > fifo_size)
1801 return USHRT_MAX;
1802 else
1803 return fifo_size - wm;
1804}
1805
Ville Syrjäläff32c542017-03-02 19:14:57 +02001806/*
1807 * Starting from 'level' set all higher
1808 * levels to 'value' in the "raw" watermarks.
1809 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001810static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001811 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001812{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001813 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001814 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001815 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001816
Ville Syrjäläff32c542017-03-02 19:14:57 +02001817 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001818 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001819
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001820 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001821 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001822 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001823
1824 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001825}
1826
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001827static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1828 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001829{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001830 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001831 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001832 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001833 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001834 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001835 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001836
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001837 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001838 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1839 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001840 }
1841
1842 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001843 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001844 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1845 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1846
Ville Syrjäläff32c542017-03-02 19:14:57 +02001847 if (wm > max_wm)
1848 break;
1849
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001850 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001851 raw->plane[plane_id] = wm;
1852 }
1853
1854 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001855 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001856
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001857out:
1858 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001859 drm_dbg_kms(&dev_priv->drm,
1860 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1861 plane->base.name,
1862 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1863 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1864 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001865
1866 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001867}
1868
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001869static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1870 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001871{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001872 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001873 &crtc_state->wm.vlv.raw[level];
1874 const struct vlv_fifo_state *fifo_state =
1875 &crtc_state->wm.vlv.fifo_state;
1876
1877 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1878}
1879
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001880static 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 +02001881{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001882 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1883 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1884 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1885 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001886}
1887
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001888static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1889 struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001890{
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001891 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001892 struct intel_crtc_state *crtc_state =
1893 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001894 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001895 const struct vlv_fifo_state *fifo_state =
1896 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001897 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1898 int num_active_planes = hweight8(active_planes);
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001899 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001900 const struct intel_plane_state *old_plane_state;
1901 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001902 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903 enum plane_id plane_id;
1904 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001905 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001906
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001907 for_each_oldnew_intel_plane_in_state(state, plane,
1908 old_plane_state,
1909 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001910 if (new_plane_state->hw.crtc != &crtc->base &&
1911 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001912 continue;
1913
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001914 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001915 dirty |= BIT(plane->id);
1916 }
1917
1918 /*
1919 * DSPARB registers may have been reset due to the
1920 * power well being turned off. Make sure we restore
1921 * them to a consistent state even if no primary/sprite
1922 * planes are initially active.
1923 */
1924 if (needs_modeset)
1925 crtc_state->fifo_changed = true;
1926
1927 if (!dirty)
1928 return 0;
1929
1930 /* cursor changes don't warrant a FIFO recompute */
1931 if (dirty & ~BIT(PLANE_CURSOR)) {
1932 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001933 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001934 const struct vlv_fifo_state *old_fifo_state =
1935 &old_crtc_state->wm.vlv.fifo_state;
1936
1937 ret = vlv_compute_fifo(crtc_state);
1938 if (ret)
1939 return ret;
1940
1941 if (needs_modeset ||
1942 memcmp(old_fifo_state, fifo_state,
1943 sizeof(*fifo_state)) != 0)
1944 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001945 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001946
Ville Syrjäläff32c542017-03-02 19:14:57 +02001947 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001948 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001949 /*
1950 * Note that enabling cxsr with no primary/sprite planes
1951 * enabled can wedge the pipe. Hence we only allow cxsr
1952 * with exactly one enabled primary/sprite plane.
1953 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001954 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001955
Ville Syrjälä5012e602017-03-02 19:14:56 +02001956 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001957 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001958 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001959
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001960 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001961 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001962
Ville Syrjäläff32c542017-03-02 19:14:57 +02001963 for_each_plane_id_on_crtc(crtc, plane_id) {
1964 wm_state->wm[level].plane[plane_id] =
1965 vlv_invert_wm_value(raw->plane[plane_id],
1966 fifo_state->plane[plane_id]);
1967 }
1968
1969 wm_state->sr[level].plane =
1970 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001971 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001972 raw->plane[PLANE_SPRITE1]),
1973 sr_fifo_size);
1974
1975 wm_state->sr[level].cursor =
1976 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1977 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001978 }
1979
Ville Syrjäläff32c542017-03-02 19:14:57 +02001980 if (level == 0)
1981 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001982
Ville Syrjäläff32c542017-03-02 19:14:57 +02001983 /* limit to only levels we can actually handle */
1984 wm_state->num_levels = level;
1985
1986 /* invalidate the higher levels */
1987 vlv_invalidate_wms(crtc, wm_state, level);
1988
1989 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001990}
1991
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001992#define VLV_FIFO(plane, value) \
1993 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1994
Ville Syrjäläff32c542017-03-02 19:14:57 +02001995static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001996 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001997{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001998 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001999 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002000 const struct intel_crtc_state *crtc_state =
2001 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002002 const struct vlv_fifo_state *fifo_state =
2003 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002004 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002005 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002006
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002007 if (!crtc_state->fifo_changed)
2008 return;
2009
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002010 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2011 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2012 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002013
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302014 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2015 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002016
Ville Syrjäläc137d662017-03-02 19:15:06 +02002017 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2018
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002019 /*
2020 * uncore.lock serves a double purpose here. It allows us to
2021 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2022 * it protects the DSPARB registers from getting clobbered by
2023 * parallel updates from multiple pipes.
2024 *
2025 * intel_pipe_update_start() has already disabled interrupts
2026 * for us, so a plain spin_lock() is sufficient here.
2027 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002028 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002029
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002030 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002031 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002032 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2033 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002034
2035 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2036 VLV_FIFO(SPRITEB, 0xff));
2037 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2038 VLV_FIFO(SPRITEB, sprite1_start));
2039
2040 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2041 VLV_FIFO(SPRITEB_HI, 0x1));
2042 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2043 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2044
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002045 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2046 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002047 break;
2048 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002049 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2050 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002051
2052 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2053 VLV_FIFO(SPRITED, 0xff));
2054 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2055 VLV_FIFO(SPRITED, sprite1_start));
2056
2057 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2058 VLV_FIFO(SPRITED_HI, 0xff));
2059 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2060 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2061
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002062 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2063 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002064 break;
2065 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002066 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2067 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002068
2069 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2070 VLV_FIFO(SPRITEF, 0xff));
2071 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2072 VLV_FIFO(SPRITEF, sprite1_start));
2073
2074 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2075 VLV_FIFO(SPRITEF_HI, 0xff));
2076 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2077 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2078
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002079 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2080 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002081 break;
2082 default:
2083 break;
2084 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002085
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002086 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002087
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002088 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002089}
2090
2091#undef VLV_FIFO
2092
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002093static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
2094 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002095{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002096 struct intel_crtc_state *new_crtc_state =
2097 intel_atomic_get_new_crtc_state(state, crtc);
2098 const struct intel_crtc_state *old_crtc_state =
2099 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002100 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2101 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002102 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002103 int level;
2104
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002105 if (!new_crtc_state->hw.active ||
2106 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002107 *intermediate = *optimal;
2108
2109 intermediate->cxsr = false;
2110 goto out;
2111 }
2112
Ville Syrjälä4841da52017-03-02 19:14:59 +02002113 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002114 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002115 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002116
2117 for (level = 0; level < intermediate->num_levels; level++) {
2118 enum plane_id plane_id;
2119
2120 for_each_plane_id_on_crtc(crtc, plane_id) {
2121 intermediate->wm[level].plane[plane_id] =
2122 min(optimal->wm[level].plane[plane_id],
2123 active->wm[level].plane[plane_id]);
2124 }
2125
2126 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2127 active->sr[level].plane);
2128 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2129 active->sr[level].cursor);
2130 }
2131
2132 vlv_invalidate_wms(crtc, intermediate, level);
2133
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002134out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002135 /*
2136 * If our intermediate WM are identical to the final WM, then we can
2137 * omit the post-vblank programming; only update if it's different.
2138 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002139 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002140 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002141
2142 return 0;
2143}
2144
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002145static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002146 struct vlv_wm_values *wm)
2147{
2148 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002149 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002150
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002151 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002152 wm->cxsr = true;
2153
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002154 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002155 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002156
2157 if (!crtc->active)
2158 continue;
2159
2160 if (!wm_state->cxsr)
2161 wm->cxsr = false;
2162
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002163 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002164 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2165 }
2166
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002167 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002168 wm->cxsr = false;
2169
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002170 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002171 wm->level = VLV_WM_LEVEL_PM2;
2172
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002173 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002174 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002175 enum pipe pipe = crtc->pipe;
2176
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002177 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002178 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002179 wm->sr = wm_state->sr[wm->level];
2180
Ville Syrjälä1b313892016-11-28 19:37:08 +02002181 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2182 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2183 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2184 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002185 }
2186}
2187
Ville Syrjäläff32c542017-03-02 19:14:57 +02002188static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002189{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002190 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2191 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002192
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002193 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002194
Ville Syrjäläff32c542017-03-02 19:14:57 +02002195 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002196 return;
2197
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002198 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002199 chv_set_memory_dvfs(dev_priv, false);
2200
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002201 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002202 chv_set_memory_pm5(dev_priv, false);
2203
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002204 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002205 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002206
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002207 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002208
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002209 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002210 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002211
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002212 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002213 chv_set_memory_pm5(dev_priv, true);
2214
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002215 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002216 chv_set_memory_dvfs(dev_priv, true);
2217
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002218 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002219}
2220
Ville Syrjäläff32c542017-03-02 19:14:57 +02002221static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002222 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002223{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002224 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2225 const struct intel_crtc_state *crtc_state =
2226 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002227
2228 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002229 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2230 vlv_program_watermarks(dev_priv);
2231 mutex_unlock(&dev_priv->wm.wm_mutex);
2232}
2233
2234static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002235 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002236{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002237 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2238 const struct intel_crtc_state *crtc_state =
2239 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002240
2241 if (!crtc_state->wm.need_postvbl_update)
2242 return;
2243
2244 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002245 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002246 vlv_program_watermarks(dev_priv);
2247 mutex_unlock(&dev_priv->wm.wm_mutex);
2248}
2249
Dave Airlieef9c66a2021-09-29 01:57:47 +03002250static void i965_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002251{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002252 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002253 int srwm = 1;
2254 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002255 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002256
2257 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002258 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002259 if (crtc) {
2260 /* self-refresh has much higher latency */
2261 static const int sr_latency_ns = 12000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002262 const struct drm_display_mode *pipe_mode =
2263 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002264 const struct drm_framebuffer *fb =
2265 crtc->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002266 int clock = pipe_mode->crtc_clock;
2267 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002268 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002269 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002270 int entries;
2271
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002272 entries = intel_wm_method2(clock, htotal,
2273 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002274 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2275 srwm = I965_FIFO_SIZE - entries;
2276 if (srwm < 0)
2277 srwm = 1;
2278 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002279 drm_dbg_kms(&dev_priv->drm,
2280 "self-refresh entries: %d, wm: %d\n",
2281 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002282
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002283 entries = intel_wm_method2(clock, htotal,
2284 crtc->base.cursor->state->crtc_w, 4,
2285 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002286 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002287 i965_cursor_wm_info.cacheline_size) +
2288 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002289
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002290 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002291 if (cursor_sr > i965_cursor_wm_info.max_wm)
2292 cursor_sr = i965_cursor_wm_info.max_wm;
2293
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002294 drm_dbg_kms(&dev_priv->drm,
2295 "self-refresh watermark: display plane %d "
2296 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002297
Imre Deak98584252014-06-13 14:54:20 +03002298 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002299 } else {
Imre Deak98584252014-06-13 14:54:20 +03002300 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002301 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002302 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002303 }
2304
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002305 drm_dbg_kms(&dev_priv->drm,
2306 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2307 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002308
2309 /* 965 has limitations... */
Jani Nikula5f461662020-11-30 13:15:58 +02002310 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002311 FW_WM(8, CURSORB) |
2312 FW_WM(8, PLANEB) |
2313 FW_WM(8, PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02002314 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002315 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002316 /* update cursor SR watermark */
Jani Nikula5f461662020-11-30 13:15:58 +02002317 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002318
2319 if (cxsr_enabled)
2320 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002321}
2322
Ville Syrjäläf4998962015-03-10 17:02:21 +02002323#undef FW_WM
2324
Dave Airlieef9c66a2021-09-29 01:57:47 +03002325static void i9xx_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002326{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002327 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002328 u32 fwater_lo;
2329 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002330 int cwm, srwm = 1;
2331 int fifo_size;
2332 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002333 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002334
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002335 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002336 wm_info = &i945_wm_info;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002337 else if (DISPLAY_VER(dev_priv) != 2)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002338 wm_info = &i915_wm_info;
2339 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002340 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341
Dave Airlie758b2fc2021-09-29 01:57:46 +03002342 if (DISPLAY_VER(dev_priv) == 2)
2343 fifo_size = i830_get_fifo_size(dev_priv, PLANE_A);
2344 else
2345 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_A);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002346 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)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002348 const struct drm_display_mode *pipe_mode =
2349 &crtc->config->hw.pipe_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 Marchi93e7e612021-04-12 22:09:53 -07002354 if (DISPLAY_VER(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
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002359 planea_wm = intel_calculate_wm(pipe_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 Marchi93e7e612021-04-12 22:09:53 -07002369 if (DISPLAY_VER(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
Dave Airlie758b2fc2021-09-29 01:57:46 +03002372 if (DISPLAY_VER(dev_priv) == 2)
2373 fifo_size = i830_get_fifo_size(dev_priv, PLANE_B);
2374 else
2375 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_B);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002376 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002377 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002378 const struct drm_display_mode *pipe_mode =
2379 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002380 const struct drm_framebuffer *fb =
2381 crtc->base.primary->state->fb;
2382 int cpp;
2383
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002384 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002385 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002386 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002387 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002388
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002389 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002390 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002391 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002392 if (enabled == NULL)
2393 enabled = crtc;
2394 else
2395 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002396 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002397 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002398 if (planeb_wm > (long)wm_info->max_wm)
2399 planeb_wm = wm_info->max_wm;
2400 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002401
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002402 drm_dbg_kms(&dev_priv->drm,
2403 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002404
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002405 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002406 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002407
Ville Syrjäläefc26112016-10-31 22:37:04 +02002408 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002409
2410 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002411 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002412 enabled = NULL;
2413 }
2414
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002415 /*
2416 * Overlay gets an aggressive default since video jitter is bad.
2417 */
2418 cwm = 2;
2419
2420 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002421 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002422
2423 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002424 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002425 /* self-refresh has much higher latency */
2426 static const int sr_latency_ns = 6000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002427 const struct drm_display_mode *pipe_mode =
2428 &enabled->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002429 const struct drm_framebuffer *fb =
2430 enabled->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002431 int clock = pipe_mode->crtc_clock;
2432 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002433 int hdisplay = enabled->config->pipe_src_w;
2434 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002435 int entries;
2436
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002437 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002438 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002439 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002440 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002441
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002442 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2443 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002444 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002445 drm_dbg_kms(&dev_priv->drm,
2446 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002447 srwm = wm_info->fifo_size - entries;
2448 if (srwm < 0)
2449 srwm = 1;
2450
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002451 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02002452 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002453 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002454 else
Jani Nikula5f461662020-11-30 13:15:58 +02002455 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002456 }
2457
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002458 drm_dbg_kms(&dev_priv->drm,
2459 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2460 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002461
2462 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2463 fwater_hi = (cwm & 0x1f);
2464
2465 /* Set request length to 8 cachelines per fetch */
2466 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2467 fwater_hi = fwater_hi | (1 << 8);
2468
Jani Nikula5f461662020-11-30 13:15:58 +02002469 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2470 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002471
Imre Deak5209b1f2014-07-01 12:36:17 +03002472 if (enabled)
2473 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002474}
2475
Dave Airlieef9c66a2021-09-29 01:57:47 +03002476static void i845_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002477{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002478 struct intel_crtc *crtc;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002479 const struct drm_display_mode *pipe_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002480 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002481 int planea_wm;
2482
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002483 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002484 if (crtc == NULL)
2485 return;
2486
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002487 pipe_mode = &crtc->config->hw.pipe_mode;
2488 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002489 &i845_wm_info,
Dave Airlie758b2fc2021-09-29 01:57:46 +03002490 i845_get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002491 4, pessimal_latency_ns);
Jani Nikula5f461662020-11-30 13:15:58 +02002492 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002493 fwater_lo |= (3<<8) | planea_wm;
2494
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002495 drm_dbg_kms(&dev_priv->drm,
2496 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002497
Jani Nikula5f461662020-11-30 13:15:58 +02002498 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002499}
2500
Ville Syrjälä37126462013-08-01 16:18:55 +03002501/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002502static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2503 unsigned int cpp,
2504 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002505{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002506 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002507
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002508 ret = intel_wm_method1(pixel_rate, cpp, latency);
2509 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002510
2511 return ret;
2512}
2513
Ville Syrjälä37126462013-08-01 16:18:55 +03002514/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002515static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2516 unsigned int htotal,
2517 unsigned int width,
2518 unsigned int cpp,
2519 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002520{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002521 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002522
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002523 ret = intel_wm_method2(pixel_rate, htotal,
2524 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002525 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002526
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002527 return ret;
2528}
2529
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002530static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002531{
Matt Roper15126882015-12-03 11:37:40 -08002532 /*
2533 * Neither of these should be possible since this function shouldn't be
2534 * called if the CRTC is off or the plane is invisible. But let's be
2535 * extra paranoid to avoid a potential divide-by-zero if we screw up
2536 * elsewhere in the driver.
2537 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002538 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002539 return 0;
2540 if (WARN_ON(!horiz_pixels))
2541 return 0;
2542
Ville Syrjäläac484962016-01-20 21:05:26 +02002543 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002544}
2545
Imre Deak820c1982013-12-17 14:46:36 +02002546struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002547 u16 pri;
2548 u16 spr;
2549 u16 cur;
2550 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002551};
2552
Ville Syrjälä37126462013-08-01 16:18:55 +03002553/*
2554 * For both WM_PIPE and WM_LP.
2555 * mem_value must be in 0.1us units.
2556 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002557static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2558 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002559 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002560{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002561 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002562 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002563
Ville Syrjälä03981c62018-11-14 19:34:40 +02002564 if (mem_value == 0)
2565 return U32_MAX;
2566
Maarten Lankhorstec193642019-06-28 10:55:17 +02002567 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002568 return 0;
2569
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002570 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002571
Maarten Lankhorstec193642019-06-28 10:55:17 +02002572 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002573
2574 if (!is_lp)
2575 return method1;
2576
Maarten Lankhorstec193642019-06-28 10:55:17 +02002577 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002578 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002579 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002580 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002581
2582 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002583}
2584
Ville Syrjälä37126462013-08-01 16:18:55 +03002585/*
2586 * For both WM_PIPE and WM_LP.
2587 * mem_value must be in 0.1us units.
2588 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002589static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2590 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002591 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002592{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002593 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002594 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002595
Ville Syrjälä03981c62018-11-14 19:34:40 +02002596 if (mem_value == 0)
2597 return U32_MAX;
2598
Maarten Lankhorstec193642019-06-28 10:55:17 +02002599 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002600 return 0;
2601
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002602 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002603
Maarten Lankhorstec193642019-06-28 10:55:17 +02002604 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2605 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002606 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002607 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002608 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002609 return min(method1, method2);
2610}
2611
Ville Syrjälä37126462013-08-01 16:18:55 +03002612/*
2613 * For both WM_PIPE and WM_LP.
2614 * mem_value must be in 0.1us units.
2615 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002616static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2617 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002618 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002619{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002620 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002621
Ville Syrjälä03981c62018-11-14 19:34:40 +02002622 if (mem_value == 0)
2623 return U32_MAX;
2624
Maarten Lankhorstec193642019-06-28 10:55:17 +02002625 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002626 return 0;
2627
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002628 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002629
Maarten Lankhorstec193642019-06-28 10:55:17 +02002630 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002631 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002632 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002633 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002634}
2635
Paulo Zanonicca32e92013-05-31 11:45:06 -03002636/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002637static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2638 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002639 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002640{
Ville Syrjälä83054942016-11-18 21:53:00 +02002641 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002642
Maarten Lankhorstec193642019-06-28 10:55:17 +02002643 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002644 return 0;
2645
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002646 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002647
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002648 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2649 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002650}
2651
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002652static unsigned int
2653ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002654{
Matt Roper7dadd282021-03-19 21:42:43 -07002655 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002656 return 3072;
Matt Roper7dadd282021-03-19 21:42:43 -07002657 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002658 return 768;
2659 else
2660 return 512;
2661}
2662
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002663static unsigned int
2664ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2665 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002666{
Matt Roper7dadd282021-03-19 21:42:43 -07002667 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002668 /* BDW primary/sprite plane watermarks */
2669 return level == 0 ? 255 : 2047;
Matt Roper7dadd282021-03-19 21:42:43 -07002670 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002671 /* IVB/HSW primary/sprite plane watermarks */
2672 return level == 0 ? 127 : 1023;
2673 else if (!is_sprite)
2674 /* ILK/SNB primary plane watermarks */
2675 return level == 0 ? 127 : 511;
2676 else
2677 /* ILK/SNB sprite plane watermarks */
2678 return level == 0 ? 63 : 255;
2679}
2680
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002681static unsigned int
2682ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002683{
Matt Roper7dadd282021-03-19 21:42:43 -07002684 if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002685 return level == 0 ? 63 : 255;
2686 else
2687 return level == 0 ? 31 : 63;
2688}
2689
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002690static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002691{
Matt Roper7dadd282021-03-19 21:42:43 -07002692 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002693 return 31;
2694 else
2695 return 15;
2696}
2697
Ville Syrjälä158ae642013-08-07 13:28:19 +03002698/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002699static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002700 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002701 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002702 enum intel_ddb_partitioning ddb_partitioning,
2703 bool is_sprite)
2704{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002705 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002706
2707 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002708 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002709 return 0;
2710
2711 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002712 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002713 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002714
2715 /*
2716 * For some reason the non self refresh
2717 * FIFO size is only half of the self
2718 * refresh FIFO size on ILK/SNB.
2719 */
Matt Roper7dadd282021-03-19 21:42:43 -07002720 if (DISPLAY_VER(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002721 fifo_size /= 2;
2722 }
2723
Ville Syrjälä240264f2013-08-07 13:29:12 +03002724 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002725 /* level 0 is always calculated with 1:1 split */
2726 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2727 if (is_sprite)
2728 fifo_size *= 5;
2729 fifo_size /= 6;
2730 } else {
2731 fifo_size /= 2;
2732 }
2733 }
2734
2735 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002736 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002737}
2738
2739/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002740static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002741 int level,
2742 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002743{
2744 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002745 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002746 return 64;
2747
2748 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002749 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002750}
2751
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002752static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002753 int level,
2754 const struct intel_wm_config *config,
2755 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002756 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002757{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002758 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2759 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2760 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2761 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002762}
2763
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002764static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002765 int level,
2766 struct ilk_wm_maximums *max)
2767{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002768 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2769 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2770 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2771 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002772}
2773
Ville Syrjäläd9395652013-10-09 19:18:10 +03002774static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002775 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002776 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002777{
2778 bool ret;
2779
2780 /* already determined to be invalid? */
2781 if (!result->enable)
2782 return false;
2783
2784 result->enable = result->pri_val <= max->pri &&
2785 result->spr_val <= max->spr &&
2786 result->cur_val <= max->cur;
2787
2788 ret = result->enable;
2789
2790 /*
2791 * HACK until we can pre-compute everything,
2792 * and thus fail gracefully if LP0 watermarks
2793 * are exceeded...
2794 */
2795 if (level == 0 && !result->enable) {
2796 if (result->pri_val > max->pri)
2797 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2798 level, result->pri_val, max->pri);
2799 if (result->spr_val > max->spr)
2800 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2801 level, result->spr_val, max->spr);
2802 if (result->cur_val > max->cur)
2803 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2804 level, result->cur_val, max->cur);
2805
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002806 result->pri_val = min_t(u32, result->pri_val, max->pri);
2807 result->spr_val = min_t(u32, result->spr_val, max->spr);
2808 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002809 result->enable = true;
2810 }
2811
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002812 return ret;
2813}
2814
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002815static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002816 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002817 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002818 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002819 const struct intel_plane_state *pristate,
2820 const struct intel_plane_state *sprstate,
2821 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002822 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002823{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002824 u16 pri_latency = dev_priv->wm.pri_latency[level];
2825 u16 spr_latency = dev_priv->wm.spr_latency[level];
2826 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002827
2828 /* WM1+ latency values stored in 0.5us units */
2829 if (level > 0) {
2830 pri_latency *= 5;
2831 spr_latency *= 5;
2832 cur_latency *= 5;
2833 }
2834
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002835 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002836 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002837 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002838 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002839 }
2840
2841 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002842 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002843
2844 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002845 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002846
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002847 result->enable = true;
2848}
2849
Ville Syrjäläbb726512016-10-31 22:37:24 +02002850static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002851 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002852{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002853 struct intel_uncore *uncore = &dev_priv->uncore;
2854
Matt Roper7dadd282021-03-19 21:42:43 -07002855 if (DISPLAY_VER(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002856 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002857 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002858 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roperd3252e12021-08-20 15:57:10 -07002859 int mult = IS_DG2(dev_priv) ? 2 : 1;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002860
2861 /* read the first set of memory latencies[0:3] */
2862 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002863 ret = sandybridge_pcode_read(dev_priv,
2864 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002865 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002866
2867 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002868 drm_err(&dev_priv->drm,
2869 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002870 return;
2871 }
2872
Matt Roperd3252e12021-08-20 15:57:10 -07002873 wm[0] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2874 wm[1] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2875 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2876 wm[2] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2877 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2878 wm[3] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2879 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002880
2881 /* read the second set of memory latencies[4:7] */
2882 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002883 ret = sandybridge_pcode_read(dev_priv,
2884 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002885 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002886 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002887 drm_err(&dev_priv->drm,
2888 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002889 return;
2890 }
2891
Matt Roperd3252e12021-08-20 15:57:10 -07002892 wm[4] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2893 wm[5] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2894 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2895 wm[6] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2896 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2897 wm[7] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2898 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002899
Vandana Kannan367294b2014-11-04 17:06:46 +00002900 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002901 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2902 * need to be disabled. We make sure to sanitize the values out
2903 * of the punit to satisfy this requirement.
2904 */
2905 for (level = 1; level <= max_level; level++) {
2906 if (wm[level] == 0) {
2907 for (i = level + 1; i <= max_level; i++)
2908 wm[i] = 0;
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002909
2910 max_level = level - 1;
2911
Paulo Zanoni0727e402016-09-22 18:00:30 -03002912 break;
2913 }
2914 }
2915
2916 /*
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002917 * WaWmMemoryReadLatency
Damien Lespiau6f972352015-02-09 19:33:07 +00002918 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002919 * punit doesn't take into account the read latency so we need
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002920 * to add proper adjustement to each valid level we retrieve
2921 * from the punit when level 0 response data is 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002922 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002923 if (wm[0] == 0) {
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002924 u8 adjust = DISPLAY_VER(dev_priv) >= 12 ? 3 : 2;
2925
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002926 for (level = 0; level <= max_level; level++)
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002927 wm[level] += adjust;
Paulo Zanoni0727e402016-09-22 18:00:30 -03002928 }
2929
Mahesh Kumar86b59282018-08-31 16:39:42 +05302930 /*
2931 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2932 * If we could not get dimm info enable this WA to prevent from
2933 * any underrun. If not able to get Dimm info assume 16GB dimm
2934 * to avoid any underrun.
2935 */
José Roberto de Souza66a24502021-01-28 08:43:12 -08002936 if (dev_priv->dram_info.wm_lv_0_adjust_needed)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302937 wm[0] += 1;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002938 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002939 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002940
2941 wm[0] = (sskpd >> 56) & 0xFF;
2942 if (wm[0] == 0)
2943 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002944 wm[1] = (sskpd >> 4) & 0xFF;
2945 wm[2] = (sskpd >> 12) & 0xFF;
2946 wm[3] = (sskpd >> 20) & 0x1FF;
2947 wm[4] = (sskpd >> 32) & 0x1FF;
Matt Roper7dadd282021-03-19 21:42:43 -07002948 } else if (DISPLAY_VER(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002949 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002950
2951 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2952 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2953 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2954 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Matt Roper7dadd282021-03-19 21:42:43 -07002955 } else if (DISPLAY_VER(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002956 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002957
2958 /* ILK primary LP0 latency is 700 ns */
2959 wm[0] = 7;
2960 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2961 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002962 } else {
2963 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002964 }
2965}
2966
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002967static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002968 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002969{
2970 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002971 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002972 wm[0] = 13;
2973}
2974
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002975static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002976 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002977{
2978 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002979 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002980 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002981}
2982
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002983int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002984{
2985 /* how many WM levels are we expecting */
Matt Roper7959ffe2021-05-18 17:06:11 -07002986 if (HAS_HW_SAGV_WM(dev_priv))
2987 return 5;
2988 else if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002989 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002990 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002991 return 4;
Matt Roper7dadd282021-03-19 21:42:43 -07002992 else if (DISPLAY_VER(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002993 return 3;
2994 else
2995 return 2;
2996}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002997
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002998static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002999 const char *name,
Linus Torvaldse7c6e402021-04-27 17:05:53 -07003000 const u16 wm[])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003001{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003002 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003003
3004 for (level = 0; level <= max_level; level++) {
3005 unsigned int latency = wm[level];
3006
3007 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003008 drm_dbg_kms(&dev_priv->drm,
3009 "%s WM%d latency not provided\n",
3010 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003011 continue;
3012 }
3013
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003014 /*
3015 * - latencies are in us on gen9.
3016 * - before then, WM1+ latency values are in 0.5us units
3017 */
Matt Roper7dadd282021-03-19 21:42:43 -07003018 if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003019 latency *= 10;
3020 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003021 latency *= 5;
3022
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003023 drm_dbg_kms(&dev_priv->drm,
3024 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3025 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003026 }
3027}
3028
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003029static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003030 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003031{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003032 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003033
3034 if (wm[0] >= min)
3035 return false;
3036
3037 wm[0] = max(wm[0], min);
3038 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003039 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003040
3041 return true;
3042}
3043
Ville Syrjäläbb726512016-10-31 22:37:24 +02003044static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003045{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003046 bool changed;
3047
3048 /*
3049 * The BIOS provided WM memory latency values are often
3050 * inadequate for high resolution displays. Adjust them.
3051 */
3052 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3053 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3054 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3055
3056 if (!changed)
3057 return;
3058
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003059 drm_dbg_kms(&dev_priv->drm,
3060 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003061 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3062 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3063 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003064}
3065
Ville Syrjälä03981c62018-11-14 19:34:40 +02003066static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3067{
3068 /*
3069 * On some SNB machines (Thinkpad X220 Tablet at least)
3070 * LP3 usage can cause vblank interrupts to be lost.
3071 * The DEIIR bit will go high but it looks like the CPU
3072 * never gets interrupted.
3073 *
3074 * It's not clear whether other interrupt source could
3075 * be affected or if this is somehow limited to vblank
3076 * interrupts only. To play it safe we disable LP3
3077 * watermarks entirely.
3078 */
3079 if (dev_priv->wm.pri_latency[3] == 0 &&
3080 dev_priv->wm.spr_latency[3] == 0 &&
3081 dev_priv->wm.cur_latency[3] == 0)
3082 return;
3083
3084 dev_priv->wm.pri_latency[3] = 0;
3085 dev_priv->wm.spr_latency[3] = 0;
3086 dev_priv->wm.cur_latency[3] = 0;
3087
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003088 drm_dbg_kms(&dev_priv->drm,
3089 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003090 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3091 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3092 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3093}
3094
Ville Syrjäläbb726512016-10-31 22:37:24 +02003095static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003096{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003097 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003098
3099 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3100 sizeof(dev_priv->wm.pri_latency));
3101 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3102 sizeof(dev_priv->wm.pri_latency));
3103
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003104 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003105 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003106
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003107 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3108 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3109 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003110
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003111 if (DISPLAY_VER(dev_priv) == 6) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003112 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003113 snb_wm_lp3_irq_quirk(dev_priv);
3114 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003115}
3116
Ville Syrjäläbb726512016-10-31 22:37:24 +02003117static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003118{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003119 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003120 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003121}
3122
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003123static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003124 struct intel_pipe_wm *pipe_wm)
3125{
3126 /* LP0 watermark maximums depend on this pipe alone */
3127 const struct intel_wm_config config = {
3128 .num_pipes_active = 1,
3129 .sprites_enabled = pipe_wm->sprites_enabled,
3130 .sprites_scaled = pipe_wm->sprites_scaled,
3131 };
3132 struct ilk_wm_maximums max;
3133
3134 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003135 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003136
3137 /* At least LP0 must be valid */
3138 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003139 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003140 return false;
3141 }
3142
3143 return true;
3144}
3145
Matt Roper261a27d2015-10-08 15:28:25 -07003146/* Compute new watermarks for the pipe */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003147static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
3148 struct intel_crtc *crtc)
Matt Roper261a27d2015-10-08 15:28:25 -07003149{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003150 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3151 struct intel_crtc_state *crtc_state =
3152 intel_atomic_get_new_crtc_state(state, crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003153 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003154 struct intel_plane *plane;
3155 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003156 const struct intel_plane_state *pristate = NULL;
3157 const struct intel_plane_state *sprstate = NULL;
3158 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003159 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003160 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003161
Maarten Lankhorstec193642019-06-28 10:55:17 +02003162 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003163
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003164 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3165 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3166 pristate = plane_state;
3167 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3168 sprstate = plane_state;
3169 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3170 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003171 }
3172
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003173 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003174 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003175 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3176 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3177 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3178 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003179 }
3180
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003181 usable_level = max_level;
3182
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003183 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Matt Roper7dadd282021-03-19 21:42:43 -07003184 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003185 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003186
3187 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003188 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003189 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003190
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003191 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003192 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003193 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003194
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003195 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003196 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003197
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003198 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003199
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003200 for (level = 1; level <= usable_level; level++) {
3201 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003202
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003203 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003204 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003205
3206 /*
3207 * Disable any watermark level that exceeds the
3208 * register maximums since such watermarks are
3209 * always invalid.
3210 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003211 if (!ilk_validate_wm_level(level, &max, wm)) {
3212 memset(wm, 0, sizeof(*wm));
3213 break;
3214 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003215 }
3216
Matt Roper86c8bbb2015-09-24 15:53:16 -07003217 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003218}
3219
3220/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003221 * Build a set of 'intermediate' watermark values that satisfy both the old
3222 * state and the new state. These can be programmed to the hardware
3223 * immediately.
3224 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003225static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
3226 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08003227{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003228 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3229 struct intel_crtc_state *new_crtc_state =
3230 intel_atomic_get_new_crtc_state(state, crtc);
3231 const struct intel_crtc_state *old_crtc_state =
3232 intel_atomic_get_old_crtc_state(state, crtc);
3233 struct intel_pipe_wm *a = &new_crtc_state->wm.ilk.intermediate;
3234 const struct intel_pipe_wm *b = &old_crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003235 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003236
3237 /*
3238 * Start with the final, target watermarks, then combine with the
3239 * currently active watermarks to get values that are safe both before
3240 * and after the vblank.
3241 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003242 *a = new_crtc_state->wm.ilk.optimal;
3243 if (!new_crtc_state->hw.active ||
3244 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) ||
3245 state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003246 return 0;
3247
Matt Ropered4a6a72016-02-23 17:20:13 -08003248 a->pipe_enabled |= b->pipe_enabled;
3249 a->sprites_enabled |= b->sprites_enabled;
3250 a->sprites_scaled |= b->sprites_scaled;
3251
3252 for (level = 0; level <= max_level; level++) {
3253 struct intel_wm_level *a_wm = &a->wm[level];
3254 const struct intel_wm_level *b_wm = &b->wm[level];
3255
3256 a_wm->enable &= b_wm->enable;
3257 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3258 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3259 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3260 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3261 }
3262
3263 /*
3264 * We need to make sure that these merged watermark values are
3265 * actually a valid configuration themselves. If they're not,
3266 * there's no safe way to transition from the old state to
3267 * the new state, so we need to fail the atomic transaction.
3268 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003269 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003270 return -EINVAL;
3271
3272 /*
3273 * If our intermediate WM are identical to the final WM, then we can
3274 * omit the post-vblank programming; only update if it's different.
3275 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003276 if (memcmp(a, &new_crtc_state->wm.ilk.optimal, sizeof(*a)) != 0)
3277 new_crtc_state->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003278
3279 return 0;
3280}
3281
3282/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003283 * Merge the watermarks from all active pipes for a specific level.
3284 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003285static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003286 int level,
3287 struct intel_wm_level *ret_wm)
3288{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003289 const struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003290
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003291 ret_wm->enable = true;
3292
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003293 for_each_intel_crtc(&dev_priv->drm, crtc) {
3294 const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003295 const struct intel_wm_level *wm = &active->wm[level];
3296
3297 if (!active->pipe_enabled)
3298 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003299
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003300 /*
3301 * The watermark values may have been used in the past,
3302 * so we must maintain them in the registers for some
3303 * time even if the level is now disabled.
3304 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003305 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003306 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003307
3308 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3309 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3310 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3311 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3312 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003313}
3314
3315/*
3316 * Merge all low power watermarks for all active pipes.
3317 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003318static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003319 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003320 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003321 struct intel_pipe_wm *merged)
3322{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003323 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003324 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003325
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003326 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Matt Roper7dadd282021-03-19 21:42:43 -07003327 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003328 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003329 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003330
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003331 /* ILK: FBC WM must be disabled always */
Matt Roper7dadd282021-03-19 21:42:43 -07003332 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003333
3334 /* merge each WM1+ level */
3335 for (level = 1; level <= max_level; level++) {
3336 struct intel_wm_level *wm = &merged->wm[level];
3337
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003338 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003339
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003340 if (level > last_enabled_level)
3341 wm->enable = false;
3342 else if (!ilk_validate_wm_level(level, max, wm))
3343 /* make sure all following levels get disabled */
3344 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003345
3346 /*
3347 * The spec says it is preferred to disable
3348 * FBC WMs instead of disabling a WM level.
3349 */
3350 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003351 if (wm->enable)
3352 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003353 wm->fbc_val = 0;
3354 }
3355 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003356
3357 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3358 /*
3359 * FIXME this is racy. FBC might get enabled later.
3360 * What we should check here is whether FBC can be
3361 * enabled sometime later.
3362 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003363 if (DISPLAY_VER(dev_priv) == 5 && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003364 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003365 for (level = 2; level <= max_level; level++) {
3366 struct intel_wm_level *wm = &merged->wm[level];
3367
3368 wm->enable = false;
3369 }
3370 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003371}
3372
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003373static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3374{
3375 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3376 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3377}
3378
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003379/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003380static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3381 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003382{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003383 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003384 return 2 * level;
3385 else
3386 return dev_priv->wm.pri_latency[level];
3387}
3388
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003389static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003390 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003391 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003392 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003393{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003394 struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003395 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003396
Ville Syrjälä0362c782013-10-09 19:17:57 +03003397 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003398 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003399
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003400 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003401 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003402 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003403
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003404 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003405
Ville Syrjälä0362c782013-10-09 19:17:57 +03003406 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003407
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003408 /*
3409 * Maintain the watermark values even if the level is
3410 * disabled. Doing otherwise could cause underruns.
3411 */
3412 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003413 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003414 (r->pri_val << WM1_LP_SR_SHIFT) |
3415 r->cur_val;
3416
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003417 if (r->enable)
3418 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3419
Matt Roper7dadd282021-03-19 21:42:43 -07003420 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003421 results->wm_lp[wm_lp - 1] |=
3422 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3423 else
3424 results->wm_lp[wm_lp - 1] |=
3425 r->fbc_val << WM1_LP_FBC_SHIFT;
3426
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003427 /*
3428 * Always set WM1S_LP_EN when spr_val != 0, even if the
3429 * level is disabled. Doing otherwise could cause underruns.
3430 */
Matt Roper7dadd282021-03-19 21:42:43 -07003431 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303432 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003433 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3434 } else
3435 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003436 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003437
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003438 /* LP0 register values */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003439 for_each_intel_crtc(&dev_priv->drm, crtc) {
3440 enum pipe pipe = crtc->pipe;
3441 const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003442 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003443
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303444 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003445 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003446
3447 results->wm_pipe[pipe] =
3448 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3449 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3450 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003451 }
3452}
3453
Paulo Zanoni861f3382013-05-31 10:19:21 -03003454/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3455 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003456static struct intel_pipe_wm *
3457ilk_find_best_result(struct drm_i915_private *dev_priv,
3458 struct intel_pipe_wm *r1,
3459 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003460{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003461 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003462 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003463
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003464 for (level = 1; level <= max_level; level++) {
3465 if (r1->wm[level].enable)
3466 level1 = level;
3467 if (r2->wm[level].enable)
3468 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003469 }
3470
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003471 if (level1 == level2) {
3472 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003473 return r2;
3474 else
3475 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003476 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003477 return r1;
3478 } else {
3479 return r2;
3480 }
3481}
3482
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003483/* dirty bits used to track which watermarks need changes */
3484#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003485#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3486#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3487#define WM_DIRTY_FBC (1 << 24)
3488#define WM_DIRTY_DDB (1 << 25)
3489
Damien Lespiau055e3932014-08-18 13:49:10 +01003490static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003491 const struct ilk_wm_values *old,
3492 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003493{
3494 unsigned int dirty = 0;
3495 enum pipe pipe;
3496 int wm_lp;
3497
Damien Lespiau055e3932014-08-18 13:49:10 +01003498 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003499 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3500 dirty |= WM_DIRTY_PIPE(pipe);
3501 /* Must disable LP1+ watermarks too */
3502 dirty |= WM_DIRTY_LP_ALL;
3503 }
3504 }
3505
3506 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3507 dirty |= WM_DIRTY_FBC;
3508 /* Must disable LP1+ watermarks too */
3509 dirty |= WM_DIRTY_LP_ALL;
3510 }
3511
3512 if (old->partitioning != new->partitioning) {
3513 dirty |= WM_DIRTY_DDB;
3514 /* Must disable LP1+ watermarks too */
3515 dirty |= WM_DIRTY_LP_ALL;
3516 }
3517
3518 /* LP1+ watermarks already deemed dirty, no need to continue */
3519 if (dirty & WM_DIRTY_LP_ALL)
3520 return dirty;
3521
3522 /* Find the lowest numbered LP1+ watermark in need of an update... */
3523 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3524 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3525 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3526 break;
3527 }
3528
3529 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3530 for (; wm_lp <= 3; wm_lp++)
3531 dirty |= WM_DIRTY_LP(wm_lp);
3532
3533 return dirty;
3534}
3535
Ville Syrjälä8553c182013-12-05 15:51:39 +02003536static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3537 unsigned int dirty)
3538{
Imre Deak820c1982013-12-17 14:46:36 +02003539 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003540 bool changed = false;
3541
3542 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3543 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003544 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003545 changed = true;
3546 }
3547 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3548 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003549 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003550 changed = true;
3551 }
3552 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3553 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003554 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003555 changed = true;
3556 }
3557
3558 /*
3559 * Don't touch WM1S_LP_EN here.
3560 * Doing so could cause underruns.
3561 */
3562
3563 return changed;
3564}
3565
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003566/*
3567 * The spec says we shouldn't write when we don't need, because every write
3568 * causes WMs to be re-evaluated, expending some power.
3569 */
Imre Deak820c1982013-12-17 14:46:36 +02003570static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3571 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003572{
Imre Deak820c1982013-12-17 14:46:36 +02003573 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003574 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003575 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003576
Damien Lespiau055e3932014-08-18 13:49:10 +01003577 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003578 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003579 return;
3580
Ville Syrjälä8553c182013-12-05 15:51:39 +02003581 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003582
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003583 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Jani Nikula5f461662020-11-30 13:15:58 +02003584 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003585 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Jani Nikula5f461662020-11-30 13:15:58 +02003586 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003587 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Jani Nikula5f461662020-11-30 13:15:58 +02003588 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003589
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003590 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003591 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02003592 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003593 if (results->partitioning == INTEL_DDB_PART_1_2)
3594 val &= ~WM_MISC_DATA_PARTITION_5_6;
3595 else
3596 val |= WM_MISC_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003597 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003598 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02003599 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003600 if (results->partitioning == INTEL_DDB_PART_1_2)
3601 val &= ~DISP_DATA_PARTITION_5_6;
3602 else
3603 val |= DISP_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003604 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003605 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003606 }
3607
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003608 if (dirty & WM_DIRTY_FBC) {
Jani Nikula5f461662020-11-30 13:15:58 +02003609 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003610 if (results->enable_fbc_wm)
3611 val &= ~DISP_FBC_WM_DIS;
3612 else
3613 val |= DISP_FBC_WM_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02003614 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003615 }
3616
Imre Deak954911e2013-12-17 14:46:34 +02003617 if (dirty & WM_DIRTY_LP(1) &&
3618 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003619 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
Imre Deak954911e2013-12-17 14:46:34 +02003620
Matt Roper7dadd282021-03-19 21:42:43 -07003621 if (DISPLAY_VER(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003622 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003623 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003624 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003625 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003626 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003627
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003628 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003629 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003630 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003631 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003632 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003633 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003634
3635 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003636}
3637
Ville Syrjälä60aca572019-11-27 21:05:51 +02003638bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003639{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003640 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3641}
3642
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003643u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303644{
Ville Syrjäläb88da662021-04-16 20:10:09 +03003645 u8 enabled_slices = 0;
3646 enum dbuf_slice slice;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303647
Ville Syrjäläb88da662021-04-16 20:10:09 +03003648 for_each_dbuf_slice(dev_priv, slice) {
3649 if (intel_uncore_read(&dev_priv->uncore,
3650 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3651 enabled_slices |= BIT(slice);
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003652 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303653
Ville Syrjäläb88da662021-04-16 20:10:09 +03003654 return enabled_slices;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303655}
3656
Matt Roper024c9042015-09-24 15:53:11 -07003657/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003658 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3659 * so assume we'll always need it in order to avoid underruns.
3660 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003661static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003662{
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003663 return DISPLAY_VER(dev_priv) == 9;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003664}
3665
Paulo Zanoni56feca92016-09-22 18:00:28 -03003666static bool
3667intel_has_sagv(struct drm_i915_private *dev_priv)
3668{
Matt Roper70bfb302021-04-07 13:39:45 -07003669 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003670 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003671}
3672
James Ausmusb068a862019-10-09 10:23:14 -07003673static void
3674skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3675{
Matt Roper7dadd282021-03-19 21:42:43 -07003676 if (DISPLAY_VER(dev_priv) >= 12) {
James Ausmusda80f042019-10-09 10:23:15 -07003677 u32 val = 0;
3678 int ret;
3679
3680 ret = sandybridge_pcode_read(dev_priv,
3681 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3682 &val, NULL);
3683 if (!ret) {
3684 dev_priv->sagv_block_time_us = val;
3685 return;
3686 }
3687
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003688 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003689 } else if (DISPLAY_VER(dev_priv) == 11) {
James Ausmusb068a862019-10-09 10:23:14 -07003690 dev_priv->sagv_block_time_us = 10;
3691 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003692 } else if (DISPLAY_VER(dev_priv) == 10) {
James Ausmusb068a862019-10-09 10:23:14 -07003693 dev_priv->sagv_block_time_us = 20;
3694 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003695 } else if (DISPLAY_VER(dev_priv) == 9) {
James Ausmusb068a862019-10-09 10:23:14 -07003696 dev_priv->sagv_block_time_us = 30;
3697 return;
3698 } else {
Matt Roper7dadd282021-03-19 21:42:43 -07003699 MISSING_CASE(DISPLAY_VER(dev_priv));
James Ausmusb068a862019-10-09 10:23:14 -07003700 }
3701
3702 /* Default to an unusable block time */
3703 dev_priv->sagv_block_time_us = -1;
3704}
3705
Lyude656d1b82016-08-17 15:55:54 -04003706/*
3707 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3708 * depending on power and performance requirements. The display engine access
3709 * to system memory is blocked during the adjustment time. Because of the
3710 * blocking time, having this enabled can cause full system hangs and/or pipe
3711 * underruns if we don't meet all of the following requirements:
3712 *
3713 * - <= 1 pipe enabled
3714 * - All planes can enable watermarks for latencies >= SAGV engine block time
3715 * - We're not using an interlaced display configuration
3716 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003717static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003718intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003719{
3720 int ret;
3721
Paulo Zanoni56feca92016-09-22 18:00:28 -03003722 if (!intel_has_sagv(dev_priv))
3723 return 0;
3724
3725 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003726 return 0;
3727
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003728 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003729 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3730 GEN9_SAGV_ENABLE);
3731
Ville Syrjäläff61a972018-12-21 19:14:34 +02003732 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003733
3734 /*
3735 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003736 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003737 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003738 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003739 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003740 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003741 return 0;
3742 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003743 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003744 return ret;
3745 }
3746
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003747 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003748 return 0;
3749}
3750
Ville Syrjälä71024042020-09-25 15:17:48 +03003751static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003752intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003753{
Imre Deakb3b8e992016-12-05 18:27:38 +02003754 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003755
Paulo Zanoni56feca92016-09-22 18:00:28 -03003756 if (!intel_has_sagv(dev_priv))
3757 return 0;
3758
3759 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003760 return 0;
3761
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003762 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003763 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003764 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3765 GEN9_SAGV_DISABLE,
3766 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3767 1);
Lyude656d1b82016-08-17 15:55:54 -04003768 /*
3769 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003770 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003771 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003772 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003773 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003774 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003775 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003776 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003777 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003778 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003779 }
3780
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003781 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003782 return 0;
3783}
3784
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003785void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3786{
3787 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003788 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003789 const struct intel_bw_state *old_bw_state;
3790 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003791
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003792 /*
3793 * Just return if we can't control SAGV or don't have it.
3794 * This is different from situation when we have SAGV but just can't
3795 * afford it due to DBuf limitation - in case if SAGV is completely
3796 * disabled in a BIOS, we are not even allowed to send a PCode request,
3797 * as it will throw an error. So have to check it here.
3798 */
3799 if (!intel_has_sagv(dev_priv))
3800 return;
3801
3802 new_bw_state = intel_atomic_get_new_bw_state(state);
3803 if (!new_bw_state)
3804 return;
3805
Matt Roper7dadd282021-03-19 21:42:43 -07003806 if (DISPLAY_VER(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003807 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003808 return;
3809 }
3810
3811 old_bw_state = intel_atomic_get_old_bw_state(state);
3812 /*
3813 * Nothing to mask
3814 */
3815 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3816 return;
3817
3818 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3819
3820 /*
3821 * If new mask is zero - means there is nothing to mask,
3822 * we can only unmask, which should be done in unmask.
3823 */
3824 if (!new_mask)
3825 return;
3826
3827 /*
3828 * Restrict required qgv points before updating the configuration.
3829 * According to BSpec we can't mask and unmask qgv points at the same
3830 * time. Also masking should be done before updating the configuration
3831 * and unmasking afterwards.
3832 */
3833 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003834}
3835
3836void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3837{
3838 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003839 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003840 const struct intel_bw_state *old_bw_state;
3841 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003842
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003843 /*
3844 * Just return if we can't control SAGV or don't have it.
3845 * This is different from situation when we have SAGV but just can't
3846 * afford it due to DBuf limitation - in case if SAGV is completely
3847 * disabled in a BIOS, we are not even allowed to send a PCode request,
3848 * as it will throw an error. So have to check it here.
3849 */
3850 if (!intel_has_sagv(dev_priv))
3851 return;
3852
3853 new_bw_state = intel_atomic_get_new_bw_state(state);
3854 if (!new_bw_state)
3855 return;
3856
Matt Roper7dadd282021-03-19 21:42:43 -07003857 if (DISPLAY_VER(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003858 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003859 return;
3860 }
3861
3862 old_bw_state = intel_atomic_get_old_bw_state(state);
3863 /*
3864 * Nothing to unmask
3865 */
3866 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3867 return;
3868
3869 new_mask = new_bw_state->qgv_points_mask;
3870
3871 /*
3872 * Allow required qgv points after updating the configuration.
3873 * According to BSpec we can't mask and unmask qgv points at the same
3874 * time. Also masking should be done before updating the configuration
3875 * and unmasking afterwards.
3876 */
3877 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003878}
3879
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003880static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003881{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003882 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003883 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä9c312122020-11-06 19:30:40 +02003884 enum plane_id plane_id;
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003885 int max_level = INT_MAX;
Lyude656d1b82016-08-17 15:55:54 -04003886
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003887 if (!intel_has_sagv(dev_priv))
3888 return false;
3889
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003890 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003891 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003892
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02003893 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003894 return false;
3895
Ville Syrjälä9c312122020-11-06 19:30:40 +02003896 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003897 const struct skl_plane_wm *wm =
Ville Syrjälä9c312122020-11-06 19:30:40 +02003898 &crtc_state->wm.skl.optimal.planes[plane_id];
3899 int level;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003900
Lyude656d1b82016-08-17 15:55:54 -04003901 /* Skip this plane if it's not enabled */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003902 if (!wm->wm[0].enable)
Lyude656d1b82016-08-17 15:55:54 -04003903 continue;
3904
3905 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003906 for (level = ilk_wm_max_level(dev_priv);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003907 !wm->wm[level].enable; --level)
Lyude656d1b82016-08-17 15:55:54 -04003908 { }
3909
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003910 /* Highest common enabled wm level for all planes */
3911 max_level = min(level, max_level);
3912 }
3913
3914 /* No enabled planes? */
3915 if (max_level == INT_MAX)
3916 return true;
3917
3918 for_each_plane_id_on_crtc(crtc, plane_id) {
3919 const struct skl_plane_wm *wm =
3920 &crtc_state->wm.skl.optimal.planes[plane_id];
3921
Lyude656d1b82016-08-17 15:55:54 -04003922 /*
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003923 * All enabled planes must have enabled a common wm level that
3924 * can tolerate memory latencies higher than sagv_block_time_us
Lyude656d1b82016-08-17 15:55:54 -04003925 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003926 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
Lyude656d1b82016-08-17 15:55:54 -04003927 return false;
3928 }
3929
3930 return true;
3931}
3932
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003933static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3934{
3935 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3936 enum plane_id plane_id;
3937
3938 if (!crtc_state->hw.active)
3939 return true;
3940
3941 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003942 const struct skl_plane_wm *wm =
3943 &crtc_state->wm.skl.optimal.planes[plane_id];
3944
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003945 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003946 return false;
3947 }
3948
3949 return true;
3950}
3951
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003952static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3953{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003954 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3955 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3956
Matt Roper7dadd282021-03-19 21:42:43 -07003957 if (DISPLAY_VER(dev_priv) >= 12)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003958 return tgl_crtc_can_enable_sagv(crtc_state);
3959 else
3960 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003961}
3962
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003963bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3964 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003965{
Matt Roper7dadd282021-03-19 21:42:43 -07003966 if (DISPLAY_VER(dev_priv) < 11 &&
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003967 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003968 return false;
3969
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003970 return bw_state->pipe_sagv_reject == 0;
3971}
3972
3973static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3974{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003975 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003976 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003977 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003978 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003979 struct intel_bw_state *new_bw_state = NULL;
3980 const struct intel_bw_state *old_bw_state = NULL;
3981 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003982
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003983 for_each_new_intel_crtc_in_state(state, crtc,
3984 new_crtc_state, i) {
3985 new_bw_state = intel_atomic_get_bw_state(state);
3986 if (IS_ERR(new_bw_state))
3987 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003988
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003989 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003990
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003991 if (intel_crtc_can_enable_sagv(new_crtc_state))
3992 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
3993 else
3994 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
3995 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003996
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003997 if (!new_bw_state)
3998 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003999
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004000 new_bw_state->active_pipes =
4001 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03004002
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004003 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4004 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4005 if (ret)
4006 return ret;
4007 }
4008
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004009 for_each_new_intel_crtc_in_state(state, crtc,
4010 new_crtc_state, i) {
4011 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4012
4013 /*
4014 * We store use_sagv_wm in the crtc state rather than relying on
4015 * that bw state since we have no convenient way to get at the
4016 * latter from the plane commit hooks (especially in the legacy
4017 * cursor case)
4018 */
Matt Roper7959ffe2021-05-18 17:06:11 -07004019 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4020 DISPLAY_VER(dev_priv) >= 12 &&
4021 intel_can_enable_sagv(dev_priv, new_bw_state);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004022 }
4023
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004024 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4025 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004026 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4027 if (ret)
4028 return ret;
4029 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4030 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4031 if (ret)
4032 return ret;
4033 }
4034
4035 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004036}
4037
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004038static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4039{
Ville Syrjäläb88da662021-04-16 20:10:09 +03004040 return INTEL_INFO(dev_priv)->dbuf.size /
4041 hweight8(INTEL_INFO(dev_priv)->dbuf.slice_mask);
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004042}
4043
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004044static void
4045skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4046 struct skl_ddb_entry *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304047{
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004048 int slice_size = intel_dbuf_slice_size(dev_priv);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004049
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004050 if (!slice_mask) {
4051 ddb->start = 0;
4052 ddb->end = 0;
4053 return;
4054 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004055
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004056 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4057 ddb->end = fls(slice_mask) * slice_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004058
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004059 WARN_ON(ddb->start >= ddb->end);
Ville Syrjäläb88da662021-04-16 20:10:09 +03004060 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->dbuf.size);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004061}
4062
Ville Syrjälä835c1762021-05-18 17:06:16 -07004063static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
4064{
4065 struct skl_ddb_entry ddb;
4066
4067 if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
4068 slice_mask = BIT(DBUF_S1);
4069 else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
4070 slice_mask = BIT(DBUF_S3);
4071
4072 skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
4073
4074 return ddb.start;
4075}
4076
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004077u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4078 const struct skl_ddb_entry *entry)
4079{
Ville Syrjälä6390e5a2021-04-16 20:10:07 +03004080 int slice_size = intel_dbuf_slice_size(dev_priv);
4081 enum dbuf_slice start_slice, end_slice;
4082 u8 slice_mask = 0;
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004083
4084 if (!skl_ddb_entry_size(entry))
4085 return 0;
4086
4087 start_slice = entry->start / slice_size;
4088 end_slice = (entry->end - 1) / slice_size;
4089
4090 /*
4091 * Per plane DDB entry can in a really worst case be on multiple slices
4092 * but single entry is anyway contigious.
4093 */
4094 while (start_slice <= end_slice) {
4095 slice_mask |= BIT(start_slice);
4096 start_slice++;
4097 }
4098
4099 return slice_mask;
4100}
4101
Ville Syrjälä2791a402021-01-22 22:56:26 +02004102static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4103{
4104 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4105 int hdisplay, vdisplay;
4106
4107 if (!crtc_state->hw.active)
4108 return 0;
4109
4110 /*
4111 * Watermark/ddb requirement highly depends upon width of the
4112 * framebuffer, So instead of allocating DDB equally among pipes
4113 * distribute DDB based on resolution/width of the display.
4114 */
4115 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4116
4117 return hdisplay;
4118}
4119
Ville Syrjäläef79d622021-01-22 22:56:32 +02004120static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4121 enum pipe for_pipe,
4122 unsigned int *weight_start,
4123 unsigned int *weight_end,
4124 unsigned int *weight_total)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004125{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004126 struct drm_i915_private *dev_priv =
4127 to_i915(dbuf_state->base.state->base.dev);
4128 enum pipe pipe;
Ville Syrjälä53630962021-01-22 22:56:31 +02004129
4130 *weight_start = 0;
4131 *weight_end = 0;
4132 *weight_total = 0;
4133
Ville Syrjäläef79d622021-01-22 22:56:32 +02004134 for_each_pipe(dev_priv, pipe) {
4135 int weight = dbuf_state->weight[pipe];
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004136
4137 /*
4138 * Do not account pipes using other slice sets
4139 * luckily as of current BSpec slice sets do not partially
4140 * intersect(pipes share either same one slice or same slice set
4141 * i.e no partial intersection), so it is enough to check for
4142 * equality for now.
4143 */
Ville Syrjäläef79d622021-01-22 22:56:32 +02004144 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304145 continue;
4146
Ville Syrjälä53630962021-01-22 22:56:31 +02004147 *weight_total += weight;
Ville Syrjälä53630962021-01-22 22:56:31 +02004148 if (pipe < for_pipe) {
4149 *weight_start += weight;
4150 *weight_end += weight;
4151 } else if (pipe == for_pipe) {
4152 *weight_end += weight;
4153 }
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304154 }
Ville Syrjälä53630962021-01-22 22:56:31 +02004155}
4156
4157static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02004158skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
Ville Syrjälä53630962021-01-22 22:56:31 +02004159{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004160 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4161 unsigned int weight_total, weight_start, weight_end;
Ville Syrjälä53630962021-01-22 22:56:31 +02004162 const struct intel_dbuf_state *old_dbuf_state =
4163 intel_atomic_get_old_dbuf_state(state);
4164 struct intel_dbuf_state *new_dbuf_state =
4165 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004166 struct intel_crtc_state *crtc_state;
Ville Syrjälä53630962021-01-22 22:56:31 +02004167 struct skl_ddb_entry ddb_slices;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004168 enum pipe pipe = crtc->pipe;
Manasi Navaree2bebb92021-06-03 14:53:38 -07004169 unsigned int mbus_offset = 0;
Ville Syrjälä53630962021-01-22 22:56:31 +02004170 u32 ddb_range_size;
4171 u32 dbuf_slice_mask;
4172 u32 start, end;
4173 int ret;
4174
Ville Syrjäläef79d622021-01-22 22:56:32 +02004175 if (new_dbuf_state->weight[pipe] == 0) {
4176 new_dbuf_state->ddb[pipe].start = 0;
4177 new_dbuf_state->ddb[pipe].end = 0;
4178 goto out;
Ville Syrjälä53630962021-01-22 22:56:31 +02004179 }
4180
Ville Syrjäläef79d622021-01-22 22:56:32 +02004181 dbuf_slice_mask = new_dbuf_state->slices[pipe];
Ville Syrjälä53630962021-01-22 22:56:31 +02004182
4183 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
Ville Syrjälä835c1762021-05-18 17:06:16 -07004184 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_slice_mask);
Ville Syrjälä53630962021-01-22 22:56:31 +02004185 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4186
Ville Syrjäläef79d622021-01-22 22:56:32 +02004187 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4188 &weight_start, &weight_end, &weight_total);
Ville Syrjälä53630962021-01-22 22:56:31 +02004189
4190 start = ddb_range_size * weight_start / weight_total;
4191 end = ddb_range_size * weight_end / weight_total;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004192
Ville Syrjälä835c1762021-05-18 17:06:16 -07004193 new_dbuf_state->ddb[pipe].start = ddb_slices.start - mbus_offset + start;
4194 new_dbuf_state->ddb[pipe].end = ddb_slices.start - mbus_offset + end;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004195out:
Ville Syrjälä835c1762021-05-18 17:06:16 -07004196 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
4197 skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
Ville Syrjäläef79d622021-01-22 22:56:32 +02004198 &new_dbuf_state->ddb[pipe]))
4199 return 0;
4200
4201 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4202 if (ret)
4203 return ret;
4204
4205 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4206 if (IS_ERR(crtc_state))
4207 return PTR_ERR(crtc_state);
4208
Ville Syrjälä835c1762021-05-18 17:06:16 -07004209 /*
4210 * Used for checking overlaps, so we need absolute
4211 * offsets instead of MBUS relative offsets.
4212 */
4213 crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
4214 crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004215
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004216 drm_dbg_kms(&dev_priv->drm,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004217 "[CRTC:%d:%s] dbuf slices 0x%x -> 0x%x, ddb (%d - %d) -> (%d - %d), active pipes 0x%x -> 0x%x\n",
Ville Syrjälä53630962021-01-22 22:56:31 +02004218 crtc->base.base.id, crtc->base.name,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004219 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4220 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4221 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4222 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004223
4224 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004225}
4226
Ville Syrjälädf331de2019-03-19 18:03:11 +02004227static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4228 int width, const struct drm_format_info *format,
4229 u64 modifier, unsigned int rotation,
4230 u32 plane_pixel_rate, struct skl_wm_params *wp,
4231 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004232static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004233 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004234 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004235 const struct skl_wm_params *wp,
4236 const struct skl_wm_level *result_prev,
4237 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004238
Ville Syrjälädf331de2019-03-19 18:03:11 +02004239static unsigned int
4240skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4241 int num_active)
4242{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004243 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004244 int level, max_level = ilk_wm_max_level(dev_priv);
4245 struct skl_wm_level wm = {};
4246 int ret, min_ddb_alloc = 0;
4247 struct skl_wm_params wp;
4248
4249 ret = skl_compute_wm_params(crtc_state, 256,
4250 drm_format_info(DRM_FORMAT_ARGB8888),
4251 DRM_FORMAT_MOD_LINEAR,
4252 DRM_MODE_ROTATE_0,
4253 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304254 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004255
4256 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004257 unsigned int latency = dev_priv->wm.skl_latency[level];
4258
4259 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004260 if (wm.min_ddb_alloc == U16_MAX)
4261 break;
4262
4263 min_ddb_alloc = wm.min_ddb_alloc;
4264 }
4265
4266 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004267}
4268
Mahesh Kumar37cde112018-04-26 19:55:17 +05304269static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4270 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004271{
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004272 entry->start = reg & DDB_ENTRY_MASK;
4273 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304274
Damien Lespiau16160e32014-11-04 17:06:53 +00004275 if (entry->end)
4276 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004277}
4278
Mahesh Kumarddf34312018-04-09 09:11:03 +05304279static void
4280skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4281 const enum pipe pipe,
4282 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004283 struct skl_ddb_entry *ddb_y,
4284 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304285{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004286 u32 val, val2;
4287 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304288
4289 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4290 if (plane_id == PLANE_CURSOR) {
Jani Nikula5f461662020-11-30 13:15:58 +02004291 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004292 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304293 return;
4294 }
4295
Jani Nikula5f461662020-11-30 13:15:58 +02004296 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304297
4298 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004299 if (val & PLANE_CTL_ENABLE)
4300 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4301 val & PLANE_CTL_ORDER_RGBX,
4302 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304303
Matt Roper7dadd282021-03-19 21:42:43 -07004304 if (DISPLAY_VER(dev_priv) >= 11) {
Jani Nikula5f461662020-11-30 13:15:58 +02004305 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004306 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4307 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02004308 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4309 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304310
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004311 if (fourcc &&
4312 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004313 swap(val, val2);
4314
4315 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4316 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304317 }
4318}
4319
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004320void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4321 struct skl_ddb_entry *ddb_y,
4322 struct skl_ddb_entry *ddb_uv)
4323{
4324 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4325 enum intel_display_power_domain power_domain;
4326 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004327 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004328 enum plane_id plane_id;
4329
4330 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004331 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4332 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004333 return;
4334
4335 for_each_plane_id_on_crtc(crtc, plane_id)
4336 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4337 plane_id,
4338 &ddb_y[plane_id],
4339 &ddb_uv[plane_id]);
4340
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004341 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004342}
4343
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004344/*
4345 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4346 * The bspec defines downscale amount as:
4347 *
4348 * """
4349 * Horizontal down scale amount = maximum[1, Horizontal source size /
4350 * Horizontal destination size]
4351 * Vertical down scale amount = maximum[1, Vertical source size /
4352 * Vertical destination size]
4353 * Total down scale amount = Horizontal down scale amount *
4354 * Vertical down scale amount
4355 * """
4356 *
4357 * Return value is provided in 16.16 fixed point form to retain fractional part.
4358 * Caller should take care of dividing & rounding off the value.
4359 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304360static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004361skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4362 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004363{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304364 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004365 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304366 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4367 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004368
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304369 if (drm_WARN_ON(&dev_priv->drm,
4370 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304371 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004372
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004373 /*
4374 * Src coordinates are already rotated by 270 degrees for
4375 * the 90/270 degree plane rotation cases (to match the
4376 * GTT mapping), hence no need to account for rotation here.
4377 *
4378 * n.b., src is 16.16 fixed point, dst is whole integer.
4379 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004380 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4381 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4382 dst_w = drm_rect_width(&plane_state->uapi.dst);
4383 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004384
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304385 fp_w_ratio = div_fixed16(src_w, dst_w);
4386 fp_h_ratio = div_fixed16(src_h, dst_h);
4387 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4388 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004389
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304390 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004391}
4392
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004393struct dbuf_slice_conf_entry {
4394 u8 active_pipes;
4395 u8 dbuf_mask[I915_MAX_PIPES];
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004396 bool join_mbus;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004397};
4398
4399/*
4400 * Table taken from Bspec 12716
4401 * Pipes do have some preferred DBuf slice affinity,
4402 * plus there are some hardcoded requirements on how
4403 * those should be distributed for multipipe scenarios.
4404 * For more DBuf slices algorithm can get even more messy
4405 * and less readable, so decided to use a table almost
4406 * as is from BSpec itself - that way it is at least easier
4407 * to compare, change and check.
4408 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004409static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004410/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4411{
4412 {
4413 .active_pipes = BIT(PIPE_A),
4414 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004415 [PIPE_A] = BIT(DBUF_S1),
4416 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004417 },
4418 {
4419 .active_pipes = BIT(PIPE_B),
4420 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004421 [PIPE_B] = BIT(DBUF_S1),
4422 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004423 },
4424 {
4425 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4426 .dbuf_mask = {
4427 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004428 [PIPE_B] = BIT(DBUF_S2),
4429 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004430 },
4431 {
4432 .active_pipes = BIT(PIPE_C),
4433 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004434 [PIPE_C] = BIT(DBUF_S2),
4435 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004436 },
4437 {
4438 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4439 .dbuf_mask = {
4440 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004441 [PIPE_C] = BIT(DBUF_S2),
4442 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004443 },
4444 {
4445 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4446 .dbuf_mask = {
4447 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004448 [PIPE_C] = BIT(DBUF_S2),
4449 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004450 },
4451 {
4452 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4453 .dbuf_mask = {
4454 [PIPE_A] = BIT(DBUF_S1),
4455 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004456 [PIPE_C] = BIT(DBUF_S2),
4457 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004458 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004459 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004460};
4461
4462/*
4463 * Table taken from Bspec 49255
4464 * Pipes do have some preferred DBuf slice affinity,
4465 * plus there are some hardcoded requirements on how
4466 * those should be distributed for multipipe scenarios.
4467 * For more DBuf slices algorithm can get even more messy
4468 * and less readable, so decided to use a table almost
4469 * as is from BSpec itself - that way it is at least easier
4470 * to compare, change and check.
4471 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004472static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004473/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4474{
4475 {
4476 .active_pipes = BIT(PIPE_A),
4477 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004478 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4479 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004480 },
4481 {
4482 .active_pipes = BIT(PIPE_B),
4483 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004484 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4485 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004486 },
4487 {
4488 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4489 .dbuf_mask = {
4490 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004491 [PIPE_B] = BIT(DBUF_S1),
4492 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004493 },
4494 {
4495 .active_pipes = BIT(PIPE_C),
4496 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004497 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4498 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004499 },
4500 {
4501 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4502 .dbuf_mask = {
4503 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004504 [PIPE_C] = BIT(DBUF_S2),
4505 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004506 },
4507 {
4508 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4509 .dbuf_mask = {
4510 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004511 [PIPE_C] = BIT(DBUF_S2),
4512 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004513 },
4514 {
4515 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4516 .dbuf_mask = {
4517 [PIPE_A] = BIT(DBUF_S1),
4518 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004519 [PIPE_C] = BIT(DBUF_S2),
4520 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004521 },
4522 {
4523 .active_pipes = BIT(PIPE_D),
4524 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004525 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4526 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004527 },
4528 {
4529 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4530 .dbuf_mask = {
4531 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004532 [PIPE_D] = BIT(DBUF_S2),
4533 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004534 },
4535 {
4536 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4537 .dbuf_mask = {
4538 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004539 [PIPE_D] = BIT(DBUF_S2),
4540 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004541 },
4542 {
4543 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4544 .dbuf_mask = {
4545 [PIPE_A] = BIT(DBUF_S1),
4546 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004547 [PIPE_D] = BIT(DBUF_S2),
4548 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004549 },
4550 {
4551 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4552 .dbuf_mask = {
4553 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004554 [PIPE_D] = BIT(DBUF_S2),
4555 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004556 },
4557 {
4558 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4559 .dbuf_mask = {
4560 [PIPE_A] = BIT(DBUF_S1),
4561 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004562 [PIPE_D] = BIT(DBUF_S2),
4563 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004564 },
4565 {
4566 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4567 .dbuf_mask = {
4568 [PIPE_B] = BIT(DBUF_S1),
4569 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004570 [PIPE_D] = BIT(DBUF_S2),
4571 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004572 },
4573 {
4574 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4575 .dbuf_mask = {
4576 [PIPE_A] = BIT(DBUF_S1),
4577 [PIPE_B] = BIT(DBUF_S1),
4578 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004579 [PIPE_D] = BIT(DBUF_S2),
4580 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004581 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004582 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004583};
4584
Matt Roper49f75632021-07-21 15:30:40 -07004585static const struct dbuf_slice_conf_entry dg2_allowed_dbufs[] = {
4586 {
4587 .active_pipes = BIT(PIPE_A),
4588 .dbuf_mask = {
4589 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4590 },
4591 },
4592 {
4593 .active_pipes = BIT(PIPE_B),
4594 .dbuf_mask = {
4595 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4596 },
4597 },
4598 {
4599 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4600 .dbuf_mask = {
4601 [PIPE_A] = BIT(DBUF_S1),
4602 [PIPE_B] = BIT(DBUF_S2),
4603 },
4604 },
4605 {
4606 .active_pipes = BIT(PIPE_C),
4607 .dbuf_mask = {
4608 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4609 },
4610 },
4611 {
4612 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4613 .dbuf_mask = {
4614 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4615 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4616 },
4617 },
4618 {
4619 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4620 .dbuf_mask = {
4621 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4622 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4623 },
4624 },
4625 {
4626 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4627 .dbuf_mask = {
4628 [PIPE_A] = BIT(DBUF_S1),
4629 [PIPE_B] = BIT(DBUF_S2),
4630 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4631 },
4632 },
4633 {
4634 .active_pipes = BIT(PIPE_D),
4635 .dbuf_mask = {
4636 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4637 },
4638 },
4639 {
4640 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4641 .dbuf_mask = {
4642 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4643 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4644 },
4645 },
4646 {
4647 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4648 .dbuf_mask = {
4649 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4650 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4651 },
4652 },
4653 {
4654 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4655 .dbuf_mask = {
4656 [PIPE_A] = BIT(DBUF_S1),
4657 [PIPE_B] = BIT(DBUF_S2),
4658 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4659 },
4660 },
4661 {
4662 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4663 .dbuf_mask = {
4664 [PIPE_C] = BIT(DBUF_S3),
4665 [PIPE_D] = BIT(DBUF_S4),
4666 },
4667 },
4668 {
4669 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4670 .dbuf_mask = {
4671 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4672 [PIPE_C] = BIT(DBUF_S3),
4673 [PIPE_D] = BIT(DBUF_S4),
4674 },
4675 },
4676 {
4677 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4678 .dbuf_mask = {
4679 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4680 [PIPE_C] = BIT(DBUF_S3),
4681 [PIPE_D] = BIT(DBUF_S4),
4682 },
4683 },
4684 {
4685 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4686 .dbuf_mask = {
4687 [PIPE_A] = BIT(DBUF_S1),
4688 [PIPE_B] = BIT(DBUF_S2),
4689 [PIPE_C] = BIT(DBUF_S3),
4690 [PIPE_D] = BIT(DBUF_S4),
4691 },
4692 },
4693 {}
4694};
4695
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004696static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
4697 {
4698 .active_pipes = BIT(PIPE_A),
4699 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004700 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004701 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004702 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004703 },
4704 {
4705 .active_pipes = BIT(PIPE_B),
4706 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004707 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004708 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004709 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004710 },
4711 {
4712 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4713 .dbuf_mask = {
4714 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4715 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4716 },
4717 },
4718 {
4719 .active_pipes = BIT(PIPE_C),
4720 .dbuf_mask = {
4721 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4722 },
4723 },
4724 {
4725 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4726 .dbuf_mask = {
4727 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4728 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4729 },
4730 },
4731 {
4732 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4733 .dbuf_mask = {
4734 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4735 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4736 },
4737 },
4738 {
4739 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4740 .dbuf_mask = {
4741 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4742 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4743 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4744 },
4745 },
4746 {
4747 .active_pipes = BIT(PIPE_D),
4748 .dbuf_mask = {
4749 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4750 },
4751 },
4752 {
4753 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4754 .dbuf_mask = {
4755 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4756 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4757 },
4758 },
4759 {
4760 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4761 .dbuf_mask = {
4762 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4763 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4764 },
4765 },
4766 {
4767 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4768 .dbuf_mask = {
4769 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4770 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4771 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4772 },
4773 },
4774 {
4775 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4776 .dbuf_mask = {
4777 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4778 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4779 },
4780 },
4781 {
4782 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4783 .dbuf_mask = {
4784 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4785 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4786 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4787 },
4788 },
4789 {
4790 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4791 .dbuf_mask = {
4792 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4793 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4794 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4795 },
4796 },
4797 {
4798 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4799 .dbuf_mask = {
4800 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4801 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4802 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4803 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4804 },
4805 },
4806 {}
4807
4808};
4809
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004810static bool check_mbus_joined(u8 active_pipes,
4811 const struct dbuf_slice_conf_entry *dbuf_slices)
4812{
4813 int i;
4814
4815 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
4816 if (dbuf_slices[i].active_pipes == active_pipes)
4817 return dbuf_slices[i].join_mbus;
4818 }
4819 return false;
4820}
4821
4822static bool adlp_check_mbus_joined(u8 active_pipes)
4823{
4824 return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
4825}
4826
Ville Syrjälä05e81552020-02-25 19:11:09 +02004827static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4828 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004829{
4830 int i;
4831
Ville Syrjälä05e81552020-02-25 19:11:09 +02004832 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004833 if (dbuf_slices[i].active_pipes == active_pipes)
4834 return dbuf_slices[i].dbuf_mask[pipe];
4835 }
4836 return 0;
4837}
4838
4839/*
4840 * This function finds an entry with same enabled pipe configuration and
4841 * returns correspondent DBuf slice mask as stated in BSpec for particular
4842 * platform.
4843 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004844static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004845{
4846 /*
4847 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4848 * required calculating "pipe ratio" in order to determine
4849 * if one or two slices can be used for single pipe configurations
4850 * as additional constraint to the existing table.
4851 * However based on recent info, it should be not "pipe ratio"
4852 * but rather ratio between pixel_rate and cdclk with additional
4853 * constants, so for now we are using only table until this is
4854 * clarified. Also this is the reason why crtc_state param is
4855 * still here - we will need it once those additional constraints
4856 * pop up.
4857 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004858 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004859}
4860
Ville Syrjälä05e81552020-02-25 19:11:09 +02004861static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004862{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004863 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004864}
4865
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004866static u32 adlp_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4867{
4868 return compute_dbuf_slices(pipe, active_pipes, adlp_allowed_dbufs);
4869}
4870
Matt Roper49f75632021-07-21 15:30:40 -07004871static u32 dg2_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4872{
4873 return compute_dbuf_slices(pipe, active_pipes, dg2_allowed_dbufs);
4874}
4875
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004876static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004877{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004878 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4879 enum pipe pipe = crtc->pipe;
4880
Matt Roper49f75632021-07-21 15:30:40 -07004881 if (IS_DG2(dev_priv))
4882 return dg2_compute_dbuf_slices(pipe, active_pipes);
4883 else if (IS_ALDERLAKE_P(dev_priv))
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004884 return adlp_compute_dbuf_slices(pipe, active_pipes);
4885 else if (DISPLAY_VER(dev_priv) == 12)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004886 return tgl_compute_dbuf_slices(pipe, active_pipes);
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004887 else if (DISPLAY_VER(dev_priv) == 11)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004888 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004889 /*
4890 * For anything else just return one slice yet.
4891 * Should be extended for other platforms.
4892 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004893 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004894}
4895
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004896static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004897skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4898 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004899 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004900{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004901 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004902 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004903 u32 data_rate;
4904 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304905 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004906 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004907
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004908 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004909 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004910
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004911 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004912 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004913
4914 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004915 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004916 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004917
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004918 /*
4919 * Src coordinates are already rotated by 270 degrees for
4920 * the 90/270 degree plane rotation cases (to match the
4921 * GTT mapping), hence no need to account for rotation here.
4922 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004923 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4924 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004925
Mahesh Kumarb879d582018-04-09 09:11:01 +05304926 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004927 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304928 width /= 2;
4929 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004930 }
4931
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004932 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304933
Maarten Lankhorstec193642019-06-28 10:55:17 +02004934 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004935
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004936 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4937
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004938 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004939 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004940}
4941
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004942static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004943skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4944 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004945{
Ville Syrjäläab016302020-11-06 19:30:41 +02004946 struct intel_crtc_state *crtc_state =
4947 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004948 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004949 struct intel_plane *plane;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004950 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004951 enum plane_id plane_id;
4952 int i;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004953
Matt Ropera1de91e2016-05-12 07:05:57 -07004954 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004955 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4956 if (plane->pipe != crtc->pipe)
4957 continue;
4958
4959 plane_id = plane->id;
Matt Roper024c9042015-09-24 15:53:11 -07004960
Mahesh Kumarb879d582018-04-09 09:11:01 +05304961 /* packed/y */
Ville Syrjäläab016302020-11-06 19:30:41 +02004962 crtc_state->plane_data_rate[plane_id] =
4963 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Matt Roper9c74d822016-05-12 07:05:58 -07004964
Mahesh Kumarb879d582018-04-09 09:11:01 +05304965 /* uv-plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004966 crtc_state->uv_plane_data_rate[plane_id] =
4967 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4968 }
4969
4970 for_each_plane_id_on_crtc(crtc, plane_id) {
4971 total_data_rate += crtc_state->plane_data_rate[plane_id];
4972 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004973 }
4974
4975 return total_data_rate;
4976}
4977
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004978static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004979icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4980 struct intel_crtc *crtc)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004981{
Ville Syrjäläab016302020-11-06 19:30:41 +02004982 struct intel_crtc_state *crtc_state =
4983 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004984 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004985 struct intel_plane *plane;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004986 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004987 enum plane_id plane_id;
4988 int i;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004989
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004990 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004991 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4992 if (plane->pipe != crtc->pipe)
4993 continue;
4994
4995 plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004996
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004997 if (!plane_state->planar_linked_plane) {
Ville Syrjäläab016302020-11-06 19:30:41 +02004998 crtc_state->plane_data_rate[plane_id] =
4999 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005000 } else {
5001 enum plane_id y_plane_id;
5002
5003 /*
5004 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005005 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005006 * and needs the master plane state which may be
5007 * NULL if we try get_new_plane_state(), so we
5008 * always calculate from the master.
5009 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005010 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005011 continue;
5012
5013 /* Y plane rate is calculated on the slave */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005014 y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjäläab016302020-11-06 19:30:41 +02005015 crtc_state->plane_data_rate[y_plane_id] =
5016 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005017
Ville Syrjäläab016302020-11-06 19:30:41 +02005018 crtc_state->plane_data_rate[plane_id] =
5019 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005020 }
5021 }
5022
Ville Syrjäläab016302020-11-06 19:30:41 +02005023 for_each_plane_id_on_crtc(crtc, plane_id)
5024 total_data_rate += crtc_state->plane_data_rate[plane_id];
5025
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005026 return total_data_rate;
5027}
5028
Ville Syrjälä5516e892021-02-26 17:32:03 +02005029const struct skl_wm_level *
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005030skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005031 enum plane_id plane_id,
5032 int level)
5033{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005034 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5035
5036 if (level == 0 && pipe_wm->use_sagv_wm)
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005037 return &wm->sagv.wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005038
5039 return &wm->wm[level];
5040}
5041
Ville Syrjälä5516e892021-02-26 17:32:03 +02005042const struct skl_wm_level *
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005043skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
5044 enum plane_id plane_id)
5045{
5046 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5047
5048 if (pipe_wm->use_sagv_wm)
5049 return &wm->sagv.trans_wm;
5050
5051 return &wm->trans_wm;
5052}
5053
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005054/*
5055 * We only disable the watermarks for each plane if
5056 * they exceed the ddb allocation of said plane. This
5057 * is done so that we don't end up touching cursor
5058 * watermarks needlessly when some other plane reduces
5059 * our max possible watermark level.
5060 *
5061 * Bspec has this to say about the PLANE_WM enable bit:
5062 * "All the watermarks at this level for all enabled
5063 * planes must be enabled before the level will be used."
5064 * So this is actually safe to do.
5065 */
5066static void
5067skl_check_wm_level(struct skl_wm_level *wm, u64 total)
5068{
5069 if (wm->min_ddb_alloc > total)
5070 memset(wm, 0, sizeof(*wm));
5071}
5072
5073static void
5074skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
5075 u64 total, u64 uv_total)
5076{
5077 if (wm->min_ddb_alloc > total ||
5078 uv_wm->min_ddb_alloc > uv_total) {
5079 memset(wm, 0, sizeof(*wm));
5080 memset(uv_wm, 0, sizeof(*uv_wm));
5081 }
5082}
5083
Matt Roperc107acf2016-05-12 07:06:01 -07005084static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02005085skl_allocate_plane_ddb(struct intel_atomic_state *state,
5086 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00005087{
Ville Syrjäläef79d622021-01-22 22:56:32 +02005088 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläffc90032020-11-06 19:30:37 +02005089 struct intel_crtc_state *crtc_state =
5090 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005091 const struct intel_dbuf_state *dbuf_state =
Ville Syrjälä47a14952021-01-22 22:56:30 +02005092 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005093 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
5094 int num_active = hweight8(dbuf_state->active_pipes);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005095 u16 alloc_size, start = 0;
5096 u16 total[I915_MAX_PLANES] = {};
5097 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02005098 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005099 enum plane_id plane_id;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005100 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08005101 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005102
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005103 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005104 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
5105 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005106
Ville Syrjäläef79d622021-01-22 22:56:32 +02005107 if (!crtc_state->hw.active)
Matt Roperc107acf2016-05-12 07:06:01 -07005108 return 0;
Matt Roperc107acf2016-05-12 07:06:01 -07005109
Matt Roper7dadd282021-03-19 21:42:43 -07005110 if (DISPLAY_VER(dev_priv) >= 11)
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005111 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005112 icl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005113 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005114 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005115 skl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005116
Damien Lespiau34bb56a2014-11-04 17:07:01 +00005117 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05305118 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005119 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005120
Matt Roperd8e87492018-12-11 09:31:07 -08005121 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005122 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08005123 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005124 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08005125 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005126 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005127
Matt Ropera1de91e2016-05-12 07:05:57 -07005128 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005129 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005130
Matt Roperd8e87492018-12-11 09:31:07 -08005131 /*
5132 * Find the highest watermark level for which we can satisfy the block
5133 * requirement of active planes.
5134 */
5135 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08005136 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005137 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005138 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005139 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005140
5141 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05305142 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305143 drm_WARN_ON(&dev_priv->drm,
5144 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005145 blocks = U32_MAX;
5146 break;
5147 }
5148 continue;
5149 }
5150
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005151 blocks += wm->wm[level].min_ddb_alloc;
5152 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08005153 }
5154
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02005155 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08005156 alloc_size -= blocks;
5157 break;
5158 }
5159 }
5160
5161 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005162 drm_dbg_kms(&dev_priv->drm,
5163 "Requested display configuration exceeds system DDB limitations");
5164 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
5165 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08005166 return -EINVAL;
5167 }
5168
5169 /*
5170 * Grant each plane the blocks it requires at the highest achievable
5171 * watermark level, plus an extra share of the leftover blocks
5172 * proportional to its relative data rate.
5173 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005174 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005175 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005176 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005177 u64 rate;
5178 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005179
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005180 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005181 continue;
5182
Damien Lespiaub9cec072014-11-04 17:06:43 +00005183 /*
Matt Roperd8e87492018-12-11 09:31:07 -08005184 * We've accounted for all active planes; remaining planes are
5185 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00005186 */
Matt Roperd8e87492018-12-11 09:31:07 -08005187 if (total_data_rate == 0)
5188 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005189
Ville Syrjäläab016302020-11-06 19:30:41 +02005190 rate = crtc_state->plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005191 extra = min_t(u16, alloc_size,
5192 DIV64_U64_ROUND_UP(alloc_size * rate,
5193 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005194 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005195 alloc_size -= extra;
5196 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005197
Matt Roperd8e87492018-12-11 09:31:07 -08005198 if (total_data_rate == 0)
5199 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07005200
Ville Syrjäläab016302020-11-06 19:30:41 +02005201 rate = crtc_state->uv_plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005202 extra = min_t(u16, alloc_size,
5203 DIV64_U64_ROUND_UP(alloc_size * rate,
5204 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005205 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005206 alloc_size -= extra;
5207 total_data_rate -= rate;
5208 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305209 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08005210
5211 /* Set the actual DDB start/end points for each plane */
5212 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005213 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005214 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005215 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005216 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005217 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005218
5219 if (plane_id == PLANE_CURSOR)
5220 continue;
5221
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005222 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305223 drm_WARN_ON(&dev_priv->drm,
Matt Roper7dadd282021-03-19 21:42:43 -07005224 DISPLAY_VER(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005225
Matt Roperd8e87492018-12-11 09:31:07 -08005226 /* Leave disabled planes at (0,0) */
5227 if (total[plane_id]) {
5228 plane_alloc->start = start;
5229 start += total[plane_id];
5230 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07005231 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005232
Matt Roperd8e87492018-12-11 09:31:07 -08005233 if (uv_total[plane_id]) {
5234 uv_plane_alloc->start = start;
5235 start += uv_total[plane_id];
5236 uv_plane_alloc->end = start;
5237 }
5238 }
5239
5240 /*
5241 * When we calculated watermark values we didn't know how high
5242 * of a level we'd actually be able to hit, so we just marked
5243 * all levels as "enabled." Go back now and disable the ones
5244 * that aren't actually possible.
5245 */
5246 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005247 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005248 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005249 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02005250
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005251 skl_check_nv12_wm_level(&wm->wm[level], &wm->uv_wm[level],
5252 total[plane_id], uv_total[plane_id]);
Ville Syrjälä290248c2019-02-13 18:54:24 +02005253
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005254 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005255 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005256 * Underruns with WM1+ disabled
5257 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005258 if (DISPLAY_VER(dev_priv) == 11 &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005259 level == 1 && wm->wm[0].enable) {
5260 wm->wm[level].blocks = wm->wm[0].blocks;
5261 wm->wm[level].lines = wm->wm[0].lines;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005262 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005263 }
Matt Roperd8e87492018-12-11 09:31:07 -08005264 }
5265 }
5266
5267 /*
Ville Syrjälädf4a50a2021-02-26 17:31:59 +02005268 * Go back and disable the transition and SAGV watermarks
5269 * if it turns out we don't have enough DDB blocks for them.
Matt Roperd8e87492018-12-11 09:31:07 -08005270 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005271 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005272 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005273 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005274
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005275 skl_check_wm_level(&wm->trans_wm, total[plane_id]);
5276 skl_check_wm_level(&wm->sagv.wm0, total[plane_id]);
5277 skl_check_wm_level(&wm->sagv.trans_wm, total[plane_id]);
Damien Lespiaub9cec072014-11-04 17:06:43 +00005278 }
5279
Matt Roperc107acf2016-05-12 07:06:01 -07005280 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005281}
5282
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005283/*
5284 * 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 +02005285 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005286 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5287 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5288*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005289static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005290skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5291 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005292{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005293 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305294 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005295
5296 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305297 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005298
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305299 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005300 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005301
Matt Roper2b5a4562021-03-22 16:38:40 -07005302 if (DISPLAY_VER(dev_priv) >= 10)
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005303 ret = add_fixed16_u32(ret, 1);
5304
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005305 return ret;
5306}
5307
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005308static uint_fixed_16_16_t
5309skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5310 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005311{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005312 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305313 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005314
5315 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305316 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005317
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005318 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305319 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5320 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305321 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005322 return ret;
5323}
5324
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305325static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005326intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305327{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305328 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005329 u32 pixel_rate;
5330 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305331 uint_fixed_16_16_t linetime_us;
5332
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005333 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305334 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305335
Maarten Lankhorstec193642019-06-28 10:55:17 +02005336 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305337
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305338 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305339 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305340
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005341 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305342 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305343
5344 return linetime_us;
5345}
5346
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305347static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005348skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5349 int width, const struct drm_format_info *format,
5350 u64 modifier, unsigned int rotation,
5351 u32 plane_pixel_rate, struct skl_wm_params *wp,
5352 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305353{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005354 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005355 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005356 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305357
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305358 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005359 if (color_plane == 1 &&
5360 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005361 drm_dbg_kms(&dev_priv->drm,
5362 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305363 return -EINVAL;
5364 }
5365
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005366 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5367 modifier == I915_FORMAT_MOD_Yf_TILED ||
5368 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5369 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5370 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5371 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5372 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005373 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305374
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005375 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005376 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305377 wp->width /= 2;
5378
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005379 wp->cpp = format->cpp[color_plane];
5380 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305381
Matt Roper7dadd282021-03-19 21:42:43 -07005382 if (DISPLAY_VER(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005383 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005384 wp->dbuf_block_size = 256;
5385 else
5386 wp->dbuf_block_size = 512;
5387
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005388 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305389 switch (wp->cpp) {
5390 case 1:
5391 wp->y_min_scanlines = 16;
5392 break;
5393 case 2:
5394 wp->y_min_scanlines = 8;
5395 break;
5396 case 4:
5397 wp->y_min_scanlines = 4;
5398 break;
5399 default:
5400 MISSING_CASE(wp->cpp);
5401 return -EINVAL;
5402 }
5403 } else {
5404 wp->y_min_scanlines = 4;
5405 }
5406
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005407 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305408 wp->y_min_scanlines *= 2;
5409
5410 wp->plane_bytes_per_line = wp->width * wp->cpp;
5411 if (wp->y_tiled) {
5412 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005413 wp->y_min_scanlines,
5414 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305415
Matt Roper2b5a4562021-03-22 16:38:40 -07005416 if (DISPLAY_VER(dev_priv) >= 10)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305417 interm_pbpl++;
5418
5419 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5420 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305421 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005422 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005423 wp->dbuf_block_size);
5424
Matt Roper2b5a4562021-03-22 16:38:40 -07005425 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005426 interm_pbpl++;
5427
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305428 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5429 }
5430
5431 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5432 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005433
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305434 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005435 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305436
5437 return 0;
5438}
5439
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005440static int
5441skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5442 const struct intel_plane_state *plane_state,
5443 struct skl_wm_params *wp, int color_plane)
5444{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005445 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005446 int width;
5447
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005448 /*
5449 * Src coordinates are already rotated by 270 degrees for
5450 * the 90/270 degree plane rotation cases (to match the
5451 * GTT mapping), hence no need to account for rotation here.
5452 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005453 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005454
5455 return skl_compute_wm_params(crtc_state, width,
5456 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005457 plane_state->hw.rotation,
Ville Syrjälä3df3fe22020-11-06 19:30:42 +02005458 intel_plane_pixel_rate(crtc_state, plane_state),
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005459 wp, color_plane);
5460}
5461
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005462static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5463{
Matt Roper2b5a4562021-03-22 16:38:40 -07005464 if (DISPLAY_VER(dev_priv) >= 10)
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005465 return true;
5466
5467 /* The number of lines are ignored for the level 0 watermark. */
5468 return level > 0;
5469}
5470
Matt Roper1003cee2021-05-14 08:36:54 -07005471static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5472{
5473 if (DISPLAY_VER(dev_priv) >= 13)
5474 return 255;
5475 else
5476 return 31;
5477}
5478
Maarten Lankhorstec193642019-06-28 10:55:17 +02005479static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005480 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005481 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005482 const struct skl_wm_params *wp,
5483 const struct skl_wm_level *result_prev,
5484 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005485{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005486 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305487 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305488 uint_fixed_16_16_t selected_result;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005489 u32 blocks, lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005490
Ville Syrjälä0aded172019-02-05 17:50:53 +02005491 if (latency == 0) {
5492 /* reject it */
5493 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005494 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005495 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005496
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005497 /*
5498 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5499 * Display WA #1141: kbl,cfl
5500 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005501 if ((IS_KABYLAKE(dev_priv) ||
5502 IS_COFFEELAKE(dev_priv) ||
5503 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005504 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305505 latency += 4;
5506
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005507 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005508 latency += 15;
5509
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305510 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005511 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305512 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005513 crtc_state->hw.pipe_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005514 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305515 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005516
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305517 if (wp->y_tiled) {
5518 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005519 } else {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005520 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005521 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005522 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005523 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005524 } else if (latency >= wp->linetime_us) {
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005525 if (DISPLAY_VER(dev_priv) == 9)
Paulo Zanoni077b5822018-10-04 16:15:57 -07005526 selected_result = min_fixed16(method1, method2);
5527 else
5528 selected_result = method2;
5529 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005530 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005531 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005532 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005533
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005534 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5535 lines = div_round_up_fixed16(selected_result,
5536 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005537
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005538 if (DISPLAY_VER(dev_priv) == 9) {
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005539 /* Display WA #1125: skl,bxt,kbl */
5540 if (level == 0 && wp->rc_surface)
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005541 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005542
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005543 /* Display WA #1126: skl,bxt,kbl */
5544 if (level >= 1 && level <= 7) {
5545 if (wp->y_tiled) {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005546 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5547 lines += wp->y_min_scanlines;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005548 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005549 blocks++;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005550 }
5551
5552 /*
5553 * Make sure result blocks for higher latency levels are
5554 * atleast as high as level below the current level.
5555 * Assumption in DDB algorithm optimization for special
5556 * cases. Also covers Display WA #1125 for RC.
5557 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005558 if (result_prev->blocks > blocks)
5559 blocks = result_prev->blocks;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005560 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005561 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005562
Matt Roper7dadd282021-03-19 21:42:43 -07005563 if (DISPLAY_VER(dev_priv) >= 11) {
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005564 if (wp->y_tiled) {
5565 int extra_lines;
5566
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005567 if (lines % wp->y_min_scanlines == 0)
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005568 extra_lines = wp->y_min_scanlines;
5569 else
5570 extra_lines = wp->y_min_scanlines * 2 -
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005571 lines % wp->y_min_scanlines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005572
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005573 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005574 wp->plane_blocks_per_line);
5575 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005576 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005577 }
5578 }
5579
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005580 if (!skl_wm_has_lines(dev_priv, level))
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005581 lines = 0;
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005582
Matt Roper1003cee2021-05-14 08:36:54 -07005583 if (lines > skl_wm_max_lines(dev_priv)) {
Ville Syrjälä0aded172019-02-05 17:50:53 +02005584 /* reject it */
5585 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005586 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005587 }
Matt Roperd8e87492018-12-11 09:31:07 -08005588
5589 /*
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005590 * If lines is valid, assume we can use this watermark level
Matt Roperd8e87492018-12-11 09:31:07 -08005591 * for now. We'll come back and disable it after we calculate the
5592 * DDB allocation if it turns out we don't actually have enough
5593 * blocks to satisfy it.
5594 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005595 result->blocks = blocks;
5596 result->lines = lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005597 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005598 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5599 result->enable = true;
Ville Syrjälä9c312122020-11-06 19:30:40 +02005600
Matt Roper7dadd282021-03-19 21:42:43 -07005601 if (DISPLAY_VER(dev_priv) < 12)
Ville Syrjälä9c312122020-11-06 19:30:40 +02005602 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005603}
5604
Matt Roperd8e87492018-12-11 09:31:07 -08005605static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005606skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305607 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005608 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005609{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005610 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305611 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005612 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005613
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305614 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005615 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005616 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305617
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005618 skl_compute_plane_wm(crtc_state, level, latency,
5619 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005620
5621 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305622 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005623}
5624
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005625static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5626 const struct skl_wm_params *wm_params,
5627 struct skl_plane_wm *plane_wm)
5628{
5629 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005630 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005631 struct skl_wm_level *levels = plane_wm->wm;
5632 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5633
5634 skl_compute_plane_wm(crtc_state, 0, latency,
5635 wm_params, &levels[0],
5636 sagv_wm);
5637}
5638
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005639static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5640 struct skl_wm_level *trans_wm,
5641 const struct skl_wm_level *wm0,
5642 const struct skl_wm_params *wp)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005643{
Ville Syrjäläc834d032020-02-28 22:35:52 +02005644 u16 trans_min, trans_amount, trans_y_tile_min;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005645 u16 wm0_blocks, trans_offset, blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005646
Kumar, Maheshca476672017-08-17 19:15:24 +05305647 /* Transition WM don't make any sense if ipc is disabled */
5648 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005649 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305650
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005651 /*
5652 * WaDisableTWM:skl,kbl,cfl,bxt
5653 * Transition WM are not recommended by HW team for GEN9
5654 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005655 if (DISPLAY_VER(dev_priv) == 9)
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005656 return;
5657
Matt Roper7dadd282021-03-19 21:42:43 -07005658 if (DISPLAY_VER(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305659 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005660 else
5661 trans_min = 14;
5662
5663 /* Display WA #1140: glk,cnl */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005664 if (DISPLAY_VER(dev_priv) == 10)
Ville Syrjäläc834d032020-02-28 22:35:52 +02005665 trans_amount = 0;
5666 else
5667 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305668
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005669 trans_offset = trans_min + trans_amount;
Kumar, Maheshca476672017-08-17 19:15:24 +05305670
Paulo Zanonicbacc792018-10-04 16:15:58 -07005671 /*
5672 * The spec asks for Selected Result Blocks for wm0 (the real value),
5673 * not Result Blocks (the integer value). Pay attention to the capital
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005674 * letters. The value wm_l0->blocks is actually Result Blocks, but
Paulo Zanonicbacc792018-10-04 16:15:58 -07005675 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5676 * and since we later will have to get the ceiling of the sum in the
5677 * transition watermarks calculation, we can just pretend Selected
5678 * Result Blocks is Result Blocks minus 1 and it should work for the
5679 * current platforms.
5680 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005681 wm0_blocks = wm0->blocks - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005682
Kumar, Maheshca476672017-08-17 19:15:24 +05305683 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005684 trans_y_tile_min =
5685 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005686 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305687 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005688 blocks = wm0_blocks + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305689 }
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005690 blocks++;
Kumar, Maheshca476672017-08-17 19:15:24 +05305691
Matt Roperd8e87492018-12-11 09:31:07 -08005692 /*
5693 * Just assume we can enable the transition watermark. After
5694 * computing the DDB we'll come back and disable it if that
5695 * assumption turns out to be false.
5696 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005697 trans_wm->blocks = blocks;
5698 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5699 trans_wm->enable = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005700}
5701
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005702static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005703 const struct intel_plane_state *plane_state,
5704 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005705{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005706 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5707 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälädbf71382020-11-06 19:30:38 +02005708 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005709 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005710 int ret;
5711
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005712 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005713 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005714 if (ret)
5715 return ret;
5716
Ville Syrjälä67155a62019-03-12 22:58:37 +02005717 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005718
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005719 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5720 &wm->wm[0], &wm_params);
5721
Matt Roper7dadd282021-03-19 21:42:43 -07005722 if (DISPLAY_VER(dev_priv) >= 12) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005723 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5724
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005725 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5726 &wm->sagv.wm0, &wm_params);
5727 }
Ville Syrjälä83158472018-11-27 18:57:26 +02005728
5729 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005730}
5731
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005732static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005733 const struct intel_plane_state *plane_state,
5734 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005735{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005736 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005737 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005738 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005739
Ville Syrjälä83158472018-11-27 18:57:26 +02005740 wm->is_planar = true;
5741
5742 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005743 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005744 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005745 if (ret)
5746 return ret;
5747
Ville Syrjälä67155a62019-03-12 22:58:37 +02005748 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005749
5750 return 0;
5751}
5752
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005753static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005754 const struct intel_plane_state *plane_state)
5755{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005756 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä83158472018-11-27 18:57:26 +02005757 enum plane_id plane_id = plane->id;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005758 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5759 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005760 int ret;
5761
Ville Syrjälädbf71382020-11-06 19:30:38 +02005762 memset(wm, 0, sizeof(*wm));
5763
Ville Syrjälä83158472018-11-27 18:57:26 +02005764 if (!intel_wm_plane_visible(crtc_state, plane_state))
5765 return 0;
5766
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005767 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005768 plane_id, 0);
5769 if (ret)
5770 return ret;
5771
5772 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005773 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005774 plane_id);
5775 if (ret)
5776 return ret;
5777 }
5778
5779 return 0;
5780}
5781
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005782static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005783 const struct intel_plane_state *plane_state)
5784{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005785 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5786 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5787 enum plane_id plane_id = plane->id;
5788 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005789 int ret;
5790
5791 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005792 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005793 return 0;
5794
Ville Syrjäläf99b8052021-03-27 02:59:45 +02005795 memset(wm, 0, sizeof(*wm));
5796
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005797 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005798 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005799 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005800
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305801 drm_WARN_ON(&dev_priv->drm,
5802 !intel_wm_plane_visible(crtc_state, plane_state));
5803 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5804 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005805
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005806 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005807 y_plane_id, 0);
5808 if (ret)
5809 return ret;
5810
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005811 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005812 plane_id, 1);
5813 if (ret)
5814 return ret;
5815 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005816 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005817 plane_id, 0);
5818 if (ret)
5819 return ret;
5820 }
5821
5822 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005823}
5824
Ville Syrjäläffc90032020-11-06 19:30:37 +02005825static int skl_build_pipe_wm(struct intel_atomic_state *state,
5826 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005827{
Ville Syrjäläffc90032020-11-06 19:30:37 +02005828 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5829 struct intel_crtc_state *crtc_state =
5830 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005831 const struct intel_plane_state *plane_state;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005832 struct intel_plane *plane;
5833 int ret, i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005834
Ville Syrjälädbf71382020-11-06 19:30:38 +02005835 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5836 /*
5837 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5838 * instead but we don't populate that correctly for NV12 Y
5839 * planes so for now hack this.
5840 */
5841 if (plane->pipe != crtc->pipe)
5842 continue;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305843
Matt Roper7dadd282021-03-19 21:42:43 -07005844 if (DISPLAY_VER(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005845 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005846 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005847 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305848 if (ret)
5849 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005850 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305851
Ville Syrjälädbf71382020-11-06 19:30:38 +02005852 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5853
Matt Roper55994c22016-05-12 07:06:08 -07005854 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005855}
5856
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005857static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5858 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005859 const struct skl_ddb_entry *entry)
5860{
5861 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005862 intel_de_write_fw(dev_priv, reg,
5863 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005864 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005865 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005866}
5867
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005868static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5869 i915_reg_t reg,
5870 const struct skl_wm_level *level)
5871{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005872 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005873
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005874 if (level->enable)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005875 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005876 if (level->ignore_lines)
5877 val |= PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005878 val |= level->blocks;
Matt Roper1003cee2021-05-14 08:36:54 -07005879 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005880
Jani Nikula9b6320a2020-01-23 16:00:04 +02005881 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005882}
5883
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005884void skl_write_plane_wm(struct intel_plane *plane,
5885 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005886{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005887 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005888 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005889 enum plane_id plane_id = plane->id;
5890 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005891 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5892 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005893 const struct skl_ddb_entry *ddb_y =
5894 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5895 const struct skl_ddb_entry *ddb_uv =
5896 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005897
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005898 for (level = 0; level <= max_level; level++)
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005899 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005900 skl_plane_wm_level(pipe_wm, plane_id, level));
5901
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005902 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005903 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005904
Matt Roper7959ffe2021-05-18 17:06:11 -07005905 if (HAS_HW_SAGV_WM(dev_priv)) {
5906 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5907 &wm->sagv.wm0);
5908 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5909 &wm->sagv.trans_wm);
5910 }
5911
Matt Roper7dadd282021-03-19 21:42:43 -07005912 if (DISPLAY_VER(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005913 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005914 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5915 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305916 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005917
5918 if (wm->is_planar)
5919 swap(ddb_y, ddb_uv);
5920
5921 skl_ddb_entry_write(dev_priv,
5922 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5923 skl_ddb_entry_write(dev_priv,
5924 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005925}
5926
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005927void skl_write_cursor_wm(struct intel_plane *plane,
5928 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005929{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005930 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005931 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005932 enum plane_id plane_id = plane->id;
5933 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005934 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005935 const struct skl_ddb_entry *ddb =
5936 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005937
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005938 for (level = 0; level <= max_level; level++)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005939 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005940 skl_plane_wm_level(pipe_wm, plane_id, level));
5941
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005942 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5943 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005944
Matt Roper7959ffe2021-05-18 17:06:11 -07005945 if (HAS_HW_SAGV_WM(dev_priv)) {
5946 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5947
5948 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5949 &wm->sagv.wm0);
5950 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5951 &wm->sagv.trans_wm);
5952 }
5953
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005954 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005955}
5956
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005957bool skl_wm_level_equals(const struct skl_wm_level *l1,
5958 const struct skl_wm_level *l2)
5959{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005960 return l1->enable == l2->enable &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005961 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005962 l1->lines == l2->lines &&
5963 l1->blocks == l2->blocks;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005964}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005965
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005966static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5967 const struct skl_plane_wm *wm1,
5968 const struct skl_plane_wm *wm2)
5969{
5970 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005971
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005972 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005973 /*
5974 * We don't check uv_wm as the hardware doesn't actually
5975 * use it. It only gets used for calculating the required
5976 * ddb allocation.
5977 */
5978 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005979 return false;
5980 }
5981
Ville Syrjäläf11449d2021-02-26 17:32:00 +02005982 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005983 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5984 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005985}
5986
Jani Nikula81b55ef2020-04-20 17:04:38 +03005987static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5988 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005989{
Lyude27082492016-08-24 07:48:10 +02005990 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005991}
5992
Ville Syrjälä33c9c502021-01-22 22:56:33 +02005993static void skl_ddb_entry_union(struct skl_ddb_entry *a,
5994 const struct skl_ddb_entry *b)
5995{
5996 if (a->end && b->end) {
5997 a->start = min(a->start, b->start);
5998 a->end = max(a->end, b->end);
5999 } else if (b->end) {
6000 a->start = b->start;
6001 a->end = b->end;
6002 }
6003}
6004
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006005bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03006006 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006007 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006008{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006009 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006010
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006011 for (i = 0; i < num_entries; i++) {
6012 if (i != ignore_idx &&
6013 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02006014 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03006015 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006016
Lyude27082492016-08-24 07:48:10 +02006017 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006018}
6019
Jani Nikulabb7791b2016-10-04 12:29:17 +03006020static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006021skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
6022 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006023{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006024 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
6025 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006026 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6027 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006028
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006029 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6030 struct intel_plane_state *plane_state;
6031 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006032
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006033 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
6034 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
6035 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
6036 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006037 continue;
6038
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006039 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006040 if (IS_ERR(plane_state))
6041 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02006042
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006043 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006044 }
6045
6046 return 0;
6047}
6048
Ville Syrjäläef79d622021-01-22 22:56:32 +02006049static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
6050{
6051 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
6052 u8 enabled_slices;
6053 enum pipe pipe;
6054
6055 /*
6056 * FIXME: For now we always enable slice S1 as per
6057 * the Bspec display initialization sequence.
6058 */
6059 enabled_slices = BIT(DBUF_S1);
6060
6061 for_each_pipe(dev_priv, pipe)
6062 enabled_slices |= dbuf_state->slices[pipe];
6063
6064 return enabled_slices;
6065}
6066
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006067static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006068skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07006069{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006070 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6071 const struct intel_dbuf_state *old_dbuf_state;
Ville Syrjäläef79d622021-01-22 22:56:32 +02006072 struct intel_dbuf_state *new_dbuf_state = NULL;
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006073 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006074 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306075 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306076 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07006077
Ville Syrjäläef79d622021-01-22 22:56:32 +02006078 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6079 new_dbuf_state = intel_atomic_get_dbuf_state(state);
6080 if (IS_ERR(new_dbuf_state))
6081 return PTR_ERR(new_dbuf_state);
6082
6083 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6084 break;
6085 }
6086
6087 if (!new_dbuf_state)
6088 return 0;
6089
6090 new_dbuf_state->active_pipes =
6091 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6092
6093 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
6094 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6095 if (ret)
6096 return ret;
6097 }
6098
6099 for_each_intel_crtc(&dev_priv->drm, crtc) {
6100 enum pipe pipe = crtc->pipe;
6101
6102 new_dbuf_state->slices[pipe] =
6103 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes);
6104
6105 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
6106 continue;
6107
6108 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6109 if (ret)
6110 return ret;
6111 }
6112
6113 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
6114
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006115 if (IS_ALDERLAKE_P(dev_priv))
6116 new_dbuf_state->joined_mbus = adlp_check_mbus_joined(new_dbuf_state->active_pipes);
6117
6118 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
6119 old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006120 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
6121 if (ret)
6122 return ret;
6123
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006124 if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6125 /* TODO: Implement vblank synchronized MBUS joining changes */
6126 ret = intel_modeset_all_pipes(state);
6127 if (ret)
6128 return ret;
6129 }
6130
Ville Syrjäläef79d622021-01-22 22:56:32 +02006131 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006132 "Enabled dbuf slices 0x%x -> 0x%x (total dbuf slices 0x%x), mbus joined? %s->%s\n",
Ville Syrjäläef79d622021-01-22 22:56:32 +02006133 old_dbuf_state->enabled_slices,
6134 new_dbuf_state->enabled_slices,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006135 INTEL_INFO(dev_priv)->dbuf.slice_mask,
6136 yesno(old_dbuf_state->joined_mbus),
6137 yesno(new_dbuf_state->joined_mbus));
Ville Syrjäläef79d622021-01-22 22:56:32 +02006138 }
6139
6140 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6141 enum pipe pipe = crtc->pipe;
6142
6143 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
6144
6145 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
6146 continue;
6147
6148 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6149 if (ret)
6150 return ret;
6151 }
6152
6153 for_each_intel_crtc(&dev_priv->drm, crtc) {
6154 ret = skl_crtc_allocate_ddb(state, crtc);
6155 if (ret)
6156 return ret;
6157 }
6158
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006159 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006160 new_crtc_state, i) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006161 ret = skl_allocate_plane_ddb(state, crtc);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006162 if (ret)
6163 return ret;
6164
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006165 ret = skl_ddb_add_affected_planes(old_crtc_state,
6166 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006167 if (ret)
6168 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07006169 }
6170
6171 return 0;
6172}
6173
Ville Syrjäläab98e942019-02-08 22:05:27 +02006174static char enast(bool enable)
6175{
6176 return enable ? '*' : ' ';
6177}
6178
Matt Roper2722efb2016-08-17 15:55:55 -04006179static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006180skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006181{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006182 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6183 const struct intel_crtc_state *old_crtc_state;
6184 const struct intel_crtc_state *new_crtc_state;
6185 struct intel_plane *plane;
6186 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01006187 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006188
Jani Nikulabdbf43d2019-10-28 12:38:15 +02006189 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02006190 return;
6191
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006192 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6193 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02006194 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
6195
6196 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
6197 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
6198
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006199 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6200 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006201 const struct skl_ddb_entry *old, *new;
6202
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006203 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
6204 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006205
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006206 if (skl_ddb_entry_equal(old, new))
6207 continue;
6208
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006209 drm_dbg_kms(&dev_priv->drm,
6210 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
6211 plane->base.base.id, plane->base.name,
6212 old->start, old->end, new->start, new->end,
6213 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006214 }
6215
6216 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6217 enum plane_id plane_id = plane->id;
6218 const struct skl_plane_wm *old_wm, *new_wm;
6219
6220 old_wm = &old_pipe_wm->planes[plane_id];
6221 new_wm = &new_pipe_wm->planes[plane_id];
6222
6223 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
6224 continue;
6225
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006226 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006227 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
6228 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006229 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006230 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
6231 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
6232 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
6233 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
6234 enast(old_wm->trans_wm.enable),
6235 enast(old_wm->sagv.wm0.enable),
6236 enast(old_wm->sagv.trans_wm.enable),
6237 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
6238 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
6239 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
6240 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
6241 enast(new_wm->trans_wm.enable),
6242 enast(new_wm->sagv.wm0.enable),
6243 enast(new_wm->sagv.trans_wm.enable));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006244
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006245 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006246 "[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d"
6247 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006248 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006249 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
6250 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
6251 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
6252 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
6253 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
6254 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
6255 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
6256 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
6257 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
6258 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
6259 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
6260 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
6261 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
6262 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
6263 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
6264 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
6265 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
6266 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
6267 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
6268 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
6269 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
6270 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006271
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006272 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006273 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6274 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006275 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006276 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
6277 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
6278 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
6279 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
6280 old_wm->trans_wm.blocks,
6281 old_wm->sagv.wm0.blocks,
6282 old_wm->sagv.trans_wm.blocks,
6283 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
6284 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
6285 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
6286 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
6287 new_wm->trans_wm.blocks,
6288 new_wm->sagv.wm0.blocks,
6289 new_wm->sagv.trans_wm.blocks);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006290
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006291 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006292 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6293 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006294 plane->base.base.id, plane->base.name,
6295 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6296 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6297 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6298 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6299 old_wm->trans_wm.min_ddb_alloc,
Ville Syrjäläa68aa482021-02-26 17:32:01 +02006300 old_wm->sagv.wm0.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006301 old_wm->sagv.trans_wm.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006302 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6303 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6304 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6305 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006306 new_wm->trans_wm.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006307 new_wm->sagv.wm0.min_ddb_alloc,
6308 new_wm->sagv.trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006309 }
6310 }
6311}
6312
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006313static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6314 const struct skl_pipe_wm *old_pipe_wm,
6315 const struct skl_pipe_wm *new_pipe_wm)
6316{
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006317 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6318 int level, max_level = ilk_wm_max_level(i915);
6319
6320 for (level = 0; level <= max_level; level++) {
6321 /*
6322 * We don't check uv_wm as the hardware doesn't actually
6323 * use it. It only gets used for calculating the required
6324 * ddb allocation.
6325 */
Ville Syrjälä93fe8622021-03-25 02:44:14 +02006326 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6327 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006328 return false;
6329 }
6330
Matt Roper7959ffe2021-05-18 17:06:11 -07006331 if (HAS_HW_SAGV_WM(i915)) {
6332 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6333 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6334
6335 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6336 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6337 return false;
6338 }
6339
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006340 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6341 skl_plane_trans_wm(new_pipe_wm, plane->id));
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006342}
6343
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006344/*
6345 * To make sure the cursor watermark registers are always consistent
6346 * with our computed state the following scenario needs special
6347 * treatment:
6348 *
6349 * 1. enable cursor
6350 * 2. move cursor entirely offscreen
6351 * 3. disable cursor
6352 *
6353 * Step 2. does call .disable_plane() but does not zero the watermarks
6354 * (since we consider an offscreen cursor still active for the purposes
6355 * of watermarks). Step 3. would not normally call .disable_plane()
6356 * because the actual plane visibility isn't changing, and we don't
6357 * deallocate the cursor ddb until the pipe gets disabled. So we must
6358 * force step 3. to call .disable_plane() to update the watermark
6359 * registers properly.
6360 *
6361 * Other planes do not suffer from this issues as their watermarks are
6362 * calculated based on the actual plane visibility. The only time this
6363 * can trigger for the other planes is during the initial readout as the
6364 * default value of the watermarks registers is not zero.
6365 */
6366static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6367 struct intel_crtc *crtc)
6368{
6369 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6370 const struct intel_crtc_state *old_crtc_state =
6371 intel_atomic_get_old_crtc_state(state, crtc);
6372 struct intel_crtc_state *new_crtc_state =
6373 intel_atomic_get_new_crtc_state(state, crtc);
6374 struct intel_plane *plane;
6375
6376 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6377 struct intel_plane_state *plane_state;
6378 enum plane_id plane_id = plane->id;
6379
6380 /*
6381 * Force a full wm update for every plane on modeset.
6382 * Required because the reset value of the wm registers
6383 * is non-zero, whereas we want all disabled planes to
6384 * have zero watermarks. So if we turn off the relevant
6385 * power well the hardware state will go out of sync
6386 * with the software state.
6387 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006388 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006389 skl_plane_selected_wm_equals(plane,
6390 &old_crtc_state->wm.skl.optimal,
6391 &new_crtc_state->wm.skl.optimal))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006392 continue;
6393
6394 plane_state = intel_atomic_get_plane_state(state, plane);
6395 if (IS_ERR(plane_state))
6396 return PTR_ERR(plane_state);
6397
6398 new_crtc_state->update_planes |= BIT(plane_id);
6399 }
6400
6401 return 0;
6402}
6403
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306404static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006405skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306406{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006407 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006408 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306409 int ret, i;
6410
Ville Syrjäläffc90032020-11-06 19:30:37 +02006411 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6412 ret = skl_build_pipe_wm(state, crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006413 if (ret)
6414 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006415 }
6416
Matt Roperd8e87492018-12-11 09:31:07 -08006417 ret = skl_compute_ddb(state);
6418 if (ret)
6419 return ret;
6420
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006421 ret = intel_compute_sagv_mask(state);
6422 if (ret)
6423 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006424
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006425 /*
6426 * skl_compute_ddb() will have adjusted the final watermarks
6427 * based on how much ddb is available. Now we can actually
6428 * check if the final watermarks changed.
6429 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006430 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006431 ret = skl_wm_add_affected_planes(state, crtc);
6432 if (ret)
6433 return ret;
6434 }
6435
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006436 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006437
Matt Roper98d39492016-05-12 07:06:03 -07006438 return 0;
6439}
6440
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006441static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006442 struct intel_wm_config *config)
6443{
6444 struct intel_crtc *crtc;
6445
6446 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006447 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006448 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6449
6450 if (!wm->pipe_enabled)
6451 continue;
6452
6453 config->sprites_enabled |= wm->sprites_enabled;
6454 config->sprites_scaled |= wm->sprites_scaled;
6455 config->num_pipes_active++;
6456 }
6457}
6458
Matt Ropered4a6a72016-02-23 17:20:13 -08006459static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006460{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006461 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006462 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006463 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006464 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006465 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006466
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006467 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006468
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006469 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6470 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006471
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006472 /* 5/6 split only in single pipe config on IVB+ */
Matt Roper7dadd282021-03-19 21:42:43 -07006473 if (DISPLAY_VER(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006474 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006475 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6476 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006477
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006478 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006479 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006480 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006481 }
6482
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006483 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006484 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006485
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006486 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006487
Imre Deak820c1982013-12-17 14:46:36 +02006488 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006489}
6490
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006491static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006492 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006493{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006494 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6495 const struct intel_crtc_state *crtc_state =
6496 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006497
Matt Ropered4a6a72016-02-23 17:20:13 -08006498 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006499 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006500 ilk_program_watermarks(dev_priv);
6501 mutex_unlock(&dev_priv->wm.wm_mutex);
6502}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006503
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006504static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006505 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006506{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006507 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6508 const struct intel_crtc_state *crtc_state =
6509 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006510
6511 if (!crtc_state->wm.need_postvbl_update)
6512 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006513
6514 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006515 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6516 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006517 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006518}
6519
Jani Nikula81b55ef2020-04-20 17:04:38 +03006520static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006521{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006522 level->enable = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006523 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006524 level->blocks = val & PLANE_WM_BLOCKS_MASK;
Matt Roper1003cee2021-05-14 08:36:54 -07006525 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
Pradeep Bhat30789992014-11-04 17:06:45 +00006526}
6527
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006528void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006529 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006530{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006531 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6532 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006533 int level, max_level;
6534 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006535 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006536
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006537 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006538
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006539 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006540 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006541
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006542 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006543 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006544 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006545 else
Jani Nikula5f461662020-11-30 13:15:58 +02006546 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006547
6548 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6549 }
6550
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006551 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006552 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006553 else
Jani Nikula5f461662020-11-30 13:15:58 +02006554 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006555
6556 skl_wm_level_from_reg_val(val, &wm->trans_wm);
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006557
Matt Roper7959ffe2021-05-18 17:06:11 -07006558 if (HAS_HW_SAGV_WM(dev_priv)) {
6559 if (plane_id != PLANE_CURSOR)
6560 val = intel_uncore_read(&dev_priv->uncore,
6561 PLANE_WM_SAGV(pipe, plane_id));
6562 else
6563 val = intel_uncore_read(&dev_priv->uncore,
6564 CUR_WM_SAGV(pipe));
6565
6566 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6567
6568 if (plane_id != PLANE_CURSOR)
6569 val = intel_uncore_read(&dev_priv->uncore,
6570 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6571 else
6572 val = intel_uncore_read(&dev_priv->uncore,
6573 CUR_WM_SAGV_TRANS(pipe));
6574
6575 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6576 } else if (DISPLAY_VER(dev_priv) >= 12) {
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006577 wm->sagv.wm0 = wm->wm[0];
6578 wm->sagv.trans_wm = wm->trans_wm;
6579 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006580 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006581}
6582
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006583void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006584{
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006585 struct intel_dbuf_state *dbuf_state =
6586 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006587 struct intel_crtc *crtc;
Pradeep Bhat30789992014-11-04 17:06:45 +00006588
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006589 if (IS_ALDERLAKE_P(dev_priv))
6590 dbuf_state->joined_mbus = intel_de_read(dev_priv, MBUS_CTL) & MBUS_JOIN;
6591
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006592 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006593 struct intel_crtc_state *crtc_state =
6594 to_intel_crtc_state(crtc->base.state);
6595 enum pipe pipe = crtc->pipe;
Ville Syrjälä835c1762021-05-18 17:06:16 -07006596 unsigned int mbus_offset;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006597 enum plane_id plane_id;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006598
Maarten Lankhorstec193642019-06-28 10:55:17 +02006599 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
Ville Syrjälädbf71382020-11-06 19:30:38 +02006600 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006601
6602 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6603
6604 for_each_plane_id_on_crtc(crtc, plane_id) {
6605 struct skl_ddb_entry *ddb_y =
6606 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6607 struct skl_ddb_entry *ddb_uv =
6608 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
6609
6610 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6611 plane_id, ddb_y, ddb_uv);
6612
6613 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6614 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_uv);
6615 }
6616
6617 dbuf_state->slices[pipe] =
6618 skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes);
6619
6620 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6621
Ville Syrjälä835c1762021-05-18 17:06:16 -07006622 /*
6623 * Used for checking overlaps, so we need absolute
6624 * offsets instead of MBUS relative offsets.
6625 */
6626 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_state->slices[pipe]);
6627 crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
6628 crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006629
6630 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006631 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x, mbus joined: %s\n",
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006632 crtc->base.base.id, crtc->base.name,
6633 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006634 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
6635 yesno(dbuf_state->joined_mbus));
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006636 }
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006637
6638 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
Pradeep Bhat30789992014-11-04 17:06:45 +00006639}
6640
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006641static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006642{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006643 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006644 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006645 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006646 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6647 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006648 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006649
Jani Nikula5f461662020-11-30 13:15:58 +02006650 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006651
Ville Syrjälä15606532016-05-13 17:55:17 +03006652 memset(active, 0, sizeof(*active));
6653
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006654 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006655
6656 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006657 u32 tmp = hw->wm_pipe[pipe];
6658
6659 /*
6660 * For active pipes LP0 watermark is marked as
6661 * enabled, and LP1+ watermaks as disabled since
6662 * we can't really reverse compute them in case
6663 * multiple pipes are active.
6664 */
6665 active->wm[0].enable = true;
6666 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6667 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6668 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006669 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006670 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006671
6672 /*
6673 * For inactive pipes, all watermark levels
6674 * should be marked as enabled but zeroed,
6675 * which is what we'd compute them to.
6676 */
6677 for (level = 0; level <= max_level; level++)
6678 active->wm[level].enable = true;
6679 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006680
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006681 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006682}
6683
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006684#define _FW_WM(value, plane) \
6685 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6686#define _FW_WM_VLV(value, plane) \
6687 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6688
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006689static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6690 struct g4x_wm_values *wm)
6691{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006692 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006693
Jani Nikula5f461662020-11-30 13:15:58 +02006694 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006695 wm->sr.plane = _FW_WM(tmp, SR);
6696 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6697 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6698 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6699
Jani Nikula5f461662020-11-30 13:15:58 +02006700 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006701 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6702 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6703 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6704 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6705 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6706 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6707
Jani Nikula5f461662020-11-30 13:15:58 +02006708 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006709 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6710 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6711 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6712 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6713}
6714
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006715static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6716 struct vlv_wm_values *wm)
6717{
6718 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006719 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006720
6721 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006722 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006723
Ville Syrjälä1b313892016-11-28 19:37:08 +02006724 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006725 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006726 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006727 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006728 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006729 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006730 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006731 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6732 }
6733
Jani Nikula5f461662020-11-30 13:15:58 +02006734 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006735 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006736 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6737 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6738 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006739
Jani Nikula5f461662020-11-30 13:15:58 +02006740 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006741 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6742 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6743 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006744
Jani Nikula5f461662020-11-30 13:15:58 +02006745 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006746 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6747
6748 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02006749 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006750 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6751 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006752
Jani Nikula5f461662020-11-30 13:15:58 +02006753 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006754 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6755 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006756
Jani Nikula5f461662020-11-30 13:15:58 +02006757 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006758 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6759 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006760
Jani Nikula5f461662020-11-30 13:15:58 +02006761 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006762 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006763 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6764 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6765 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6766 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6767 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6768 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6769 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6770 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6771 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006772 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02006773 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006774 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6775 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006776
Jani Nikula5f461662020-11-30 13:15:58 +02006777 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006778 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006779 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6780 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6781 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6782 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6783 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6784 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006785 }
6786}
6787
6788#undef _FW_WM
6789#undef _FW_WM_VLV
6790
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006791void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006792{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006793 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6794 struct intel_crtc *crtc;
6795
6796 g4x_read_wm_values(dev_priv, wm);
6797
Jani Nikula5f461662020-11-30 13:15:58 +02006798 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006799
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006800 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006801 struct intel_crtc_state *crtc_state =
6802 to_intel_crtc_state(crtc->base.state);
6803 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6804 struct g4x_pipe_wm *raw;
6805 enum pipe pipe = crtc->pipe;
6806 enum plane_id plane_id;
6807 int level, max_level;
6808
6809 active->cxsr = wm->cxsr;
6810 active->hpll_en = wm->hpll_en;
6811 active->fbc_en = wm->fbc_en;
6812
6813 active->sr = wm->sr;
6814 active->hpll = wm->hpll;
6815
6816 for_each_plane_id_on_crtc(crtc, plane_id) {
6817 active->wm.plane[plane_id] =
6818 wm->pipe[pipe].plane[plane_id];
6819 }
6820
6821 if (wm->cxsr && wm->hpll_en)
6822 max_level = G4X_WM_LEVEL_HPLL;
6823 else if (wm->cxsr)
6824 max_level = G4X_WM_LEVEL_SR;
6825 else
6826 max_level = G4X_WM_LEVEL_NORMAL;
6827
6828 level = G4X_WM_LEVEL_NORMAL;
6829 raw = &crtc_state->wm.g4x.raw[level];
6830 for_each_plane_id_on_crtc(crtc, plane_id)
6831 raw->plane[plane_id] = active->wm.plane[plane_id];
6832
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006833 level = G4X_WM_LEVEL_SR;
6834 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006835 goto out;
6836
6837 raw = &crtc_state->wm.g4x.raw[level];
6838 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6839 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6840 raw->plane[PLANE_SPRITE0] = 0;
6841 raw->fbc = active->sr.fbc;
6842
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006843 level = G4X_WM_LEVEL_HPLL;
6844 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006845 goto out;
6846
6847 raw = &crtc_state->wm.g4x.raw[level];
6848 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6849 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6850 raw->plane[PLANE_SPRITE0] = 0;
6851 raw->fbc = active->hpll.fbc;
6852
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006853 level++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006854 out:
6855 for_each_plane_id_on_crtc(crtc, plane_id)
6856 g4x_raw_plane_wm_set(crtc_state, level,
6857 plane_id, USHRT_MAX);
6858 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6859
6860 crtc_state->wm.g4x.optimal = *active;
6861 crtc_state->wm.g4x.intermediate = *active;
6862
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006863 drm_dbg_kms(&dev_priv->drm,
6864 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6865 pipe_name(pipe),
6866 wm->pipe[pipe].plane[PLANE_PRIMARY],
6867 wm->pipe[pipe].plane[PLANE_CURSOR],
6868 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006869 }
6870
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006871 drm_dbg_kms(&dev_priv->drm,
6872 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6873 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6874 drm_dbg_kms(&dev_priv->drm,
6875 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6876 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6877 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6878 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006879}
6880
6881void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6882{
6883 struct intel_plane *plane;
6884 struct intel_crtc *crtc;
6885
6886 mutex_lock(&dev_priv->wm.wm_mutex);
6887
6888 for_each_intel_plane(&dev_priv->drm, plane) {
6889 struct intel_crtc *crtc =
6890 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6891 struct intel_crtc_state *crtc_state =
6892 to_intel_crtc_state(crtc->base.state);
6893 struct intel_plane_state *plane_state =
6894 to_intel_plane_state(plane->base.state);
6895 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6896 enum plane_id plane_id = plane->id;
6897 int level;
6898
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006899 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006900 continue;
6901
6902 for (level = 0; level < 3; level++) {
6903 struct g4x_pipe_wm *raw =
6904 &crtc_state->wm.g4x.raw[level];
6905
6906 raw->plane[plane_id] = 0;
6907 wm_state->wm.plane[plane_id] = 0;
6908 }
6909
6910 if (plane_id == PLANE_PRIMARY) {
6911 for (level = 0; level < 3; level++) {
6912 struct g4x_pipe_wm *raw =
6913 &crtc_state->wm.g4x.raw[level];
6914 raw->fbc = 0;
6915 }
6916
6917 wm_state->sr.fbc = 0;
6918 wm_state->hpll.fbc = 0;
6919 wm_state->fbc_en = false;
6920 }
6921 }
6922
6923 for_each_intel_crtc(&dev_priv->drm, crtc) {
6924 struct intel_crtc_state *crtc_state =
6925 to_intel_crtc_state(crtc->base.state);
6926
6927 crtc_state->wm.g4x.intermediate =
6928 crtc_state->wm.g4x.optimal;
6929 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6930 }
6931
6932 g4x_program_watermarks(dev_priv);
6933
6934 mutex_unlock(&dev_priv->wm.wm_mutex);
6935}
6936
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006937void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006938{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006939 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006940 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006941 u32 val;
6942
6943 vlv_read_wm_values(dev_priv, wm);
6944
Jani Nikula5f461662020-11-30 13:15:58 +02006945 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006946 wm->level = VLV_WM_LEVEL_PM2;
6947
6948 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006949 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006950
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006951 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006952 if (val & DSP_MAXFIFO_PM5_ENABLE)
6953 wm->level = VLV_WM_LEVEL_PM5;
6954
Ville Syrjälä58590c12015-09-08 21:05:12 +03006955 /*
6956 * If DDR DVFS is disabled in the BIOS, Punit
6957 * will never ack the request. So if that happens
6958 * assume we don't have to enable/disable DDR DVFS
6959 * dynamically. To test that just set the REQ_ACK
6960 * bit to poke the Punit, but don't change the
6961 * HIGH/LOW bits so that we don't actually change
6962 * the current state.
6963 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006964 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006965 val |= FORCE_DDR_FREQ_REQ_ACK;
6966 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6967
6968 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6969 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006970 drm_dbg_kms(&dev_priv->drm,
6971 "Punit not acking DDR DVFS request, "
6972 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006973 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6974 } else {
6975 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6976 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6977 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6978 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006979
Chris Wilson337fa6e2019-04-26 09:17:20 +01006980 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006981 }
6982
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006983 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006984 struct intel_crtc_state *crtc_state =
6985 to_intel_crtc_state(crtc->base.state);
6986 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6987 const struct vlv_fifo_state *fifo_state =
6988 &crtc_state->wm.vlv.fifo_state;
6989 enum pipe pipe = crtc->pipe;
6990 enum plane_id plane_id;
6991 int level;
6992
6993 vlv_get_fifo_size(crtc_state);
6994
6995 active->num_levels = wm->level + 1;
6996 active->cxsr = wm->cxsr;
6997
Ville Syrjäläff32c542017-03-02 19:14:57 +02006998 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006999 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02007000 &crtc_state->wm.vlv.raw[level];
7001
7002 active->sr[level].plane = wm->sr.plane;
7003 active->sr[level].cursor = wm->sr.cursor;
7004
7005 for_each_plane_id_on_crtc(crtc, plane_id) {
7006 active->wm[level].plane[plane_id] =
7007 wm->pipe[pipe].plane[plane_id];
7008
7009 raw->plane[plane_id] =
7010 vlv_invert_wm_value(active->wm[level].plane[plane_id],
7011 fifo_state->plane[plane_id]);
7012 }
7013 }
7014
7015 for_each_plane_id_on_crtc(crtc, plane_id)
7016 vlv_raw_plane_wm_set(crtc_state, level,
7017 plane_id, USHRT_MAX);
7018 vlv_invalidate_wms(crtc, active, level);
7019
7020 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007021 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007022
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007023 drm_dbg_kms(&dev_priv->drm,
7024 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
7025 pipe_name(pipe),
7026 wm->pipe[pipe].plane[PLANE_PRIMARY],
7027 wm->pipe[pipe].plane[PLANE_CURSOR],
7028 wm->pipe[pipe].plane[PLANE_SPRITE0],
7029 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007030 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007031
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007032 drm_dbg_kms(&dev_priv->drm,
7033 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
7034 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007035}
7036
Ville Syrjälä602ae832017-03-02 19:15:02 +02007037void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
7038{
7039 struct intel_plane *plane;
7040 struct intel_crtc *crtc;
7041
7042 mutex_lock(&dev_priv->wm.wm_mutex);
7043
7044 for_each_intel_plane(&dev_priv->drm, plane) {
7045 struct intel_crtc *crtc =
7046 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
7047 struct intel_crtc_state *crtc_state =
7048 to_intel_crtc_state(crtc->base.state);
7049 struct intel_plane_state *plane_state =
7050 to_intel_plane_state(plane->base.state);
7051 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
7052 const struct vlv_fifo_state *fifo_state =
7053 &crtc_state->wm.vlv.fifo_state;
7054 enum plane_id plane_id = plane->id;
7055 int level;
7056
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01007057 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02007058 continue;
7059
7060 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03007061 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02007062 &crtc_state->wm.vlv.raw[level];
7063
7064 raw->plane[plane_id] = 0;
7065
7066 wm_state->wm[level].plane[plane_id] =
7067 vlv_invert_wm_value(raw->plane[plane_id],
7068 fifo_state->plane[plane_id]);
7069 }
7070 }
7071
7072 for_each_intel_crtc(&dev_priv->drm, crtc) {
7073 struct intel_crtc_state *crtc_state =
7074 to_intel_crtc_state(crtc->base.state);
7075
7076 crtc_state->wm.vlv.intermediate =
7077 crtc_state->wm.vlv.optimal;
7078 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
7079 }
7080
7081 vlv_program_watermarks(dev_priv);
7082
7083 mutex_unlock(&dev_priv->wm.wm_mutex);
7084}
7085
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007086/*
7087 * FIXME should probably kill this and improve
7088 * the real watermark readout/sanitation instead
7089 */
7090static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
7091{
Jani Nikula5f461662020-11-30 13:15:58 +02007092 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM1_LP_SR_EN);
7093 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM1_LP_SR_EN);
7094 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK) & ~WM1_LP_SR_EN);
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007095
7096 /*
7097 * Don't touch WM1S_LP_EN here.
7098 * Doing so could cause underruns.
7099 */
7100}
7101
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007102void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007103{
Imre Deak820c1982013-12-17 14:46:36 +02007104 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007105 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007106
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007107 ilk_init_lp_watermarks(dev_priv);
7108
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007109 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007110 ilk_pipe_wm_get_hw_state(crtc);
7111
Jani Nikula5f461662020-11-30 13:15:58 +02007112 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
7113 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
7114 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007115
Jani Nikula5f461662020-11-30 13:15:58 +02007116 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
Matt Roper7dadd282021-03-19 21:42:43 -07007117 if (DISPLAY_VER(dev_priv) >= 7) {
Jani Nikula5f461662020-11-30 13:15:58 +02007118 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
7119 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02007120 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007121
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007122 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007123 hw->partitioning = (intel_uncore_read(&dev_priv->uncore, WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
Ville Syrjäläac9545f2013-12-05 15:51:28 +02007124 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01007125 else if (IS_IVYBRIDGE(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007126 hw->partitioning = (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
Ville Syrjäläac9545f2013-12-05 15:51:28 +02007127 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007128
7129 hw->enable_fbc_wm =
Jani Nikula5f461662020-11-30 13:15:58 +02007130 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007131}
7132
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307133void intel_enable_ipc(struct drm_i915_private *dev_priv)
7134{
7135 u32 val;
7136
José Roberto de Souzafd847b82018-09-18 13:47:11 -07007137 if (!HAS_IPC(dev_priv))
7138 return;
7139
Jani Nikula5f461662020-11-30 13:15:58 +02007140 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307141
7142 if (dev_priv->ipc_enabled)
7143 val |= DISP_IPC_ENABLE;
7144 else
7145 val &= ~DISP_IPC_ENABLE;
7146
Jani Nikula5f461662020-11-30 13:15:58 +02007147 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307148}
7149
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007150static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
7151{
7152 /* Display WA #0477 WaDisableIPC: skl */
7153 if (IS_SKYLAKE(dev_priv))
7154 return false;
7155
7156 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01007157 if (IS_KABYLAKE(dev_priv) ||
7158 IS_COFFEELAKE(dev_priv) ||
7159 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007160 return dev_priv->dram_info.symmetric_memory;
7161
7162 return true;
7163}
7164
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307165void intel_init_ipc(struct drm_i915_private *dev_priv)
7166{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307167 if (!HAS_IPC(dev_priv))
7168 return;
7169
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007170 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07007171
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307172 intel_enable_ipc(dev_priv);
7173}
7174
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007175static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007176{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007177 /*
7178 * On Ibex Peak and Cougar Point, we need to disable clock
7179 * gating for the panel power sequencer or it will fail to
7180 * start up when no ports are active.
7181 */
Jani Nikula5f461662020-11-30 13:15:58 +02007182 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007183}
7184
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007185static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007186{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007187 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007188
Damien Lespiau055e3932014-08-18 13:49:10 +01007189 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007190 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
7191 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007192 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007193
Jani Nikula5f461662020-11-30 13:15:58 +02007194 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
7195 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007196 }
7197}
7198
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007199static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007200{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007201 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007202
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007203 /*
7204 * Required for FBC
7205 * WaFbcDisableDpfcClockGating:ilk
7206 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007207 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7208 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7209 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007210
Jani Nikula5f461662020-11-30 13:15:58 +02007211 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007212 MARIUNIT_CLOCK_GATE_DISABLE |
7213 SVSMUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007214 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007215 VFMUNIT_CLOCK_GATE_DISABLE);
7216
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007217 /*
7218 * According to the spec the following bits should be set in
7219 * order to enable memory self-refresh
7220 * The bit 22/21 of 0x42004
7221 * The bit 5 of 0x42020
7222 * The bit 15 of 0x45000
7223 */
Jani Nikula5f461662020-11-30 13:15:58 +02007224 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7225 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007226 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007227 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007228 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
7229 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007230 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007231
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007232 /*
7233 * Based on the document from hardware guys the following bits
7234 * should be set unconditionally in order to enable FBC.
7235 * The bit 22 of 0x42000
7236 * The bit 22 of 0x42004
7237 * The bit 7,8,9 of 0x42020.
7238 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007239 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007240 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Jani Nikula5f461662020-11-30 13:15:58 +02007241 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7242 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007243 ILK_FBCQ_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007244 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7245 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007246 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007247 }
7248
Jani Nikula5f461662020-11-30 13:15:58 +02007249 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007250
Jani Nikula5f461662020-11-30 13:15:58 +02007251 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7252 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007253 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05307254
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007255 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007256
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007257 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007258}
7259
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007260static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007261{
Ville Syrjäläd048a262019-08-21 20:30:31 +03007262 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007263 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007264
7265 /*
7266 * On Ibex Peak and Cougar Point, we need to disable clock
7267 * gating for the panel power sequencer or it will fail to
7268 * start up when no ports are active.
7269 */
Jani Nikula5f461662020-11-30 13:15:58 +02007270 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnescd664072013-10-02 10:34:19 -07007271 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7272 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007273 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
Daniel Vetter3107bd42012-10-31 22:52:31 +01007274 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007275 /* The below fixes the weird display corruption, a few pixels shifted
7276 * downward, on (only) LVDS of some HP laptops with IVY.
7277 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007278 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007279 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007280 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7281 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007282 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007283 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007284 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7285 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Jani Nikula5f461662020-11-30 13:15:58 +02007286 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007287 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007288 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007289 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007290 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
Daniel Vetter3107bd42012-10-31 22:52:31 +01007291 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7292 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007293}
7294
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007295static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007296{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007297 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007298
Jani Nikula5f461662020-11-30 13:15:58 +02007299 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007300 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007301 drm_dbg_kms(&dev_priv->drm,
7302 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7303 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007304}
7305
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007306static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007307{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007308 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007309
Jani Nikula5f461662020-11-30 13:15:58 +02007310 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007311
Jani Nikula5f461662020-11-30 13:15:58 +02007312 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7313 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007314 ILK_ELPIN_409_SELECT);
7315
Jani Nikula5f461662020-11-30 13:15:58 +02007316 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7317 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007318 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7319 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7320
7321 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7322 * gating disable must be set. Failure to set it results in
7323 * flickering pixels due to Z write ordering failures after
7324 * some amount of runtime in the Mesa "fire" demo, and Unigine
7325 * Sanctuary and Tropics, and apparently anything else with
7326 * alpha test or pixel discard.
7327 *
7328 * According to the spec, bit 11 (RCCUNIT) must also be set,
7329 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007330 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007331 * WaDisableRCCUnitClockGating:snb
7332 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007333 */
Jani Nikula5f461662020-11-30 13:15:58 +02007334 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007335 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7336 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7337
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007338 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007339 * According to the spec the following bits should be
7340 * set in order to enable memory self-refresh and fbc:
7341 * The bit21 and bit22 of 0x42000
7342 * The bit21 and bit22 of 0x42004
7343 * The bit5 and bit7 of 0x42020
7344 * The bit14 of 0x70180
7345 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007346 *
7347 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007348 */
Jani Nikula5f461662020-11-30 13:15:58 +02007349 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7350 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007351 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007352 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7353 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007354 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Jani Nikula5f461662020-11-30 13:15:58 +02007355 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7356 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
Damien Lespiau231e54f2012-10-19 17:55:41 +01007357 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7358 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007359
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007360 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007361
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007362 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007363
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007364 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007365}
7366
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007367static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007368{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007369 /*
7370 * TODO: this bit should only be enabled when really needed, then
7371 * disabled when not needed anymore in order to save power.
7372 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007373 if (HAS_PCH_LPT_LP(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007374 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7375 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007376 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007377
7378 /* WADPOClockGatingDisable:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007379 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7380 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007381 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007382}
7383
Ville Syrjälä712bf362016-10-31 22:37:23 +02007384static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007385{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007386 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02007387 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007388
7389 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007390 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
Imre Deak7d708ee2013-04-17 14:04:50 +03007391 }
7392}
7393
Imre Deak450174f2016-05-03 15:54:21 +03007394static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7395 int general_prio_credits,
7396 int high_prio_credits)
7397{
7398 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007399 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007400
7401 /* WaTempDisableDOPClkGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007402 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7403 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
Imre Deak450174f2016-05-03 15:54:21 +03007404
Jani Nikula5f461662020-11-30 13:15:58 +02007405 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Oscar Mateo930a7842017-10-17 13:25:45 -07007406 val &= ~L3_PRIO_CREDITS_MASK;
7407 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7408 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
Jani Nikula5f461662020-11-30 13:15:58 +02007409 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007410
7411 /*
7412 * Wait at least 100 clocks before re-enabling clock gating.
7413 * See the definition of L3SQCREG1 in BSpec.
7414 */
Jani Nikula5f461662020-11-30 13:15:58 +02007415 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Imre Deak450174f2016-05-03 15:54:21 +03007416 udelay(1);
Jani Nikula5f461662020-11-30 13:15:58 +02007417 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
Imre Deak450174f2016-05-03 15:54:21 +03007418}
7419
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007420static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7421{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007422 /* Wa_1409120013:icl,ehl */
Jani Nikula5f461662020-11-30 13:15:58 +02007423 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä885f1822020-07-08 16:12:20 +03007424 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7425
Matt Atwood6f4194c2020-01-13 23:11:28 -05007426 /*Wa_14010594013:icl, ehl */
7427 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
Lucas De Marchidbac4f32021-07-28 14:59:38 -07007428 0, ICL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007429}
7430
José Roberto de Souza35f08372021-01-13 05:37:59 -08007431static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
Michel Thierry5d869232019-08-23 01:20:34 -07007432{
José Roberto de Souzac4924052021-07-12 17:38:50 -07007433 /* Wa_1409120013:tgl,rkl,adl-s,dg1 */
Clint Taylor8c209f42021-06-08 10:47:21 -07007434 if (IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv) ||
7435 IS_ALDERLAKE_S(dev_priv) || IS_DG1(dev_priv))
7436 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
7437 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007438
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007439 /* Wa_1409825376:tgl (pre-prod)*/
Matt Roper46b0d702021-07-16 22:14:26 -07007440 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007441 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007442 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007443
José Roberto de Souza41c70d22021-04-08 13:49:16 -07007444 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7445 if (DISPLAY_VER(dev_priv) == 12)
7446 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7447 CLKREQ_POLICY_MEM_UP_OVRD, 0);
Michel Thierry5d869232019-08-23 01:20:34 -07007448}
7449
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007450static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7451{
7452 gen12lp_init_clock_gating(dev_priv);
7453
7454 /* Wa_22011091694:adlp */
7455 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7456}
7457
Stuart Summersda9427502020-10-14 12:19:34 -07007458static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7459{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007460 gen12lp_init_clock_gating(dev_priv);
7461
Stuart Summersda9427502020-10-14 12:19:34 -07007462 /* Wa_1409836686:dg1[a0] */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007463 if (IS_DG1_GT_STEP(dev_priv, STEP_A0, STEP_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007464 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
Stuart Summersda9427502020-10-14 12:19:34 -07007465 DPT_GATING_DIS);
7466}
7467
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007468static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7469{
7470 if (!HAS_PCH_CNP(dev_priv))
7471 return;
7472
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007473 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Jani Nikula5f461662020-11-30 13:15:58 +02007474 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07007475 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007476}
7477
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007478static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7479{
7480 cnp_init_clock_gating(dev_priv);
7481 gen9_init_clock_gating(dev_priv);
7482
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007483 /* WAC6entrylatency:cfl */
Jani Nikula5f461662020-11-30 13:15:58 +02007484 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007485 FBC_LLC_FULLY_OPEN);
7486
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007487 /*
7488 * WaFbcTurnOffFbcWatermark:cfl
7489 * Display WA #0562: cfl
7490 */
Jani Nikula5f461662020-11-30 13:15:58 +02007491 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007492 DISP_FBC_WM_DIS);
7493
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007494 /*
7495 * WaFbcNukeOnHostModify:cfl
7496 * Display WA #0873: cfl
7497 */
Jani Nikula5f461662020-11-30 13:15:58 +02007498 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007499 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7500}
7501
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007502static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007503{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007504 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007505
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007506 /* WAC6entrylatency:kbl */
Jani Nikula5f461662020-11-30 13:15:58 +02007507 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007508 FBC_LLC_FULLY_OPEN);
7509
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007510 /* WaDisableSDEUnitClockGating:kbl */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007511 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007512 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007513 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007514
7515 /* WaDisableGamClockGating:kbl */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007516 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007517 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007518 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007519
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007520 /*
7521 * WaFbcTurnOffFbcWatermark:kbl
7522 * Display WA #0562: kbl
7523 */
Jani Nikula5f461662020-11-30 13:15:58 +02007524 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007525 DISP_FBC_WM_DIS);
7526
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007527 /*
7528 * WaFbcNukeOnHostModify:kbl
7529 * Display WA #0873: kbl
7530 */
Jani Nikula5f461662020-11-30 13:15:58 +02007531 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007532 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007533}
7534
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007535static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007536{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007537 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007538
Ville Syrjäläf1421192020-07-16 22:04:25 +03007539 /* WaDisableDopClockGating:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007540 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL) &
Ville Syrjäläf1421192020-07-16 22:04:25 +03007541 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7542
Mika Kuoppala44fff992016-06-07 17:19:09 +03007543 /* WAC6entrylatency:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007544 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
Mika Kuoppala44fff992016-06-07 17:19:09 +03007545 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007546
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007547 /*
7548 * WaFbcTurnOffFbcWatermark:skl
7549 * Display WA #0562: skl
7550 */
Jani Nikula5f461662020-11-30 13:15:58 +02007551 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007552 DISP_FBC_WM_DIS);
7553
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007554 /*
7555 * WaFbcNukeOnHostModify:skl
7556 * Display WA #0873: skl
7557 */
Jani Nikula5f461662020-11-30 13:15:58 +02007558 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007559 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007560
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007561 /*
7562 * WaFbcHighMemBwCorruptionAvoidance:skl
7563 * Display WA #0883: skl
7564 */
Jani Nikula5f461662020-11-30 13:15:58 +02007565 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007566 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007567}
7568
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007569static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007570{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007571 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007572
Ville Syrjälä885f1822020-07-08 16:12:20 +03007573 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007574 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7575 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007576 HSW_FBCQ_DIS);
7577
Ben Widawskyab57fff2013-12-12 15:28:04 -08007578 /* WaSwitchSolVfFArbitrationPriority:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007579 intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007580
Ben Widawskyab57fff2013-12-12 15:28:04 -08007581 /* WaPsrDPAMaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007582 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7583 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007584
Damien Lespiau055e3932014-08-18 13:49:10 +01007585 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007586 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007587 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7588 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007589 BDW_DPRS_MASK_VBLANK_SRD);
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007590
7591 /* Undocumented but fixes async flip + VT-d corruption */
7592 if (intel_vtd_active())
7593 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7594 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007595 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007596
Ben Widawskyab57fff2013-12-12 15:28:04 -08007597 /* WaVSRefCountFullforceMissDisable:bdw */
7598 /* WaDSRefCountFullforceMissDisable:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007599 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7600 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ben Widawskyab57fff2013-12-12 15:28:04 -08007601 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007602
Jani Nikula5f461662020-11-30 13:15:58 +02007603 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007604 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007605
7606 /* WaDisableSDEUnitClockGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007607 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007608 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007609
Imre Deak450174f2016-05-03 15:54:21 +03007610 /* WaProgramL3SqcReg1Default:bdw */
7611 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007612
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007613 /* WaKVMNotificationOnConfigChange:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007614 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR2_1, intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR2_1)
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007615 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7616
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007617 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007618
7619 /* WaDisableDopClockGating:bdw
7620 *
7621 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7622 * clock gating.
7623 */
Jani Nikula5f461662020-11-30 13:15:58 +02007624 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7625 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007626}
7627
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007628static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007629{
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007630 enum pipe pipe;
7631
Ville Syrjälä885f1822020-07-08 16:12:20 +03007632 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007633 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7634 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007635 HSW_FBCQ_DIS);
7636
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007637 for_each_pipe(dev_priv, pipe) {
7638 /* Undocumented but fixes async flip + VT-d corruption */
7639 if (intel_vtd_active())
7640 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7641 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
7642 }
7643
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007644 /* This is required by WaCatErrorRejectionIssue:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007645 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7646 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007647 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007648
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007649 /* WaSwitchSolVfFArbitrationPriority:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007650 intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskye3dff582013-03-20 14:49:14 -07007651
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007652 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007653}
7654
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007655static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007656{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007657 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007658
Jani Nikula5f461662020-11-30 13:15:58 +02007659 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007660
Ville Syrjälä885f1822020-07-08 16:12:20 +03007661 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007662 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7663 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007664 ILK_FBCQ_DIS);
7665
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007666 /* WaDisableBackToBackFlipFix:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007667 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007668 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7669 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7670
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007671 if (IS_IVB_GT1(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007672 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007673 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007674 else {
7675 /* must write both registers */
Jani Nikula5f461662020-11-30 13:15:58 +02007676 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Ville Syrjälä412236c2014-01-22 21:32:44 +02007677 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jani Nikula5f461662020-11-30 13:15:58 +02007678 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007679 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007680 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007681
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007682 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007683 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007684 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007685 */
Jani Nikula5f461662020-11-30 13:15:58 +02007686 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007687 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007688
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007689 /* This is required by WaCatErrorRejectionIssue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007690 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7691 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007692 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7693
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007694 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007695
Jani Nikula5f461662020-11-30 13:15:58 +02007696 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
Ben Widawsky20848222012-05-04 18:58:59 -07007697 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7698 snpcr |= GEN6_MBC_SNPCR_MED;
Jani Nikula5f461662020-11-30 13:15:58 +02007699 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007700
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007701 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007702 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007703
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007704 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007705}
7706
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007707static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007708{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007709 /* WaDisableBackToBackFlipFix:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007710 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007711 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7712 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7713
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007714 /* WaDisableDopClockGating:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007715 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007716 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7717
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007718 /* This is required by WaCatErrorRejectionIssue:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007719 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7720 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007721 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7722
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007723 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007724 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007725 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007726 */
Jani Nikula5f461662020-11-30 13:15:58 +02007727 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007728 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007729
Akash Goelc98f5062014-03-24 23:00:07 +05307730 /* WaDisableL3Bank2xClockGate:vlv
7731 * Disabling L3 clock gating- MMIO 940c[25] = 1
7732 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
Jani Nikula5f461662020-11-30 13:15:58 +02007733 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7734 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007735
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007736 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007737 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007738 * Disable clock gating on th GCFG unit to prevent a delay
7739 * in the reporting of vblank events.
7740 */
Jani Nikula5f461662020-11-30 13:15:58 +02007741 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007742}
7743
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007744static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007745{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007746 /* WaVSRefCountFullforceMissDisable:chv */
7747 /* WaDSRefCountFullforceMissDisable:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007748 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7749 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ville Syrjälä232ce332014-04-09 13:28:35 +03007750 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007751
7752 /* WaDisableSemaphoreAndSyncFlipWait:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007753 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007754 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007755
7756 /* WaDisableCSUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007757 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Ville Syrjälä08466972014-04-09 13:28:37 +03007758 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007759
7760 /* WaDisableSDEUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007761 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ville Syrjäläc6317802014-04-09 13:28:38 +03007762 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007763
7764 /*
Imre Deak450174f2016-05-03 15:54:21 +03007765 * WaProgramL3SqcReg1Default:chv
7766 * See gfxspecs/Related Documents/Performance Guide/
7767 * LSQC Setting Recommendations.
7768 */
7769 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007770}
7771
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007772static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007773{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007774 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007775
Jani Nikula5f461662020-11-30 13:15:58 +02007776 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7777 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007778 GS_UNIT_CLOCK_GATE_DISABLE |
7779 CL_UNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007780 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007781 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7782 OVRUNIT_CLOCK_GATE_DISABLE |
7783 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007784 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007785 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007786 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007787
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007788 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007789}
7790
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007791static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007792{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007793 struct intel_uncore *uncore = &dev_priv->uncore;
7794
7795 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7796 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7797 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7798 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7799 intel_uncore_write16(uncore, DEUC, 0);
7800 intel_uncore_write(uncore,
7801 MI_ARB_STATE,
7802 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007803}
7804
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007805static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007806{
Jani Nikula5f461662020-11-30 13:15:58 +02007807 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007808 I965_RCC_CLOCK_GATE_DISABLE |
7809 I965_RCPB_CLOCK_GATE_DISABLE |
7810 I965_ISC_CLOCK_GATE_DISABLE |
7811 I965_FBC_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007812 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7813 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä20f94962013-06-07 10:47:02 +03007814 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007815}
7816
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007817static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007818{
Jani Nikula5f461662020-11-30 13:15:58 +02007819 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007820
7821 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7822 DSTATE_DOT_CLOCK_GATING;
Jani Nikula5f461662020-11-30 13:15:58 +02007823 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007824
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007825 if (IS_PINEVIEW(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007826 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007827
7828 /* IIR "flip pending" means done if this bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007829 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007830
7831 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007832 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007833
7834 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007835 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007836
Jani Nikula5f461662020-11-30 13:15:58 +02007837 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007838 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007839}
7840
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007841static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007842{
Jani Nikula5f461662020-11-30 13:15:58 +02007843 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007844
7845 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007846 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007847 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007848
Jani Nikula5f461662020-11-30 13:15:58 +02007849 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007850 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007851
7852 /*
7853 * Have FBC ignore 3D activity since we use software
7854 * render tracking, and otherwise a pure 3D workload
7855 * (even if it just renders a single frame and then does
7856 * abosultely nothing) would not allow FBC to recompress
7857 * until a 2D blit occurs.
7858 */
Jani Nikula5f461662020-11-30 13:15:58 +02007859 intel_uncore_write(&dev_priv->uncore, SCPD0,
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007860 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007861}
7862
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007863static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007864{
Jani Nikula5f461662020-11-30 13:15:58 +02007865 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007866 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7867 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007868}
7869
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007870void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007871{
Dave Airlieeba4b792021-09-29 01:58:07 +03007872 dev_priv->clock_gating_funcs->init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007873}
7874
Ville Syrjälä712bf362016-10-31 22:37:23 +02007875void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007876{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007877 if (HAS_PCH_LPT(dev_priv))
7878 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007879}
7880
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007881static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007882{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007883 drm_dbg_kms(&dev_priv->drm,
7884 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007885}
7886
Dave Airlieeba4b792021-09-29 01:58:07 +03007887#define CG_FUNCS(platform) \
7888static const struct drm_i915_clock_gating_funcs platform##_clock_gating_funcs = { \
7889 .init_clock_gating = platform##_init_clock_gating, \
7890}
7891
7892CG_FUNCS(adlp);
7893CG_FUNCS(dg1);
7894CG_FUNCS(gen12lp);
7895CG_FUNCS(icl);
7896CG_FUNCS(cfl);
7897CG_FUNCS(skl);
7898CG_FUNCS(kbl);
7899CG_FUNCS(bxt);
7900CG_FUNCS(glk);
7901CG_FUNCS(bdw);
7902CG_FUNCS(chv);
7903CG_FUNCS(hsw);
7904CG_FUNCS(ivb);
7905CG_FUNCS(vlv);
7906CG_FUNCS(gen6);
7907CG_FUNCS(ilk);
7908CG_FUNCS(g4x);
7909CG_FUNCS(i965gm);
7910CG_FUNCS(i965g);
7911CG_FUNCS(gen3);
7912CG_FUNCS(i85x);
7913CG_FUNCS(i830);
7914CG_FUNCS(nop);
7915#undef CG_FUNCS
7916
Imre Deakbb400da2016-03-16 13:38:54 +02007917/**
7918 * intel_init_clock_gating_hooks - setup the clock gating hooks
7919 * @dev_priv: device private
7920 *
7921 * Setup the hooks that configure which clocks of a given platform can be
7922 * gated and also apply various GT and display specific workarounds for these
7923 * platforms. Note that some GT specific workarounds are applied separately
7924 * when GPU contexts or batchbuffers start their execution.
7925 */
7926void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7927{
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007928 if (IS_ALDERLAKE_P(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007929 dev_priv->clock_gating_funcs = &adlp_clock_gating_funcs;
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007930 else if (IS_DG1(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007931 dev_priv->clock_gating_funcs = &dg1_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007932 else if (GRAPHICS_VER(dev_priv) == 12)
Dave Airlieeba4b792021-09-29 01:58:07 +03007933 dev_priv->clock_gating_funcs = &gen12lp_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007934 else if (GRAPHICS_VER(dev_priv) == 11)
Dave Airlieeba4b792021-09-29 01:58:07 +03007935 dev_priv->clock_gating_funcs = &icl_clock_gating_funcs;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007936 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007937 dev_priv->clock_gating_funcs = &cfl_clock_gating_funcs;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007938 else if (IS_SKYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007939 dev_priv->clock_gating_funcs = &skl_clock_gating_funcs;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007940 else if (IS_KABYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007941 dev_priv->clock_gating_funcs = &kbl_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007942 else if (IS_BROXTON(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007943 dev_priv->clock_gating_funcs = &bxt_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007944 else if (IS_GEMINILAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007945 dev_priv->clock_gating_funcs = &glk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007946 else if (IS_BROADWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007947 dev_priv->clock_gating_funcs = &bdw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007948 else if (IS_CHERRYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007949 dev_priv->clock_gating_funcs = &chv_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007950 else if (IS_HASWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007951 dev_priv->clock_gating_funcs = &hsw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007952 else if (IS_IVYBRIDGE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007953 dev_priv->clock_gating_funcs = &ivb_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007954 else if (IS_VALLEYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007955 dev_priv->clock_gating_funcs = &vlv_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007956 else if (GRAPHICS_VER(dev_priv) == 6)
Dave Airlieeba4b792021-09-29 01:58:07 +03007957 dev_priv->clock_gating_funcs = &gen6_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007958 else if (GRAPHICS_VER(dev_priv) == 5)
Dave Airlieeba4b792021-09-29 01:58:07 +03007959 dev_priv->clock_gating_funcs = &ilk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007960 else if (IS_G4X(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007961 dev_priv->clock_gating_funcs = &g4x_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007962 else if (IS_I965GM(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007963 dev_priv->clock_gating_funcs = &i965gm_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007964 else if (IS_I965G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007965 dev_priv->clock_gating_funcs = &i965g_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007966 else if (GRAPHICS_VER(dev_priv) == 3)
Dave Airlieeba4b792021-09-29 01:58:07 +03007967 dev_priv->clock_gating_funcs = &gen3_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007968 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007969 dev_priv->clock_gating_funcs = &i85x_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007970 else if (GRAPHICS_VER(dev_priv) == 2)
Dave Airlieeba4b792021-09-29 01:58:07 +03007971 dev_priv->clock_gating_funcs = &i830_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007972 else {
7973 MISSING_CASE(INTEL_DEVID(dev_priv));
Dave Airlieeba4b792021-09-29 01:58:07 +03007974 dev_priv->clock_gating_funcs = &nop_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007975 }
7976}
7977
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007978/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007979void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007980{
Daniel Vetterc921aba2012-04-26 23:28:17 +02007981 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007982 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08007983 pnv_get_mem_freq(dev_priv);
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007984 else if (GRAPHICS_VER(dev_priv) == 5)
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08007985 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007986
James Ausmusb068a862019-10-09 10:23:14 -07007987 if (intel_has_sagv(dev_priv))
7988 skl_setup_sagv_block_time(dev_priv);
7989
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007990 /* For FIFO watermark updates */
Matt Roper7dadd282021-03-19 21:42:43 -07007991 if (DISPLAY_VER(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007992 skl_setup_wm_latency(dev_priv);
Dave Airlie27057882021-09-29 01:57:52 +03007993 dev_priv->wm_disp.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007994 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007995 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007996
Lucas De Marchi93e7e612021-04-12 22:09:53 -07007997 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007998 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchi93e7e612021-04-12 22:09:53 -07007999 (DISPLAY_VER(dev_priv) != 5 && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008000 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Dave Airlie27057882021-09-29 01:57:52 +03008001 dev_priv->wm_disp.compute_pipe_wm = ilk_compute_pipe_wm;
8002 dev_priv->wm_disp.compute_intermediate_wm =
Matt Ropered4a6a72016-02-23 17:20:13 -08008003 ilk_compute_intermediate_wm;
Dave Airlie27057882021-09-29 01:57:52 +03008004 dev_priv->wm_disp.initial_watermarks =
Matt Ropered4a6a72016-02-23 17:20:13 -08008005 ilk_initial_watermarks;
Dave Airlie27057882021-09-29 01:57:52 +03008006 dev_priv->wm_disp.optimize_watermarks =
Matt Ropered4a6a72016-02-23 17:20:13 -08008007 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008008 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008009 drm_dbg_kms(&dev_priv->drm,
8010 "Failed to read display plane latency. "
8011 "Disable CxSR\n");
Ville Syrjäläbd602542014-01-07 16:14:10 +02008012 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008013 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008014 vlv_setup_wm_latency(dev_priv);
Dave Airlie27057882021-09-29 01:57:52 +03008015 dev_priv->wm_disp.compute_pipe_wm = vlv_compute_pipe_wm;
8016 dev_priv->wm_disp.compute_intermediate_wm = vlv_compute_intermediate_wm;
8017 dev_priv->wm_disp.initial_watermarks = vlv_initial_watermarks;
8018 dev_priv->wm_disp.optimize_watermarks = vlv_optimize_watermarks;
8019 dev_priv->wm_disp.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008020 } else if (IS_G4X(dev_priv)) {
8021 g4x_setup_wm_latency(dev_priv);
Dave Airlie27057882021-09-29 01:57:52 +03008022 dev_priv->wm_disp.compute_pipe_wm = g4x_compute_pipe_wm;
8023 dev_priv->wm_disp.compute_intermediate_wm = g4x_compute_intermediate_wm;
8024 dev_priv->wm_disp.initial_watermarks = g4x_initial_watermarks;
8025 dev_priv->wm_disp.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008026 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00008027 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008028 dev_priv->is_ddr3,
8029 dev_priv->fsb_freq,
8030 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008031 drm_info(&dev_priv->drm,
8032 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008033 "(found ddr%s fsb freq %d, mem freq %d), "
8034 "disabling CxSR\n",
8035 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8036 dev_priv->fsb_freq, dev_priv->mem_freq);
8037 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008038 intel_set_memory_cxsr(dev_priv, false);
Dave Airlie27057882021-09-29 01:57:52 +03008039 dev_priv->wm_disp.update_wm = NULL;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008040 } else
Dave Airlie27057882021-09-29 01:57:52 +03008041 dev_priv->wm_disp.update_wm = pnv_update_wm;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008042 } else if (DISPLAY_VER(dev_priv) == 4) {
Dave Airlie27057882021-09-29 01:57:52 +03008043 dev_priv->wm_disp.update_wm = i965_update_wm;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008044 } else if (DISPLAY_VER(dev_priv) == 3) {
Dave Airlie27057882021-09-29 01:57:52 +03008045 dev_priv->wm_disp.update_wm = i9xx_update_wm;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008046 } else if (DISPLAY_VER(dev_priv) == 2) {
Dave Airlie758b2fc2021-09-29 01:57:46 +03008047 if (INTEL_NUM_PIPES(dev_priv) == 1)
Dave Airlie27057882021-09-29 01:57:52 +03008048 dev_priv->wm_disp.update_wm = i845_update_wm;
Dave Airlie758b2fc2021-09-29 01:57:46 +03008049 else
Dave Airlie27057882021-09-29 01:57:52 +03008050 dev_priv->wm_disp.update_wm = i9xx_update_wm;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008051 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008052 drm_err(&dev_priv->drm,
8053 "unexpected fall-through in %s\n", __func__);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008054 }
8055}
8056
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008057void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008058{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01008059 dev_priv->runtime_pm.suspended = false;
8060 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008061}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02008062
8063static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
8064{
8065 struct intel_dbuf_state *dbuf_state;
8066
8067 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
8068 if (!dbuf_state)
8069 return NULL;
8070
8071 return &dbuf_state->base;
8072}
8073
8074static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
8075 struct intel_global_state *state)
8076{
8077 kfree(state);
8078}
8079
8080static const struct intel_global_state_funcs intel_dbuf_funcs = {
8081 .atomic_duplicate_state = intel_dbuf_duplicate_state,
8082 .atomic_destroy_state = intel_dbuf_destroy_state,
8083};
8084
8085struct intel_dbuf_state *
8086intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
8087{
8088 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8089 struct intel_global_state *dbuf_state;
8090
8091 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
8092 if (IS_ERR(dbuf_state))
8093 return ERR_CAST(dbuf_state);
8094
8095 return to_intel_dbuf_state(dbuf_state);
8096}
8097
8098int intel_dbuf_init(struct drm_i915_private *dev_priv)
8099{
8100 struct intel_dbuf_state *dbuf_state;
8101
8102 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
8103 if (!dbuf_state)
8104 return -ENOMEM;
8105
8106 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
8107 &dbuf_state->base, &intel_dbuf_funcs);
8108
8109 return 0;
8110}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008111
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008112/*
8113 * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
8114 * update the request state of all DBUS slices.
8115 */
8116static void update_mbus_pre_enable(struct intel_atomic_state *state)
8117{
8118 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8119 u32 mbus_ctl, dbuf_min_tracker_val;
8120 enum dbuf_slice slice;
8121 const struct intel_dbuf_state *dbuf_state =
8122 intel_atomic_get_new_dbuf_state(state);
8123
8124 if (!IS_ALDERLAKE_P(dev_priv))
8125 return;
8126
8127 /*
8128 * TODO: Implement vblank synchronized MBUS joining changes.
8129 * Must be properly coordinated with dbuf reprogramming.
8130 */
8131 if (dbuf_state->joined_mbus) {
8132 mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
8133 MBUS_JOIN_PIPE_SELECT_NONE;
8134 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
8135 } else {
8136 mbus_ctl = MBUS_HASHING_MODE_2x2 |
8137 MBUS_JOIN_PIPE_SELECT_NONE;
8138 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
8139 }
8140
8141 intel_de_rmw(dev_priv, MBUS_CTL,
8142 MBUS_HASHING_MODE_MASK | MBUS_JOIN |
8143 MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
8144
8145 for_each_dbuf_slice(dev_priv, slice)
8146 intel_de_rmw(dev_priv, DBUF_CTL_S(slice),
8147 DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
8148 dbuf_min_tracker_val);
8149}
8150
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008151void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
8152{
8153 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8154 const struct intel_dbuf_state *new_dbuf_state =
8155 intel_atomic_get_new_dbuf_state(state);
8156 const struct intel_dbuf_state *old_dbuf_state =
8157 intel_atomic_get_old_dbuf_state(state);
8158
8159 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008160 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8161 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008162 return;
8163
8164 WARN_ON(!new_dbuf_state->base.changed);
8165
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008166 update_mbus_pre_enable(state);
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008167 gen9_dbuf_slices_update(dev_priv,
8168 old_dbuf_state->enabled_slices |
8169 new_dbuf_state->enabled_slices);
8170}
8171
8172void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
8173{
8174 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8175 const struct intel_dbuf_state *new_dbuf_state =
8176 intel_atomic_get_new_dbuf_state(state);
8177 const struct intel_dbuf_state *old_dbuf_state =
8178 intel_atomic_get_old_dbuf_state(state);
8179
8180 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008181 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8182 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008183 return;
8184
8185 WARN_ON(!new_dbuf_state->base.changed);
8186
8187 gen9_dbuf_slices_update(dev_priv,
8188 new_dbuf_state->enabled_slices);
8189}