blob: cfc41f8fa74a743f85e0938149bf3627b36a8aba [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
Lucas De Marchi1d218222019-12-24 00:40:04 -0800884static void pnv_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200886 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200887 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300888 const struct cxsr_latency *latency;
889 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300890 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300891
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000892 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100893 dev_priv->is_ddr3,
894 dev_priv->fsb_freq,
895 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300896 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300897 drm_dbg_kms(&dev_priv->drm,
898 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300899 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300900 return;
901 }
902
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200903 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300904 if (crtc) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200905 const struct drm_display_mode *pipe_mode =
906 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200907 const struct drm_framebuffer *fb =
908 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200909 int cpp = fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200910 int clock = pipe_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300911
912 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800913 wm = intel_calculate_wm(clock, &pnv_display_wm,
914 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200915 cpp, latency->display_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200916 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300917 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200918 reg |= FW_WM(wm, SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200919 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300920 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300921
922 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800923 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
924 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300925 4, latency->cursor_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200926 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300927 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200928 reg |= FW_WM(wm, CURSOR_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200929 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300930
931 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800932 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
933 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200934 cpp, latency->display_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200935 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300936 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200937 reg |= FW_WM(wm, HPLL_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200938 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300939
940 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800941 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
942 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300943 4, latency->cursor_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200944 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300945 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200946 reg |= FW_WM(wm, HPLL_CURSOR);
Jani Nikula5f461662020-11-30 13:15:58 +0200947 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300948 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300949
Imre Deak5209b1f2014-07-01 12:36:17 +0300950 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300951 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300952 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300953 }
954}
955
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300956/*
957 * Documentation says:
958 * "If the line size is small, the TLB fetches can get in the way of the
959 * data fetches, causing some lag in the pixel data return which is not
960 * accounted for in the above formulas. The following adjustment only
961 * needs to be applied if eight whole lines fit in the buffer at once.
962 * The WM is adjusted upwards by the difference between the FIFO size
963 * and the size of 8 whole lines. This adjustment is always performed
964 * in the actual pixel depth regardless of whether FBC is enabled or not."
965 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000966static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300967{
968 int tlb_miss = fifo_size * 64 - width * cpp * 8;
969
970 return max(0, tlb_miss);
971}
972
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300973static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
974 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300975{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300976 enum pipe pipe;
977
978 for_each_pipe(dev_priv, pipe)
979 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
980
Jani Nikula5f461662020-11-30 13:15:58 +0200981 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300982 FW_WM(wm->sr.plane, SR) |
983 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
984 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
985 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200986 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300987 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
988 FW_WM(wm->sr.fbc, FBC_SR) |
989 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
990 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
991 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
992 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200993 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300994 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
995 FW_WM(wm->sr.cursor, CURSOR_SR) |
996 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
997 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300998
Jani Nikula5f461662020-11-30 13:15:58 +0200999 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001000}
1001
Ville Syrjälä15665972015-03-10 16:16:28 +02001002#define FW_WM_VLV(value, plane) \
1003 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1004
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001005static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001006 const struct vlv_wm_values *wm)
1007{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001008 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001009
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001010 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001011 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1012
Jani Nikula5f461662020-11-30 13:15:58 +02001013 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001014 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1015 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1016 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1017 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1018 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001019
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001020 /*
1021 * Zero the (unused) WM1 watermarks, and also clear all the
1022 * high order bits so that there are no out of bounds values
1023 * present in the registers during the reprogramming.
1024 */
Jani Nikula5f461662020-11-30 13:15:58 +02001025 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1026 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1027 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1028 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1029 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001030
Jani Nikula5f461662020-11-30 13:15:58 +02001031 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001032 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001033 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1034 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1035 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001036 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001037 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1038 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1039 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001040 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001041 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001042
1043 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02001044 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001045 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1046 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001047 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001048 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1049 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Jani Nikula5f461662020-11-30 13:15:58 +02001050 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001051 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1052 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Jani Nikula5f461662020-11-30 13:15:58 +02001053 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001054 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001055 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1056 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1057 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1058 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1059 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1060 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1061 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1062 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1063 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001064 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02001065 intel_uncore_write(&dev_priv->uncore, DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001066 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1067 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001068 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001069 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001070 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1071 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1072 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1073 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1074 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1075 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001076 }
1077
Jani Nikula5f461662020-11-30 13:15:58 +02001078 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001079}
1080
Ville Syrjälä15665972015-03-10 16:16:28 +02001081#undef FW_WM_VLV
1082
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001083static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1084{
1085 /* all latencies in usec */
1086 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1087 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001088 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001089
Ville Syrjälä79d94302017-04-21 21:14:30 +03001090 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001091}
1092
1093static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1094{
1095 /*
1096 * DSPCNTR[13] supposedly controls whether the
1097 * primary plane can use the FIFO space otherwise
1098 * reserved for the sprite plane. It's not 100% clear
1099 * what the actual FIFO size is, but it looks like we
1100 * can happily set both primary and sprite watermarks
1101 * up to 127 cachelines. So that would seem to mean
1102 * that either DSPCNTR[13] doesn't do anything, or that
1103 * the total FIFO is >= 256 cachelines in size. Either
1104 * way, we don't seem to have to worry about this
1105 * repartitioning as the maximum watermark value the
1106 * register can hold for each plane is lower than the
1107 * minimum FIFO size.
1108 */
1109 switch (plane_id) {
1110 case PLANE_CURSOR:
1111 return 63;
1112 case PLANE_PRIMARY:
1113 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1114 case PLANE_SPRITE0:
1115 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1116 default:
1117 MISSING_CASE(plane_id);
1118 return 0;
1119 }
1120}
1121
1122static int g4x_fbc_fifo_size(int level)
1123{
1124 switch (level) {
1125 case G4X_WM_LEVEL_SR:
1126 return 7;
1127 case G4X_WM_LEVEL_HPLL:
1128 return 15;
1129 default:
1130 MISSING_CASE(level);
1131 return 0;
1132 }
1133}
1134
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001135static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1136 const struct intel_plane_state *plane_state,
1137 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001138{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001139 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001140 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001141 const struct drm_display_mode *pipe_mode =
1142 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001143 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1144 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001145
1146 if (latency == 0)
1147 return USHRT_MAX;
1148
1149 if (!intel_wm_plane_visible(crtc_state, plane_state))
1150 return 0;
1151
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001152 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001153
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001154 /*
1155 * Not 100% sure which way ELK should go here as the
1156 * spec only says CL/CTG should assume 32bpp and BW
1157 * doesn't need to. But as these things followed the
1158 * mobile vs. desktop lines on gen3 as well, let's
1159 * assume ELK doesn't need this.
1160 *
1161 * The spec also fails to list such a restriction for
1162 * the HPLL watermark, which seems a little strange.
1163 * Let's use 32bpp for the HPLL watermark as well.
1164 */
1165 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1166 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001167 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001168
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001169 clock = pipe_mode->crtc_clock;
1170 htotal = pipe_mode->crtc_htotal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001171
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001172 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001173
1174 if (plane->id == PLANE_CURSOR) {
1175 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1176 } else if (plane->id == PLANE_PRIMARY &&
1177 level == G4X_WM_LEVEL_NORMAL) {
1178 wm = intel_wm_method1(clock, cpp, latency);
1179 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001180 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001181
1182 small = intel_wm_method1(clock, cpp, latency);
1183 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1184
1185 wm = min(small, large);
1186 }
1187
1188 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1189 width, cpp);
1190
1191 wm = DIV_ROUND_UP(wm, 64) + 2;
1192
Chris Wilson1a1f1282017-11-07 14:03:38 +00001193 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001194}
1195
1196static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1197 int level, enum plane_id plane_id, u16 value)
1198{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001199 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001200 bool dirty = false;
1201
1202 for (; level < intel_wm_num_levels(dev_priv); level++) {
1203 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1204
1205 dirty |= raw->plane[plane_id] != value;
1206 raw->plane[plane_id] = value;
1207 }
1208
1209 return dirty;
1210}
1211
1212static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1213 int level, u16 value)
1214{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001215 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001216 bool dirty = false;
1217
1218 /* NORMAL level doesn't have an FBC watermark */
1219 level = max(level, G4X_WM_LEVEL_SR);
1220
1221 for (; level < intel_wm_num_levels(dev_priv); level++) {
1222 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1223
1224 dirty |= raw->fbc != value;
1225 raw->fbc = value;
1226 }
1227
1228 return dirty;
1229}
1230
Maarten Lankhorstec193642019-06-28 10:55:17 +02001231static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1232 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001233 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001234
1235static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1236 const struct intel_plane_state *plane_state)
1237{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001238 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001239 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001240 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1241 enum plane_id plane_id = plane->id;
1242 bool dirty = false;
1243 int level;
1244
1245 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1246 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1247 if (plane_id == PLANE_PRIMARY)
1248 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1249 goto out;
1250 }
1251
1252 for (level = 0; level < num_levels; level++) {
1253 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1254 int wm, max_wm;
1255
1256 wm = g4x_compute_wm(crtc_state, plane_state, level);
1257 max_wm = g4x_plane_fifo_size(plane_id, level);
1258
1259 if (wm > max_wm)
1260 break;
1261
1262 dirty |= raw->plane[plane_id] != wm;
1263 raw->plane[plane_id] = wm;
1264
1265 if (plane_id != PLANE_PRIMARY ||
1266 level == G4X_WM_LEVEL_NORMAL)
1267 continue;
1268
1269 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1270 raw->plane[plane_id]);
1271 max_wm = g4x_fbc_fifo_size(level);
1272
1273 /*
1274 * FBC wm is not mandatory as we
1275 * can always just disable its use.
1276 */
1277 if (wm > max_wm)
1278 wm = USHRT_MAX;
1279
1280 dirty |= raw->fbc != wm;
1281 raw->fbc = wm;
1282 }
1283
1284 /* mark watermarks as invalid */
1285 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1286
1287 if (plane_id == PLANE_PRIMARY)
1288 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1289
1290 out:
1291 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001292 drm_dbg_kms(&dev_priv->drm,
1293 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1294 plane->base.name,
1295 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1296 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1297 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001298
1299 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001300 drm_dbg_kms(&dev_priv->drm,
1301 "FBC watermarks: SR=%d, HPLL=%d\n",
1302 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1303 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001304 }
1305
1306 return dirty;
1307}
1308
1309static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1310 enum plane_id plane_id, int level)
1311{
1312 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1313
1314 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1315}
1316
1317static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1318 int level)
1319{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001320 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001321
1322 if (level > dev_priv->wm.max_level)
1323 return false;
1324
1325 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1326 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1327 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1328}
1329
1330/* mark all levels starting from 'level' as invalid */
1331static void g4x_invalidate_wms(struct intel_crtc *crtc,
1332 struct g4x_wm_state *wm_state, int level)
1333{
1334 if (level <= G4X_WM_LEVEL_NORMAL) {
1335 enum plane_id plane_id;
1336
1337 for_each_plane_id_on_crtc(crtc, plane_id)
1338 wm_state->wm.plane[plane_id] = USHRT_MAX;
1339 }
1340
1341 if (level <= G4X_WM_LEVEL_SR) {
1342 wm_state->cxsr = false;
1343 wm_state->sr.cursor = USHRT_MAX;
1344 wm_state->sr.plane = USHRT_MAX;
1345 wm_state->sr.fbc = USHRT_MAX;
1346 }
1347
1348 if (level <= G4X_WM_LEVEL_HPLL) {
1349 wm_state->hpll_en = false;
1350 wm_state->hpll.cursor = USHRT_MAX;
1351 wm_state->hpll.plane = USHRT_MAX;
1352 wm_state->hpll.fbc = USHRT_MAX;
1353 }
1354}
1355
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001356static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1357 int level)
1358{
1359 if (level < G4X_WM_LEVEL_SR)
1360 return false;
1361
1362 if (level >= G4X_WM_LEVEL_SR &&
1363 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1364 return false;
1365
1366 if (level >= G4X_WM_LEVEL_HPLL &&
1367 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1368 return false;
1369
1370 return true;
1371}
1372
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001373static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1374 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001375{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001376 struct intel_crtc_state *crtc_state =
1377 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001378 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001379 int num_active_planes = hweight8(crtc_state->active_planes &
1380 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001381 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001382 const struct intel_plane_state *old_plane_state;
1383 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001384 struct intel_plane *plane;
1385 enum plane_id plane_id;
1386 int i, level;
1387 unsigned int dirty = 0;
1388
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001389 for_each_oldnew_intel_plane_in_state(state, plane,
1390 old_plane_state,
1391 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001392 if (new_plane_state->hw.crtc != &crtc->base &&
1393 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001394 continue;
1395
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001396 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001397 dirty |= BIT(plane->id);
1398 }
1399
1400 if (!dirty)
1401 return 0;
1402
1403 level = G4X_WM_LEVEL_NORMAL;
1404 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1405 goto out;
1406
1407 raw = &crtc_state->wm.g4x.raw[level];
1408 for_each_plane_id_on_crtc(crtc, plane_id)
1409 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1410
1411 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001412 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1413 goto out;
1414
1415 raw = &crtc_state->wm.g4x.raw[level];
1416 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1417 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1418 wm_state->sr.fbc = raw->fbc;
1419
1420 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1421
1422 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001423 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1424 goto out;
1425
1426 raw = &crtc_state->wm.g4x.raw[level];
1427 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1428 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1429 wm_state->hpll.fbc = raw->fbc;
1430
1431 wm_state->hpll_en = wm_state->cxsr;
1432
1433 level++;
1434
1435 out:
1436 if (level == G4X_WM_LEVEL_NORMAL)
1437 return -EINVAL;
1438
1439 /* invalidate the higher levels */
1440 g4x_invalidate_wms(crtc, wm_state, level);
1441
1442 /*
1443 * Determine if the FBC watermark(s) can be used. IF
1444 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001445 * watermark(s) rather than disable the SR/HPLL
1446 * level(s) entirely. 'level-1' is the highest valid
1447 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001448 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001449 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001450
1451 return 0;
1452}
1453
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001454static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1455 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001456{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301457 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001458 struct intel_crtc_state *new_crtc_state =
1459 intel_atomic_get_new_crtc_state(state, crtc);
1460 const struct intel_crtc_state *old_crtc_state =
1461 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001462 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1463 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001464 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001465 enum plane_id plane_id;
1466
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001467 if (!new_crtc_state->hw.active ||
1468 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001469 *intermediate = *optimal;
1470
1471 intermediate->cxsr = false;
1472 intermediate->hpll_en = false;
1473 goto out;
1474 }
1475
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001476 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001477 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001478 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001479 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001480 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1481
1482 for_each_plane_id_on_crtc(crtc, plane_id) {
1483 intermediate->wm.plane[plane_id] =
1484 max(optimal->wm.plane[plane_id],
1485 active->wm.plane[plane_id]);
1486
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301487 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1488 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001489 }
1490
1491 intermediate->sr.plane = max(optimal->sr.plane,
1492 active->sr.plane);
1493 intermediate->sr.cursor = max(optimal->sr.cursor,
1494 active->sr.cursor);
1495 intermediate->sr.fbc = max(optimal->sr.fbc,
1496 active->sr.fbc);
1497
1498 intermediate->hpll.plane = max(optimal->hpll.plane,
1499 active->hpll.plane);
1500 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1501 active->hpll.cursor);
1502 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1503 active->hpll.fbc);
1504
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301505 drm_WARN_ON(&dev_priv->drm,
1506 (intermediate->sr.plane >
1507 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1508 intermediate->sr.cursor >
1509 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1510 intermediate->cxsr);
1511 drm_WARN_ON(&dev_priv->drm,
1512 (intermediate->sr.plane >
1513 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1514 intermediate->sr.cursor >
1515 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1516 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001517
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301518 drm_WARN_ON(&dev_priv->drm,
1519 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1520 intermediate->fbc_en && intermediate->cxsr);
1521 drm_WARN_ON(&dev_priv->drm,
1522 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1523 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001524
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001525out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001526 /*
1527 * If our intermediate WM are identical to the final WM, then we can
1528 * omit the post-vblank programming; only update if it's different.
1529 */
1530 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001531 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001532
1533 return 0;
1534}
1535
1536static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1537 struct g4x_wm_values *wm)
1538{
1539 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001540 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001541
1542 wm->cxsr = true;
1543 wm->hpll_en = true;
1544 wm->fbc_en = true;
1545
1546 for_each_intel_crtc(&dev_priv->drm, crtc) {
1547 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1548
1549 if (!crtc->active)
1550 continue;
1551
1552 if (!wm_state->cxsr)
1553 wm->cxsr = false;
1554 if (!wm_state->hpll_en)
1555 wm->hpll_en = false;
1556 if (!wm_state->fbc_en)
1557 wm->fbc_en = false;
1558
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001559 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001560 }
1561
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001562 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001563 wm->cxsr = false;
1564 wm->hpll_en = false;
1565 wm->fbc_en = false;
1566 }
1567
1568 for_each_intel_crtc(&dev_priv->drm, crtc) {
1569 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1570 enum pipe pipe = crtc->pipe;
1571
1572 wm->pipe[pipe] = wm_state->wm;
1573 if (crtc->active && wm->cxsr)
1574 wm->sr = wm_state->sr;
1575 if (crtc->active && wm->hpll_en)
1576 wm->hpll = wm_state->hpll;
1577 }
1578}
1579
1580static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1581{
1582 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1583 struct g4x_wm_values new_wm = {};
1584
1585 g4x_merge_wm(dev_priv, &new_wm);
1586
1587 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1588 return;
1589
1590 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1591 _intel_set_memory_cxsr(dev_priv, false);
1592
1593 g4x_write_wm_values(dev_priv, &new_wm);
1594
1595 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1596 _intel_set_memory_cxsr(dev_priv, true);
1597
1598 *old_wm = new_wm;
1599}
1600
1601static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001602 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001603{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001604 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1605 const struct intel_crtc_state *crtc_state =
1606 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001607
1608 mutex_lock(&dev_priv->wm.wm_mutex);
1609 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1610 g4x_program_watermarks(dev_priv);
1611 mutex_unlock(&dev_priv->wm.wm_mutex);
1612}
1613
1614static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001615 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001616{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001617 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1618 const struct intel_crtc_state *crtc_state =
1619 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001620
1621 if (!crtc_state->wm.need_postvbl_update)
1622 return;
1623
1624 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001625 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001626 g4x_program_watermarks(dev_priv);
1627 mutex_unlock(&dev_priv->wm.wm_mutex);
1628}
1629
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001630/* latency must be in 0.1us units. */
1631static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001632 unsigned int htotal,
1633 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001634 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001635 unsigned int latency)
1636{
1637 unsigned int ret;
1638
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001639 ret = intel_wm_method2(pixel_rate, htotal,
1640 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001641 ret = DIV_ROUND_UP(ret, 64);
1642
1643 return ret;
1644}
1645
Ville Syrjäläbb726512016-10-31 22:37:24 +02001646static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001647{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001648 /* all latencies in usec */
1649 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1650
Ville Syrjälä58590c12015-09-08 21:05:12 +03001651 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1652
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001653 if (IS_CHERRYVIEW(dev_priv)) {
1654 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1655 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001656
1657 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001658 }
1659}
1660
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001661static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1662 const struct intel_plane_state *plane_state,
1663 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001664{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001665 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001666 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001667 const struct drm_display_mode *pipe_mode =
1668 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001669 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001670
1671 if (dev_priv->wm.pri_latency[level] == 0)
1672 return USHRT_MAX;
1673
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001674 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001675 return 0;
1676
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001677 cpp = plane_state->hw.fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001678 clock = pipe_mode->crtc_clock;
1679 htotal = pipe_mode->crtc_htotal;
Ville Syrjäläe339d672016-11-28 19:37:17 +02001680 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001681
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001682 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001683 /*
1684 * FIXME the formula gives values that are
1685 * too big for the cursor FIFO, and hence we
1686 * would never be able to use cursors. For
1687 * now just hardcode the watermark.
1688 */
1689 wm = 63;
1690 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001691 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001692 dev_priv->wm.pri_latency[level] * 10);
1693 }
1694
Chris Wilson1a1f1282017-11-07 14:03:38 +00001695 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001696}
1697
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001698static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1699{
1700 return (active_planes & (BIT(PLANE_SPRITE0) |
1701 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1702}
1703
Ville Syrjälä5012e602017-03-02 19:14:56 +02001704static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001705{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001706 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301707 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001708 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001709 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001710 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001711 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001712 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001713 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001714 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001715 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001716 unsigned int total_rate;
1717 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001718
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001719 /*
1720 * When enabling sprite0 after sprite1 has already been enabled
1721 * we tend to get an underrun unless sprite0 already has some
1722 * FIFO space allcoated. Hence we always allocate at least one
1723 * cacheline for sprite0 whenever sprite1 is enabled.
1724 *
1725 * All other plane enable sequences appear immune to this problem.
1726 */
1727 if (vlv_need_sprite0_fifo_workaround(active_planes))
1728 sprite0_fifo_extra = 1;
1729
Ville Syrjälä5012e602017-03-02 19:14:56 +02001730 total_rate = raw->plane[PLANE_PRIMARY] +
1731 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001732 raw->plane[PLANE_SPRITE1] +
1733 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001734
Ville Syrjälä5012e602017-03-02 19:14:56 +02001735 if (total_rate > fifo_size)
1736 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001737
Ville Syrjälä5012e602017-03-02 19:14:56 +02001738 if (total_rate == 0)
1739 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001740
Ville Syrjälä5012e602017-03-02 19:14:56 +02001741 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001742 unsigned int rate;
1743
Ville Syrjälä5012e602017-03-02 19:14:56 +02001744 if ((active_planes & BIT(plane_id)) == 0) {
1745 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001746 continue;
1747 }
1748
Ville Syrjälä5012e602017-03-02 19:14:56 +02001749 rate = raw->plane[plane_id];
1750 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1751 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001752 }
1753
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001754 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1755 fifo_left -= sprite0_fifo_extra;
1756
Ville Syrjälä5012e602017-03-02 19:14:56 +02001757 fifo_state->plane[PLANE_CURSOR] = 63;
1758
1759 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001760
1761 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001762 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001763 int plane_extra;
1764
1765 if (fifo_left == 0)
1766 break;
1767
Ville Syrjälä5012e602017-03-02 19:14:56 +02001768 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001769 continue;
1770
1771 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001772 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001773 fifo_left -= plane_extra;
1774 }
1775
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301776 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001777
1778 /* give it all to the first plane if none are active */
1779 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301780 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001781 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1782 }
1783
1784 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001785}
1786
Ville Syrjäläff32c542017-03-02 19:14:57 +02001787/* mark all levels starting from 'level' as invalid */
1788static void vlv_invalidate_wms(struct intel_crtc *crtc,
1789 struct vlv_wm_state *wm_state, int level)
1790{
1791 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1792
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001793 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001794 enum plane_id plane_id;
1795
1796 for_each_plane_id_on_crtc(crtc, plane_id)
1797 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1798
1799 wm_state->sr[level].cursor = USHRT_MAX;
1800 wm_state->sr[level].plane = USHRT_MAX;
1801 }
1802}
1803
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001804static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1805{
1806 if (wm > fifo_size)
1807 return USHRT_MAX;
1808 else
1809 return fifo_size - wm;
1810}
1811
Ville Syrjäläff32c542017-03-02 19:14:57 +02001812/*
1813 * Starting from 'level' set all higher
1814 * levels to 'value' in the "raw" watermarks.
1815 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001816static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001817 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001818{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001819 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001820 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001821 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001822
Ville Syrjäläff32c542017-03-02 19:14:57 +02001823 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001824 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001825
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001826 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001827 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001828 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001829
1830 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001831}
1832
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001833static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1834 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001836 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001837 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001838 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001839 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001840 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001841 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001842
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001843 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001844 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1845 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001846 }
1847
1848 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001849 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001850 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1851 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1852
Ville Syrjäläff32c542017-03-02 19:14:57 +02001853 if (wm > max_wm)
1854 break;
1855
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001856 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001857 raw->plane[plane_id] = wm;
1858 }
1859
1860 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001861 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001862
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001863out:
1864 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001865 drm_dbg_kms(&dev_priv->drm,
1866 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1867 plane->base.name,
1868 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1869 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1870 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001871
1872 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001873}
1874
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001875static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1876 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001877{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001878 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001879 &crtc_state->wm.vlv.raw[level];
1880 const struct vlv_fifo_state *fifo_state =
1881 &crtc_state->wm.vlv.fifo_state;
1882
1883 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1884}
1885
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001886static 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 +02001887{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001888 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1889 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1890 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1891 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001892}
1893
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001894static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1895 struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001896{
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001897 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001898 struct intel_crtc_state *crtc_state =
1899 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001900 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001901 const struct vlv_fifo_state *fifo_state =
1902 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001903 int num_active_planes = hweight8(crtc_state->active_planes &
1904 ~BIT(PLANE_CURSOR));
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001905 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001906 const struct intel_plane_state *old_plane_state;
1907 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001908 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001909 enum plane_id plane_id;
1910 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001911 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001912
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001913 for_each_oldnew_intel_plane_in_state(state, plane,
1914 old_plane_state,
1915 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001916 if (new_plane_state->hw.crtc != &crtc->base &&
1917 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001918 continue;
1919
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001920 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001921 dirty |= BIT(plane->id);
1922 }
1923
1924 /*
1925 * DSPARB registers may have been reset due to the
1926 * power well being turned off. Make sure we restore
1927 * them to a consistent state even if no primary/sprite
1928 * planes are initially active.
1929 */
1930 if (needs_modeset)
1931 crtc_state->fifo_changed = true;
1932
1933 if (!dirty)
1934 return 0;
1935
1936 /* cursor changes don't warrant a FIFO recompute */
1937 if (dirty & ~BIT(PLANE_CURSOR)) {
1938 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001939 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001940 const struct vlv_fifo_state *old_fifo_state =
1941 &old_crtc_state->wm.vlv.fifo_state;
1942
1943 ret = vlv_compute_fifo(crtc_state);
1944 if (ret)
1945 return ret;
1946
1947 if (needs_modeset ||
1948 memcmp(old_fifo_state, fifo_state,
1949 sizeof(*fifo_state)) != 0)
1950 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001951 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001952
Ville Syrjäläff32c542017-03-02 19:14:57 +02001953 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001954 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001955 /*
1956 * Note that enabling cxsr with no primary/sprite planes
1957 * enabled can wedge the pipe. Hence we only allow cxsr
1958 * with exactly one enabled primary/sprite plane.
1959 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001960 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001961
Ville Syrjälä5012e602017-03-02 19:14:56 +02001962 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001963 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001964 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001965
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001966 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001967 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001968
Ville Syrjäläff32c542017-03-02 19:14:57 +02001969 for_each_plane_id_on_crtc(crtc, plane_id) {
1970 wm_state->wm[level].plane[plane_id] =
1971 vlv_invert_wm_value(raw->plane[plane_id],
1972 fifo_state->plane[plane_id]);
1973 }
1974
1975 wm_state->sr[level].plane =
1976 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001977 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001978 raw->plane[PLANE_SPRITE1]),
1979 sr_fifo_size);
1980
1981 wm_state->sr[level].cursor =
1982 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1983 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001984 }
1985
Ville Syrjäläff32c542017-03-02 19:14:57 +02001986 if (level == 0)
1987 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001988
Ville Syrjäläff32c542017-03-02 19:14:57 +02001989 /* limit to only levels we can actually handle */
1990 wm_state->num_levels = level;
1991
1992 /* invalidate the higher levels */
1993 vlv_invalidate_wms(crtc, wm_state, level);
1994
1995 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001996}
1997
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001998#define VLV_FIFO(plane, value) \
1999 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
2000
Ville Syrjäläff32c542017-03-02 19:14:57 +02002001static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002002 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002003{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002004 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002005 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002006 const struct intel_crtc_state *crtc_state =
2007 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002008 const struct vlv_fifo_state *fifo_state =
2009 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002010 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002011 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002012
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002013 if (!crtc_state->fifo_changed)
2014 return;
2015
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002016 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2017 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2018 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002019
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302020 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2021 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002022
Ville Syrjäläc137d662017-03-02 19:15:06 +02002023 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2024
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002025 /*
2026 * uncore.lock serves a double purpose here. It allows us to
2027 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2028 * it protects the DSPARB registers from getting clobbered by
2029 * parallel updates from multiple pipes.
2030 *
2031 * intel_pipe_update_start() has already disabled interrupts
2032 * for us, so a plain spin_lock() is sufficient here.
2033 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002034 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002035
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002036 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002037 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002038 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2039 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002040
2041 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2042 VLV_FIFO(SPRITEB, 0xff));
2043 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2044 VLV_FIFO(SPRITEB, sprite1_start));
2045
2046 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2047 VLV_FIFO(SPRITEB_HI, 0x1));
2048 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2049 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2050
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002051 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2052 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002053 break;
2054 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002055 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2056 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002057
2058 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2059 VLV_FIFO(SPRITED, 0xff));
2060 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2061 VLV_FIFO(SPRITED, sprite1_start));
2062
2063 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2064 VLV_FIFO(SPRITED_HI, 0xff));
2065 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2066 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2067
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002068 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2069 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002070 break;
2071 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002072 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2073 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002074
2075 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2076 VLV_FIFO(SPRITEF, 0xff));
2077 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2078 VLV_FIFO(SPRITEF, sprite1_start));
2079
2080 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2081 VLV_FIFO(SPRITEF_HI, 0xff));
2082 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2083 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2084
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002085 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2086 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002087 break;
2088 default:
2089 break;
2090 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002091
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002092 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002093
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002094 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002095}
2096
2097#undef VLV_FIFO
2098
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002099static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
2100 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002101{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002102 struct intel_crtc_state *new_crtc_state =
2103 intel_atomic_get_new_crtc_state(state, crtc);
2104 const struct intel_crtc_state *old_crtc_state =
2105 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002106 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2107 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002108 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002109 int level;
2110
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002111 if (!new_crtc_state->hw.active ||
2112 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002113 *intermediate = *optimal;
2114
2115 intermediate->cxsr = false;
2116 goto out;
2117 }
2118
Ville Syrjälä4841da52017-03-02 19:14:59 +02002119 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002120 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002121 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002122
2123 for (level = 0; level < intermediate->num_levels; level++) {
2124 enum plane_id plane_id;
2125
2126 for_each_plane_id_on_crtc(crtc, plane_id) {
2127 intermediate->wm[level].plane[plane_id] =
2128 min(optimal->wm[level].plane[plane_id],
2129 active->wm[level].plane[plane_id]);
2130 }
2131
2132 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2133 active->sr[level].plane);
2134 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2135 active->sr[level].cursor);
2136 }
2137
2138 vlv_invalidate_wms(crtc, intermediate, level);
2139
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002140out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002141 /*
2142 * If our intermediate WM are identical to the final WM, then we can
2143 * omit the post-vblank programming; only update if it's different.
2144 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002145 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002146 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002147
2148 return 0;
2149}
2150
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002151static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002152 struct vlv_wm_values *wm)
2153{
2154 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002155 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002156
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002157 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002158 wm->cxsr = true;
2159
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002160 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002161 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002162
2163 if (!crtc->active)
2164 continue;
2165
2166 if (!wm_state->cxsr)
2167 wm->cxsr = false;
2168
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002169 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002170 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2171 }
2172
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002173 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002174 wm->cxsr = false;
2175
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002176 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002177 wm->level = VLV_WM_LEVEL_PM2;
2178
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002179 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002180 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002181 enum pipe pipe = crtc->pipe;
2182
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002183 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002184 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002185 wm->sr = wm_state->sr[wm->level];
2186
Ville Syrjälä1b313892016-11-28 19:37:08 +02002187 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2188 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2189 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2190 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002191 }
2192}
2193
Ville Syrjäläff32c542017-03-02 19:14:57 +02002194static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002195{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002196 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2197 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002198
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002199 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002200
Ville Syrjäläff32c542017-03-02 19:14:57 +02002201 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002202 return;
2203
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002204 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002205 chv_set_memory_dvfs(dev_priv, false);
2206
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002207 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002208 chv_set_memory_pm5(dev_priv, false);
2209
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002210 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002211 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002212
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002213 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002214
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002215 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002216 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002217
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002218 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002219 chv_set_memory_pm5(dev_priv, true);
2220
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002221 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002222 chv_set_memory_dvfs(dev_priv, true);
2223
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002224 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002225}
2226
Ville Syrjäläff32c542017-03-02 19:14:57 +02002227static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002228 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002229{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002230 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2231 const struct intel_crtc_state *crtc_state =
2232 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002233
2234 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002235 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2236 vlv_program_watermarks(dev_priv);
2237 mutex_unlock(&dev_priv->wm.wm_mutex);
2238}
2239
2240static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002241 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002242{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002243 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2244 const struct intel_crtc_state *crtc_state =
2245 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002246
2247 if (!crtc_state->wm.need_postvbl_update)
2248 return;
2249
2250 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002251 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002252 vlv_program_watermarks(dev_priv);
2253 mutex_unlock(&dev_priv->wm.wm_mutex);
2254}
2255
Ville Syrjälä432081b2016-10-31 22:37:03 +02002256static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002257{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002258 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002259 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002260 int srwm = 1;
2261 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002262 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002263
2264 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002265 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002266 if (crtc) {
2267 /* self-refresh has much higher latency */
2268 static const int sr_latency_ns = 12000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002269 const struct drm_display_mode *pipe_mode =
2270 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002271 const struct drm_framebuffer *fb =
2272 crtc->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002273 int clock = pipe_mode->crtc_clock;
2274 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002275 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002276 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002277 int entries;
2278
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002279 entries = intel_wm_method2(clock, htotal,
2280 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002281 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2282 srwm = I965_FIFO_SIZE - entries;
2283 if (srwm < 0)
2284 srwm = 1;
2285 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002286 drm_dbg_kms(&dev_priv->drm,
2287 "self-refresh entries: %d, wm: %d\n",
2288 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002289
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002290 entries = intel_wm_method2(clock, htotal,
2291 crtc->base.cursor->state->crtc_w, 4,
2292 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002293 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002294 i965_cursor_wm_info.cacheline_size) +
2295 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002296
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002297 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002298 if (cursor_sr > i965_cursor_wm_info.max_wm)
2299 cursor_sr = i965_cursor_wm_info.max_wm;
2300
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002301 drm_dbg_kms(&dev_priv->drm,
2302 "self-refresh watermark: display plane %d "
2303 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002304
Imre Deak98584252014-06-13 14:54:20 +03002305 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002306 } else {
Imre Deak98584252014-06-13 14:54:20 +03002307 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002308 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002309 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002310 }
2311
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002312 drm_dbg_kms(&dev_priv->drm,
2313 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2314 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002315
2316 /* 965 has limitations... */
Jani Nikula5f461662020-11-30 13:15:58 +02002317 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002318 FW_WM(8, CURSORB) |
2319 FW_WM(8, PLANEB) |
2320 FW_WM(8, PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02002321 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002322 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002323 /* update cursor SR watermark */
Jani Nikula5f461662020-11-30 13:15:58 +02002324 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002325
2326 if (cxsr_enabled)
2327 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002328}
2329
Ville Syrjäläf4998962015-03-10 17:02:21 +02002330#undef FW_WM
2331
Ville Syrjälä432081b2016-10-31 22:37:03 +02002332static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002333{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002334 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002335 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002336 u32 fwater_lo;
2337 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002338 int cwm, srwm = 1;
2339 int fifo_size;
2340 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002341 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002342
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002343 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344 wm_info = &i945_wm_info;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002345 else if (DISPLAY_VER(dev_priv) != 2)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002346 wm_info = &i915_wm_info;
2347 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002348 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002349
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002350 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2351 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002352 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002353 const struct drm_display_mode *pipe_mode =
2354 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002355 const struct drm_framebuffer *fb =
2356 crtc->base.primary->state->fb;
2357 int cpp;
2358
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002359 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002360 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002361 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002362 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002363
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002364 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002365 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002366 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002367 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002368 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002369 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002370 if (planea_wm > (long)wm_info->max_wm)
2371 planea_wm = wm_info->max_wm;
2372 }
2373
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002374 if (DISPLAY_VER(dev_priv) == 2)
Ville Syrjälä9d539102014-08-15 01:21:53 +03002375 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002376
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002377 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2378 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002379 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002380 const struct drm_display_mode *pipe_mode =
2381 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002382 const struct drm_framebuffer *fb =
2383 crtc->base.primary->state->fb;
2384 int cpp;
2385
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002386 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002387 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002388 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002389 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002390
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002391 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002392 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002393 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002394 if (enabled == NULL)
2395 enabled = crtc;
2396 else
2397 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002398 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002399 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002400 if (planeb_wm > (long)wm_info->max_wm)
2401 planeb_wm = wm_info->max_wm;
2402 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002403
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002404 drm_dbg_kms(&dev_priv->drm,
2405 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002406
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002407 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002408 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002409
Ville Syrjäläefc26112016-10-31 22:37:04 +02002410 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002411
2412 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002413 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002414 enabled = NULL;
2415 }
2416
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002417 /*
2418 * Overlay gets an aggressive default since video jitter is bad.
2419 */
2420 cwm = 2;
2421
2422 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002423 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002424
2425 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002426 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002427 /* self-refresh has much higher latency */
2428 static const int sr_latency_ns = 6000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002429 const struct drm_display_mode *pipe_mode =
2430 &enabled->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002431 const struct drm_framebuffer *fb =
2432 enabled->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002433 int clock = pipe_mode->crtc_clock;
2434 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002435 int hdisplay = enabled->config->pipe_src_w;
2436 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002437 int entries;
2438
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002439 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002440 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002441 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002442 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002443
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002444 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2445 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002446 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002447 drm_dbg_kms(&dev_priv->drm,
2448 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002449 srwm = wm_info->fifo_size - entries;
2450 if (srwm < 0)
2451 srwm = 1;
2452
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002453 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02002454 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002455 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002456 else
Jani Nikula5f461662020-11-30 13:15:58 +02002457 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002458 }
2459
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002460 drm_dbg_kms(&dev_priv->drm,
2461 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2462 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002463
2464 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2465 fwater_hi = (cwm & 0x1f);
2466
2467 /* Set request length to 8 cachelines per fetch */
2468 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2469 fwater_hi = fwater_hi | (1 << 8);
2470
Jani Nikula5f461662020-11-30 13:15:58 +02002471 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2472 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002473
Imre Deak5209b1f2014-07-01 12:36:17 +03002474 if (enabled)
2475 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002476}
2477
Ville Syrjälä432081b2016-10-31 22:37:03 +02002478static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002479{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002480 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002481 struct intel_crtc *crtc;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002482 const struct drm_display_mode *pipe_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002483 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002484 int planea_wm;
2485
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002486 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002487 if (crtc == NULL)
2488 return;
2489
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002490 pipe_mode = &crtc->config->hw.pipe_mode;
2491 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002492 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002493 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002494 4, pessimal_latency_ns);
Jani Nikula5f461662020-11-30 13:15:58 +02002495 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002496 fwater_lo |= (3<<8) | planea_wm;
2497
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002498 drm_dbg_kms(&dev_priv->drm,
2499 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002500
Jani Nikula5f461662020-11-30 13:15:58 +02002501 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002502}
2503
Ville Syrjälä37126462013-08-01 16:18:55 +03002504/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002505static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2506 unsigned int cpp,
2507 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002509 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002510
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002511 ret = intel_wm_method1(pixel_rate, cpp, latency);
2512 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002513
2514 return ret;
2515}
2516
Ville Syrjälä37126462013-08-01 16:18:55 +03002517/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002518static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2519 unsigned int htotal,
2520 unsigned int width,
2521 unsigned int cpp,
2522 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002523{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002524 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002525
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002526 ret = intel_wm_method2(pixel_rate, htotal,
2527 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002528 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002529
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002530 return ret;
2531}
2532
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002533static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002534{
Matt Roper15126882015-12-03 11:37:40 -08002535 /*
2536 * Neither of these should be possible since this function shouldn't be
2537 * called if the CRTC is off or the plane is invisible. But let's be
2538 * extra paranoid to avoid a potential divide-by-zero if we screw up
2539 * elsewhere in the driver.
2540 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002541 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002542 return 0;
2543 if (WARN_ON(!horiz_pixels))
2544 return 0;
2545
Ville Syrjäläac484962016-01-20 21:05:26 +02002546 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002547}
2548
Imre Deak820c1982013-12-17 14:46:36 +02002549struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002550 u16 pri;
2551 u16 spr;
2552 u16 cur;
2553 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002554};
2555
Ville Syrjälä37126462013-08-01 16:18:55 +03002556/*
2557 * For both WM_PIPE and WM_LP.
2558 * mem_value must be in 0.1us units.
2559 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002560static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2561 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002562 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002563{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002564 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002565 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002566
Ville Syrjälä03981c62018-11-14 19:34:40 +02002567 if (mem_value == 0)
2568 return U32_MAX;
2569
Maarten Lankhorstec193642019-06-28 10:55:17 +02002570 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002571 return 0;
2572
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002573 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002574
Maarten Lankhorstec193642019-06-28 10:55:17 +02002575 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002576
2577 if (!is_lp)
2578 return method1;
2579
Maarten Lankhorstec193642019-06-28 10:55:17 +02002580 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002581 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002582 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002583 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002584
2585 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002586}
2587
Ville Syrjälä37126462013-08-01 16:18:55 +03002588/*
2589 * For both WM_PIPE and WM_LP.
2590 * mem_value must be in 0.1us units.
2591 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002592static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2593 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002594 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002595{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002596 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002597 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002598
Ville Syrjälä03981c62018-11-14 19:34:40 +02002599 if (mem_value == 0)
2600 return U32_MAX;
2601
Maarten Lankhorstec193642019-06-28 10:55:17 +02002602 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002603 return 0;
2604
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002605 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002606
Maarten Lankhorstec193642019-06-28 10:55:17 +02002607 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2608 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002609 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002610 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002611 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002612 return min(method1, method2);
2613}
2614
Ville Syrjälä37126462013-08-01 16:18:55 +03002615/*
2616 * For both WM_PIPE and WM_LP.
2617 * mem_value must be in 0.1us units.
2618 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002619static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2620 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002621 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002622{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002623 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002624
Ville Syrjälä03981c62018-11-14 19:34:40 +02002625 if (mem_value == 0)
2626 return U32_MAX;
2627
Maarten Lankhorstec193642019-06-28 10:55:17 +02002628 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002629 return 0;
2630
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002631 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002632
Maarten Lankhorstec193642019-06-28 10:55:17 +02002633 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002634 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002635 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002636 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002637}
2638
Paulo Zanonicca32e92013-05-31 11:45:06 -03002639/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002640static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2641 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002642 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002643{
Ville Syrjälä83054942016-11-18 21:53:00 +02002644 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002645
Maarten Lankhorstec193642019-06-28 10:55:17 +02002646 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002647 return 0;
2648
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002649 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002650
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002651 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2652 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002653}
2654
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002655static unsigned int
2656ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002657{
Matt Roper7dadd282021-03-19 21:42:43 -07002658 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002659 return 3072;
Matt Roper7dadd282021-03-19 21:42:43 -07002660 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002661 return 768;
2662 else
2663 return 512;
2664}
2665
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002666static unsigned int
2667ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2668 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002669{
Matt Roper7dadd282021-03-19 21:42:43 -07002670 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002671 /* BDW primary/sprite plane watermarks */
2672 return level == 0 ? 255 : 2047;
Matt Roper7dadd282021-03-19 21:42:43 -07002673 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002674 /* IVB/HSW primary/sprite plane watermarks */
2675 return level == 0 ? 127 : 1023;
2676 else if (!is_sprite)
2677 /* ILK/SNB primary plane watermarks */
2678 return level == 0 ? 127 : 511;
2679 else
2680 /* ILK/SNB sprite plane watermarks */
2681 return level == 0 ? 63 : 255;
2682}
2683
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002684static unsigned int
2685ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002686{
Matt Roper7dadd282021-03-19 21:42:43 -07002687 if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002688 return level == 0 ? 63 : 255;
2689 else
2690 return level == 0 ? 31 : 63;
2691}
2692
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002693static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002694{
Matt Roper7dadd282021-03-19 21:42:43 -07002695 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002696 return 31;
2697 else
2698 return 15;
2699}
2700
Ville Syrjälä158ae642013-08-07 13:28:19 +03002701/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002702static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002703 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002704 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002705 enum intel_ddb_partitioning ddb_partitioning,
2706 bool is_sprite)
2707{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002708 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002709
2710 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002711 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002712 return 0;
2713
2714 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002715 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002716 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002717
2718 /*
2719 * For some reason the non self refresh
2720 * FIFO size is only half of the self
2721 * refresh FIFO size on ILK/SNB.
2722 */
Matt Roper7dadd282021-03-19 21:42:43 -07002723 if (DISPLAY_VER(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002724 fifo_size /= 2;
2725 }
2726
Ville Syrjälä240264f2013-08-07 13:29:12 +03002727 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002728 /* level 0 is always calculated with 1:1 split */
2729 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2730 if (is_sprite)
2731 fifo_size *= 5;
2732 fifo_size /= 6;
2733 } else {
2734 fifo_size /= 2;
2735 }
2736 }
2737
2738 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002739 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002740}
2741
2742/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002743static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002744 int level,
2745 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002746{
2747 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002748 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002749 return 64;
2750
2751 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002752 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002753}
2754
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002755static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002756 int level,
2757 const struct intel_wm_config *config,
2758 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002759 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002760{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002761 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2762 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2763 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2764 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002765}
2766
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002767static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002768 int level,
2769 struct ilk_wm_maximums *max)
2770{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002771 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2772 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2773 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2774 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002775}
2776
Ville Syrjäläd9395652013-10-09 19:18:10 +03002777static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002778 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002779 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002780{
2781 bool ret;
2782
2783 /* already determined to be invalid? */
2784 if (!result->enable)
2785 return false;
2786
2787 result->enable = result->pri_val <= max->pri &&
2788 result->spr_val <= max->spr &&
2789 result->cur_val <= max->cur;
2790
2791 ret = result->enable;
2792
2793 /*
2794 * HACK until we can pre-compute everything,
2795 * and thus fail gracefully if LP0 watermarks
2796 * are exceeded...
2797 */
2798 if (level == 0 && !result->enable) {
2799 if (result->pri_val > max->pri)
2800 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2801 level, result->pri_val, max->pri);
2802 if (result->spr_val > max->spr)
2803 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2804 level, result->spr_val, max->spr);
2805 if (result->cur_val > max->cur)
2806 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2807 level, result->cur_val, max->cur);
2808
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002809 result->pri_val = min_t(u32, result->pri_val, max->pri);
2810 result->spr_val = min_t(u32, result->spr_val, max->spr);
2811 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002812 result->enable = true;
2813 }
2814
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002815 return ret;
2816}
2817
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002818static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002819 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002820 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002821 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002822 const struct intel_plane_state *pristate,
2823 const struct intel_plane_state *sprstate,
2824 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002825 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002826{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002827 u16 pri_latency = dev_priv->wm.pri_latency[level];
2828 u16 spr_latency = dev_priv->wm.spr_latency[level];
2829 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002830
2831 /* WM1+ latency values stored in 0.5us units */
2832 if (level > 0) {
2833 pri_latency *= 5;
2834 spr_latency *= 5;
2835 cur_latency *= 5;
2836 }
2837
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002838 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002839 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002840 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002841 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002842 }
2843
2844 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002845 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002846
2847 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002848 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002849
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002850 result->enable = true;
2851}
2852
Ville Syrjäläbb726512016-10-31 22:37:24 +02002853static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002854 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002855{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002856 struct intel_uncore *uncore = &dev_priv->uncore;
2857
Matt Roper7dadd282021-03-19 21:42:43 -07002858 if (DISPLAY_VER(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002859 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002860 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002861 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roperd3252e12021-08-20 15:57:10 -07002862 int mult = IS_DG2(dev_priv) ? 2 : 1;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002863
2864 /* read the first set of memory latencies[0:3] */
2865 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002866 ret = sandybridge_pcode_read(dev_priv,
2867 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002868 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002869
2870 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002871 drm_err(&dev_priv->drm,
2872 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002873 return;
2874 }
2875
Matt Roperd3252e12021-08-20 15:57:10 -07002876 wm[0] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2877 wm[1] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2878 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2879 wm[2] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2880 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2881 wm[3] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2882 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002883
2884 /* read the second set of memory latencies[4:7] */
2885 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002886 ret = sandybridge_pcode_read(dev_priv,
2887 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002888 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002889 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002890 drm_err(&dev_priv->drm,
2891 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002892 return;
2893 }
2894
Matt Roperd3252e12021-08-20 15:57:10 -07002895 wm[4] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2896 wm[5] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2897 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2898 wm[6] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2899 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2900 wm[7] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2901 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002902
Vandana Kannan367294b2014-11-04 17:06:46 +00002903 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002904 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2905 * need to be disabled. We make sure to sanitize the values out
2906 * of the punit to satisfy this requirement.
2907 */
2908 for (level = 1; level <= max_level; level++) {
2909 if (wm[level] == 0) {
2910 for (i = level + 1; i <= max_level; i++)
2911 wm[i] = 0;
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002912
2913 max_level = level - 1;
2914
Paulo Zanoni0727e402016-09-22 18:00:30 -03002915 break;
2916 }
2917 }
2918
2919 /*
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002920 * WaWmMemoryReadLatency
Damien Lespiau6f972352015-02-09 19:33:07 +00002921 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002922 * punit doesn't take into account the read latency so we need
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002923 * to add proper adjustement to each valid level we retrieve
2924 * from the punit when level 0 response data is 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002925 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002926 if (wm[0] == 0) {
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002927 u8 adjust = DISPLAY_VER(dev_priv) >= 12 ? 3 : 2;
2928
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002929 for (level = 0; level <= max_level; level++)
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002930 wm[level] += adjust;
Paulo Zanoni0727e402016-09-22 18:00:30 -03002931 }
2932
Mahesh Kumar86b59282018-08-31 16:39:42 +05302933 /*
2934 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2935 * If we could not get dimm info enable this WA to prevent from
2936 * any underrun. If not able to get Dimm info assume 16GB dimm
2937 * to avoid any underrun.
2938 */
José Roberto de Souza66a24502021-01-28 08:43:12 -08002939 if (dev_priv->dram_info.wm_lv_0_adjust_needed)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302940 wm[0] += 1;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002941 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002942 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002943
2944 wm[0] = (sskpd >> 56) & 0xFF;
2945 if (wm[0] == 0)
2946 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002947 wm[1] = (sskpd >> 4) & 0xFF;
2948 wm[2] = (sskpd >> 12) & 0xFF;
2949 wm[3] = (sskpd >> 20) & 0x1FF;
2950 wm[4] = (sskpd >> 32) & 0x1FF;
Matt Roper7dadd282021-03-19 21:42:43 -07002951 } else if (DISPLAY_VER(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002952 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002953
2954 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2955 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2956 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2957 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Matt Roper7dadd282021-03-19 21:42:43 -07002958 } else if (DISPLAY_VER(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002959 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002960
2961 /* ILK primary LP0 latency is 700 ns */
2962 wm[0] = 7;
2963 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2964 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002965 } else {
2966 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002967 }
2968}
2969
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002970static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002971 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002972{
2973 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002974 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002975 wm[0] = 13;
2976}
2977
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002978static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002979 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002980{
2981 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002982 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002983 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002984}
2985
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002986int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002987{
2988 /* how many WM levels are we expecting */
Matt Roper7959ffe2021-05-18 17:06:11 -07002989 if (HAS_HW_SAGV_WM(dev_priv))
2990 return 5;
2991 else if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002992 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002993 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002994 return 4;
Matt Roper7dadd282021-03-19 21:42:43 -07002995 else if (DISPLAY_VER(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002996 return 3;
2997 else
2998 return 2;
2999}
Daniel Vetter7526ed72014-09-29 15:07:19 +02003000
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003001static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003002 const char *name,
Linus Torvaldse7c6e402021-04-27 17:05:53 -07003003 const u16 wm[])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003004{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003005 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003006
3007 for (level = 0; level <= max_level; level++) {
3008 unsigned int latency = wm[level];
3009
3010 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003011 drm_dbg_kms(&dev_priv->drm,
3012 "%s WM%d latency not provided\n",
3013 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003014 continue;
3015 }
3016
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003017 /*
3018 * - latencies are in us on gen9.
3019 * - before then, WM1+ latency values are in 0.5us units
3020 */
Matt Roper7dadd282021-03-19 21:42:43 -07003021 if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003022 latency *= 10;
3023 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003024 latency *= 5;
3025
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003026 drm_dbg_kms(&dev_priv->drm,
3027 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3028 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003029 }
3030}
3031
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003032static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003033 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003034{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003035 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003036
3037 if (wm[0] >= min)
3038 return false;
3039
3040 wm[0] = max(wm[0], min);
3041 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003042 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003043
3044 return true;
3045}
3046
Ville Syrjäläbb726512016-10-31 22:37:24 +02003047static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003048{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003049 bool changed;
3050
3051 /*
3052 * The BIOS provided WM memory latency values are often
3053 * inadequate for high resolution displays. Adjust them.
3054 */
3055 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3056 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3057 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3058
3059 if (!changed)
3060 return;
3061
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003062 drm_dbg_kms(&dev_priv->drm,
3063 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003064 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3065 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3066 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003067}
3068
Ville Syrjälä03981c62018-11-14 19:34:40 +02003069static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3070{
3071 /*
3072 * On some SNB machines (Thinkpad X220 Tablet at least)
3073 * LP3 usage can cause vblank interrupts to be lost.
3074 * The DEIIR bit will go high but it looks like the CPU
3075 * never gets interrupted.
3076 *
3077 * It's not clear whether other interrupt source could
3078 * be affected or if this is somehow limited to vblank
3079 * interrupts only. To play it safe we disable LP3
3080 * watermarks entirely.
3081 */
3082 if (dev_priv->wm.pri_latency[3] == 0 &&
3083 dev_priv->wm.spr_latency[3] == 0 &&
3084 dev_priv->wm.cur_latency[3] == 0)
3085 return;
3086
3087 dev_priv->wm.pri_latency[3] = 0;
3088 dev_priv->wm.spr_latency[3] = 0;
3089 dev_priv->wm.cur_latency[3] = 0;
3090
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003091 drm_dbg_kms(&dev_priv->drm,
3092 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003093 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3094 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3095 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3096}
3097
Ville Syrjäläbb726512016-10-31 22:37:24 +02003098static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003099{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003100 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003101
3102 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3103 sizeof(dev_priv->wm.pri_latency));
3104 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3105 sizeof(dev_priv->wm.pri_latency));
3106
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003107 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003108 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003109
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003110 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3111 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3112 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003113
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003114 if (DISPLAY_VER(dev_priv) == 6) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003115 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003116 snb_wm_lp3_irq_quirk(dev_priv);
3117 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003118}
3119
Ville Syrjäläbb726512016-10-31 22:37:24 +02003120static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003121{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003122 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003123 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003124}
3125
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003126static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003127 struct intel_pipe_wm *pipe_wm)
3128{
3129 /* LP0 watermark maximums depend on this pipe alone */
3130 const struct intel_wm_config config = {
3131 .num_pipes_active = 1,
3132 .sprites_enabled = pipe_wm->sprites_enabled,
3133 .sprites_scaled = pipe_wm->sprites_scaled,
3134 };
3135 struct ilk_wm_maximums max;
3136
3137 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003138 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003139
3140 /* At least LP0 must be valid */
3141 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003142 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003143 return false;
3144 }
3145
3146 return true;
3147}
3148
Matt Roper261a27d2015-10-08 15:28:25 -07003149/* Compute new watermarks for the pipe */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003150static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
3151 struct intel_crtc *crtc)
Matt Roper261a27d2015-10-08 15:28:25 -07003152{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003153 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3154 struct intel_crtc_state *crtc_state =
3155 intel_atomic_get_new_crtc_state(state, crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003156 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003157 struct intel_plane *plane;
3158 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003159 const struct intel_plane_state *pristate = NULL;
3160 const struct intel_plane_state *sprstate = NULL;
3161 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003162 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003163 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003164
Maarten Lankhorstec193642019-06-28 10:55:17 +02003165 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003166
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003167 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3168 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3169 pristate = plane_state;
3170 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3171 sprstate = plane_state;
3172 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3173 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003174 }
3175
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003176 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003177 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003178 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3179 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3180 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3181 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003182 }
3183
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003184 usable_level = max_level;
3185
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003186 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Matt Roper7dadd282021-03-19 21:42:43 -07003187 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003188 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003189
3190 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003191 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003192 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003193
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003194 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003195 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003196 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003197
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003198 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003199 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003200
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003201 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003202
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003203 for (level = 1; level <= usable_level; level++) {
3204 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003205
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003206 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003207 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003208
3209 /*
3210 * Disable any watermark level that exceeds the
3211 * register maximums since such watermarks are
3212 * always invalid.
3213 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003214 if (!ilk_validate_wm_level(level, &max, wm)) {
3215 memset(wm, 0, sizeof(*wm));
3216 break;
3217 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003218 }
3219
Matt Roper86c8bbb2015-09-24 15:53:16 -07003220 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003221}
3222
3223/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003224 * Build a set of 'intermediate' watermark values that satisfy both the old
3225 * state and the new state. These can be programmed to the hardware
3226 * immediately.
3227 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003228static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
3229 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08003230{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003231 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3232 struct intel_crtc_state *new_crtc_state =
3233 intel_atomic_get_new_crtc_state(state, crtc);
3234 const struct intel_crtc_state *old_crtc_state =
3235 intel_atomic_get_old_crtc_state(state, crtc);
3236 struct intel_pipe_wm *a = &new_crtc_state->wm.ilk.intermediate;
3237 const struct intel_pipe_wm *b = &old_crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003238 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003239
3240 /*
3241 * Start with the final, target watermarks, then combine with the
3242 * currently active watermarks to get values that are safe both before
3243 * and after the vblank.
3244 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003245 *a = new_crtc_state->wm.ilk.optimal;
3246 if (!new_crtc_state->hw.active ||
3247 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) ||
3248 state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003249 return 0;
3250
Matt Ropered4a6a72016-02-23 17:20:13 -08003251 a->pipe_enabled |= b->pipe_enabled;
3252 a->sprites_enabled |= b->sprites_enabled;
3253 a->sprites_scaled |= b->sprites_scaled;
3254
3255 for (level = 0; level <= max_level; level++) {
3256 struct intel_wm_level *a_wm = &a->wm[level];
3257 const struct intel_wm_level *b_wm = &b->wm[level];
3258
3259 a_wm->enable &= b_wm->enable;
3260 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3261 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3262 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3263 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3264 }
3265
3266 /*
3267 * We need to make sure that these merged watermark values are
3268 * actually a valid configuration themselves. If they're not,
3269 * there's no safe way to transition from the old state to
3270 * the new state, so we need to fail the atomic transaction.
3271 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003272 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003273 return -EINVAL;
3274
3275 /*
3276 * If our intermediate WM are identical to the final WM, then we can
3277 * omit the post-vblank programming; only update if it's different.
3278 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003279 if (memcmp(a, &new_crtc_state->wm.ilk.optimal, sizeof(*a)) != 0)
3280 new_crtc_state->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003281
3282 return 0;
3283}
3284
3285/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003286 * Merge the watermarks from all active pipes for a specific level.
3287 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003288static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003289 int level,
3290 struct intel_wm_level *ret_wm)
3291{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003292 const struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003293
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003294 ret_wm->enable = true;
3295
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003296 for_each_intel_crtc(&dev_priv->drm, crtc) {
3297 const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003298 const struct intel_wm_level *wm = &active->wm[level];
3299
3300 if (!active->pipe_enabled)
3301 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003302
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003303 /*
3304 * The watermark values may have been used in the past,
3305 * so we must maintain them in the registers for some
3306 * time even if the level is now disabled.
3307 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003308 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003309 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003310
3311 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3312 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3313 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3314 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3315 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003316}
3317
3318/*
3319 * Merge all low power watermarks for all active pipes.
3320 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003321static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003322 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003323 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003324 struct intel_pipe_wm *merged)
3325{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003326 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003327 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003328
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003329 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Matt Roper7dadd282021-03-19 21:42:43 -07003330 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003331 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003332 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003333
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003334 /* ILK: FBC WM must be disabled always */
Matt Roper7dadd282021-03-19 21:42:43 -07003335 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003336
3337 /* merge each WM1+ level */
3338 for (level = 1; level <= max_level; level++) {
3339 struct intel_wm_level *wm = &merged->wm[level];
3340
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003341 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003342
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003343 if (level > last_enabled_level)
3344 wm->enable = false;
3345 else if (!ilk_validate_wm_level(level, max, wm))
3346 /* make sure all following levels get disabled */
3347 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003348
3349 /*
3350 * The spec says it is preferred to disable
3351 * FBC WMs instead of disabling a WM level.
3352 */
3353 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003354 if (wm->enable)
3355 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003356 wm->fbc_val = 0;
3357 }
3358 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003359
3360 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3361 /*
3362 * FIXME this is racy. FBC might get enabled later.
3363 * What we should check here is whether FBC can be
3364 * enabled sometime later.
3365 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003366 if (DISPLAY_VER(dev_priv) == 5 && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003367 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003368 for (level = 2; level <= max_level; level++) {
3369 struct intel_wm_level *wm = &merged->wm[level];
3370
3371 wm->enable = false;
3372 }
3373 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003374}
3375
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003376static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3377{
3378 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3379 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3380}
3381
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003382/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003383static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3384 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003385{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003386 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003387 return 2 * level;
3388 else
3389 return dev_priv->wm.pri_latency[level];
3390}
3391
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003392static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003393 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003394 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003395 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003396{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003397 struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003398 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003399
Ville Syrjälä0362c782013-10-09 19:17:57 +03003400 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003401 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003402
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003403 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003404 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003405 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003406
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003407 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003408
Ville Syrjälä0362c782013-10-09 19:17:57 +03003409 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003410
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003411 /*
3412 * Maintain the watermark values even if the level is
3413 * disabled. Doing otherwise could cause underruns.
3414 */
3415 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003416 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003417 (r->pri_val << WM1_LP_SR_SHIFT) |
3418 r->cur_val;
3419
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003420 if (r->enable)
3421 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3422
Matt Roper7dadd282021-03-19 21:42:43 -07003423 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003424 results->wm_lp[wm_lp - 1] |=
3425 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3426 else
3427 results->wm_lp[wm_lp - 1] |=
3428 r->fbc_val << WM1_LP_FBC_SHIFT;
3429
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003430 /*
3431 * Always set WM1S_LP_EN when spr_val != 0, even if the
3432 * level is disabled. Doing otherwise could cause underruns.
3433 */
Matt Roper7dadd282021-03-19 21:42:43 -07003434 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303435 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003436 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3437 } else
3438 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003439 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003440
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003441 /* LP0 register values */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003442 for_each_intel_crtc(&dev_priv->drm, crtc) {
3443 enum pipe pipe = crtc->pipe;
3444 const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003445 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003446
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303447 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003448 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003449
3450 results->wm_pipe[pipe] =
3451 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3452 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3453 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003454 }
3455}
3456
Paulo Zanoni861f3382013-05-31 10:19:21 -03003457/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3458 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003459static struct intel_pipe_wm *
3460ilk_find_best_result(struct drm_i915_private *dev_priv,
3461 struct intel_pipe_wm *r1,
3462 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003463{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003464 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003465 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003466
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003467 for (level = 1; level <= max_level; level++) {
3468 if (r1->wm[level].enable)
3469 level1 = level;
3470 if (r2->wm[level].enable)
3471 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003472 }
3473
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003474 if (level1 == level2) {
3475 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003476 return r2;
3477 else
3478 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003479 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003480 return r1;
3481 } else {
3482 return r2;
3483 }
3484}
3485
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003486/* dirty bits used to track which watermarks need changes */
3487#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003488#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3489#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3490#define WM_DIRTY_FBC (1 << 24)
3491#define WM_DIRTY_DDB (1 << 25)
3492
Damien Lespiau055e3932014-08-18 13:49:10 +01003493static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003494 const struct ilk_wm_values *old,
3495 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003496{
3497 unsigned int dirty = 0;
3498 enum pipe pipe;
3499 int wm_lp;
3500
Damien Lespiau055e3932014-08-18 13:49:10 +01003501 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003502 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3503 dirty |= WM_DIRTY_PIPE(pipe);
3504 /* Must disable LP1+ watermarks too */
3505 dirty |= WM_DIRTY_LP_ALL;
3506 }
3507 }
3508
3509 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3510 dirty |= WM_DIRTY_FBC;
3511 /* Must disable LP1+ watermarks too */
3512 dirty |= WM_DIRTY_LP_ALL;
3513 }
3514
3515 if (old->partitioning != new->partitioning) {
3516 dirty |= WM_DIRTY_DDB;
3517 /* Must disable LP1+ watermarks too */
3518 dirty |= WM_DIRTY_LP_ALL;
3519 }
3520
3521 /* LP1+ watermarks already deemed dirty, no need to continue */
3522 if (dirty & WM_DIRTY_LP_ALL)
3523 return dirty;
3524
3525 /* Find the lowest numbered LP1+ watermark in need of an update... */
3526 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3527 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3528 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3529 break;
3530 }
3531
3532 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3533 for (; wm_lp <= 3; wm_lp++)
3534 dirty |= WM_DIRTY_LP(wm_lp);
3535
3536 return dirty;
3537}
3538
Ville Syrjälä8553c182013-12-05 15:51:39 +02003539static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3540 unsigned int dirty)
3541{
Imre Deak820c1982013-12-17 14:46:36 +02003542 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003543 bool changed = false;
3544
3545 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3546 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003547 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003548 changed = true;
3549 }
3550 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3551 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003552 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003553 changed = true;
3554 }
3555 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3556 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003557 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003558 changed = true;
3559 }
3560
3561 /*
3562 * Don't touch WM1S_LP_EN here.
3563 * Doing so could cause underruns.
3564 */
3565
3566 return changed;
3567}
3568
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003569/*
3570 * The spec says we shouldn't write when we don't need, because every write
3571 * causes WMs to be re-evaluated, expending some power.
3572 */
Imre Deak820c1982013-12-17 14:46:36 +02003573static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3574 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003575{
Imre Deak820c1982013-12-17 14:46:36 +02003576 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003577 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003578 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003579
Damien Lespiau055e3932014-08-18 13:49:10 +01003580 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003581 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003582 return;
3583
Ville Syrjälä8553c182013-12-05 15:51:39 +02003584 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003585
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003586 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Jani Nikula5f461662020-11-30 13:15:58 +02003587 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003588 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Jani Nikula5f461662020-11-30 13:15:58 +02003589 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003590 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Jani Nikula5f461662020-11-30 13:15:58 +02003591 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003592
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003593 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003594 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02003595 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003596 if (results->partitioning == INTEL_DDB_PART_1_2)
3597 val &= ~WM_MISC_DATA_PARTITION_5_6;
3598 else
3599 val |= WM_MISC_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003600 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003601 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02003602 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003603 if (results->partitioning == INTEL_DDB_PART_1_2)
3604 val &= ~DISP_DATA_PARTITION_5_6;
3605 else
3606 val |= DISP_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003607 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003608 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003609 }
3610
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003611 if (dirty & WM_DIRTY_FBC) {
Jani Nikula5f461662020-11-30 13:15:58 +02003612 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003613 if (results->enable_fbc_wm)
3614 val &= ~DISP_FBC_WM_DIS;
3615 else
3616 val |= DISP_FBC_WM_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02003617 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003618 }
3619
Imre Deak954911e2013-12-17 14:46:34 +02003620 if (dirty & WM_DIRTY_LP(1) &&
3621 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003622 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
Imre Deak954911e2013-12-17 14:46:34 +02003623
Matt Roper7dadd282021-03-19 21:42:43 -07003624 if (DISPLAY_VER(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003625 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003626 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003627 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003628 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003629 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003630
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003631 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003632 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003633 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003634 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003635 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003636 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003637
3638 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003639}
3640
Ville Syrjälä60aca572019-11-27 21:05:51 +02003641bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003642{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003643 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3644}
3645
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003646u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303647{
Ville Syrjäläb88da662021-04-16 20:10:09 +03003648 u8 enabled_slices = 0;
3649 enum dbuf_slice slice;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303650
Ville Syrjäläb88da662021-04-16 20:10:09 +03003651 for_each_dbuf_slice(dev_priv, slice) {
3652 if (intel_uncore_read(&dev_priv->uncore,
3653 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3654 enabled_slices |= BIT(slice);
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003655 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303656
Ville Syrjäläb88da662021-04-16 20:10:09 +03003657 return enabled_slices;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303658}
3659
Matt Roper024c9042015-09-24 15:53:11 -07003660/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003661 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3662 * so assume we'll always need it in order to avoid underruns.
3663 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003664static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003665{
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003666 return DISPLAY_VER(dev_priv) == 9;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003667}
3668
Paulo Zanoni56feca92016-09-22 18:00:28 -03003669static bool
3670intel_has_sagv(struct drm_i915_private *dev_priv)
3671{
Matt Roper70bfb302021-04-07 13:39:45 -07003672 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003673 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003674}
3675
James Ausmusb068a862019-10-09 10:23:14 -07003676static void
3677skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3678{
Matt Roper7dadd282021-03-19 21:42:43 -07003679 if (DISPLAY_VER(dev_priv) >= 12) {
James Ausmusda80f042019-10-09 10:23:15 -07003680 u32 val = 0;
3681 int ret;
3682
3683 ret = sandybridge_pcode_read(dev_priv,
3684 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3685 &val, NULL);
3686 if (!ret) {
3687 dev_priv->sagv_block_time_us = val;
3688 return;
3689 }
3690
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003691 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003692 } else if (DISPLAY_VER(dev_priv) == 11) {
James Ausmusb068a862019-10-09 10:23:14 -07003693 dev_priv->sagv_block_time_us = 10;
3694 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003695 } else if (DISPLAY_VER(dev_priv) == 10) {
James Ausmusb068a862019-10-09 10:23:14 -07003696 dev_priv->sagv_block_time_us = 20;
3697 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003698 } else if (DISPLAY_VER(dev_priv) == 9) {
James Ausmusb068a862019-10-09 10:23:14 -07003699 dev_priv->sagv_block_time_us = 30;
3700 return;
3701 } else {
Matt Roper7dadd282021-03-19 21:42:43 -07003702 MISSING_CASE(DISPLAY_VER(dev_priv));
James Ausmusb068a862019-10-09 10:23:14 -07003703 }
3704
3705 /* Default to an unusable block time */
3706 dev_priv->sagv_block_time_us = -1;
3707}
3708
Lyude656d1b82016-08-17 15:55:54 -04003709/*
3710 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3711 * depending on power and performance requirements. The display engine access
3712 * to system memory is blocked during the adjustment time. Because of the
3713 * blocking time, having this enabled can cause full system hangs and/or pipe
3714 * underruns if we don't meet all of the following requirements:
3715 *
3716 * - <= 1 pipe enabled
3717 * - All planes can enable watermarks for latencies >= SAGV engine block time
3718 * - We're not using an interlaced display configuration
3719 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003720static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003721intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003722{
3723 int ret;
3724
Paulo Zanoni56feca92016-09-22 18:00:28 -03003725 if (!intel_has_sagv(dev_priv))
3726 return 0;
3727
3728 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003729 return 0;
3730
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003731 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003732 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3733 GEN9_SAGV_ENABLE);
3734
Ville Syrjäläff61a972018-12-21 19:14:34 +02003735 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003736
3737 /*
3738 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003739 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003740 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003741 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003742 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003743 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003744 return 0;
3745 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003746 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003747 return ret;
3748 }
3749
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003750 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003751 return 0;
3752}
3753
Ville Syrjälä71024042020-09-25 15:17:48 +03003754static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003755intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003756{
Imre Deakb3b8e992016-12-05 18:27:38 +02003757 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003758
Paulo Zanoni56feca92016-09-22 18:00:28 -03003759 if (!intel_has_sagv(dev_priv))
3760 return 0;
3761
3762 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003763 return 0;
3764
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003765 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003766 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003767 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3768 GEN9_SAGV_DISABLE,
3769 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3770 1);
Lyude656d1b82016-08-17 15:55:54 -04003771 /*
3772 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003773 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003774 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003775 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003776 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003777 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003778 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003779 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003780 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003781 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003782 }
3783
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003784 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003785 return 0;
3786}
3787
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003788void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3789{
3790 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003791 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003792 const struct intel_bw_state *old_bw_state;
3793 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003794
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003795 /*
3796 * Just return if we can't control SAGV or don't have it.
3797 * This is different from situation when we have SAGV but just can't
3798 * afford it due to DBuf limitation - in case if SAGV is completely
3799 * disabled in a BIOS, we are not even allowed to send a PCode request,
3800 * as it will throw an error. So have to check it here.
3801 */
3802 if (!intel_has_sagv(dev_priv))
3803 return;
3804
3805 new_bw_state = intel_atomic_get_new_bw_state(state);
3806 if (!new_bw_state)
3807 return;
3808
Matt Roper7dadd282021-03-19 21:42:43 -07003809 if (DISPLAY_VER(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003810 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003811 return;
3812 }
3813
3814 old_bw_state = intel_atomic_get_old_bw_state(state);
3815 /*
3816 * Nothing to mask
3817 */
3818 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3819 return;
3820
3821 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3822
3823 /*
3824 * If new mask is zero - means there is nothing to mask,
3825 * we can only unmask, which should be done in unmask.
3826 */
3827 if (!new_mask)
3828 return;
3829
3830 /*
3831 * Restrict required qgv points before updating the configuration.
3832 * According to BSpec we can't mask and unmask qgv points at the same
3833 * time. Also masking should be done before updating the configuration
3834 * and unmasking afterwards.
3835 */
3836 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003837}
3838
3839void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3840{
3841 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003842 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003843 const struct intel_bw_state *old_bw_state;
3844 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003845
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003846 /*
3847 * Just return if we can't control SAGV or don't have it.
3848 * This is different from situation when we have SAGV but just can't
3849 * afford it due to DBuf limitation - in case if SAGV is completely
3850 * disabled in a BIOS, we are not even allowed to send a PCode request,
3851 * as it will throw an error. So have to check it here.
3852 */
3853 if (!intel_has_sagv(dev_priv))
3854 return;
3855
3856 new_bw_state = intel_atomic_get_new_bw_state(state);
3857 if (!new_bw_state)
3858 return;
3859
Matt Roper7dadd282021-03-19 21:42:43 -07003860 if (DISPLAY_VER(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003861 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003862 return;
3863 }
3864
3865 old_bw_state = intel_atomic_get_old_bw_state(state);
3866 /*
3867 * Nothing to unmask
3868 */
3869 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3870 return;
3871
3872 new_mask = new_bw_state->qgv_points_mask;
3873
3874 /*
3875 * Allow required qgv points after updating the configuration.
3876 * According to BSpec we can't mask and unmask qgv points at the same
3877 * time. Also masking should be done before updating the configuration
3878 * and unmasking afterwards.
3879 */
3880 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003881}
3882
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003883static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003884{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003885 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003886 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä9c312122020-11-06 19:30:40 +02003887 enum plane_id plane_id;
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003888 int max_level = INT_MAX;
Lyude656d1b82016-08-17 15:55:54 -04003889
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003890 if (!intel_has_sagv(dev_priv))
3891 return false;
3892
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003893 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003894 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003895
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02003896 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003897 return false;
3898
Ville Syrjälä9c312122020-11-06 19:30:40 +02003899 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003900 const struct skl_plane_wm *wm =
Ville Syrjälä9c312122020-11-06 19:30:40 +02003901 &crtc_state->wm.skl.optimal.planes[plane_id];
3902 int level;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003903
Lyude656d1b82016-08-17 15:55:54 -04003904 /* Skip this plane if it's not enabled */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003905 if (!wm->wm[0].enable)
Lyude656d1b82016-08-17 15:55:54 -04003906 continue;
3907
3908 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003909 for (level = ilk_wm_max_level(dev_priv);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003910 !wm->wm[level].enable; --level)
Lyude656d1b82016-08-17 15:55:54 -04003911 { }
3912
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003913 /* Highest common enabled wm level for all planes */
3914 max_level = min(level, max_level);
3915 }
3916
3917 /* No enabled planes? */
3918 if (max_level == INT_MAX)
3919 return true;
3920
3921 for_each_plane_id_on_crtc(crtc, plane_id) {
3922 const struct skl_plane_wm *wm =
3923 &crtc_state->wm.skl.optimal.planes[plane_id];
3924
Lyude656d1b82016-08-17 15:55:54 -04003925 /*
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003926 * All enabled planes must have enabled a common wm level that
3927 * can tolerate memory latencies higher than sagv_block_time_us
Lyude656d1b82016-08-17 15:55:54 -04003928 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003929 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
Lyude656d1b82016-08-17 15:55:54 -04003930 return false;
3931 }
3932
3933 return true;
3934}
3935
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003936static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3937{
3938 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3939 enum plane_id plane_id;
3940
3941 if (!crtc_state->hw.active)
3942 return true;
3943
3944 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003945 const struct skl_plane_wm *wm =
3946 &crtc_state->wm.skl.optimal.planes[plane_id];
3947
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003948 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003949 return false;
3950 }
3951
3952 return true;
3953}
3954
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003955static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3956{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003957 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3958 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3959
Matt Roper7dadd282021-03-19 21:42:43 -07003960 if (DISPLAY_VER(dev_priv) >= 12)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003961 return tgl_crtc_can_enable_sagv(crtc_state);
3962 else
3963 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003964}
3965
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003966bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3967 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003968{
Matt Roper7dadd282021-03-19 21:42:43 -07003969 if (DISPLAY_VER(dev_priv) < 11 &&
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003970 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003971 return false;
3972
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003973 return bw_state->pipe_sagv_reject == 0;
3974}
3975
3976static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3977{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003978 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003979 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003980 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003981 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003982 struct intel_bw_state *new_bw_state = NULL;
3983 const struct intel_bw_state *old_bw_state = NULL;
3984 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003985
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003986 for_each_new_intel_crtc_in_state(state, crtc,
3987 new_crtc_state, i) {
3988 new_bw_state = intel_atomic_get_bw_state(state);
3989 if (IS_ERR(new_bw_state))
3990 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003991
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003992 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003993
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003994 if (intel_crtc_can_enable_sagv(new_crtc_state))
3995 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
3996 else
3997 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
3998 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003999
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004000 if (!new_bw_state)
4001 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004002
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004003 new_bw_state->active_pipes =
4004 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03004005
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004006 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4007 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4008 if (ret)
4009 return ret;
4010 }
4011
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004012 for_each_new_intel_crtc_in_state(state, crtc,
4013 new_crtc_state, i) {
4014 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4015
4016 /*
4017 * We store use_sagv_wm in the crtc state rather than relying on
4018 * that bw state since we have no convenient way to get at the
4019 * latter from the plane commit hooks (especially in the legacy
4020 * cursor case)
4021 */
Matt Roper7959ffe2021-05-18 17:06:11 -07004022 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4023 DISPLAY_VER(dev_priv) >= 12 &&
4024 intel_can_enable_sagv(dev_priv, new_bw_state);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004025 }
4026
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004027 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4028 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004029 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4030 if (ret)
4031 return ret;
4032 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4033 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4034 if (ret)
4035 return ret;
4036 }
4037
4038 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004039}
4040
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004041static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4042{
Ville Syrjäläb88da662021-04-16 20:10:09 +03004043 return INTEL_INFO(dev_priv)->dbuf.size /
4044 hweight8(INTEL_INFO(dev_priv)->dbuf.slice_mask);
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004045}
4046
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004047static void
4048skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4049 struct skl_ddb_entry *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304050{
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004051 int slice_size = intel_dbuf_slice_size(dev_priv);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004052
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004053 if (!slice_mask) {
4054 ddb->start = 0;
4055 ddb->end = 0;
4056 return;
4057 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004058
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004059 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4060 ddb->end = fls(slice_mask) * slice_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004061
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004062 WARN_ON(ddb->start >= ddb->end);
Ville Syrjäläb88da662021-04-16 20:10:09 +03004063 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->dbuf.size);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004064}
4065
Ville Syrjälä835c1762021-05-18 17:06:16 -07004066static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
4067{
4068 struct skl_ddb_entry ddb;
4069
4070 if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
4071 slice_mask = BIT(DBUF_S1);
4072 else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
4073 slice_mask = BIT(DBUF_S3);
4074
4075 skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
4076
4077 return ddb.start;
4078}
4079
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004080u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4081 const struct skl_ddb_entry *entry)
4082{
Ville Syrjälä6390e5a2021-04-16 20:10:07 +03004083 int slice_size = intel_dbuf_slice_size(dev_priv);
4084 enum dbuf_slice start_slice, end_slice;
4085 u8 slice_mask = 0;
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004086
4087 if (!skl_ddb_entry_size(entry))
4088 return 0;
4089
4090 start_slice = entry->start / slice_size;
4091 end_slice = (entry->end - 1) / slice_size;
4092
4093 /*
4094 * Per plane DDB entry can in a really worst case be on multiple slices
4095 * but single entry is anyway contigious.
4096 */
4097 while (start_slice <= end_slice) {
4098 slice_mask |= BIT(start_slice);
4099 start_slice++;
4100 }
4101
4102 return slice_mask;
4103}
4104
Ville Syrjälä2791a402021-01-22 22:56:26 +02004105static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4106{
4107 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4108 int hdisplay, vdisplay;
4109
4110 if (!crtc_state->hw.active)
4111 return 0;
4112
4113 /*
4114 * Watermark/ddb requirement highly depends upon width of the
4115 * framebuffer, So instead of allocating DDB equally among pipes
4116 * distribute DDB based on resolution/width of the display.
4117 */
4118 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4119
4120 return hdisplay;
4121}
4122
Ville Syrjäläef79d622021-01-22 22:56:32 +02004123static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4124 enum pipe for_pipe,
4125 unsigned int *weight_start,
4126 unsigned int *weight_end,
4127 unsigned int *weight_total)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004128{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004129 struct drm_i915_private *dev_priv =
4130 to_i915(dbuf_state->base.state->base.dev);
4131 enum pipe pipe;
Ville Syrjälä53630962021-01-22 22:56:31 +02004132
4133 *weight_start = 0;
4134 *weight_end = 0;
4135 *weight_total = 0;
4136
Ville Syrjäläef79d622021-01-22 22:56:32 +02004137 for_each_pipe(dev_priv, pipe) {
4138 int weight = dbuf_state->weight[pipe];
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004139
4140 /*
4141 * Do not account pipes using other slice sets
4142 * luckily as of current BSpec slice sets do not partially
4143 * intersect(pipes share either same one slice or same slice set
4144 * i.e no partial intersection), so it is enough to check for
4145 * equality for now.
4146 */
Ville Syrjäläef79d622021-01-22 22:56:32 +02004147 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304148 continue;
4149
Ville Syrjälä53630962021-01-22 22:56:31 +02004150 *weight_total += weight;
Ville Syrjälä53630962021-01-22 22:56:31 +02004151 if (pipe < for_pipe) {
4152 *weight_start += weight;
4153 *weight_end += weight;
4154 } else if (pipe == for_pipe) {
4155 *weight_end += weight;
4156 }
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304157 }
Ville Syrjälä53630962021-01-22 22:56:31 +02004158}
4159
4160static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02004161skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
Ville Syrjälä53630962021-01-22 22:56:31 +02004162{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004163 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4164 unsigned int weight_total, weight_start, weight_end;
Ville Syrjälä53630962021-01-22 22:56:31 +02004165 const struct intel_dbuf_state *old_dbuf_state =
4166 intel_atomic_get_old_dbuf_state(state);
4167 struct intel_dbuf_state *new_dbuf_state =
4168 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004169 struct intel_crtc_state *crtc_state;
Ville Syrjälä53630962021-01-22 22:56:31 +02004170 struct skl_ddb_entry ddb_slices;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004171 enum pipe pipe = crtc->pipe;
Manasi Navaree2bebb92021-06-03 14:53:38 -07004172 unsigned int mbus_offset = 0;
Ville Syrjälä53630962021-01-22 22:56:31 +02004173 u32 ddb_range_size;
4174 u32 dbuf_slice_mask;
4175 u32 start, end;
4176 int ret;
4177
Ville Syrjäläef79d622021-01-22 22:56:32 +02004178 if (new_dbuf_state->weight[pipe] == 0) {
4179 new_dbuf_state->ddb[pipe].start = 0;
4180 new_dbuf_state->ddb[pipe].end = 0;
4181 goto out;
Ville Syrjälä53630962021-01-22 22:56:31 +02004182 }
4183
Ville Syrjäläef79d622021-01-22 22:56:32 +02004184 dbuf_slice_mask = new_dbuf_state->slices[pipe];
Ville Syrjälä53630962021-01-22 22:56:31 +02004185
4186 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
Ville Syrjälä835c1762021-05-18 17:06:16 -07004187 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_slice_mask);
Ville Syrjälä53630962021-01-22 22:56:31 +02004188 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4189
Ville Syrjäläef79d622021-01-22 22:56:32 +02004190 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4191 &weight_start, &weight_end, &weight_total);
Ville Syrjälä53630962021-01-22 22:56:31 +02004192
4193 start = ddb_range_size * weight_start / weight_total;
4194 end = ddb_range_size * weight_end / weight_total;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004195
Ville Syrjälä835c1762021-05-18 17:06:16 -07004196 new_dbuf_state->ddb[pipe].start = ddb_slices.start - mbus_offset + start;
4197 new_dbuf_state->ddb[pipe].end = ddb_slices.start - mbus_offset + end;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004198out:
Ville Syrjälä835c1762021-05-18 17:06:16 -07004199 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
4200 skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
Ville Syrjäläef79d622021-01-22 22:56:32 +02004201 &new_dbuf_state->ddb[pipe]))
4202 return 0;
4203
4204 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4205 if (ret)
4206 return ret;
4207
4208 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4209 if (IS_ERR(crtc_state))
4210 return PTR_ERR(crtc_state);
4211
Ville Syrjälä835c1762021-05-18 17:06:16 -07004212 /*
4213 * Used for checking overlaps, so we need absolute
4214 * offsets instead of MBUS relative offsets.
4215 */
4216 crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
4217 crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004218
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004219 drm_dbg_kms(&dev_priv->drm,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004220 "[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 +02004221 crtc->base.base.id, crtc->base.name,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004222 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4223 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4224 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4225 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004226
4227 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004228}
4229
Ville Syrjälädf331de2019-03-19 18:03:11 +02004230static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4231 int width, const struct drm_format_info *format,
4232 u64 modifier, unsigned int rotation,
4233 u32 plane_pixel_rate, struct skl_wm_params *wp,
4234 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004235static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004236 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004237 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004238 const struct skl_wm_params *wp,
4239 const struct skl_wm_level *result_prev,
4240 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004241
Ville Syrjälädf331de2019-03-19 18:03:11 +02004242static unsigned int
4243skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4244 int num_active)
4245{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004246 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004247 int level, max_level = ilk_wm_max_level(dev_priv);
4248 struct skl_wm_level wm = {};
4249 int ret, min_ddb_alloc = 0;
4250 struct skl_wm_params wp;
4251
4252 ret = skl_compute_wm_params(crtc_state, 256,
4253 drm_format_info(DRM_FORMAT_ARGB8888),
4254 DRM_FORMAT_MOD_LINEAR,
4255 DRM_MODE_ROTATE_0,
4256 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304257 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004258
4259 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004260 unsigned int latency = dev_priv->wm.skl_latency[level];
4261
4262 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004263 if (wm.min_ddb_alloc == U16_MAX)
4264 break;
4265
4266 min_ddb_alloc = wm.min_ddb_alloc;
4267 }
4268
4269 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004270}
4271
Mahesh Kumar37cde112018-04-26 19:55:17 +05304272static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4273 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004274{
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004275 entry->start = reg & DDB_ENTRY_MASK;
4276 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304277
Damien Lespiau16160e32014-11-04 17:06:53 +00004278 if (entry->end)
4279 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004280}
4281
Mahesh Kumarddf34312018-04-09 09:11:03 +05304282static void
4283skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4284 const enum pipe pipe,
4285 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004286 struct skl_ddb_entry *ddb_y,
4287 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304288{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004289 u32 val, val2;
4290 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304291
4292 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4293 if (plane_id == PLANE_CURSOR) {
Jani Nikula5f461662020-11-30 13:15:58 +02004294 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004295 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304296 return;
4297 }
4298
Jani Nikula5f461662020-11-30 13:15:58 +02004299 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304300
4301 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004302 if (val & PLANE_CTL_ENABLE)
4303 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4304 val & PLANE_CTL_ORDER_RGBX,
4305 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304306
Matt Roper7dadd282021-03-19 21:42:43 -07004307 if (DISPLAY_VER(dev_priv) >= 11) {
Jani Nikula5f461662020-11-30 13:15:58 +02004308 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004309 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4310 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02004311 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4312 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304313
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004314 if (fourcc &&
4315 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004316 swap(val, val2);
4317
4318 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4319 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304320 }
4321}
4322
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004323void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4324 struct skl_ddb_entry *ddb_y,
4325 struct skl_ddb_entry *ddb_uv)
4326{
4327 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4328 enum intel_display_power_domain power_domain;
4329 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004330 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004331 enum plane_id plane_id;
4332
4333 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004334 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4335 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004336 return;
4337
4338 for_each_plane_id_on_crtc(crtc, plane_id)
4339 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4340 plane_id,
4341 &ddb_y[plane_id],
4342 &ddb_uv[plane_id]);
4343
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004344 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004345}
4346
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004347/*
4348 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4349 * The bspec defines downscale amount as:
4350 *
4351 * """
4352 * Horizontal down scale amount = maximum[1, Horizontal source size /
4353 * Horizontal destination size]
4354 * Vertical down scale amount = maximum[1, Vertical source size /
4355 * Vertical destination size]
4356 * Total down scale amount = Horizontal down scale amount *
4357 * Vertical down scale amount
4358 * """
4359 *
4360 * Return value is provided in 16.16 fixed point form to retain fractional part.
4361 * Caller should take care of dividing & rounding off the value.
4362 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304363static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004364skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4365 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004366{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304367 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004368 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304369 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4370 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004371
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304372 if (drm_WARN_ON(&dev_priv->drm,
4373 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304374 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004375
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004376 /*
4377 * Src coordinates are already rotated by 270 degrees for
4378 * the 90/270 degree plane rotation cases (to match the
4379 * GTT mapping), hence no need to account for rotation here.
4380 *
4381 * n.b., src is 16.16 fixed point, dst is whole integer.
4382 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004383 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4384 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4385 dst_w = drm_rect_width(&plane_state->uapi.dst);
4386 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004387
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304388 fp_w_ratio = div_fixed16(src_w, dst_w);
4389 fp_h_ratio = div_fixed16(src_h, dst_h);
4390 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4391 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004392
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304393 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004394}
4395
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004396struct dbuf_slice_conf_entry {
4397 u8 active_pipes;
4398 u8 dbuf_mask[I915_MAX_PIPES];
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004399 bool join_mbus;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004400};
4401
4402/*
4403 * Table taken from Bspec 12716
4404 * Pipes do have some preferred DBuf slice affinity,
4405 * plus there are some hardcoded requirements on how
4406 * those should be distributed for multipipe scenarios.
4407 * For more DBuf slices algorithm can get even more messy
4408 * and less readable, so decided to use a table almost
4409 * as is from BSpec itself - that way it is at least easier
4410 * to compare, change and check.
4411 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004412static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004413/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4414{
4415 {
4416 .active_pipes = BIT(PIPE_A),
4417 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004418 [PIPE_A] = BIT(DBUF_S1),
4419 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004420 },
4421 {
4422 .active_pipes = BIT(PIPE_B),
4423 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004424 [PIPE_B] = BIT(DBUF_S1),
4425 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004426 },
4427 {
4428 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4429 .dbuf_mask = {
4430 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004431 [PIPE_B] = BIT(DBUF_S2),
4432 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004433 },
4434 {
4435 .active_pipes = BIT(PIPE_C),
4436 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004437 [PIPE_C] = BIT(DBUF_S2),
4438 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004439 },
4440 {
4441 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4442 .dbuf_mask = {
4443 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004444 [PIPE_C] = BIT(DBUF_S2),
4445 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004446 },
4447 {
4448 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4449 .dbuf_mask = {
4450 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004451 [PIPE_C] = BIT(DBUF_S2),
4452 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004453 },
4454 {
4455 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4456 .dbuf_mask = {
4457 [PIPE_A] = BIT(DBUF_S1),
4458 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004459 [PIPE_C] = BIT(DBUF_S2),
4460 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004461 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004462 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004463};
4464
4465/*
4466 * Table taken from Bspec 49255
4467 * Pipes do have some preferred DBuf slice affinity,
4468 * plus there are some hardcoded requirements on how
4469 * those should be distributed for multipipe scenarios.
4470 * For more DBuf slices algorithm can get even more messy
4471 * and less readable, so decided to use a table almost
4472 * as is from BSpec itself - that way it is at least easier
4473 * to compare, change and check.
4474 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004475static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004476/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4477{
4478 {
4479 .active_pipes = BIT(PIPE_A),
4480 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004481 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4482 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004483 },
4484 {
4485 .active_pipes = BIT(PIPE_B),
4486 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004487 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4488 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004489 },
4490 {
4491 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4492 .dbuf_mask = {
4493 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004494 [PIPE_B] = BIT(DBUF_S1),
4495 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004496 },
4497 {
4498 .active_pipes = BIT(PIPE_C),
4499 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004500 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4501 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004502 },
4503 {
4504 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4505 .dbuf_mask = {
4506 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004507 [PIPE_C] = BIT(DBUF_S2),
4508 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004509 },
4510 {
4511 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4512 .dbuf_mask = {
4513 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004514 [PIPE_C] = BIT(DBUF_S2),
4515 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004516 },
4517 {
4518 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4519 .dbuf_mask = {
4520 [PIPE_A] = BIT(DBUF_S1),
4521 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004522 [PIPE_C] = BIT(DBUF_S2),
4523 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004524 },
4525 {
4526 .active_pipes = BIT(PIPE_D),
4527 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004528 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4529 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004530 },
4531 {
4532 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4533 .dbuf_mask = {
4534 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004535 [PIPE_D] = BIT(DBUF_S2),
4536 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004537 },
4538 {
4539 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4540 .dbuf_mask = {
4541 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004542 [PIPE_D] = BIT(DBUF_S2),
4543 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004544 },
4545 {
4546 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4547 .dbuf_mask = {
4548 [PIPE_A] = BIT(DBUF_S1),
4549 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004550 [PIPE_D] = BIT(DBUF_S2),
4551 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004552 },
4553 {
4554 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4555 .dbuf_mask = {
4556 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004557 [PIPE_D] = BIT(DBUF_S2),
4558 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004559 },
4560 {
4561 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4562 .dbuf_mask = {
4563 [PIPE_A] = BIT(DBUF_S1),
4564 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004565 [PIPE_D] = BIT(DBUF_S2),
4566 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004567 },
4568 {
4569 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4570 .dbuf_mask = {
4571 [PIPE_B] = BIT(DBUF_S1),
4572 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004573 [PIPE_D] = BIT(DBUF_S2),
4574 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004575 },
4576 {
4577 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4578 .dbuf_mask = {
4579 [PIPE_A] = BIT(DBUF_S1),
4580 [PIPE_B] = BIT(DBUF_S1),
4581 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004582 [PIPE_D] = BIT(DBUF_S2),
4583 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004584 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004585 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004586};
4587
Matt Roper49f75632021-07-21 15:30:40 -07004588static const struct dbuf_slice_conf_entry dg2_allowed_dbufs[] = {
4589 {
4590 .active_pipes = BIT(PIPE_A),
4591 .dbuf_mask = {
4592 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4593 },
4594 },
4595 {
4596 .active_pipes = BIT(PIPE_B),
4597 .dbuf_mask = {
4598 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4599 },
4600 },
4601 {
4602 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4603 .dbuf_mask = {
4604 [PIPE_A] = BIT(DBUF_S1),
4605 [PIPE_B] = BIT(DBUF_S2),
4606 },
4607 },
4608 {
4609 .active_pipes = BIT(PIPE_C),
4610 .dbuf_mask = {
4611 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4612 },
4613 },
4614 {
4615 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4616 .dbuf_mask = {
4617 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4618 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4619 },
4620 },
4621 {
4622 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4623 .dbuf_mask = {
4624 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4625 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4626 },
4627 },
4628 {
4629 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4630 .dbuf_mask = {
4631 [PIPE_A] = BIT(DBUF_S1),
4632 [PIPE_B] = BIT(DBUF_S2),
4633 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4634 },
4635 },
4636 {
4637 .active_pipes = BIT(PIPE_D),
4638 .dbuf_mask = {
4639 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4640 },
4641 },
4642 {
4643 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4644 .dbuf_mask = {
4645 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4646 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4647 },
4648 },
4649 {
4650 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4651 .dbuf_mask = {
4652 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4653 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4654 },
4655 },
4656 {
4657 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4658 .dbuf_mask = {
4659 [PIPE_A] = BIT(DBUF_S1),
4660 [PIPE_B] = BIT(DBUF_S2),
4661 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4662 },
4663 },
4664 {
4665 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4666 .dbuf_mask = {
4667 [PIPE_C] = BIT(DBUF_S3),
4668 [PIPE_D] = BIT(DBUF_S4),
4669 },
4670 },
4671 {
4672 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4673 .dbuf_mask = {
4674 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4675 [PIPE_C] = BIT(DBUF_S3),
4676 [PIPE_D] = BIT(DBUF_S4),
4677 },
4678 },
4679 {
4680 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4681 .dbuf_mask = {
4682 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4683 [PIPE_C] = BIT(DBUF_S3),
4684 [PIPE_D] = BIT(DBUF_S4),
4685 },
4686 },
4687 {
4688 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4689 .dbuf_mask = {
4690 [PIPE_A] = BIT(DBUF_S1),
4691 [PIPE_B] = BIT(DBUF_S2),
4692 [PIPE_C] = BIT(DBUF_S3),
4693 [PIPE_D] = BIT(DBUF_S4),
4694 },
4695 },
4696 {}
4697};
4698
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004699static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
4700 {
4701 .active_pipes = BIT(PIPE_A),
4702 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004703 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004704 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004705 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004706 },
4707 {
4708 .active_pipes = BIT(PIPE_B),
4709 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004710 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004711 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004712 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004713 },
4714 {
4715 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4716 .dbuf_mask = {
4717 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4718 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4719 },
4720 },
4721 {
4722 .active_pipes = BIT(PIPE_C),
4723 .dbuf_mask = {
4724 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4725 },
4726 },
4727 {
4728 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4729 .dbuf_mask = {
4730 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4731 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4732 },
4733 },
4734 {
4735 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4736 .dbuf_mask = {
4737 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4738 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4739 },
4740 },
4741 {
4742 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4743 .dbuf_mask = {
4744 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4745 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4746 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4747 },
4748 },
4749 {
4750 .active_pipes = BIT(PIPE_D),
4751 .dbuf_mask = {
4752 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4753 },
4754 },
4755 {
4756 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4757 .dbuf_mask = {
4758 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4759 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4760 },
4761 },
4762 {
4763 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4764 .dbuf_mask = {
4765 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4766 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4767 },
4768 },
4769 {
4770 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4771 .dbuf_mask = {
4772 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4773 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4774 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4775 },
4776 },
4777 {
4778 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4779 .dbuf_mask = {
4780 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4781 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4782 },
4783 },
4784 {
4785 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4786 .dbuf_mask = {
4787 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4788 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4789 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4790 },
4791 },
4792 {
4793 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4794 .dbuf_mask = {
4795 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4796 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4797 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4798 },
4799 },
4800 {
4801 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4802 .dbuf_mask = {
4803 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4804 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4805 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4806 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4807 },
4808 },
4809 {}
4810
4811};
4812
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004813static bool check_mbus_joined(u8 active_pipes,
4814 const struct dbuf_slice_conf_entry *dbuf_slices)
4815{
4816 int i;
4817
4818 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
4819 if (dbuf_slices[i].active_pipes == active_pipes)
4820 return dbuf_slices[i].join_mbus;
4821 }
4822 return false;
4823}
4824
4825static bool adlp_check_mbus_joined(u8 active_pipes)
4826{
4827 return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
4828}
4829
Ville Syrjälä05e81552020-02-25 19:11:09 +02004830static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4831 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004832{
4833 int i;
4834
Ville Syrjälä05e81552020-02-25 19:11:09 +02004835 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004836 if (dbuf_slices[i].active_pipes == active_pipes)
4837 return dbuf_slices[i].dbuf_mask[pipe];
4838 }
4839 return 0;
4840}
4841
4842/*
4843 * This function finds an entry with same enabled pipe configuration and
4844 * returns correspondent DBuf slice mask as stated in BSpec for particular
4845 * platform.
4846 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004847static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004848{
4849 /*
4850 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4851 * required calculating "pipe ratio" in order to determine
4852 * if one or two slices can be used for single pipe configurations
4853 * as additional constraint to the existing table.
4854 * However based on recent info, it should be not "pipe ratio"
4855 * but rather ratio between pixel_rate and cdclk with additional
4856 * constants, so for now we are using only table until this is
4857 * clarified. Also this is the reason why crtc_state param is
4858 * still here - we will need it once those additional constraints
4859 * pop up.
4860 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004861 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004862}
4863
Ville Syrjälä05e81552020-02-25 19:11:09 +02004864static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004865{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004866 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004867}
4868
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004869static u32 adlp_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4870{
4871 return compute_dbuf_slices(pipe, active_pipes, adlp_allowed_dbufs);
4872}
4873
Matt Roper49f75632021-07-21 15:30:40 -07004874static u32 dg2_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4875{
4876 return compute_dbuf_slices(pipe, active_pipes, dg2_allowed_dbufs);
4877}
4878
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004879static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004880{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004881 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4882 enum pipe pipe = crtc->pipe;
4883
Matt Roper49f75632021-07-21 15:30:40 -07004884 if (IS_DG2(dev_priv))
4885 return dg2_compute_dbuf_slices(pipe, active_pipes);
4886 else if (IS_ALDERLAKE_P(dev_priv))
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004887 return adlp_compute_dbuf_slices(pipe, active_pipes);
4888 else if (DISPLAY_VER(dev_priv) == 12)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004889 return tgl_compute_dbuf_slices(pipe, active_pipes);
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004890 else if (DISPLAY_VER(dev_priv) == 11)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004891 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004892 /*
4893 * For anything else just return one slice yet.
4894 * Should be extended for other platforms.
4895 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004896 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004897}
4898
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004899static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004900skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4901 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004902 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004903{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004904 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004905 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004906 u32 data_rate;
4907 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304908 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004909 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004910
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004911 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004912 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004913
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004914 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004915 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004916
4917 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004918 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004919 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004920
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004921 /*
4922 * Src coordinates are already rotated by 270 degrees for
4923 * the 90/270 degree plane rotation cases (to match the
4924 * GTT mapping), hence no need to account for rotation here.
4925 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004926 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4927 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004928
Mahesh Kumarb879d582018-04-09 09:11:01 +05304929 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004930 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304931 width /= 2;
4932 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004933 }
4934
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004935 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304936
Maarten Lankhorstec193642019-06-28 10:55:17 +02004937 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004938
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004939 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4940
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004941 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004942 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004943}
4944
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004945static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004946skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4947 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004948{
Ville Syrjäläab016302020-11-06 19:30:41 +02004949 struct intel_crtc_state *crtc_state =
4950 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004951 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004952 struct intel_plane *plane;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004953 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004954 enum plane_id plane_id;
4955 int i;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004956
Matt Ropera1de91e2016-05-12 07:05:57 -07004957 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004958 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4959 if (plane->pipe != crtc->pipe)
4960 continue;
4961
4962 plane_id = plane->id;
Matt Roper024c9042015-09-24 15:53:11 -07004963
Mahesh Kumarb879d582018-04-09 09:11:01 +05304964 /* packed/y */
Ville Syrjäläab016302020-11-06 19:30:41 +02004965 crtc_state->plane_data_rate[plane_id] =
4966 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Matt Roper9c74d822016-05-12 07:05:58 -07004967
Mahesh Kumarb879d582018-04-09 09:11:01 +05304968 /* uv-plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004969 crtc_state->uv_plane_data_rate[plane_id] =
4970 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4971 }
4972
4973 for_each_plane_id_on_crtc(crtc, plane_id) {
4974 total_data_rate += crtc_state->plane_data_rate[plane_id];
4975 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004976 }
4977
4978 return total_data_rate;
4979}
4980
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004981static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004982icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4983 struct intel_crtc *crtc)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004984{
Ville Syrjäläab016302020-11-06 19:30:41 +02004985 struct intel_crtc_state *crtc_state =
4986 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004987 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004988 struct intel_plane *plane;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004989 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004990 enum plane_id plane_id;
4991 int i;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004992
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004993 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004994 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4995 if (plane->pipe != crtc->pipe)
4996 continue;
4997
4998 plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004999
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005000 if (!plane_state->planar_linked_plane) {
Ville Syrjäläab016302020-11-06 19:30:41 +02005001 crtc_state->plane_data_rate[plane_id] =
5002 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005003 } else {
5004 enum plane_id y_plane_id;
5005
5006 /*
5007 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005008 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005009 * and needs the master plane state which may be
5010 * NULL if we try get_new_plane_state(), so we
5011 * always calculate from the master.
5012 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005013 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005014 continue;
5015
5016 /* Y plane rate is calculated on the slave */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005017 y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjäläab016302020-11-06 19:30:41 +02005018 crtc_state->plane_data_rate[y_plane_id] =
5019 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005020
Ville Syrjäläab016302020-11-06 19:30:41 +02005021 crtc_state->plane_data_rate[plane_id] =
5022 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005023 }
5024 }
5025
Ville Syrjäläab016302020-11-06 19:30:41 +02005026 for_each_plane_id_on_crtc(crtc, plane_id)
5027 total_data_rate += crtc_state->plane_data_rate[plane_id];
5028
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005029 return total_data_rate;
5030}
5031
Ville Syrjälä5516e892021-02-26 17:32:03 +02005032const struct skl_wm_level *
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005033skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005034 enum plane_id plane_id,
5035 int level)
5036{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005037 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5038
5039 if (level == 0 && pipe_wm->use_sagv_wm)
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005040 return &wm->sagv.wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005041
5042 return &wm->wm[level];
5043}
5044
Ville Syrjälä5516e892021-02-26 17:32:03 +02005045const struct skl_wm_level *
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005046skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
5047 enum plane_id plane_id)
5048{
5049 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5050
5051 if (pipe_wm->use_sagv_wm)
5052 return &wm->sagv.trans_wm;
5053
5054 return &wm->trans_wm;
5055}
5056
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005057/*
5058 * We only disable the watermarks for each plane if
5059 * they exceed the ddb allocation of said plane. This
5060 * is done so that we don't end up touching cursor
5061 * watermarks needlessly when some other plane reduces
5062 * our max possible watermark level.
5063 *
5064 * Bspec has this to say about the PLANE_WM enable bit:
5065 * "All the watermarks at this level for all enabled
5066 * planes must be enabled before the level will be used."
5067 * So this is actually safe to do.
5068 */
5069static void
5070skl_check_wm_level(struct skl_wm_level *wm, u64 total)
5071{
5072 if (wm->min_ddb_alloc > total)
5073 memset(wm, 0, sizeof(*wm));
5074}
5075
5076static void
5077skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
5078 u64 total, u64 uv_total)
5079{
5080 if (wm->min_ddb_alloc > total ||
5081 uv_wm->min_ddb_alloc > uv_total) {
5082 memset(wm, 0, sizeof(*wm));
5083 memset(uv_wm, 0, sizeof(*uv_wm));
5084 }
5085}
5086
Matt Roperc107acf2016-05-12 07:06:01 -07005087static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02005088skl_allocate_plane_ddb(struct intel_atomic_state *state,
5089 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00005090{
Ville Syrjäläef79d622021-01-22 22:56:32 +02005091 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläffc90032020-11-06 19:30:37 +02005092 struct intel_crtc_state *crtc_state =
5093 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005094 const struct intel_dbuf_state *dbuf_state =
Ville Syrjälä47a14952021-01-22 22:56:30 +02005095 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005096 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
5097 int num_active = hweight8(dbuf_state->active_pipes);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005098 u16 alloc_size, start = 0;
5099 u16 total[I915_MAX_PLANES] = {};
5100 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02005101 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005102 enum plane_id plane_id;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005103 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08005104 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005105
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005106 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005107 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
5108 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005109
Ville Syrjäläef79d622021-01-22 22:56:32 +02005110 if (!crtc_state->hw.active)
Matt Roperc107acf2016-05-12 07:06:01 -07005111 return 0;
Matt Roperc107acf2016-05-12 07:06:01 -07005112
Matt Roper7dadd282021-03-19 21:42:43 -07005113 if (DISPLAY_VER(dev_priv) >= 11)
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005114 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005115 icl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005116 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005117 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005118 skl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005119
Damien Lespiau34bb56a2014-11-04 17:07:01 +00005120 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05305121 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005122 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005123
Matt Roperd8e87492018-12-11 09:31:07 -08005124 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005125 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08005126 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005127 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08005128 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005129 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005130
Matt Ropera1de91e2016-05-12 07:05:57 -07005131 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005132 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005133
Matt Roperd8e87492018-12-11 09:31:07 -08005134 /*
5135 * Find the highest watermark level for which we can satisfy the block
5136 * requirement of active planes.
5137 */
5138 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08005139 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005140 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005141 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005142 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005143
5144 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05305145 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305146 drm_WARN_ON(&dev_priv->drm,
5147 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005148 blocks = U32_MAX;
5149 break;
5150 }
5151 continue;
5152 }
5153
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005154 blocks += wm->wm[level].min_ddb_alloc;
5155 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08005156 }
5157
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02005158 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08005159 alloc_size -= blocks;
5160 break;
5161 }
5162 }
5163
5164 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005165 drm_dbg_kms(&dev_priv->drm,
5166 "Requested display configuration exceeds system DDB limitations");
5167 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
5168 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08005169 return -EINVAL;
5170 }
5171
5172 /*
5173 * Grant each plane the blocks it requires at the highest achievable
5174 * watermark level, plus an extra share of the leftover blocks
5175 * proportional to its relative data rate.
5176 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005177 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005178 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005179 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005180 u64 rate;
5181 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005182
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005183 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005184 continue;
5185
Damien Lespiaub9cec072014-11-04 17:06:43 +00005186 /*
Matt Roperd8e87492018-12-11 09:31:07 -08005187 * We've accounted for all active planes; remaining planes are
5188 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00005189 */
Matt Roperd8e87492018-12-11 09:31:07 -08005190 if (total_data_rate == 0)
5191 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005192
Ville Syrjäläab016302020-11-06 19:30:41 +02005193 rate = crtc_state->plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005194 extra = min_t(u16, alloc_size,
5195 DIV64_U64_ROUND_UP(alloc_size * rate,
5196 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005197 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005198 alloc_size -= extra;
5199 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005200
Matt Roperd8e87492018-12-11 09:31:07 -08005201 if (total_data_rate == 0)
5202 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07005203
Ville Syrjäläab016302020-11-06 19:30:41 +02005204 rate = crtc_state->uv_plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005205 extra = min_t(u16, alloc_size,
5206 DIV64_U64_ROUND_UP(alloc_size * rate,
5207 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005208 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005209 alloc_size -= extra;
5210 total_data_rate -= rate;
5211 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305212 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08005213
5214 /* Set the actual DDB start/end points for each plane */
5215 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005216 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005217 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005218 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005219 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005220 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005221
5222 if (plane_id == PLANE_CURSOR)
5223 continue;
5224
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005225 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305226 drm_WARN_ON(&dev_priv->drm,
Matt Roper7dadd282021-03-19 21:42:43 -07005227 DISPLAY_VER(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005228
Matt Roperd8e87492018-12-11 09:31:07 -08005229 /* Leave disabled planes at (0,0) */
5230 if (total[plane_id]) {
5231 plane_alloc->start = start;
5232 start += total[plane_id];
5233 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07005234 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005235
Matt Roperd8e87492018-12-11 09:31:07 -08005236 if (uv_total[plane_id]) {
5237 uv_plane_alloc->start = start;
5238 start += uv_total[plane_id];
5239 uv_plane_alloc->end = start;
5240 }
5241 }
5242
5243 /*
5244 * When we calculated watermark values we didn't know how high
5245 * of a level we'd actually be able to hit, so we just marked
5246 * all levels as "enabled." Go back now and disable the ones
5247 * that aren't actually possible.
5248 */
5249 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005250 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005251 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005252 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02005253
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005254 skl_check_nv12_wm_level(&wm->wm[level], &wm->uv_wm[level],
5255 total[plane_id], uv_total[plane_id]);
Ville Syrjälä290248c2019-02-13 18:54:24 +02005256
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005257 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005258 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005259 * Underruns with WM1+ disabled
5260 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005261 if (DISPLAY_VER(dev_priv) == 11 &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005262 level == 1 && wm->wm[0].enable) {
5263 wm->wm[level].blocks = wm->wm[0].blocks;
5264 wm->wm[level].lines = wm->wm[0].lines;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005265 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005266 }
Matt Roperd8e87492018-12-11 09:31:07 -08005267 }
5268 }
5269
5270 /*
Ville Syrjälädf4a50a2021-02-26 17:31:59 +02005271 * Go back and disable the transition and SAGV watermarks
5272 * if it turns out we don't have enough DDB blocks for them.
Matt Roperd8e87492018-12-11 09:31:07 -08005273 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005274 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005275 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005276 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005277
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005278 skl_check_wm_level(&wm->trans_wm, total[plane_id]);
5279 skl_check_wm_level(&wm->sagv.wm0, total[plane_id]);
5280 skl_check_wm_level(&wm->sagv.trans_wm, total[plane_id]);
Damien Lespiaub9cec072014-11-04 17:06:43 +00005281 }
5282
Matt Roperc107acf2016-05-12 07:06:01 -07005283 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005284}
5285
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005286/*
5287 * 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 +02005288 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005289 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5290 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5291*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005292static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005293skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5294 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005295{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005296 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305297 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005298
5299 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305300 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005301
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305302 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005303 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005304
Matt Roper2b5a4562021-03-22 16:38:40 -07005305 if (DISPLAY_VER(dev_priv) >= 10)
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005306 ret = add_fixed16_u32(ret, 1);
5307
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005308 return ret;
5309}
5310
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005311static uint_fixed_16_16_t
5312skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5313 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005314{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005315 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305316 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005317
5318 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305319 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005320
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005321 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305322 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5323 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305324 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005325 return ret;
5326}
5327
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305328static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005329intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305330{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305331 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005332 u32 pixel_rate;
5333 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305334 uint_fixed_16_16_t linetime_us;
5335
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005336 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305337 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305338
Maarten Lankhorstec193642019-06-28 10:55:17 +02005339 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305340
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305341 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305342 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305343
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005344 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305345 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305346
5347 return linetime_us;
5348}
5349
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305350static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005351skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5352 int width, const struct drm_format_info *format,
5353 u64 modifier, unsigned int rotation,
5354 u32 plane_pixel_rate, struct skl_wm_params *wp,
5355 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305356{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005357 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005358 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005359 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305360
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305361 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005362 if (color_plane == 1 &&
5363 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005364 drm_dbg_kms(&dev_priv->drm,
5365 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305366 return -EINVAL;
5367 }
5368
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005369 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5370 modifier == I915_FORMAT_MOD_Yf_TILED ||
5371 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5372 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5373 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5374 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5375 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005376 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305377
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005378 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005379 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305380 wp->width /= 2;
5381
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005382 wp->cpp = format->cpp[color_plane];
5383 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305384
Matt Roper7dadd282021-03-19 21:42:43 -07005385 if (DISPLAY_VER(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005386 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005387 wp->dbuf_block_size = 256;
5388 else
5389 wp->dbuf_block_size = 512;
5390
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005391 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305392 switch (wp->cpp) {
5393 case 1:
5394 wp->y_min_scanlines = 16;
5395 break;
5396 case 2:
5397 wp->y_min_scanlines = 8;
5398 break;
5399 case 4:
5400 wp->y_min_scanlines = 4;
5401 break;
5402 default:
5403 MISSING_CASE(wp->cpp);
5404 return -EINVAL;
5405 }
5406 } else {
5407 wp->y_min_scanlines = 4;
5408 }
5409
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005410 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305411 wp->y_min_scanlines *= 2;
5412
5413 wp->plane_bytes_per_line = wp->width * wp->cpp;
5414 if (wp->y_tiled) {
5415 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005416 wp->y_min_scanlines,
5417 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305418
Matt Roper2b5a4562021-03-22 16:38:40 -07005419 if (DISPLAY_VER(dev_priv) >= 10)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305420 interm_pbpl++;
5421
5422 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5423 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305424 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005425 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005426 wp->dbuf_block_size);
5427
Matt Roper2b5a4562021-03-22 16:38:40 -07005428 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005429 interm_pbpl++;
5430
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305431 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5432 }
5433
5434 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5435 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005436
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305437 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005438 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305439
5440 return 0;
5441}
5442
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005443static int
5444skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5445 const struct intel_plane_state *plane_state,
5446 struct skl_wm_params *wp, int color_plane)
5447{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005448 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005449 int width;
5450
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005451 /*
5452 * Src coordinates are already rotated by 270 degrees for
5453 * the 90/270 degree plane rotation cases (to match the
5454 * GTT mapping), hence no need to account for rotation here.
5455 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005456 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005457
5458 return skl_compute_wm_params(crtc_state, width,
5459 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005460 plane_state->hw.rotation,
Ville Syrjälä3df3fe22020-11-06 19:30:42 +02005461 intel_plane_pixel_rate(crtc_state, plane_state),
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005462 wp, color_plane);
5463}
5464
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005465static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5466{
Matt Roper2b5a4562021-03-22 16:38:40 -07005467 if (DISPLAY_VER(dev_priv) >= 10)
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005468 return true;
5469
5470 /* The number of lines are ignored for the level 0 watermark. */
5471 return level > 0;
5472}
5473
Matt Roper1003cee2021-05-14 08:36:54 -07005474static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5475{
5476 if (DISPLAY_VER(dev_priv) >= 13)
5477 return 255;
5478 else
5479 return 31;
5480}
5481
Maarten Lankhorstec193642019-06-28 10:55:17 +02005482static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005483 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005484 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005485 const struct skl_wm_params *wp,
5486 const struct skl_wm_level *result_prev,
5487 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005488{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005489 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305490 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305491 uint_fixed_16_16_t selected_result;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005492 u32 blocks, lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005493
Ville Syrjälä0aded172019-02-05 17:50:53 +02005494 if (latency == 0) {
5495 /* reject it */
5496 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005497 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005498 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005499
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005500 /*
5501 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5502 * Display WA #1141: kbl,cfl
5503 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005504 if ((IS_KABYLAKE(dev_priv) ||
5505 IS_COFFEELAKE(dev_priv) ||
5506 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005507 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305508 latency += 4;
5509
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005510 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005511 latency += 15;
5512
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305513 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005514 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305515 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005516 crtc_state->hw.pipe_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005517 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305518 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005519
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305520 if (wp->y_tiled) {
5521 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005522 } else {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005523 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005524 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005525 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005526 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005527 } else if (latency >= wp->linetime_us) {
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005528 if (DISPLAY_VER(dev_priv) == 9)
Paulo Zanoni077b5822018-10-04 16:15:57 -07005529 selected_result = min_fixed16(method1, method2);
5530 else
5531 selected_result = method2;
5532 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005533 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005534 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005535 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005536
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005537 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5538 lines = div_round_up_fixed16(selected_result,
5539 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005540
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005541 if (DISPLAY_VER(dev_priv) == 9) {
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005542 /* Display WA #1125: skl,bxt,kbl */
5543 if (level == 0 && wp->rc_surface)
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005544 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005545
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005546 /* Display WA #1126: skl,bxt,kbl */
5547 if (level >= 1 && level <= 7) {
5548 if (wp->y_tiled) {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005549 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5550 lines += wp->y_min_scanlines;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005551 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005552 blocks++;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005553 }
5554
5555 /*
5556 * Make sure result blocks for higher latency levels are
5557 * atleast as high as level below the current level.
5558 * Assumption in DDB algorithm optimization for special
5559 * cases. Also covers Display WA #1125 for RC.
5560 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005561 if (result_prev->blocks > blocks)
5562 blocks = result_prev->blocks;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005563 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005564 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005565
Matt Roper7dadd282021-03-19 21:42:43 -07005566 if (DISPLAY_VER(dev_priv) >= 11) {
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005567 if (wp->y_tiled) {
5568 int extra_lines;
5569
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005570 if (lines % wp->y_min_scanlines == 0)
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005571 extra_lines = wp->y_min_scanlines;
5572 else
5573 extra_lines = wp->y_min_scanlines * 2 -
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005574 lines % wp->y_min_scanlines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005575
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005576 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005577 wp->plane_blocks_per_line);
5578 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005579 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005580 }
5581 }
5582
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005583 if (!skl_wm_has_lines(dev_priv, level))
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005584 lines = 0;
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005585
Matt Roper1003cee2021-05-14 08:36:54 -07005586 if (lines > skl_wm_max_lines(dev_priv)) {
Ville Syrjälä0aded172019-02-05 17:50:53 +02005587 /* reject it */
5588 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005589 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005590 }
Matt Roperd8e87492018-12-11 09:31:07 -08005591
5592 /*
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005593 * If lines is valid, assume we can use this watermark level
Matt Roperd8e87492018-12-11 09:31:07 -08005594 * for now. We'll come back and disable it after we calculate the
5595 * DDB allocation if it turns out we don't actually have enough
5596 * blocks to satisfy it.
5597 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005598 result->blocks = blocks;
5599 result->lines = lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005600 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005601 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5602 result->enable = true;
Ville Syrjälä9c312122020-11-06 19:30:40 +02005603
Matt Roper7dadd282021-03-19 21:42:43 -07005604 if (DISPLAY_VER(dev_priv) < 12)
Ville Syrjälä9c312122020-11-06 19:30:40 +02005605 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005606}
5607
Matt Roperd8e87492018-12-11 09:31:07 -08005608static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005609skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305610 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005611 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005612{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005613 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305614 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005615 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005616
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305617 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005618 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005619 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305620
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005621 skl_compute_plane_wm(crtc_state, level, latency,
5622 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005623
5624 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305625 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005626}
5627
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005628static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5629 const struct skl_wm_params *wm_params,
5630 struct skl_plane_wm *plane_wm)
5631{
5632 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005633 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005634 struct skl_wm_level *levels = plane_wm->wm;
5635 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5636
5637 skl_compute_plane_wm(crtc_state, 0, latency,
5638 wm_params, &levels[0],
5639 sagv_wm);
5640}
5641
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005642static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5643 struct skl_wm_level *trans_wm,
5644 const struct skl_wm_level *wm0,
5645 const struct skl_wm_params *wp)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005646{
Ville Syrjäläc834d032020-02-28 22:35:52 +02005647 u16 trans_min, trans_amount, trans_y_tile_min;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005648 u16 wm0_blocks, trans_offset, blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005649
Kumar, Maheshca476672017-08-17 19:15:24 +05305650 /* Transition WM don't make any sense if ipc is disabled */
5651 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005652 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305653
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005654 /*
5655 * WaDisableTWM:skl,kbl,cfl,bxt
5656 * Transition WM are not recommended by HW team for GEN9
5657 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005658 if (DISPLAY_VER(dev_priv) == 9)
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005659 return;
5660
Matt Roper7dadd282021-03-19 21:42:43 -07005661 if (DISPLAY_VER(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305662 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005663 else
5664 trans_min = 14;
5665
5666 /* Display WA #1140: glk,cnl */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005667 if (DISPLAY_VER(dev_priv) == 10)
Ville Syrjäläc834d032020-02-28 22:35:52 +02005668 trans_amount = 0;
5669 else
5670 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305671
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005672 trans_offset = trans_min + trans_amount;
Kumar, Maheshca476672017-08-17 19:15:24 +05305673
Paulo Zanonicbacc792018-10-04 16:15:58 -07005674 /*
5675 * The spec asks for Selected Result Blocks for wm0 (the real value),
5676 * not Result Blocks (the integer value). Pay attention to the capital
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005677 * letters. The value wm_l0->blocks is actually Result Blocks, but
Paulo Zanonicbacc792018-10-04 16:15:58 -07005678 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5679 * and since we later will have to get the ceiling of the sum in the
5680 * transition watermarks calculation, we can just pretend Selected
5681 * Result Blocks is Result Blocks minus 1 and it should work for the
5682 * current platforms.
5683 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005684 wm0_blocks = wm0->blocks - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005685
Kumar, Maheshca476672017-08-17 19:15:24 +05305686 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005687 trans_y_tile_min =
5688 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005689 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305690 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005691 blocks = wm0_blocks + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305692 }
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005693 blocks++;
Kumar, Maheshca476672017-08-17 19:15:24 +05305694
Matt Roperd8e87492018-12-11 09:31:07 -08005695 /*
5696 * Just assume we can enable the transition watermark. After
5697 * computing the DDB we'll come back and disable it if that
5698 * assumption turns out to be false.
5699 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005700 trans_wm->blocks = blocks;
5701 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5702 trans_wm->enable = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005703}
5704
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005705static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005706 const struct intel_plane_state *plane_state,
5707 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005708{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005709 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5710 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälädbf71382020-11-06 19:30:38 +02005711 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005712 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005713 int ret;
5714
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005715 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005716 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005717 if (ret)
5718 return ret;
5719
Ville Syrjälä67155a62019-03-12 22:58:37 +02005720 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005721
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005722 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5723 &wm->wm[0], &wm_params);
5724
Matt Roper7dadd282021-03-19 21:42:43 -07005725 if (DISPLAY_VER(dev_priv) >= 12) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005726 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5727
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005728 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5729 &wm->sagv.wm0, &wm_params);
5730 }
Ville Syrjälä83158472018-11-27 18:57:26 +02005731
5732 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005733}
5734
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005735static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005736 const struct intel_plane_state *plane_state,
5737 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005738{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005739 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005740 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005741 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005742
Ville Syrjälä83158472018-11-27 18:57:26 +02005743 wm->is_planar = true;
5744
5745 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005746 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005747 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005748 if (ret)
5749 return ret;
5750
Ville Syrjälä67155a62019-03-12 22:58:37 +02005751 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005752
5753 return 0;
5754}
5755
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005756static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005757 const struct intel_plane_state *plane_state)
5758{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005759 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä83158472018-11-27 18:57:26 +02005760 enum plane_id plane_id = plane->id;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005761 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5762 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005763 int ret;
5764
Ville Syrjälädbf71382020-11-06 19:30:38 +02005765 memset(wm, 0, sizeof(*wm));
5766
Ville Syrjälä83158472018-11-27 18:57:26 +02005767 if (!intel_wm_plane_visible(crtc_state, plane_state))
5768 return 0;
5769
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005770 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005771 plane_id, 0);
5772 if (ret)
5773 return ret;
5774
5775 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005776 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005777 plane_id);
5778 if (ret)
5779 return ret;
5780 }
5781
5782 return 0;
5783}
5784
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005785static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005786 const struct intel_plane_state *plane_state)
5787{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005788 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5789 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5790 enum plane_id plane_id = plane->id;
5791 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005792 int ret;
5793
5794 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005795 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005796 return 0;
5797
Ville Syrjäläf99b8052021-03-27 02:59:45 +02005798 memset(wm, 0, sizeof(*wm));
5799
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005800 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005801 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005802 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005803
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305804 drm_WARN_ON(&dev_priv->drm,
5805 !intel_wm_plane_visible(crtc_state, plane_state));
5806 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5807 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005808
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005809 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005810 y_plane_id, 0);
5811 if (ret)
5812 return ret;
5813
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005814 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005815 plane_id, 1);
5816 if (ret)
5817 return ret;
5818 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005819 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005820 plane_id, 0);
5821 if (ret)
5822 return ret;
5823 }
5824
5825 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005826}
5827
Ville Syrjäläffc90032020-11-06 19:30:37 +02005828static int skl_build_pipe_wm(struct intel_atomic_state *state,
5829 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005830{
Ville Syrjäläffc90032020-11-06 19:30:37 +02005831 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5832 struct intel_crtc_state *crtc_state =
5833 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005834 const struct intel_plane_state *plane_state;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005835 struct intel_plane *plane;
5836 int ret, i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005837
Ville Syrjälädbf71382020-11-06 19:30:38 +02005838 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5839 /*
5840 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5841 * instead but we don't populate that correctly for NV12 Y
5842 * planes so for now hack this.
5843 */
5844 if (plane->pipe != crtc->pipe)
5845 continue;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305846
Matt Roper7dadd282021-03-19 21:42:43 -07005847 if (DISPLAY_VER(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005848 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005849 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005850 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305851 if (ret)
5852 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005853 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305854
Ville Syrjälädbf71382020-11-06 19:30:38 +02005855 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5856
Matt Roper55994c22016-05-12 07:06:08 -07005857 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005858}
5859
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005860static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5861 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005862 const struct skl_ddb_entry *entry)
5863{
5864 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005865 intel_de_write_fw(dev_priv, reg,
5866 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005867 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005868 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005869}
5870
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005871static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5872 i915_reg_t reg,
5873 const struct skl_wm_level *level)
5874{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005875 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005876
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005877 if (level->enable)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005878 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005879 if (level->ignore_lines)
5880 val |= PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005881 val |= level->blocks;
Matt Roper1003cee2021-05-14 08:36:54 -07005882 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005883
Jani Nikula9b6320a2020-01-23 16:00:04 +02005884 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005885}
5886
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005887void skl_write_plane_wm(struct intel_plane *plane,
5888 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005889{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005890 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005891 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005892 enum plane_id plane_id = plane->id;
5893 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005894 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5895 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005896 const struct skl_ddb_entry *ddb_y =
5897 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5898 const struct skl_ddb_entry *ddb_uv =
5899 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005900
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005901 for (level = 0; level <= max_level; level++)
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005902 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005903 skl_plane_wm_level(pipe_wm, plane_id, level));
5904
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005905 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005906 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005907
Matt Roper7959ffe2021-05-18 17:06:11 -07005908 if (HAS_HW_SAGV_WM(dev_priv)) {
5909 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5910 &wm->sagv.wm0);
5911 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5912 &wm->sagv.trans_wm);
5913 }
5914
Matt Roper7dadd282021-03-19 21:42:43 -07005915 if (DISPLAY_VER(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005916 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005917 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5918 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305919 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005920
5921 if (wm->is_planar)
5922 swap(ddb_y, ddb_uv);
5923
5924 skl_ddb_entry_write(dev_priv,
5925 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5926 skl_ddb_entry_write(dev_priv,
5927 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005928}
5929
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005930void skl_write_cursor_wm(struct intel_plane *plane,
5931 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005932{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005933 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005934 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005935 enum plane_id plane_id = plane->id;
5936 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005937 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005938 const struct skl_ddb_entry *ddb =
5939 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005940
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005941 for (level = 0; level <= max_level; level++)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005942 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005943 skl_plane_wm_level(pipe_wm, plane_id, level));
5944
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005945 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5946 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005947
Matt Roper7959ffe2021-05-18 17:06:11 -07005948 if (HAS_HW_SAGV_WM(dev_priv)) {
5949 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5950
5951 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5952 &wm->sagv.wm0);
5953 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5954 &wm->sagv.trans_wm);
5955 }
5956
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005957 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005958}
5959
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005960bool skl_wm_level_equals(const struct skl_wm_level *l1,
5961 const struct skl_wm_level *l2)
5962{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005963 return l1->enable == l2->enable &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005964 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005965 l1->lines == l2->lines &&
5966 l1->blocks == l2->blocks;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005967}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005968
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005969static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5970 const struct skl_plane_wm *wm1,
5971 const struct skl_plane_wm *wm2)
5972{
5973 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005974
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005975 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005976 /*
5977 * We don't check uv_wm as the hardware doesn't actually
5978 * use it. It only gets used for calculating the required
5979 * ddb allocation.
5980 */
5981 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005982 return false;
5983 }
5984
Ville Syrjäläf11449d2021-02-26 17:32:00 +02005985 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005986 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5987 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005988}
5989
Jani Nikula81b55ef2020-04-20 17:04:38 +03005990static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5991 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005992{
Lyude27082492016-08-24 07:48:10 +02005993 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005994}
5995
Ville Syrjälä33c9c502021-01-22 22:56:33 +02005996static void skl_ddb_entry_union(struct skl_ddb_entry *a,
5997 const struct skl_ddb_entry *b)
5998{
5999 if (a->end && b->end) {
6000 a->start = min(a->start, b->start);
6001 a->end = max(a->end, b->end);
6002 } else if (b->end) {
6003 a->start = b->start;
6004 a->end = b->end;
6005 }
6006}
6007
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006008bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03006009 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006010 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006011{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006012 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006013
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006014 for (i = 0; i < num_entries; i++) {
6015 if (i != ignore_idx &&
6016 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02006017 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03006018 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006019
Lyude27082492016-08-24 07:48:10 +02006020 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006021}
6022
Jani Nikulabb7791b2016-10-04 12:29:17 +03006023static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006024skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
6025 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006026{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006027 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
6028 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006029 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6030 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006031
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006032 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6033 struct intel_plane_state *plane_state;
6034 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006035
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006036 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
6037 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
6038 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
6039 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006040 continue;
6041
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006042 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006043 if (IS_ERR(plane_state))
6044 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02006045
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006046 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006047 }
6048
6049 return 0;
6050}
6051
Ville Syrjäläef79d622021-01-22 22:56:32 +02006052static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
6053{
6054 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
6055 u8 enabled_slices;
6056 enum pipe pipe;
6057
6058 /*
6059 * FIXME: For now we always enable slice S1 as per
6060 * the Bspec display initialization sequence.
6061 */
6062 enabled_slices = BIT(DBUF_S1);
6063
6064 for_each_pipe(dev_priv, pipe)
6065 enabled_slices |= dbuf_state->slices[pipe];
6066
6067 return enabled_slices;
6068}
6069
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006070static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006071skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07006072{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006073 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6074 const struct intel_dbuf_state *old_dbuf_state;
Ville Syrjäläef79d622021-01-22 22:56:32 +02006075 struct intel_dbuf_state *new_dbuf_state = NULL;
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006076 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006077 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306078 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306079 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07006080
Ville Syrjäläef79d622021-01-22 22:56:32 +02006081 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6082 new_dbuf_state = intel_atomic_get_dbuf_state(state);
6083 if (IS_ERR(new_dbuf_state))
6084 return PTR_ERR(new_dbuf_state);
6085
6086 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6087 break;
6088 }
6089
6090 if (!new_dbuf_state)
6091 return 0;
6092
6093 new_dbuf_state->active_pipes =
6094 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6095
6096 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
6097 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6098 if (ret)
6099 return ret;
6100 }
6101
6102 for_each_intel_crtc(&dev_priv->drm, crtc) {
6103 enum pipe pipe = crtc->pipe;
6104
6105 new_dbuf_state->slices[pipe] =
6106 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes);
6107
6108 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
6109 continue;
6110
6111 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6112 if (ret)
6113 return ret;
6114 }
6115
6116 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
6117
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006118 if (IS_ALDERLAKE_P(dev_priv))
6119 new_dbuf_state->joined_mbus = adlp_check_mbus_joined(new_dbuf_state->active_pipes);
6120
6121 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
6122 old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006123 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
6124 if (ret)
6125 return ret;
6126
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006127 if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6128 /* TODO: Implement vblank synchronized MBUS joining changes */
6129 ret = intel_modeset_all_pipes(state);
6130 if (ret)
6131 return ret;
6132 }
6133
Ville Syrjäläef79d622021-01-22 22:56:32 +02006134 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006135 "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 +02006136 old_dbuf_state->enabled_slices,
6137 new_dbuf_state->enabled_slices,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006138 INTEL_INFO(dev_priv)->dbuf.slice_mask,
6139 yesno(old_dbuf_state->joined_mbus),
6140 yesno(new_dbuf_state->joined_mbus));
Ville Syrjäläef79d622021-01-22 22:56:32 +02006141 }
6142
6143 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6144 enum pipe pipe = crtc->pipe;
6145
6146 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
6147
6148 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
6149 continue;
6150
6151 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6152 if (ret)
6153 return ret;
6154 }
6155
6156 for_each_intel_crtc(&dev_priv->drm, crtc) {
6157 ret = skl_crtc_allocate_ddb(state, crtc);
6158 if (ret)
6159 return ret;
6160 }
6161
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006162 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006163 new_crtc_state, i) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006164 ret = skl_allocate_plane_ddb(state, crtc);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006165 if (ret)
6166 return ret;
6167
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006168 ret = skl_ddb_add_affected_planes(old_crtc_state,
6169 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006170 if (ret)
6171 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07006172 }
6173
6174 return 0;
6175}
6176
Ville Syrjäläab98e942019-02-08 22:05:27 +02006177static char enast(bool enable)
6178{
6179 return enable ? '*' : ' ';
6180}
6181
Matt Roper2722efb2016-08-17 15:55:55 -04006182static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006183skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006184{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006185 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6186 const struct intel_crtc_state *old_crtc_state;
6187 const struct intel_crtc_state *new_crtc_state;
6188 struct intel_plane *plane;
6189 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01006190 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006191
Jani Nikulabdbf43d2019-10-28 12:38:15 +02006192 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02006193 return;
6194
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006195 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6196 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02006197 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
6198
6199 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
6200 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
6201
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006202 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6203 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006204 const struct skl_ddb_entry *old, *new;
6205
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006206 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
6207 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006208
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006209 if (skl_ddb_entry_equal(old, new))
6210 continue;
6211
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006212 drm_dbg_kms(&dev_priv->drm,
6213 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
6214 plane->base.base.id, plane->base.name,
6215 old->start, old->end, new->start, new->end,
6216 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006217 }
6218
6219 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6220 enum plane_id plane_id = plane->id;
6221 const struct skl_plane_wm *old_wm, *new_wm;
6222
6223 old_wm = &old_pipe_wm->planes[plane_id];
6224 new_wm = &new_pipe_wm->planes[plane_id];
6225
6226 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
6227 continue;
6228
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006229 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006230 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
6231 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006232 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006233 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
6234 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
6235 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
6236 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
6237 enast(old_wm->trans_wm.enable),
6238 enast(old_wm->sagv.wm0.enable),
6239 enast(old_wm->sagv.trans_wm.enable),
6240 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
6241 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
6242 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
6243 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
6244 enast(new_wm->trans_wm.enable),
6245 enast(new_wm->sagv.wm0.enable),
6246 enast(new_wm->sagv.trans_wm.enable));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006247
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006248 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006249 "[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"
6250 " -> %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 +03006251 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006252 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
6253 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
6254 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
6255 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
6256 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
6257 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
6258 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
6259 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
6260 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
6261 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
6262 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
6263 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
6264 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
6265 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
6266 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
6267 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
6268 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
6269 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
6270 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
6271 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
6272 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
6273 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006274
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006275 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006276 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6277 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006278 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006279 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
6280 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
6281 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
6282 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
6283 old_wm->trans_wm.blocks,
6284 old_wm->sagv.wm0.blocks,
6285 old_wm->sagv.trans_wm.blocks,
6286 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
6287 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
6288 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
6289 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
6290 new_wm->trans_wm.blocks,
6291 new_wm->sagv.wm0.blocks,
6292 new_wm->sagv.trans_wm.blocks);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006293
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006294 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006295 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6296 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006297 plane->base.base.id, plane->base.name,
6298 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6299 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6300 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6301 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6302 old_wm->trans_wm.min_ddb_alloc,
Ville Syrjäläa68aa482021-02-26 17:32:01 +02006303 old_wm->sagv.wm0.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006304 old_wm->sagv.trans_wm.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006305 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6306 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6307 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6308 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006309 new_wm->trans_wm.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006310 new_wm->sagv.wm0.min_ddb_alloc,
6311 new_wm->sagv.trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006312 }
6313 }
6314}
6315
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006316static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6317 const struct skl_pipe_wm *old_pipe_wm,
6318 const struct skl_pipe_wm *new_pipe_wm)
6319{
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006320 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6321 int level, max_level = ilk_wm_max_level(i915);
6322
6323 for (level = 0; level <= max_level; level++) {
6324 /*
6325 * We don't check uv_wm as the hardware doesn't actually
6326 * use it. It only gets used for calculating the required
6327 * ddb allocation.
6328 */
Ville Syrjälä93fe8622021-03-25 02:44:14 +02006329 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6330 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006331 return false;
6332 }
6333
Matt Roper7959ffe2021-05-18 17:06:11 -07006334 if (HAS_HW_SAGV_WM(i915)) {
6335 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6336 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6337
6338 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6339 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6340 return false;
6341 }
6342
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006343 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6344 skl_plane_trans_wm(new_pipe_wm, plane->id));
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006345}
6346
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006347/*
6348 * To make sure the cursor watermark registers are always consistent
6349 * with our computed state the following scenario needs special
6350 * treatment:
6351 *
6352 * 1. enable cursor
6353 * 2. move cursor entirely offscreen
6354 * 3. disable cursor
6355 *
6356 * Step 2. does call .disable_plane() but does not zero the watermarks
6357 * (since we consider an offscreen cursor still active for the purposes
6358 * of watermarks). Step 3. would not normally call .disable_plane()
6359 * because the actual plane visibility isn't changing, and we don't
6360 * deallocate the cursor ddb until the pipe gets disabled. So we must
6361 * force step 3. to call .disable_plane() to update the watermark
6362 * registers properly.
6363 *
6364 * Other planes do not suffer from this issues as their watermarks are
6365 * calculated based on the actual plane visibility. The only time this
6366 * can trigger for the other planes is during the initial readout as the
6367 * default value of the watermarks registers is not zero.
6368 */
6369static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6370 struct intel_crtc *crtc)
6371{
6372 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6373 const struct intel_crtc_state *old_crtc_state =
6374 intel_atomic_get_old_crtc_state(state, crtc);
6375 struct intel_crtc_state *new_crtc_state =
6376 intel_atomic_get_new_crtc_state(state, crtc);
6377 struct intel_plane *plane;
6378
6379 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6380 struct intel_plane_state *plane_state;
6381 enum plane_id plane_id = plane->id;
6382
6383 /*
6384 * Force a full wm update for every plane on modeset.
6385 * Required because the reset value of the wm registers
6386 * is non-zero, whereas we want all disabled planes to
6387 * have zero watermarks. So if we turn off the relevant
6388 * power well the hardware state will go out of sync
6389 * with the software state.
6390 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006391 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006392 skl_plane_selected_wm_equals(plane,
6393 &old_crtc_state->wm.skl.optimal,
6394 &new_crtc_state->wm.skl.optimal))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006395 continue;
6396
6397 plane_state = intel_atomic_get_plane_state(state, plane);
6398 if (IS_ERR(plane_state))
6399 return PTR_ERR(plane_state);
6400
6401 new_crtc_state->update_planes |= BIT(plane_id);
6402 }
6403
6404 return 0;
6405}
6406
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306407static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006408skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306409{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006410 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006411 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306412 int ret, i;
6413
Ville Syrjäläffc90032020-11-06 19:30:37 +02006414 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6415 ret = skl_build_pipe_wm(state, crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006416 if (ret)
6417 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006418 }
6419
Matt Roperd8e87492018-12-11 09:31:07 -08006420 ret = skl_compute_ddb(state);
6421 if (ret)
6422 return ret;
6423
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006424 ret = intel_compute_sagv_mask(state);
6425 if (ret)
6426 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006427
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006428 /*
6429 * skl_compute_ddb() will have adjusted the final watermarks
6430 * based on how much ddb is available. Now we can actually
6431 * check if the final watermarks changed.
6432 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006433 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006434 ret = skl_wm_add_affected_planes(state, crtc);
6435 if (ret)
6436 return ret;
6437 }
6438
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006439 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006440
Matt Roper98d39492016-05-12 07:06:03 -07006441 return 0;
6442}
6443
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006444static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006445 struct intel_wm_config *config)
6446{
6447 struct intel_crtc *crtc;
6448
6449 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006450 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006451 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6452
6453 if (!wm->pipe_enabled)
6454 continue;
6455
6456 config->sprites_enabled |= wm->sprites_enabled;
6457 config->sprites_scaled |= wm->sprites_scaled;
6458 config->num_pipes_active++;
6459 }
6460}
6461
Matt Ropered4a6a72016-02-23 17:20:13 -08006462static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006463{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006464 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006465 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006466 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006467 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006468 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006469
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006470 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006471
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006472 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6473 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006474
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006475 /* 5/6 split only in single pipe config on IVB+ */
Matt Roper7dadd282021-03-19 21:42:43 -07006476 if (DISPLAY_VER(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006477 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006478 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6479 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006480
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006481 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006482 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006483 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006484 }
6485
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006486 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006487 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006488
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006489 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006490
Imre Deak820c1982013-12-17 14:46:36 +02006491 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006492}
6493
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006494static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006495 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006496{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006497 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6498 const struct intel_crtc_state *crtc_state =
6499 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006500
Matt Ropered4a6a72016-02-23 17:20:13 -08006501 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006502 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006503 ilk_program_watermarks(dev_priv);
6504 mutex_unlock(&dev_priv->wm.wm_mutex);
6505}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006506
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006507static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006508 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006509{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006510 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6511 const struct intel_crtc_state *crtc_state =
6512 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006513
6514 if (!crtc_state->wm.need_postvbl_update)
6515 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006516
6517 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006518 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6519 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006520 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006521}
6522
Jani Nikula81b55ef2020-04-20 17:04:38 +03006523static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006524{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006525 level->enable = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006526 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006527 level->blocks = val & PLANE_WM_BLOCKS_MASK;
Matt Roper1003cee2021-05-14 08:36:54 -07006528 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
Pradeep Bhat30789992014-11-04 17:06:45 +00006529}
6530
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006531void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006532 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006533{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006534 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6535 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006536 int level, max_level;
6537 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006538 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006539
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006540 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006541
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006542 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006543 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006544
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006545 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006546 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006547 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006548 else
Jani Nikula5f461662020-11-30 13:15:58 +02006549 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006550
6551 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6552 }
6553
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006554 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006555 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006556 else
Jani Nikula5f461662020-11-30 13:15:58 +02006557 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006558
6559 skl_wm_level_from_reg_val(val, &wm->trans_wm);
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006560
Matt Roper7959ffe2021-05-18 17:06:11 -07006561 if (HAS_HW_SAGV_WM(dev_priv)) {
6562 if (plane_id != PLANE_CURSOR)
6563 val = intel_uncore_read(&dev_priv->uncore,
6564 PLANE_WM_SAGV(pipe, plane_id));
6565 else
6566 val = intel_uncore_read(&dev_priv->uncore,
6567 CUR_WM_SAGV(pipe));
6568
6569 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6570
6571 if (plane_id != PLANE_CURSOR)
6572 val = intel_uncore_read(&dev_priv->uncore,
6573 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6574 else
6575 val = intel_uncore_read(&dev_priv->uncore,
6576 CUR_WM_SAGV_TRANS(pipe));
6577
6578 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6579 } else if (DISPLAY_VER(dev_priv) >= 12) {
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006580 wm->sagv.wm0 = wm->wm[0];
6581 wm->sagv.trans_wm = wm->trans_wm;
6582 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006583 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006584}
6585
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006586void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006587{
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006588 struct intel_dbuf_state *dbuf_state =
6589 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006590 struct intel_crtc *crtc;
Pradeep Bhat30789992014-11-04 17:06:45 +00006591
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006592 if (IS_ALDERLAKE_P(dev_priv))
6593 dbuf_state->joined_mbus = intel_de_read(dev_priv, MBUS_CTL) & MBUS_JOIN;
6594
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006595 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006596 struct intel_crtc_state *crtc_state =
6597 to_intel_crtc_state(crtc->base.state);
6598 enum pipe pipe = crtc->pipe;
Ville Syrjälä835c1762021-05-18 17:06:16 -07006599 unsigned int mbus_offset;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006600 enum plane_id plane_id;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006601
Maarten Lankhorstec193642019-06-28 10:55:17 +02006602 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
Ville Syrjälädbf71382020-11-06 19:30:38 +02006603 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006604
6605 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6606
6607 for_each_plane_id_on_crtc(crtc, plane_id) {
6608 struct skl_ddb_entry *ddb_y =
6609 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6610 struct skl_ddb_entry *ddb_uv =
6611 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
6612
6613 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6614 plane_id, ddb_y, ddb_uv);
6615
6616 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6617 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_uv);
6618 }
6619
6620 dbuf_state->slices[pipe] =
6621 skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes);
6622
6623 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6624
Ville Syrjälä835c1762021-05-18 17:06:16 -07006625 /*
6626 * Used for checking overlaps, so we need absolute
6627 * offsets instead of MBUS relative offsets.
6628 */
6629 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_state->slices[pipe]);
6630 crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
6631 crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006632
6633 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006634 "[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 +02006635 crtc->base.base.id, crtc->base.name,
6636 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006637 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
6638 yesno(dbuf_state->joined_mbus));
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006639 }
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006640
6641 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
Pradeep Bhat30789992014-11-04 17:06:45 +00006642}
6643
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006644static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006645{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006646 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006647 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006648 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006649 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6650 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006651 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006652
Jani Nikula5f461662020-11-30 13:15:58 +02006653 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006654
Ville Syrjälä15606532016-05-13 17:55:17 +03006655 memset(active, 0, sizeof(*active));
6656
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006657 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006658
6659 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006660 u32 tmp = hw->wm_pipe[pipe];
6661
6662 /*
6663 * For active pipes LP0 watermark is marked as
6664 * enabled, and LP1+ watermaks as disabled since
6665 * we can't really reverse compute them in case
6666 * multiple pipes are active.
6667 */
6668 active->wm[0].enable = true;
6669 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6670 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6671 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006672 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006673 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006674
6675 /*
6676 * For inactive pipes, all watermark levels
6677 * should be marked as enabled but zeroed,
6678 * which is what we'd compute them to.
6679 */
6680 for (level = 0; level <= max_level; level++)
6681 active->wm[level].enable = true;
6682 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006683
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006684 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006685}
6686
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006687#define _FW_WM(value, plane) \
6688 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6689#define _FW_WM_VLV(value, plane) \
6690 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6691
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006692static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6693 struct g4x_wm_values *wm)
6694{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006695 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006696
Jani Nikula5f461662020-11-30 13:15:58 +02006697 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006698 wm->sr.plane = _FW_WM(tmp, SR);
6699 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6700 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6701 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6702
Jani Nikula5f461662020-11-30 13:15:58 +02006703 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006704 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6705 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6706 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6707 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6708 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6709 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6710
Jani Nikula5f461662020-11-30 13:15:58 +02006711 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006712 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6713 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6714 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6715 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6716}
6717
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006718static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6719 struct vlv_wm_values *wm)
6720{
6721 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006722 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006723
6724 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006725 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006726
Ville Syrjälä1b313892016-11-28 19:37:08 +02006727 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006728 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006729 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006730 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006731 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006732 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006733 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006734 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6735 }
6736
Jani Nikula5f461662020-11-30 13:15:58 +02006737 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006738 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006739 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6740 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6741 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006742
Jani Nikula5f461662020-11-30 13:15:58 +02006743 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006744 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6745 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6746 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006747
Jani Nikula5f461662020-11-30 13:15:58 +02006748 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006749 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6750
6751 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02006752 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006753 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6754 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006755
Jani Nikula5f461662020-11-30 13:15:58 +02006756 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006757 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6758 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006759
Jani Nikula5f461662020-11-30 13:15:58 +02006760 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006761 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6762 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006763
Jani Nikula5f461662020-11-30 13:15:58 +02006764 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006765 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006766 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6767 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6768 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6769 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6770 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6771 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6772 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6773 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6774 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006775 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02006776 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006777 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6778 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006779
Jani Nikula5f461662020-11-30 13:15:58 +02006780 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006781 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006782 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6783 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6784 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6785 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6786 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6787 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006788 }
6789}
6790
6791#undef _FW_WM
6792#undef _FW_WM_VLV
6793
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006794void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006795{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006796 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6797 struct intel_crtc *crtc;
6798
6799 g4x_read_wm_values(dev_priv, wm);
6800
Jani Nikula5f461662020-11-30 13:15:58 +02006801 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006802
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006803 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006804 struct intel_crtc_state *crtc_state =
6805 to_intel_crtc_state(crtc->base.state);
6806 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6807 struct g4x_pipe_wm *raw;
6808 enum pipe pipe = crtc->pipe;
6809 enum plane_id plane_id;
6810 int level, max_level;
6811
6812 active->cxsr = wm->cxsr;
6813 active->hpll_en = wm->hpll_en;
6814 active->fbc_en = wm->fbc_en;
6815
6816 active->sr = wm->sr;
6817 active->hpll = wm->hpll;
6818
6819 for_each_plane_id_on_crtc(crtc, plane_id) {
6820 active->wm.plane[plane_id] =
6821 wm->pipe[pipe].plane[plane_id];
6822 }
6823
6824 if (wm->cxsr && wm->hpll_en)
6825 max_level = G4X_WM_LEVEL_HPLL;
6826 else if (wm->cxsr)
6827 max_level = G4X_WM_LEVEL_SR;
6828 else
6829 max_level = G4X_WM_LEVEL_NORMAL;
6830
6831 level = G4X_WM_LEVEL_NORMAL;
6832 raw = &crtc_state->wm.g4x.raw[level];
6833 for_each_plane_id_on_crtc(crtc, plane_id)
6834 raw->plane[plane_id] = active->wm.plane[plane_id];
6835
6836 if (++level > max_level)
6837 goto out;
6838
6839 raw = &crtc_state->wm.g4x.raw[level];
6840 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6841 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6842 raw->plane[PLANE_SPRITE0] = 0;
6843 raw->fbc = active->sr.fbc;
6844
6845 if (++level > max_level)
6846 goto out;
6847
6848 raw = &crtc_state->wm.g4x.raw[level];
6849 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6850 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6851 raw->plane[PLANE_SPRITE0] = 0;
6852 raw->fbc = active->hpll.fbc;
6853
6854 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
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03007133/**
7134 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00007135 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03007136 *
7137 * Calculate watermark values for the various WM regs based on current mode
7138 * and plane configuration.
7139 *
7140 * There are several cases to deal with here:
7141 * - normal (i.e. non-self-refresh)
7142 * - self-refresh (SR) mode
7143 * - lines are large relative to FIFO size (buffer can hold up to 2)
7144 * - lines are small relative to FIFO size (buffer can hold more than 2
7145 * lines), so need to account for TLB latency
7146 *
7147 * The normal calculation is:
7148 * watermark = dotclock * bytes per pixel * latency
7149 * where latency is platform & configuration dependent (we assume pessimal
7150 * values here).
7151 *
7152 * The SR calculation is:
7153 * watermark = (trunc(latency/line time)+1) * surface width *
7154 * bytes per pixel
7155 * where
7156 * line time = htotal / dotclock
7157 * surface width = hdisplay for normal plane and 64 for cursor
7158 * and latency is assumed to be high, as above.
7159 *
7160 * The final value programmed to the register should always be rounded up,
7161 * and include an extra 2 entries to account for clock crossings.
7162 *
7163 * We don't use the sprite, so we can ignore that. And on Crestline we have
7164 * to set the non-SR watermarks to 8.
7165 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02007166void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03007167{
Ville Syrjälä432081b2016-10-31 22:37:03 +02007168 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03007169
7170 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03007171 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03007172}
7173
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307174void intel_enable_ipc(struct drm_i915_private *dev_priv)
7175{
7176 u32 val;
7177
José Roberto de Souzafd847b82018-09-18 13:47:11 -07007178 if (!HAS_IPC(dev_priv))
7179 return;
7180
Jani Nikula5f461662020-11-30 13:15:58 +02007181 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307182
7183 if (dev_priv->ipc_enabled)
7184 val |= DISP_IPC_ENABLE;
7185 else
7186 val &= ~DISP_IPC_ENABLE;
7187
Jani Nikula5f461662020-11-30 13:15:58 +02007188 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307189}
7190
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007191static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
7192{
7193 /* Display WA #0477 WaDisableIPC: skl */
7194 if (IS_SKYLAKE(dev_priv))
7195 return false;
7196
7197 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01007198 if (IS_KABYLAKE(dev_priv) ||
7199 IS_COFFEELAKE(dev_priv) ||
7200 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007201 return dev_priv->dram_info.symmetric_memory;
7202
7203 return true;
7204}
7205
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307206void intel_init_ipc(struct drm_i915_private *dev_priv)
7207{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307208 if (!HAS_IPC(dev_priv))
7209 return;
7210
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007211 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07007212
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307213 intel_enable_ipc(dev_priv);
7214}
7215
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007216static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007217{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007218 /*
7219 * On Ibex Peak and Cougar Point, we need to disable clock
7220 * gating for the panel power sequencer or it will fail to
7221 * start up when no ports are active.
7222 */
Jani Nikula5f461662020-11-30 13:15:58 +02007223 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007224}
7225
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007226static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007227{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007228 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007229
Damien Lespiau055e3932014-08-18 13:49:10 +01007230 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007231 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
7232 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007233 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007234
Jani Nikula5f461662020-11-30 13:15:58 +02007235 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
7236 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007237 }
7238}
7239
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007240static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007241{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007242 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007243
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007244 /*
7245 * Required for FBC
7246 * WaFbcDisableDpfcClockGating:ilk
7247 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007248 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7249 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7250 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007251
Jani Nikula5f461662020-11-30 13:15:58 +02007252 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007253 MARIUNIT_CLOCK_GATE_DISABLE |
7254 SVSMUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007255 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007256 VFMUNIT_CLOCK_GATE_DISABLE);
7257
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007258 /*
7259 * According to the spec the following bits should be set in
7260 * order to enable memory self-refresh
7261 * The bit 22/21 of 0x42004
7262 * The bit 5 of 0x42020
7263 * The bit 15 of 0x45000
7264 */
Jani Nikula5f461662020-11-30 13:15:58 +02007265 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7266 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007267 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007268 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007269 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
7270 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007271 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007272
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007273 /*
7274 * Based on the document from hardware guys the following bits
7275 * should be set unconditionally in order to enable FBC.
7276 * The bit 22 of 0x42000
7277 * The bit 22 of 0x42004
7278 * The bit 7,8,9 of 0x42020.
7279 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007280 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007281 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Jani Nikula5f461662020-11-30 13:15:58 +02007282 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7283 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007284 ILK_FBCQ_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007285 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7286 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007287 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007288 }
7289
Jani Nikula5f461662020-11-30 13:15:58 +02007290 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007291
Jani Nikula5f461662020-11-30 13:15:58 +02007292 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7293 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007294 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05307295
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007296 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007297
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007298 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007299}
7300
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007301static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007302{
Ville Syrjäläd048a262019-08-21 20:30:31 +03007303 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007304 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007305
7306 /*
7307 * On Ibex Peak and Cougar Point, we need to disable clock
7308 * gating for the panel power sequencer or it will fail to
7309 * start up when no ports are active.
7310 */
Jani Nikula5f461662020-11-30 13:15:58 +02007311 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnescd664072013-10-02 10:34:19 -07007312 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7313 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007314 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
Daniel Vetter3107bd42012-10-31 22:52:31 +01007315 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007316 /* The below fixes the weird display corruption, a few pixels shifted
7317 * downward, on (only) LVDS of some HP laptops with IVY.
7318 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007319 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007320 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007321 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7322 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007323 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007324 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007325 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7326 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Jani Nikula5f461662020-11-30 13:15:58 +02007327 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007328 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007329 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007330 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007331 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
Daniel Vetter3107bd42012-10-31 22:52:31 +01007332 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7333 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007334}
7335
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007336static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007337{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007338 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007339
Jani Nikula5f461662020-11-30 13:15:58 +02007340 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007341 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007342 drm_dbg_kms(&dev_priv->drm,
7343 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7344 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007345}
7346
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007347static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007348{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007349 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007350
Jani Nikula5f461662020-11-30 13:15:58 +02007351 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007352
Jani Nikula5f461662020-11-30 13:15:58 +02007353 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7354 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007355 ILK_ELPIN_409_SELECT);
7356
Jani Nikula5f461662020-11-30 13:15:58 +02007357 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7358 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007359 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7360 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7361
7362 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7363 * gating disable must be set. Failure to set it results in
7364 * flickering pixels due to Z write ordering failures after
7365 * some amount of runtime in the Mesa "fire" demo, and Unigine
7366 * Sanctuary and Tropics, and apparently anything else with
7367 * alpha test or pixel discard.
7368 *
7369 * According to the spec, bit 11 (RCCUNIT) must also be set,
7370 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007371 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007372 * WaDisableRCCUnitClockGating:snb
7373 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007374 */
Jani Nikula5f461662020-11-30 13:15:58 +02007375 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007376 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7377 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7378
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007379 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007380 * According to the spec the following bits should be
7381 * set in order to enable memory self-refresh and fbc:
7382 * The bit21 and bit22 of 0x42000
7383 * The bit21 and bit22 of 0x42004
7384 * The bit5 and bit7 of 0x42020
7385 * The bit14 of 0x70180
7386 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007387 *
7388 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007389 */
Jani Nikula5f461662020-11-30 13:15:58 +02007390 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7391 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007392 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007393 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7394 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007395 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Jani Nikula5f461662020-11-30 13:15:58 +02007396 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7397 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
Damien Lespiau231e54f2012-10-19 17:55:41 +01007398 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7399 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007400
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007401 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007402
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007403 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007404
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007405 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007406}
7407
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007408static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007409{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007410 /*
7411 * TODO: this bit should only be enabled when really needed, then
7412 * disabled when not needed anymore in order to save power.
7413 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007414 if (HAS_PCH_LPT_LP(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007415 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7416 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007417 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007418
7419 /* WADPOClockGatingDisable:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007420 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7421 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007422 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007423}
7424
Ville Syrjälä712bf362016-10-31 22:37:23 +02007425static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007426{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007427 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02007428 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007429
7430 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007431 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
Imre Deak7d708ee2013-04-17 14:04:50 +03007432 }
7433}
7434
Imre Deak450174f2016-05-03 15:54:21 +03007435static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7436 int general_prio_credits,
7437 int high_prio_credits)
7438{
7439 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007440 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007441
7442 /* WaTempDisableDOPClkGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007443 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7444 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
Imre Deak450174f2016-05-03 15:54:21 +03007445
Jani Nikula5f461662020-11-30 13:15:58 +02007446 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Oscar Mateo930a7842017-10-17 13:25:45 -07007447 val &= ~L3_PRIO_CREDITS_MASK;
7448 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7449 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
Jani Nikula5f461662020-11-30 13:15:58 +02007450 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007451
7452 /*
7453 * Wait at least 100 clocks before re-enabling clock gating.
7454 * See the definition of L3SQCREG1 in BSpec.
7455 */
Jani Nikula5f461662020-11-30 13:15:58 +02007456 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Imre Deak450174f2016-05-03 15:54:21 +03007457 udelay(1);
Jani Nikula5f461662020-11-30 13:15:58 +02007458 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
Imre Deak450174f2016-05-03 15:54:21 +03007459}
7460
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007461static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7462{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007463 /* Wa_1409120013:icl,ehl */
Jani Nikula5f461662020-11-30 13:15:58 +02007464 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä885f1822020-07-08 16:12:20 +03007465 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7466
Matt Atwood6f4194c2020-01-13 23:11:28 -05007467 /*Wa_14010594013:icl, ehl */
7468 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
Lucas De Marchidbac4f32021-07-28 14:59:38 -07007469 0, ICL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007470}
7471
José Roberto de Souza35f08372021-01-13 05:37:59 -08007472static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
Michel Thierry5d869232019-08-23 01:20:34 -07007473{
José Roberto de Souzac4924052021-07-12 17:38:50 -07007474 /* Wa_1409120013:tgl,rkl,adl-s,dg1 */
Clint Taylor8c209f42021-06-08 10:47:21 -07007475 if (IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv) ||
7476 IS_ALDERLAKE_S(dev_priv) || IS_DG1(dev_priv))
7477 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
7478 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007479
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007480 /* Wa_1409825376:tgl (pre-prod)*/
Matt Roper46b0d702021-07-16 22:14:26 -07007481 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007482 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 -08007483 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007484
José Roberto de Souza41c70d22021-04-08 13:49:16 -07007485 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7486 if (DISPLAY_VER(dev_priv) == 12)
7487 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7488 CLKREQ_POLICY_MEM_UP_OVRD, 0);
Michel Thierry5d869232019-08-23 01:20:34 -07007489}
7490
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007491static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7492{
7493 gen12lp_init_clock_gating(dev_priv);
7494
7495 /* Wa_22011091694:adlp */
7496 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7497}
7498
Stuart Summersda9427502020-10-14 12:19:34 -07007499static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7500{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007501 gen12lp_init_clock_gating(dev_priv);
7502
Stuart Summersda9427502020-10-14 12:19:34 -07007503 /* Wa_1409836686:dg1[a0] */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007504 if (IS_DG1_GT_STEP(dev_priv, STEP_A0, STEP_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007505 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 -07007506 DPT_GATING_DIS);
7507}
7508
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007509static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7510{
7511 if (!HAS_PCH_CNP(dev_priv))
7512 return;
7513
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007514 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Jani Nikula5f461662020-11-30 13:15:58 +02007515 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 -07007516 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007517}
7518
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007519static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7520{
7521 cnp_init_clock_gating(dev_priv);
7522 gen9_init_clock_gating(dev_priv);
7523
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007524 /* WAC6entrylatency:cfl */
Jani Nikula5f461662020-11-30 13:15:58 +02007525 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 +03007526 FBC_LLC_FULLY_OPEN);
7527
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007528 /*
7529 * WaFbcTurnOffFbcWatermark:cfl
7530 * Display WA #0562: cfl
7531 */
Jani Nikula5f461662020-11-30 13:15:58 +02007532 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 +03007533 DISP_FBC_WM_DIS);
7534
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007535 /*
7536 * WaFbcNukeOnHostModify:cfl
7537 * Display WA #0873: cfl
7538 */
Jani Nikula5f461662020-11-30 13:15:58 +02007539 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 -07007540 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7541}
7542
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007543static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007544{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007545 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007546
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007547 /* WAC6entrylatency:kbl */
Jani Nikula5f461662020-11-30 13:15:58 +02007548 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 +03007549 FBC_LLC_FULLY_OPEN);
7550
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007551 /* WaDisableSDEUnitClockGating:kbl */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007552 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007553 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007554 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007555
7556 /* WaDisableGamClockGating:kbl */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007557 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007558 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007559 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007560
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007561 /*
7562 * WaFbcTurnOffFbcWatermark:kbl
7563 * Display WA #0562: kbl
7564 */
Jani Nikula5f461662020-11-30 13:15:58 +02007565 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 +03007566 DISP_FBC_WM_DIS);
7567
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007568 /*
7569 * WaFbcNukeOnHostModify:kbl
7570 * Display WA #0873: kbl
7571 */
Jani Nikula5f461662020-11-30 13:15:58 +02007572 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 +03007573 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007574}
7575
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007576static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007577{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007578 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007579
Ville Syrjäläf1421192020-07-16 22:04:25 +03007580 /* WaDisableDopClockGating:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007581 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 +03007582 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7583
Mika Kuoppala44fff992016-06-07 17:19:09 +03007584 /* WAC6entrylatency:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007585 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 +03007586 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007587
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007588 /*
7589 * WaFbcTurnOffFbcWatermark:skl
7590 * Display WA #0562: skl
7591 */
Jani Nikula5f461662020-11-30 13:15:58 +02007592 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 +03007593 DISP_FBC_WM_DIS);
7594
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007595 /*
7596 * WaFbcNukeOnHostModify:skl
7597 * Display WA #0873: skl
7598 */
Jani Nikula5f461662020-11-30 13:15:58 +02007599 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 +03007600 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007601
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007602 /*
7603 * WaFbcHighMemBwCorruptionAvoidance:skl
7604 * Display WA #0883: skl
7605 */
Jani Nikula5f461662020-11-30 13:15:58 +02007606 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 +03007607 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007608}
7609
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007610static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007611{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007612 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007613
Ville Syrjälä885f1822020-07-08 16:12:20 +03007614 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007615 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7616 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007617 HSW_FBCQ_DIS);
7618
Ben Widawskyab57fff2013-12-12 15:28:04 -08007619 /* WaSwitchSolVfFArbitrationPriority:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007620 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 -07007621
Ben Widawskyab57fff2013-12-12 15:28:04 -08007622 /* WaPsrDPAMaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007623 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7624 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007625
Damien Lespiau055e3932014-08-18 13:49:10 +01007626 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007627 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007628 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7629 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007630 BDW_DPRS_MASK_VBLANK_SRD);
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007631
7632 /* Undocumented but fixes async flip + VT-d corruption */
7633 if (intel_vtd_active())
7634 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7635 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007636 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007637
Ben Widawskyab57fff2013-12-12 15:28:04 -08007638 /* WaVSRefCountFullforceMissDisable:bdw */
7639 /* WaDSRefCountFullforceMissDisable:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007640 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7641 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ben Widawskyab57fff2013-12-12 15:28:04 -08007642 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007643
Jani Nikula5f461662020-11-30 13:15:58 +02007644 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007645 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007646
7647 /* WaDisableSDEUnitClockGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007648 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 +02007649 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007650
Imre Deak450174f2016-05-03 15:54:21 +03007651 /* WaProgramL3SqcReg1Default:bdw */
7652 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007653
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007654 /* WaKVMNotificationOnConfigChange:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007655 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 +03007656 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7657
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007658 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007659
7660 /* WaDisableDopClockGating:bdw
7661 *
7662 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7663 * clock gating.
7664 */
Jani Nikula5f461662020-11-30 13:15:58 +02007665 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7666 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007667}
7668
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007669static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007670{
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007671 enum pipe pipe;
7672
Ville Syrjälä885f1822020-07-08 16:12:20 +03007673 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007674 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7675 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007676 HSW_FBCQ_DIS);
7677
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007678 for_each_pipe(dev_priv, pipe) {
7679 /* Undocumented but fixes async flip + VT-d corruption */
7680 if (intel_vtd_active())
7681 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7682 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
7683 }
7684
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007685 /* This is required by WaCatErrorRejectionIssue:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007686 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7687 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007688 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007689
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007690 /* WaSwitchSolVfFArbitrationPriority:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007691 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 -07007692
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007693 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007694}
7695
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007696static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007697{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007698 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007699
Jani Nikula5f461662020-11-30 13:15:58 +02007700 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007701
Ville Syrjälä885f1822020-07-08 16:12:20 +03007702 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007703 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7704 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007705 ILK_FBCQ_DIS);
7706
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007707 /* WaDisableBackToBackFlipFix:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007708 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007709 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7710 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7711
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007712 if (IS_IVB_GT1(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007713 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007714 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007715 else {
7716 /* must write both registers */
Jani Nikula5f461662020-11-30 13:15:58 +02007717 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Ville Syrjälä412236c2014-01-22 21:32:44 +02007718 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jani Nikula5f461662020-11-30 13:15:58 +02007719 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007720 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007721 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007722
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +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:ivb 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ä28acf3b2014-01-22 21:32:48 +02007728 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007729
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007730 /* This is required by WaCatErrorRejectionIssue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007731 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7732 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007733 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7734
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007735 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007736
Jani Nikula5f461662020-11-30 13:15:58 +02007737 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
Ben Widawsky20848222012-05-04 18:58:59 -07007738 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7739 snpcr |= GEN6_MBC_SNPCR_MED;
Jani Nikula5f461662020-11-30 13:15:58 +02007740 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007741
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007742 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007743 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007744
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007745 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007746}
7747
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007748static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007749{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007750 /* WaDisableBackToBackFlipFix:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007751 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007752 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7753 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7754
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007755 /* WaDisableDopClockGating:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007756 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007757 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7758
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007759 /* This is required by WaCatErrorRejectionIssue:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007760 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7761 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007762 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7763
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007764 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007765 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007766 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007767 */
Jani Nikula5f461662020-11-30 13:15:58 +02007768 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007769 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007770
Akash Goelc98f5062014-03-24 23:00:07 +05307771 /* WaDisableL3Bank2xClockGate:vlv
7772 * Disabling L3 clock gating- MMIO 940c[25] = 1
7773 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
Jani Nikula5f461662020-11-30 13:15:58 +02007774 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7775 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007776
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007777 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007778 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007779 * Disable clock gating on th GCFG unit to prevent a delay
7780 * in the reporting of vblank events.
7781 */
Jani Nikula5f461662020-11-30 13:15:58 +02007782 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007783}
7784
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007785static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007786{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007787 /* WaVSRefCountFullforceMissDisable:chv */
7788 /* WaDSRefCountFullforceMissDisable:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007789 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7790 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ville Syrjälä232ce332014-04-09 13:28:35 +03007791 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007792
7793 /* WaDisableSemaphoreAndSyncFlipWait:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007794 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007795 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007796
7797 /* WaDisableCSUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007798 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 +03007799 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007800
7801 /* WaDisableSDEUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007802 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 +03007803 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007804
7805 /*
Imre Deak450174f2016-05-03 15:54:21 +03007806 * WaProgramL3SqcReg1Default:chv
7807 * See gfxspecs/Related Documents/Performance Guide/
7808 * LSQC Setting Recommendations.
7809 */
7810 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007811}
7812
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007813static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007814{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007815 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007816
Jani Nikula5f461662020-11-30 13:15:58 +02007817 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7818 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007819 GS_UNIT_CLOCK_GATE_DISABLE |
7820 CL_UNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007821 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007822 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7823 OVRUNIT_CLOCK_GATE_DISABLE |
7824 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007825 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007826 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007827 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007828
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007829 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007830}
7831
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007832static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007833{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007834 struct intel_uncore *uncore = &dev_priv->uncore;
7835
7836 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7837 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7838 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7839 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7840 intel_uncore_write16(uncore, DEUC, 0);
7841 intel_uncore_write(uncore,
7842 MI_ARB_STATE,
7843 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007844}
7845
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007846static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007847{
Jani Nikula5f461662020-11-30 13:15:58 +02007848 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007849 I965_RCC_CLOCK_GATE_DISABLE |
7850 I965_RCPB_CLOCK_GATE_DISABLE |
7851 I965_ISC_CLOCK_GATE_DISABLE |
7852 I965_FBC_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007853 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7854 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä20f94962013-06-07 10:47:02 +03007855 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007856}
7857
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007858static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007859{
Jani Nikula5f461662020-11-30 13:15:58 +02007860 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007861
7862 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7863 DSTATE_DOT_CLOCK_GATING;
Jani Nikula5f461662020-11-30 13:15:58 +02007864 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007865
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007866 if (IS_PINEVIEW(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007867 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007868
7869 /* IIR "flip pending" means done if this bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007870 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007871
7872 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007873 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007874
7875 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007876 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 +03007877
Jani Nikula5f461662020-11-30 13:15:58 +02007878 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007879 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007880}
7881
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007882static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007883{
Jani Nikula5f461662020-11-30 13:15:58 +02007884 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007885
7886 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007887 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007888 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007889
Jani Nikula5f461662020-11-30 13:15:58 +02007890 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007891 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007892
7893 /*
7894 * Have FBC ignore 3D activity since we use software
7895 * render tracking, and otherwise a pure 3D workload
7896 * (even if it just renders a single frame and then does
7897 * abosultely nothing) would not allow FBC to recompress
7898 * until a 2D blit occurs.
7899 */
Jani Nikula5f461662020-11-30 13:15:58 +02007900 intel_uncore_write(&dev_priv->uncore, SCPD0,
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007901 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007902}
7903
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007904static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007905{
Jani Nikula5f461662020-11-30 13:15:58 +02007906 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007907 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7908 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007909}
7910
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007911void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007912{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007913 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007914}
7915
Ville Syrjälä712bf362016-10-31 22:37:23 +02007916void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007917{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007918 if (HAS_PCH_LPT(dev_priv))
7919 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007920}
7921
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007922static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007923{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007924 drm_dbg_kms(&dev_priv->drm,
7925 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007926}
7927
7928/**
7929 * intel_init_clock_gating_hooks - setup the clock gating hooks
7930 * @dev_priv: device private
7931 *
7932 * Setup the hooks that configure which clocks of a given platform can be
7933 * gated and also apply various GT and display specific workarounds for these
7934 * platforms. Note that some GT specific workarounds are applied separately
7935 * when GPU contexts or batchbuffers start their execution.
7936 */
7937void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7938{
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007939 if (IS_ALDERLAKE_P(dev_priv))
7940 dev_priv->display.init_clock_gating = adlp_init_clock_gating;
7941 else if (IS_DG1(dev_priv))
Stuart Summersda9427502020-10-14 12:19:34 -07007942 dev_priv->display.init_clock_gating = dg1_init_clock_gating;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007943 else if (GRAPHICS_VER(dev_priv) == 12)
José Roberto de Souza35f08372021-01-13 05:37:59 -08007944 dev_priv->display.init_clock_gating = gen12lp_init_clock_gating;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007945 else if (GRAPHICS_VER(dev_priv) == 11)
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007946 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007947 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007948 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007949 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007950 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007951 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007952 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007953 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007954 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007955 else if (IS_GEMINILAKE(dev_priv))
7956 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007957 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007958 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007959 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007960 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007961 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007962 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007963 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007964 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007965 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007966 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007967 else if (GRAPHICS_VER(dev_priv) == 6)
Imre Deakbb400da2016-03-16 13:38:54 +02007968 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007969 else if (GRAPHICS_VER(dev_priv) == 5)
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007970 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007971 else if (IS_G4X(dev_priv))
7972 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007973 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007974 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007975 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007976 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007977 else if (GRAPHICS_VER(dev_priv) == 3)
Imre Deakbb400da2016-03-16 13:38:54 +02007978 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7979 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7980 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007981 else if (GRAPHICS_VER(dev_priv) == 2)
Imre Deakbb400da2016-03-16 13:38:54 +02007982 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7983 else {
7984 MISSING_CASE(INTEL_DEVID(dev_priv));
7985 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7986 }
7987}
7988
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007989/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007990void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007991{
Daniel Vetterc921aba2012-04-26 23:28:17 +02007992 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007993 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08007994 pnv_get_mem_freq(dev_priv);
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007995 else if (GRAPHICS_VER(dev_priv) == 5)
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08007996 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007997
James Ausmusb068a862019-10-09 10:23:14 -07007998 if (intel_has_sagv(dev_priv))
7999 skl_setup_sagv_block_time(dev_priv);
8000
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008001 /* For FIFO watermark updates */
Matt Roper7dadd282021-03-19 21:42:43 -07008002 if (DISPLAY_VER(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008003 skl_setup_wm_latency(dev_priv);
Matt Roper98d39492016-05-12 07:06:03 -07008004 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008005 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008006 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008007
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008008 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008009 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008010 (DISPLAY_VER(dev_priv) != 5 && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008011 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008012 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008013 dev_priv->display.compute_intermediate_wm =
8014 ilk_compute_intermediate_wm;
8015 dev_priv->display.initial_watermarks =
8016 ilk_initial_watermarks;
8017 dev_priv->display.optimize_watermarks =
8018 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008019 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008020 drm_dbg_kms(&dev_priv->drm,
8021 "Failed to read display plane latency. "
8022 "Disable CxSR\n");
Ville Syrjäläbd602542014-01-07 16:14:10 +02008023 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008024 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008025 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008026 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008027 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008028 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008029 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008030 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008031 } else if (IS_G4X(dev_priv)) {
8032 g4x_setup_wm_latency(dev_priv);
8033 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8034 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8035 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8036 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008037 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00008038 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008039 dev_priv->is_ddr3,
8040 dev_priv->fsb_freq,
8041 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008042 drm_info(&dev_priv->drm,
8043 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008044 "(found ddr%s fsb freq %d, mem freq %d), "
8045 "disabling CxSR\n",
8046 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8047 dev_priv->fsb_freq, dev_priv->mem_freq);
8048 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008049 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008050 dev_priv->display.update_wm = NULL;
8051 } else
Lucas De Marchi1d218222019-12-24 00:40:04 -08008052 dev_priv->display.update_wm = pnv_update_wm;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008053 } else if (DISPLAY_VER(dev_priv) == 4) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008054 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008055 } else if (DISPLAY_VER(dev_priv) == 3) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008056 dev_priv->display.update_wm = i9xx_update_wm;
8057 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008058 } else if (DISPLAY_VER(dev_priv) == 2) {
Jani Nikula24977872019-09-11 12:26:08 +03008059 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008060 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008061 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008062 } else {
8063 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008064 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008065 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008066 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008067 drm_err(&dev_priv->drm,
8068 "unexpected fall-through in %s\n", __func__);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008069 }
8070}
8071
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008072void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008073{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01008074 dev_priv->runtime_pm.suspended = false;
8075 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008076}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02008077
8078static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
8079{
8080 struct intel_dbuf_state *dbuf_state;
8081
8082 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
8083 if (!dbuf_state)
8084 return NULL;
8085
8086 return &dbuf_state->base;
8087}
8088
8089static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
8090 struct intel_global_state *state)
8091{
8092 kfree(state);
8093}
8094
8095static const struct intel_global_state_funcs intel_dbuf_funcs = {
8096 .atomic_duplicate_state = intel_dbuf_duplicate_state,
8097 .atomic_destroy_state = intel_dbuf_destroy_state,
8098};
8099
8100struct intel_dbuf_state *
8101intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
8102{
8103 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8104 struct intel_global_state *dbuf_state;
8105
8106 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
8107 if (IS_ERR(dbuf_state))
8108 return ERR_CAST(dbuf_state);
8109
8110 return to_intel_dbuf_state(dbuf_state);
8111}
8112
8113int intel_dbuf_init(struct drm_i915_private *dev_priv)
8114{
8115 struct intel_dbuf_state *dbuf_state;
8116
8117 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
8118 if (!dbuf_state)
8119 return -ENOMEM;
8120
8121 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
8122 &dbuf_state->base, &intel_dbuf_funcs);
8123
8124 return 0;
8125}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008126
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008127/*
8128 * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
8129 * update the request state of all DBUS slices.
8130 */
8131static void update_mbus_pre_enable(struct intel_atomic_state *state)
8132{
8133 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8134 u32 mbus_ctl, dbuf_min_tracker_val;
8135 enum dbuf_slice slice;
8136 const struct intel_dbuf_state *dbuf_state =
8137 intel_atomic_get_new_dbuf_state(state);
8138
8139 if (!IS_ALDERLAKE_P(dev_priv))
8140 return;
8141
8142 /*
8143 * TODO: Implement vblank synchronized MBUS joining changes.
8144 * Must be properly coordinated with dbuf reprogramming.
8145 */
8146 if (dbuf_state->joined_mbus) {
8147 mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
8148 MBUS_JOIN_PIPE_SELECT_NONE;
8149 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
8150 } else {
8151 mbus_ctl = MBUS_HASHING_MODE_2x2 |
8152 MBUS_JOIN_PIPE_SELECT_NONE;
8153 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
8154 }
8155
8156 intel_de_rmw(dev_priv, MBUS_CTL,
8157 MBUS_HASHING_MODE_MASK | MBUS_JOIN |
8158 MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
8159
8160 for_each_dbuf_slice(dev_priv, slice)
8161 intel_de_rmw(dev_priv, DBUF_CTL_S(slice),
8162 DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
8163 dbuf_min_tracker_val);
8164}
8165
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008166void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
8167{
8168 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8169 const struct intel_dbuf_state *new_dbuf_state =
8170 intel_atomic_get_new_dbuf_state(state);
8171 const struct intel_dbuf_state *old_dbuf_state =
8172 intel_atomic_get_old_dbuf_state(state);
8173
8174 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008175 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8176 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008177 return;
8178
8179 WARN_ON(!new_dbuf_state->base.changed);
8180
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008181 update_mbus_pre_enable(state);
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008182 gen9_dbuf_slices_update(dev_priv,
8183 old_dbuf_state->enabled_slices |
8184 new_dbuf_state->enabled_slices);
8185}
8186
8187void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
8188{
8189 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8190 const struct intel_dbuf_state *new_dbuf_state =
8191 intel_atomic_get_new_dbuf_state(state);
8192 const struct intel_dbuf_state *old_dbuf_state =
8193 intel_atomic_get_old_dbuf_state(state);
8194
8195 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008196 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8197 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008198 return;
8199
8200 WARN_ON(!new_dbuf_state->base.changed);
8201
8202 gen9_dbuf_slices_update(dev_priv,
8203 new_dbuf_state->enabled_slices);
8204}