blob: 95fda20d5547bb1f93c208b2b8d046b8937799ce [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ä04548cb2017-04-21 21:14:29 +03001373static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1374{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001375 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001376 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001377 to_intel_atomic_state(crtc_state->uapi.state);
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
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001454static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001455{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001456 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301457 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001458 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1459 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1460 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001461 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001462 const struct intel_crtc_state *old_crtc_state =
1463 intel_atomic_get_old_crtc_state(intel_state, crtc);
1464 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
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001467 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001468 *intermediate = *optimal;
1469
1470 intermediate->cxsr = false;
1471 intermediate->hpll_en = false;
1472 goto out;
1473 }
1474
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001475 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001476 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001477 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001478 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001479 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1480
1481 for_each_plane_id_on_crtc(crtc, plane_id) {
1482 intermediate->wm.plane[plane_id] =
1483 max(optimal->wm.plane[plane_id],
1484 active->wm.plane[plane_id]);
1485
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301486 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1487 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001488 }
1489
1490 intermediate->sr.plane = max(optimal->sr.plane,
1491 active->sr.plane);
1492 intermediate->sr.cursor = max(optimal->sr.cursor,
1493 active->sr.cursor);
1494 intermediate->sr.fbc = max(optimal->sr.fbc,
1495 active->sr.fbc);
1496
1497 intermediate->hpll.plane = max(optimal->hpll.plane,
1498 active->hpll.plane);
1499 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1500 active->hpll.cursor);
1501 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1502 active->hpll.fbc);
1503
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301504 drm_WARN_ON(&dev_priv->drm,
1505 (intermediate->sr.plane >
1506 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1507 intermediate->sr.cursor >
1508 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1509 intermediate->cxsr);
1510 drm_WARN_ON(&dev_priv->drm,
1511 (intermediate->sr.plane >
1512 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1513 intermediate->sr.cursor >
1514 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1515 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001516
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301517 drm_WARN_ON(&dev_priv->drm,
1518 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1519 intermediate->fbc_en && intermediate->cxsr);
1520 drm_WARN_ON(&dev_priv->drm,
1521 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1522 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001523
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001524out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001525 /*
1526 * If our intermediate WM are identical to the final WM, then we can
1527 * omit the post-vblank programming; only update if it's different.
1528 */
1529 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001530 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001531
1532 return 0;
1533}
1534
1535static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1536 struct g4x_wm_values *wm)
1537{
1538 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001539 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001540
1541 wm->cxsr = true;
1542 wm->hpll_en = true;
1543 wm->fbc_en = true;
1544
1545 for_each_intel_crtc(&dev_priv->drm, crtc) {
1546 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1547
1548 if (!crtc->active)
1549 continue;
1550
1551 if (!wm_state->cxsr)
1552 wm->cxsr = false;
1553 if (!wm_state->hpll_en)
1554 wm->hpll_en = false;
1555 if (!wm_state->fbc_en)
1556 wm->fbc_en = false;
1557
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001558 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001559 }
1560
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001561 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001562 wm->cxsr = false;
1563 wm->hpll_en = false;
1564 wm->fbc_en = false;
1565 }
1566
1567 for_each_intel_crtc(&dev_priv->drm, crtc) {
1568 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1569 enum pipe pipe = crtc->pipe;
1570
1571 wm->pipe[pipe] = wm_state->wm;
1572 if (crtc->active && wm->cxsr)
1573 wm->sr = wm_state->sr;
1574 if (crtc->active && wm->hpll_en)
1575 wm->hpll = wm_state->hpll;
1576 }
1577}
1578
1579static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1580{
1581 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1582 struct g4x_wm_values new_wm = {};
1583
1584 g4x_merge_wm(dev_priv, &new_wm);
1585
1586 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1587 return;
1588
1589 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1590 _intel_set_memory_cxsr(dev_priv, false);
1591
1592 g4x_write_wm_values(dev_priv, &new_wm);
1593
1594 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1595 _intel_set_memory_cxsr(dev_priv, true);
1596
1597 *old_wm = new_wm;
1598}
1599
1600static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001601 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001602{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001603 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1604 const struct intel_crtc_state *crtc_state =
1605 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001606
1607 mutex_lock(&dev_priv->wm.wm_mutex);
1608 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1609 g4x_program_watermarks(dev_priv);
1610 mutex_unlock(&dev_priv->wm.wm_mutex);
1611}
1612
1613static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001614 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001615{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001616 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1617 const struct intel_crtc_state *crtc_state =
1618 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001619
1620 if (!crtc_state->wm.need_postvbl_update)
1621 return;
1622
1623 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001624 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001625 g4x_program_watermarks(dev_priv);
1626 mutex_unlock(&dev_priv->wm.wm_mutex);
1627}
1628
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001629/* latency must be in 0.1us units. */
1630static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001631 unsigned int htotal,
1632 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001633 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001634 unsigned int latency)
1635{
1636 unsigned int ret;
1637
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001638 ret = intel_wm_method2(pixel_rate, htotal,
1639 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001640 ret = DIV_ROUND_UP(ret, 64);
1641
1642 return ret;
1643}
1644
Ville Syrjäläbb726512016-10-31 22:37:24 +02001645static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001646{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001647 /* all latencies in usec */
1648 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1649
Ville Syrjälä58590c12015-09-08 21:05:12 +03001650 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1651
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001652 if (IS_CHERRYVIEW(dev_priv)) {
1653 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1654 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001655
1656 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001657 }
1658}
1659
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001660static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1661 const struct intel_plane_state *plane_state,
1662 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001663{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001664 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001665 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001666 const struct drm_display_mode *pipe_mode =
1667 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001668 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001669
1670 if (dev_priv->wm.pri_latency[level] == 0)
1671 return USHRT_MAX;
1672
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001673 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001674 return 0;
1675
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001676 cpp = plane_state->hw.fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001677 clock = pipe_mode->crtc_clock;
1678 htotal = pipe_mode->crtc_htotal;
Ville Syrjäläe339d672016-11-28 19:37:17 +02001679 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001680
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001681 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001682 /*
1683 * FIXME the formula gives values that are
1684 * too big for the cursor FIFO, and hence we
1685 * would never be able to use cursors. For
1686 * now just hardcode the watermark.
1687 */
1688 wm = 63;
1689 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001690 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001691 dev_priv->wm.pri_latency[level] * 10);
1692 }
1693
Chris Wilson1a1f1282017-11-07 14:03:38 +00001694 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001695}
1696
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001697static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1698{
1699 return (active_planes & (BIT(PLANE_SPRITE0) |
1700 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1701}
1702
Ville Syrjälä5012e602017-03-02 19:14:56 +02001703static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001704{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001705 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301706 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001707 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001708 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001709 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001710 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001711 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001712 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001713 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001714 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001715 unsigned int total_rate;
1716 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001717
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001718 /*
1719 * When enabling sprite0 after sprite1 has already been enabled
1720 * we tend to get an underrun unless sprite0 already has some
1721 * FIFO space allcoated. Hence we always allocate at least one
1722 * cacheline for sprite0 whenever sprite1 is enabled.
1723 *
1724 * All other plane enable sequences appear immune to this problem.
1725 */
1726 if (vlv_need_sprite0_fifo_workaround(active_planes))
1727 sprite0_fifo_extra = 1;
1728
Ville Syrjälä5012e602017-03-02 19:14:56 +02001729 total_rate = raw->plane[PLANE_PRIMARY] +
1730 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001731 raw->plane[PLANE_SPRITE1] +
1732 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001733
Ville Syrjälä5012e602017-03-02 19:14:56 +02001734 if (total_rate > fifo_size)
1735 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001736
Ville Syrjälä5012e602017-03-02 19:14:56 +02001737 if (total_rate == 0)
1738 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001739
Ville Syrjälä5012e602017-03-02 19:14:56 +02001740 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001741 unsigned int rate;
1742
Ville Syrjälä5012e602017-03-02 19:14:56 +02001743 if ((active_planes & BIT(plane_id)) == 0) {
1744 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001745 continue;
1746 }
1747
Ville Syrjälä5012e602017-03-02 19:14:56 +02001748 rate = raw->plane[plane_id];
1749 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1750 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001751 }
1752
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001753 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1754 fifo_left -= sprite0_fifo_extra;
1755
Ville Syrjälä5012e602017-03-02 19:14:56 +02001756 fifo_state->plane[PLANE_CURSOR] = 63;
1757
1758 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001759
1760 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001761 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001762 int plane_extra;
1763
1764 if (fifo_left == 0)
1765 break;
1766
Ville Syrjälä5012e602017-03-02 19:14:56 +02001767 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001768 continue;
1769
1770 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001771 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001772 fifo_left -= plane_extra;
1773 }
1774
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301775 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001776
1777 /* give it all to the first plane if none are active */
1778 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301779 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001780 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1781 }
1782
1783 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001784}
1785
Ville Syrjäläff32c542017-03-02 19:14:57 +02001786/* mark all levels starting from 'level' as invalid */
1787static void vlv_invalidate_wms(struct intel_crtc *crtc,
1788 struct vlv_wm_state *wm_state, int level)
1789{
1790 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1791
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001792 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001793 enum plane_id plane_id;
1794
1795 for_each_plane_id_on_crtc(crtc, plane_id)
1796 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1797
1798 wm_state->sr[level].cursor = USHRT_MAX;
1799 wm_state->sr[level].plane = USHRT_MAX;
1800 }
1801}
1802
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001803static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1804{
1805 if (wm > fifo_size)
1806 return USHRT_MAX;
1807 else
1808 return fifo_size - wm;
1809}
1810
Ville Syrjäläff32c542017-03-02 19:14:57 +02001811/*
1812 * Starting from 'level' set all higher
1813 * levels to 'value' in the "raw" watermarks.
1814 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001815static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001816 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001817{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001818 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001819 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001820 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001821
Ville Syrjäläff32c542017-03-02 19:14:57 +02001822 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001823 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001824
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001825 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001826 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001827 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001828
1829 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001830}
1831
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001832static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1833 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001834{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001835 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001836 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001837 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001838 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001839 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001840 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001841
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001842 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001843 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1844 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001845 }
1846
1847 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001848 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001849 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1850 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1851
Ville Syrjäläff32c542017-03-02 19:14:57 +02001852 if (wm > max_wm)
1853 break;
1854
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001855 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001856 raw->plane[plane_id] = wm;
1857 }
1858
1859 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001860 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001861
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001862out:
1863 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001864 drm_dbg_kms(&dev_priv->drm,
1865 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1866 plane->base.name,
1867 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1868 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1869 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001870
1871 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001872}
1873
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001874static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1875 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001876{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001877 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001878 &crtc_state->wm.vlv.raw[level];
1879 const struct vlv_fifo_state *fifo_state =
1880 &crtc_state->wm.vlv.fifo_state;
1881
1882 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1883}
1884
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001885static 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 +02001886{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001887 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1888 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1889 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1890 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001891}
1892
1893static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001894{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001895 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001896 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001897 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001898 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001899 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001900 const struct vlv_fifo_state *fifo_state =
1901 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001902 int num_active_planes = hweight8(crtc_state->active_planes &
1903 ~BIT(PLANE_CURSOR));
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001904 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001905 const struct intel_plane_state *old_plane_state;
1906 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001907 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001908 enum plane_id plane_id;
1909 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001910 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001911
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001912 for_each_oldnew_intel_plane_in_state(state, plane,
1913 old_plane_state,
1914 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001915 if (new_plane_state->hw.crtc != &crtc->base &&
1916 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001917 continue;
1918
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001919 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001920 dirty |= BIT(plane->id);
1921 }
1922
1923 /*
1924 * DSPARB registers may have been reset due to the
1925 * power well being turned off. Make sure we restore
1926 * them to a consistent state even if no primary/sprite
1927 * planes are initially active.
1928 */
1929 if (needs_modeset)
1930 crtc_state->fifo_changed = true;
1931
1932 if (!dirty)
1933 return 0;
1934
1935 /* cursor changes don't warrant a FIFO recompute */
1936 if (dirty & ~BIT(PLANE_CURSOR)) {
1937 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001938 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001939 const struct vlv_fifo_state *old_fifo_state =
1940 &old_crtc_state->wm.vlv.fifo_state;
1941
1942 ret = vlv_compute_fifo(crtc_state);
1943 if (ret)
1944 return ret;
1945
1946 if (needs_modeset ||
1947 memcmp(old_fifo_state, fifo_state,
1948 sizeof(*fifo_state)) != 0)
1949 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001950 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001951
Ville Syrjäläff32c542017-03-02 19:14:57 +02001952 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001953 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001954 /*
1955 * Note that enabling cxsr with no primary/sprite planes
1956 * enabled can wedge the pipe. Hence we only allow cxsr
1957 * with exactly one enabled primary/sprite plane.
1958 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001959 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001960
Ville Syrjälä5012e602017-03-02 19:14:56 +02001961 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001962 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001963 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001964
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001965 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001966 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001967
Ville Syrjäläff32c542017-03-02 19:14:57 +02001968 for_each_plane_id_on_crtc(crtc, plane_id) {
1969 wm_state->wm[level].plane[plane_id] =
1970 vlv_invert_wm_value(raw->plane[plane_id],
1971 fifo_state->plane[plane_id]);
1972 }
1973
1974 wm_state->sr[level].plane =
1975 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001976 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001977 raw->plane[PLANE_SPRITE1]),
1978 sr_fifo_size);
1979
1980 wm_state->sr[level].cursor =
1981 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1982 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001983 }
1984
Ville Syrjäläff32c542017-03-02 19:14:57 +02001985 if (level == 0)
1986 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001987
Ville Syrjäläff32c542017-03-02 19:14:57 +02001988 /* limit to only levels we can actually handle */
1989 wm_state->num_levels = level;
1990
1991 /* invalidate the higher levels */
1992 vlv_invalidate_wms(crtc, wm_state, level);
1993
1994 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001995}
1996
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001997#define VLV_FIFO(plane, value) \
1998 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1999
Ville Syrjäläff32c542017-03-02 19:14:57 +02002000static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002001 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002002{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002003 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002004 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002005 const struct intel_crtc_state *crtc_state =
2006 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002007 const struct vlv_fifo_state *fifo_state =
2008 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002009 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002010 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002011
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002012 if (!crtc_state->fifo_changed)
2013 return;
2014
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002015 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2016 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2017 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002018
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302019 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2020 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002021
Ville Syrjäläc137d662017-03-02 19:15:06 +02002022 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2023
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002024 /*
2025 * uncore.lock serves a double purpose here. It allows us to
2026 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2027 * it protects the DSPARB registers from getting clobbered by
2028 * parallel updates from multiple pipes.
2029 *
2030 * intel_pipe_update_start() has already disabled interrupts
2031 * for us, so a plain spin_lock() is sufficient here.
2032 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002033 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002034
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002035 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002036 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002037 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2038 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002039
2040 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2041 VLV_FIFO(SPRITEB, 0xff));
2042 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2043 VLV_FIFO(SPRITEB, sprite1_start));
2044
2045 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2046 VLV_FIFO(SPRITEB_HI, 0x1));
2047 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2048 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2049
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002050 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2051 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002052 break;
2053 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002054 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2055 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002056
2057 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2058 VLV_FIFO(SPRITED, 0xff));
2059 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2060 VLV_FIFO(SPRITED, sprite1_start));
2061
2062 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2063 VLV_FIFO(SPRITED_HI, 0xff));
2064 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2065 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2066
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002067 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2068 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002069 break;
2070 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002071 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2072 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002073
2074 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2075 VLV_FIFO(SPRITEF, 0xff));
2076 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2077 VLV_FIFO(SPRITEF, sprite1_start));
2078
2079 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2080 VLV_FIFO(SPRITEF_HI, 0xff));
2081 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2082 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2083
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002084 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2085 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002086 break;
2087 default:
2088 break;
2089 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002090
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002091 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002092
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002093 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002094}
2095
2096#undef VLV_FIFO
2097
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002098static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002099{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002100 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002101 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2102 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2103 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002104 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002105 const struct intel_crtc_state *old_crtc_state =
2106 intel_atomic_get_old_crtc_state(intel_state, crtc);
2107 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002108 int level;
2109
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002110 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002111 *intermediate = *optimal;
2112
2113 intermediate->cxsr = false;
2114 goto out;
2115 }
2116
Ville Syrjälä4841da52017-03-02 19:14:59 +02002117 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002118 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002119 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002120
2121 for (level = 0; level < intermediate->num_levels; level++) {
2122 enum plane_id plane_id;
2123
2124 for_each_plane_id_on_crtc(crtc, plane_id) {
2125 intermediate->wm[level].plane[plane_id] =
2126 min(optimal->wm[level].plane[plane_id],
2127 active->wm[level].plane[plane_id]);
2128 }
2129
2130 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2131 active->sr[level].plane);
2132 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2133 active->sr[level].cursor);
2134 }
2135
2136 vlv_invalidate_wms(crtc, intermediate, level);
2137
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002138out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002139 /*
2140 * If our intermediate WM are identical to the final WM, then we can
2141 * omit the post-vblank programming; only update if it's different.
2142 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002143 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002144 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002145
2146 return 0;
2147}
2148
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002149static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002150 struct vlv_wm_values *wm)
2151{
2152 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002153 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002154
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002155 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002156 wm->cxsr = true;
2157
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002158 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002159 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002160
2161 if (!crtc->active)
2162 continue;
2163
2164 if (!wm_state->cxsr)
2165 wm->cxsr = false;
2166
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002167 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002168 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2169 }
2170
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002171 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002172 wm->cxsr = false;
2173
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002174 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002175 wm->level = VLV_WM_LEVEL_PM2;
2176
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002177 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002178 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002179 enum pipe pipe = crtc->pipe;
2180
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002181 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002182 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002183 wm->sr = wm_state->sr[wm->level];
2184
Ville Syrjälä1b313892016-11-28 19:37:08 +02002185 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2186 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2187 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2188 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002189 }
2190}
2191
Ville Syrjäläff32c542017-03-02 19:14:57 +02002192static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002193{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002194 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2195 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002196
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002197 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002198
Ville Syrjäläff32c542017-03-02 19:14:57 +02002199 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002200 return;
2201
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002202 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002203 chv_set_memory_dvfs(dev_priv, false);
2204
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002205 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002206 chv_set_memory_pm5(dev_priv, false);
2207
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002208 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002209 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002210
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002211 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002212
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002213 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002214 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002215
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002216 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002217 chv_set_memory_pm5(dev_priv, true);
2218
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002219 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002220 chv_set_memory_dvfs(dev_priv, true);
2221
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002222 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002223}
2224
Ville Syrjäläff32c542017-03-02 19:14:57 +02002225static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002226 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002227{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002228 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2229 const struct intel_crtc_state *crtc_state =
2230 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002231
2232 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002233 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2234 vlv_program_watermarks(dev_priv);
2235 mutex_unlock(&dev_priv->wm.wm_mutex);
2236}
2237
2238static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002239 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002240{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002241 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2242 const struct intel_crtc_state *crtc_state =
2243 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002244
2245 if (!crtc_state->wm.need_postvbl_update)
2246 return;
2247
2248 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002249 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002250 vlv_program_watermarks(dev_priv);
2251 mutex_unlock(&dev_priv->wm.wm_mutex);
2252}
2253
Ville Syrjälä432081b2016-10-31 22:37:03 +02002254static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002255{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002256 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002257 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002258 int srwm = 1;
2259 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002260 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002261
2262 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002263 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002264 if (crtc) {
2265 /* self-refresh has much higher latency */
2266 static const int sr_latency_ns = 12000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002267 const struct drm_display_mode *pipe_mode =
2268 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002269 const struct drm_framebuffer *fb =
2270 crtc->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002271 int clock = pipe_mode->crtc_clock;
2272 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002273 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002274 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002275 int entries;
2276
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002277 entries = intel_wm_method2(clock, htotal,
2278 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002279 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2280 srwm = I965_FIFO_SIZE - entries;
2281 if (srwm < 0)
2282 srwm = 1;
2283 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002284 drm_dbg_kms(&dev_priv->drm,
2285 "self-refresh entries: %d, wm: %d\n",
2286 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002287
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002288 entries = intel_wm_method2(clock, htotal,
2289 crtc->base.cursor->state->crtc_w, 4,
2290 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002291 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002292 i965_cursor_wm_info.cacheline_size) +
2293 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002294
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002295 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002296 if (cursor_sr > i965_cursor_wm_info.max_wm)
2297 cursor_sr = i965_cursor_wm_info.max_wm;
2298
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002299 drm_dbg_kms(&dev_priv->drm,
2300 "self-refresh watermark: display plane %d "
2301 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002302
Imre Deak98584252014-06-13 14:54:20 +03002303 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002304 } else {
Imre Deak98584252014-06-13 14:54:20 +03002305 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002306 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002307 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002308 }
2309
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002310 drm_dbg_kms(&dev_priv->drm,
2311 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2312 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002313
2314 /* 965 has limitations... */
Jani Nikula5f461662020-11-30 13:15:58 +02002315 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002316 FW_WM(8, CURSORB) |
2317 FW_WM(8, PLANEB) |
2318 FW_WM(8, PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02002319 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002320 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002321 /* update cursor SR watermark */
Jani Nikula5f461662020-11-30 13:15:58 +02002322 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002323
2324 if (cxsr_enabled)
2325 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002326}
2327
Ville Syrjäläf4998962015-03-10 17:02:21 +02002328#undef FW_WM
2329
Ville Syrjälä432081b2016-10-31 22:37:03 +02002330static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002331{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002332 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002333 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002334 u32 fwater_lo;
2335 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002336 int cwm, srwm = 1;
2337 int fifo_size;
2338 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002339 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002340
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002341 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002342 wm_info = &i945_wm_info;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002343 else if (DISPLAY_VER(dev_priv) != 2)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344 wm_info = &i915_wm_info;
2345 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002346 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002347
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002348 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2349 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002350 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002351 const struct drm_display_mode *pipe_mode =
2352 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002353 const struct drm_framebuffer *fb =
2354 crtc->base.primary->state->fb;
2355 int cpp;
2356
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002357 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002358 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002359 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002360 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002361
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002362 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002363 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002364 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002365 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002366 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002367 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002368 if (planea_wm > (long)wm_info->max_wm)
2369 planea_wm = wm_info->max_wm;
2370 }
2371
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002372 if (DISPLAY_VER(dev_priv) == 2)
Ville Syrjälä9d539102014-08-15 01:21:53 +03002373 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002374
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002375 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2376 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002377 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002378 const struct drm_display_mode *pipe_mode =
2379 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002380 const struct drm_framebuffer *fb =
2381 crtc->base.primary->state->fb;
2382 int cpp;
2383
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002384 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002385 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002386 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002387 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002388
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002389 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002390 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002391 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002392 if (enabled == NULL)
2393 enabled = crtc;
2394 else
2395 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002396 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002397 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002398 if (planeb_wm > (long)wm_info->max_wm)
2399 planeb_wm = wm_info->max_wm;
2400 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002401
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002402 drm_dbg_kms(&dev_priv->drm,
2403 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002404
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002405 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002406 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002407
Ville Syrjäläefc26112016-10-31 22:37:04 +02002408 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002409
2410 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002411 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002412 enabled = NULL;
2413 }
2414
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002415 /*
2416 * Overlay gets an aggressive default since video jitter is bad.
2417 */
2418 cwm = 2;
2419
2420 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002421 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002422
2423 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002424 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002425 /* self-refresh has much higher latency */
2426 static const int sr_latency_ns = 6000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002427 const struct drm_display_mode *pipe_mode =
2428 &enabled->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002429 const struct drm_framebuffer *fb =
2430 enabled->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002431 int clock = pipe_mode->crtc_clock;
2432 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002433 int hdisplay = enabled->config->pipe_src_w;
2434 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002435 int entries;
2436
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002437 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002438 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002439 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002440 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002441
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002442 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2443 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002444 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002445 drm_dbg_kms(&dev_priv->drm,
2446 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002447 srwm = wm_info->fifo_size - entries;
2448 if (srwm < 0)
2449 srwm = 1;
2450
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002451 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02002452 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002453 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002454 else
Jani Nikula5f461662020-11-30 13:15:58 +02002455 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002456 }
2457
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002458 drm_dbg_kms(&dev_priv->drm,
2459 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2460 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002461
2462 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2463 fwater_hi = (cwm & 0x1f);
2464
2465 /* Set request length to 8 cachelines per fetch */
2466 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2467 fwater_hi = fwater_hi | (1 << 8);
2468
Jani Nikula5f461662020-11-30 13:15:58 +02002469 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2470 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002471
Imre Deak5209b1f2014-07-01 12:36:17 +03002472 if (enabled)
2473 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002474}
2475
Ville Syrjälä432081b2016-10-31 22:37:03 +02002476static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002477{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002478 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002479 struct intel_crtc *crtc;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002480 const struct drm_display_mode *pipe_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002481 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002482 int planea_wm;
2483
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002484 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002485 if (crtc == NULL)
2486 return;
2487
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002488 pipe_mode = &crtc->config->hw.pipe_mode;
2489 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002490 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002491 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002492 4, pessimal_latency_ns);
Jani Nikula5f461662020-11-30 13:15:58 +02002493 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002494 fwater_lo |= (3<<8) | planea_wm;
2495
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002496 drm_dbg_kms(&dev_priv->drm,
2497 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002498
Jani Nikula5f461662020-11-30 13:15:58 +02002499 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002500}
2501
Ville Syrjälä37126462013-08-01 16:18:55 +03002502/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002503static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2504 unsigned int cpp,
2505 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002506{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002507 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002509 ret = intel_wm_method1(pixel_rate, cpp, latency);
2510 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002511
2512 return ret;
2513}
2514
Ville Syrjälä37126462013-08-01 16:18:55 +03002515/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002516static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2517 unsigned int htotal,
2518 unsigned int width,
2519 unsigned int cpp,
2520 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002522 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002523
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002524 ret = intel_wm_method2(pixel_rate, htotal,
2525 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002526 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002527
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002528 return ret;
2529}
2530
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002531static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002532{
Matt Roper15126882015-12-03 11:37:40 -08002533 /*
2534 * Neither of these should be possible since this function shouldn't be
2535 * called if the CRTC is off or the plane is invisible. But let's be
2536 * extra paranoid to avoid a potential divide-by-zero if we screw up
2537 * elsewhere in the driver.
2538 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002539 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002540 return 0;
2541 if (WARN_ON(!horiz_pixels))
2542 return 0;
2543
Ville Syrjäläac484962016-01-20 21:05:26 +02002544 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002545}
2546
Imre Deak820c1982013-12-17 14:46:36 +02002547struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002548 u16 pri;
2549 u16 spr;
2550 u16 cur;
2551 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002552};
2553
Ville Syrjälä37126462013-08-01 16:18:55 +03002554/*
2555 * For both WM_PIPE and WM_LP.
2556 * mem_value must be in 0.1us units.
2557 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002558static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2559 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002560 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002561{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002562 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002563 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002564
Ville Syrjälä03981c62018-11-14 19:34:40 +02002565 if (mem_value == 0)
2566 return U32_MAX;
2567
Maarten Lankhorstec193642019-06-28 10:55:17 +02002568 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002569 return 0;
2570
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002571 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002572
Maarten Lankhorstec193642019-06-28 10:55:17 +02002573 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002574
2575 if (!is_lp)
2576 return method1;
2577
Maarten Lankhorstec193642019-06-28 10:55:17 +02002578 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002579 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002580 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002581 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002582
2583 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002584}
2585
Ville Syrjälä37126462013-08-01 16:18:55 +03002586/*
2587 * For both WM_PIPE and WM_LP.
2588 * mem_value must be in 0.1us units.
2589 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002590static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2591 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002592 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002593{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002594 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002595 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002596
Ville Syrjälä03981c62018-11-14 19:34:40 +02002597 if (mem_value == 0)
2598 return U32_MAX;
2599
Maarten Lankhorstec193642019-06-28 10:55:17 +02002600 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002601 return 0;
2602
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002603 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002604
Maarten Lankhorstec193642019-06-28 10:55:17 +02002605 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2606 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002607 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002608 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002609 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002610 return min(method1, method2);
2611}
2612
Ville Syrjälä37126462013-08-01 16:18:55 +03002613/*
2614 * For both WM_PIPE and WM_LP.
2615 * mem_value must be in 0.1us units.
2616 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002617static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2618 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002619 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002620{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002621 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002622
Ville Syrjälä03981c62018-11-14 19:34:40 +02002623 if (mem_value == 0)
2624 return U32_MAX;
2625
Maarten Lankhorstec193642019-06-28 10:55:17 +02002626 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002627 return 0;
2628
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002629 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002630
Maarten Lankhorstec193642019-06-28 10:55:17 +02002631 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002632 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002633 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002634 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002635}
2636
Paulo Zanonicca32e92013-05-31 11:45:06 -03002637/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002638static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2639 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002640 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002641{
Ville Syrjälä83054942016-11-18 21:53:00 +02002642 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002643
Maarten Lankhorstec193642019-06-28 10:55:17 +02002644 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002645 return 0;
2646
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002647 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002648
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002649 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2650 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002651}
2652
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002653static unsigned int
2654ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002655{
Matt Roper7dadd282021-03-19 21:42:43 -07002656 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002657 return 3072;
Matt Roper7dadd282021-03-19 21:42:43 -07002658 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002659 return 768;
2660 else
2661 return 512;
2662}
2663
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002664static unsigned int
2665ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2666 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002667{
Matt Roper7dadd282021-03-19 21:42:43 -07002668 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002669 /* BDW primary/sprite plane watermarks */
2670 return level == 0 ? 255 : 2047;
Matt Roper7dadd282021-03-19 21:42:43 -07002671 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002672 /* IVB/HSW primary/sprite plane watermarks */
2673 return level == 0 ? 127 : 1023;
2674 else if (!is_sprite)
2675 /* ILK/SNB primary plane watermarks */
2676 return level == 0 ? 127 : 511;
2677 else
2678 /* ILK/SNB sprite plane watermarks */
2679 return level == 0 ? 63 : 255;
2680}
2681
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002682static unsigned int
2683ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002684{
Matt Roper7dadd282021-03-19 21:42:43 -07002685 if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002686 return level == 0 ? 63 : 255;
2687 else
2688 return level == 0 ? 31 : 63;
2689}
2690
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002691static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002692{
Matt Roper7dadd282021-03-19 21:42:43 -07002693 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002694 return 31;
2695 else
2696 return 15;
2697}
2698
Ville Syrjälä158ae642013-08-07 13:28:19 +03002699/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002700static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002701 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002702 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002703 enum intel_ddb_partitioning ddb_partitioning,
2704 bool is_sprite)
2705{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002706 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002707
2708 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002709 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002710 return 0;
2711
2712 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002713 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002714 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002715
2716 /*
2717 * For some reason the non self refresh
2718 * FIFO size is only half of the self
2719 * refresh FIFO size on ILK/SNB.
2720 */
Matt Roper7dadd282021-03-19 21:42:43 -07002721 if (DISPLAY_VER(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002722 fifo_size /= 2;
2723 }
2724
Ville Syrjälä240264f2013-08-07 13:29:12 +03002725 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002726 /* level 0 is always calculated with 1:1 split */
2727 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2728 if (is_sprite)
2729 fifo_size *= 5;
2730 fifo_size /= 6;
2731 } else {
2732 fifo_size /= 2;
2733 }
2734 }
2735
2736 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002737 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002738}
2739
2740/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002741static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002742 int level,
2743 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002744{
2745 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002746 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002747 return 64;
2748
2749 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002750 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002751}
2752
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002753static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002754 int level,
2755 const struct intel_wm_config *config,
2756 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002757 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002758{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002759 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2760 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2761 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2762 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002763}
2764
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002765static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002766 int level,
2767 struct ilk_wm_maximums *max)
2768{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002769 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2770 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2771 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2772 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002773}
2774
Ville Syrjäläd9395652013-10-09 19:18:10 +03002775static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002776 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002777 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002778{
2779 bool ret;
2780
2781 /* already determined to be invalid? */
2782 if (!result->enable)
2783 return false;
2784
2785 result->enable = result->pri_val <= max->pri &&
2786 result->spr_val <= max->spr &&
2787 result->cur_val <= max->cur;
2788
2789 ret = result->enable;
2790
2791 /*
2792 * HACK until we can pre-compute everything,
2793 * and thus fail gracefully if LP0 watermarks
2794 * are exceeded...
2795 */
2796 if (level == 0 && !result->enable) {
2797 if (result->pri_val > max->pri)
2798 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2799 level, result->pri_val, max->pri);
2800 if (result->spr_val > max->spr)
2801 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2802 level, result->spr_val, max->spr);
2803 if (result->cur_val > max->cur)
2804 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2805 level, result->cur_val, max->cur);
2806
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002807 result->pri_val = min_t(u32, result->pri_val, max->pri);
2808 result->spr_val = min_t(u32, result->spr_val, max->spr);
2809 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002810 result->enable = true;
2811 }
2812
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002813 return ret;
2814}
2815
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002816static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002817 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002818 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002819 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002820 const struct intel_plane_state *pristate,
2821 const struct intel_plane_state *sprstate,
2822 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002823 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002824{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002825 u16 pri_latency = dev_priv->wm.pri_latency[level];
2826 u16 spr_latency = dev_priv->wm.spr_latency[level];
2827 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002828
2829 /* WM1+ latency values stored in 0.5us units */
2830 if (level > 0) {
2831 pri_latency *= 5;
2832 spr_latency *= 5;
2833 cur_latency *= 5;
2834 }
2835
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002836 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002837 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002838 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002839 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002840 }
2841
2842 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002843 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002844
2845 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002846 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002847
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002848 result->enable = true;
2849}
2850
Ville Syrjäläbb726512016-10-31 22:37:24 +02002851static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002852 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002853{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002854 struct intel_uncore *uncore = &dev_priv->uncore;
2855
Matt Roper7dadd282021-03-19 21:42:43 -07002856 if (DISPLAY_VER(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002857 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002858 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002859 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002860
2861 /* read the first set of memory latencies[0:3] */
2862 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002863 ret = sandybridge_pcode_read(dev_priv,
2864 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002865 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002866
2867 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002868 drm_err(&dev_priv->drm,
2869 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002870 return;
2871 }
2872
2873 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2874 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2875 GEN9_MEM_LATENCY_LEVEL_MASK;
2876 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2877 GEN9_MEM_LATENCY_LEVEL_MASK;
2878 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2879 GEN9_MEM_LATENCY_LEVEL_MASK;
2880
2881 /* read the second set of memory latencies[4:7] */
2882 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002883 ret = sandybridge_pcode_read(dev_priv,
2884 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002885 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002886 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002887 drm_err(&dev_priv->drm,
2888 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002889 return;
2890 }
2891
2892 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2893 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2894 GEN9_MEM_LATENCY_LEVEL_MASK;
2895 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2896 GEN9_MEM_LATENCY_LEVEL_MASK;
2897 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2898 GEN9_MEM_LATENCY_LEVEL_MASK;
2899
Vandana Kannan367294b2014-11-04 17:06:46 +00002900 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002901 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2902 * need to be disabled. We make sure to sanitize the values out
2903 * of the punit to satisfy this requirement.
2904 */
2905 for (level = 1; level <= max_level; level++) {
2906 if (wm[level] == 0) {
2907 for (i = level + 1; i <= max_level; i++)
2908 wm[i] = 0;
2909 break;
2910 }
2911 }
2912
2913 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002914 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002915 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002916 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002917 * to add 2us to the various latency levels we retrieve from the
2918 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002919 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002920 if (wm[0] == 0) {
2921 wm[0] += 2;
2922 for (level = 1; level <= max_level; level++) {
2923 if (wm[level] == 0)
2924 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002925 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002926 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002927 }
2928
Mahesh Kumar86b59282018-08-31 16:39:42 +05302929 /*
2930 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2931 * If we could not get dimm info enable this WA to prevent from
2932 * any underrun. If not able to get Dimm info assume 16GB dimm
2933 * to avoid any underrun.
2934 */
José Roberto de Souza66a24502021-01-28 08:43:12 -08002935 if (dev_priv->dram_info.wm_lv_0_adjust_needed)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302936 wm[0] += 1;
2937
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002938 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002939 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002940
2941 wm[0] = (sskpd >> 56) & 0xFF;
2942 if (wm[0] == 0)
2943 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002944 wm[1] = (sskpd >> 4) & 0xFF;
2945 wm[2] = (sskpd >> 12) & 0xFF;
2946 wm[3] = (sskpd >> 20) & 0x1FF;
2947 wm[4] = (sskpd >> 32) & 0x1FF;
Matt Roper7dadd282021-03-19 21:42:43 -07002948 } else if (DISPLAY_VER(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002949 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002950
2951 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2952 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2953 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2954 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Matt Roper7dadd282021-03-19 21:42:43 -07002955 } else if (DISPLAY_VER(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002956 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002957
2958 /* ILK primary LP0 latency is 700 ns */
2959 wm[0] = 7;
2960 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2961 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002962 } else {
2963 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002964 }
2965}
2966
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002967static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002968 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002969{
2970 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002971 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002972 wm[0] = 13;
2973}
2974
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002975static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002976 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002977{
2978 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002979 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002980 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002981}
2982
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002983int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002984{
2985 /* how many WM levels are we expecting */
Matt Roper7959ffe2021-05-18 17:06:11 -07002986 if (HAS_HW_SAGV_WM(dev_priv))
2987 return 5;
2988 else if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002989 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002990 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002991 return 4;
Matt Roper7dadd282021-03-19 21:42:43 -07002992 else if (DISPLAY_VER(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002993 return 3;
2994 else
2995 return 2;
2996}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002997
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002998static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002999 const char *name,
Linus Torvaldse7c6e402021-04-27 17:05:53 -07003000 const u16 wm[])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003001{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003002 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003003
3004 for (level = 0; level <= max_level; level++) {
3005 unsigned int latency = wm[level];
3006
3007 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003008 drm_dbg_kms(&dev_priv->drm,
3009 "%s WM%d latency not provided\n",
3010 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003011 continue;
3012 }
3013
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003014 /*
3015 * - latencies are in us on gen9.
3016 * - before then, WM1+ latency values are in 0.5us units
3017 */
Matt Roper7dadd282021-03-19 21:42:43 -07003018 if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003019 latency *= 10;
3020 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003021 latency *= 5;
3022
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003023 drm_dbg_kms(&dev_priv->drm,
3024 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3025 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003026 }
3027}
3028
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003029static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003030 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003031{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003032 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003033
3034 if (wm[0] >= min)
3035 return false;
3036
3037 wm[0] = max(wm[0], min);
3038 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003039 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003040
3041 return true;
3042}
3043
Ville Syrjäläbb726512016-10-31 22:37:24 +02003044static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003045{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003046 bool changed;
3047
3048 /*
3049 * The BIOS provided WM memory latency values are often
3050 * inadequate for high resolution displays. Adjust them.
3051 */
3052 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3053 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3054 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3055
3056 if (!changed)
3057 return;
3058
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003059 drm_dbg_kms(&dev_priv->drm,
3060 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003061 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3062 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3063 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003064}
3065
Ville Syrjälä03981c62018-11-14 19:34:40 +02003066static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3067{
3068 /*
3069 * On some SNB machines (Thinkpad X220 Tablet at least)
3070 * LP3 usage can cause vblank interrupts to be lost.
3071 * The DEIIR bit will go high but it looks like the CPU
3072 * never gets interrupted.
3073 *
3074 * It's not clear whether other interrupt source could
3075 * be affected or if this is somehow limited to vblank
3076 * interrupts only. To play it safe we disable LP3
3077 * watermarks entirely.
3078 */
3079 if (dev_priv->wm.pri_latency[3] == 0 &&
3080 dev_priv->wm.spr_latency[3] == 0 &&
3081 dev_priv->wm.cur_latency[3] == 0)
3082 return;
3083
3084 dev_priv->wm.pri_latency[3] = 0;
3085 dev_priv->wm.spr_latency[3] = 0;
3086 dev_priv->wm.cur_latency[3] = 0;
3087
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003088 drm_dbg_kms(&dev_priv->drm,
3089 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003090 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3091 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3092 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3093}
3094
Ville Syrjäläbb726512016-10-31 22:37:24 +02003095static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003096{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003097 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003098
3099 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3100 sizeof(dev_priv->wm.pri_latency));
3101 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3102 sizeof(dev_priv->wm.pri_latency));
3103
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003104 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003105 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003106
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003107 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3108 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3109 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003110
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003111 if (DISPLAY_VER(dev_priv) == 6) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003112 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003113 snb_wm_lp3_irq_quirk(dev_priv);
3114 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003115}
3116
Ville Syrjäläbb726512016-10-31 22:37:24 +02003117static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003118{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003119 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003120 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003121}
3122
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003123static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003124 struct intel_pipe_wm *pipe_wm)
3125{
3126 /* LP0 watermark maximums depend on this pipe alone */
3127 const struct intel_wm_config config = {
3128 .num_pipes_active = 1,
3129 .sprites_enabled = pipe_wm->sprites_enabled,
3130 .sprites_scaled = pipe_wm->sprites_scaled,
3131 };
3132 struct ilk_wm_maximums max;
3133
3134 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003135 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003136
3137 /* At least LP0 must be valid */
3138 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003139 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003140 return false;
3141 }
3142
3143 return true;
3144}
3145
Matt Roper261a27d2015-10-08 15:28:25 -07003146/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003147static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003148{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003149 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003150 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003151 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003152 struct intel_plane *plane;
3153 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003154 const struct intel_plane_state *pristate = NULL;
3155 const struct intel_plane_state *sprstate = NULL;
3156 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003157 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003158 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003159
Maarten Lankhorstec193642019-06-28 10:55:17 +02003160 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003161
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003162 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3163 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3164 pristate = plane_state;
3165 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3166 sprstate = plane_state;
3167 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3168 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003169 }
3170
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003171 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003172 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003173 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3174 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3175 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3176 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003177 }
3178
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003179 usable_level = max_level;
3180
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003181 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Matt Roper7dadd282021-03-19 21:42:43 -07003182 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003183 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003184
3185 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003186 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003187 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003188
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003189 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003190 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003191 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003192
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003193 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003194 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003195
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003196 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003197
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003198 for (level = 1; level <= usable_level; level++) {
3199 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003200
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003201 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003202 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003203
3204 /*
3205 * Disable any watermark level that exceeds the
3206 * register maximums since such watermarks are
3207 * always invalid.
3208 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003209 if (!ilk_validate_wm_level(level, &max, wm)) {
3210 memset(wm, 0, sizeof(*wm));
3211 break;
3212 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003213 }
3214
Matt Roper86c8bbb2015-09-24 15:53:16 -07003215 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003216}
3217
3218/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003219 * Build a set of 'intermediate' watermark values that satisfy both the old
3220 * state and the new state. These can be programmed to the hardware
3221 * immediately.
3222 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003223static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003224{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003225 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->uapi.crtc);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003226 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003227 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003228 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003229 to_intel_atomic_state(newstate->uapi.state);
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003230 const struct intel_crtc_state *oldstate =
3231 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3232 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003233 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003234
3235 /*
3236 * Start with the final, target watermarks, then combine with the
3237 * currently active watermarks to get values that are safe both before
3238 * and after the vblank.
3239 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003240 *a = newstate->wm.ilk.optimal;
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003241 if (!newstate->hw.active || drm_atomic_crtc_needs_modeset(&newstate->uapi) ||
Ville Syrjäläf255c622018-11-08 17:10:13 +02003242 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003243 return 0;
3244
Matt Ropered4a6a72016-02-23 17:20:13 -08003245 a->pipe_enabled |= b->pipe_enabled;
3246 a->sprites_enabled |= b->sprites_enabled;
3247 a->sprites_scaled |= b->sprites_scaled;
3248
3249 for (level = 0; level <= max_level; level++) {
3250 struct intel_wm_level *a_wm = &a->wm[level];
3251 const struct intel_wm_level *b_wm = &b->wm[level];
3252
3253 a_wm->enable &= b_wm->enable;
3254 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3255 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3256 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3257 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3258 }
3259
3260 /*
3261 * We need to make sure that these merged watermark values are
3262 * actually a valid configuration themselves. If they're not,
3263 * there's no safe way to transition from the old state to
3264 * the new state, so we need to fail the atomic transaction.
3265 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003266 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003267 return -EINVAL;
3268
3269 /*
3270 * If our intermediate WM are identical to the final WM, then we can
3271 * omit the post-vblank programming; only update if it's different.
3272 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003273 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3274 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003275
3276 return 0;
3277}
3278
3279/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003280 * Merge the watermarks from all active pipes for a specific level.
3281 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003282static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003283 int level,
3284 struct intel_wm_level *ret_wm)
3285{
3286 const struct intel_crtc *intel_crtc;
3287
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003288 ret_wm->enable = true;
3289
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003290 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003291 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003292 const struct intel_wm_level *wm = &active->wm[level];
3293
3294 if (!active->pipe_enabled)
3295 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003296
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003297 /*
3298 * The watermark values may have been used in the past,
3299 * so we must maintain them in the registers for some
3300 * time even if the level is now disabled.
3301 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003302 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003303 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003304
3305 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3306 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3307 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3308 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3309 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003310}
3311
3312/*
3313 * Merge all low power watermarks for all active pipes.
3314 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003315static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003316 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003317 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003318 struct intel_pipe_wm *merged)
3319{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003320 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003321 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003322
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003323 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Matt Roper7dadd282021-03-19 21:42:43 -07003324 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003325 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003326 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003327
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003328 /* ILK: FBC WM must be disabled always */
Matt Roper7dadd282021-03-19 21:42:43 -07003329 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003330
3331 /* merge each WM1+ level */
3332 for (level = 1; level <= max_level; level++) {
3333 struct intel_wm_level *wm = &merged->wm[level];
3334
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003335 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003336
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003337 if (level > last_enabled_level)
3338 wm->enable = false;
3339 else if (!ilk_validate_wm_level(level, max, wm))
3340 /* make sure all following levels get disabled */
3341 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003342
3343 /*
3344 * The spec says it is preferred to disable
3345 * FBC WMs instead of disabling a WM level.
3346 */
3347 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003348 if (wm->enable)
3349 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003350 wm->fbc_val = 0;
3351 }
3352 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003353
3354 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3355 /*
3356 * FIXME this is racy. FBC might get enabled later.
3357 * What we should check here is whether FBC can be
3358 * enabled sometime later.
3359 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003360 if (DISPLAY_VER(dev_priv) == 5 && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003361 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003362 for (level = 2; level <= max_level; level++) {
3363 struct intel_wm_level *wm = &merged->wm[level];
3364
3365 wm->enable = false;
3366 }
3367 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003368}
3369
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003370static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3371{
3372 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3373 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3374}
3375
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003376/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003377static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3378 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003379{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003380 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003381 return 2 * level;
3382 else
3383 return dev_priv->wm.pri_latency[level];
3384}
3385
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003386static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003387 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003388 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003389 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003390{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003391 struct intel_crtc *intel_crtc;
3392 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003393
Ville Syrjälä0362c782013-10-09 19:17:57 +03003394 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003395 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003396
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003397 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003398 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003399 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003400
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003401 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003402
Ville Syrjälä0362c782013-10-09 19:17:57 +03003403 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003404
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003405 /*
3406 * Maintain the watermark values even if the level is
3407 * disabled. Doing otherwise could cause underruns.
3408 */
3409 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003410 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003411 (r->pri_val << WM1_LP_SR_SHIFT) |
3412 r->cur_val;
3413
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003414 if (r->enable)
3415 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3416
Matt Roper7dadd282021-03-19 21:42:43 -07003417 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003418 results->wm_lp[wm_lp - 1] |=
3419 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3420 else
3421 results->wm_lp[wm_lp - 1] |=
3422 r->fbc_val << WM1_LP_FBC_SHIFT;
3423
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003424 /*
3425 * Always set WM1S_LP_EN when spr_val != 0, even if the
3426 * level is disabled. Doing otherwise could cause underruns.
3427 */
Matt Roper7dadd282021-03-19 21:42:43 -07003428 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303429 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003430 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3431 } else
3432 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003433 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003434
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003435 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003436 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003437 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003438 const struct intel_pipe_wm *pipe_wm = &intel_crtc->wm.active.ilk;
3439 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003440
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303441 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003442 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003443
3444 results->wm_pipe[pipe] =
3445 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3446 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3447 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003448 }
3449}
3450
Paulo Zanoni861f3382013-05-31 10:19:21 -03003451/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3452 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003453static struct intel_pipe_wm *
3454ilk_find_best_result(struct drm_i915_private *dev_priv,
3455 struct intel_pipe_wm *r1,
3456 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003457{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003458 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003459 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003460
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003461 for (level = 1; level <= max_level; level++) {
3462 if (r1->wm[level].enable)
3463 level1 = level;
3464 if (r2->wm[level].enable)
3465 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003466 }
3467
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003468 if (level1 == level2) {
3469 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003470 return r2;
3471 else
3472 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003473 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003474 return r1;
3475 } else {
3476 return r2;
3477 }
3478}
3479
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003480/* dirty bits used to track which watermarks need changes */
3481#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003482#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3483#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3484#define WM_DIRTY_FBC (1 << 24)
3485#define WM_DIRTY_DDB (1 << 25)
3486
Damien Lespiau055e3932014-08-18 13:49:10 +01003487static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003488 const struct ilk_wm_values *old,
3489 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003490{
3491 unsigned int dirty = 0;
3492 enum pipe pipe;
3493 int wm_lp;
3494
Damien Lespiau055e3932014-08-18 13:49:10 +01003495 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003496 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3497 dirty |= WM_DIRTY_PIPE(pipe);
3498 /* Must disable LP1+ watermarks too */
3499 dirty |= WM_DIRTY_LP_ALL;
3500 }
3501 }
3502
3503 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3504 dirty |= WM_DIRTY_FBC;
3505 /* Must disable LP1+ watermarks too */
3506 dirty |= WM_DIRTY_LP_ALL;
3507 }
3508
3509 if (old->partitioning != new->partitioning) {
3510 dirty |= WM_DIRTY_DDB;
3511 /* Must disable LP1+ watermarks too */
3512 dirty |= WM_DIRTY_LP_ALL;
3513 }
3514
3515 /* LP1+ watermarks already deemed dirty, no need to continue */
3516 if (dirty & WM_DIRTY_LP_ALL)
3517 return dirty;
3518
3519 /* Find the lowest numbered LP1+ watermark in need of an update... */
3520 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3521 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3522 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3523 break;
3524 }
3525
3526 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3527 for (; wm_lp <= 3; wm_lp++)
3528 dirty |= WM_DIRTY_LP(wm_lp);
3529
3530 return dirty;
3531}
3532
Ville Syrjälä8553c182013-12-05 15:51:39 +02003533static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3534 unsigned int dirty)
3535{
Imre Deak820c1982013-12-17 14:46:36 +02003536 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003537 bool changed = false;
3538
3539 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3540 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003541 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003542 changed = true;
3543 }
3544 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3545 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003546 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003547 changed = true;
3548 }
3549 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3550 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003551 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003552 changed = true;
3553 }
3554
3555 /*
3556 * Don't touch WM1S_LP_EN here.
3557 * Doing so could cause underruns.
3558 */
3559
3560 return changed;
3561}
3562
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003563/*
3564 * The spec says we shouldn't write when we don't need, because every write
3565 * causes WMs to be re-evaluated, expending some power.
3566 */
Imre Deak820c1982013-12-17 14:46:36 +02003567static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3568 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003569{
Imre Deak820c1982013-12-17 14:46:36 +02003570 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003571 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003572 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003573
Damien Lespiau055e3932014-08-18 13:49:10 +01003574 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003575 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003576 return;
3577
Ville Syrjälä8553c182013-12-05 15:51:39 +02003578 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003579
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003580 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Jani Nikula5f461662020-11-30 13:15:58 +02003581 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003582 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Jani Nikula5f461662020-11-30 13:15:58 +02003583 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003584 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Jani Nikula5f461662020-11-30 13:15:58 +02003585 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003586
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003587 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003588 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02003589 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003590 if (results->partitioning == INTEL_DDB_PART_1_2)
3591 val &= ~WM_MISC_DATA_PARTITION_5_6;
3592 else
3593 val |= WM_MISC_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003594 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003595 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02003596 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003597 if (results->partitioning == INTEL_DDB_PART_1_2)
3598 val &= ~DISP_DATA_PARTITION_5_6;
3599 else
3600 val |= DISP_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003601 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003602 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003603 }
3604
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003605 if (dirty & WM_DIRTY_FBC) {
Jani Nikula5f461662020-11-30 13:15:58 +02003606 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003607 if (results->enable_fbc_wm)
3608 val &= ~DISP_FBC_WM_DIS;
3609 else
3610 val |= DISP_FBC_WM_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02003611 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003612 }
3613
Imre Deak954911e2013-12-17 14:46:34 +02003614 if (dirty & WM_DIRTY_LP(1) &&
3615 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003616 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
Imre Deak954911e2013-12-17 14:46:34 +02003617
Matt Roper7dadd282021-03-19 21:42:43 -07003618 if (DISPLAY_VER(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003619 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003620 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003621 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003622 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003623 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003624
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003625 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003626 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003627 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003628 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003629 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003630 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003631
3632 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003633}
3634
Ville Syrjälä60aca572019-11-27 21:05:51 +02003635bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003636{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003637 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3638}
3639
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003640u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303641{
Ville Syrjäläb88da662021-04-16 20:10:09 +03003642 u8 enabled_slices = 0;
3643 enum dbuf_slice slice;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303644
Ville Syrjäläb88da662021-04-16 20:10:09 +03003645 for_each_dbuf_slice(dev_priv, slice) {
3646 if (intel_uncore_read(&dev_priv->uncore,
3647 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3648 enabled_slices |= BIT(slice);
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003649 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303650
Ville Syrjäläb88da662021-04-16 20:10:09 +03003651 return enabled_slices;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303652}
3653
Matt Roper024c9042015-09-24 15:53:11 -07003654/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003655 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3656 * so assume we'll always need it in order to avoid underruns.
3657 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003658static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003659{
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003660 return DISPLAY_VER(dev_priv) == 9;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003661}
3662
Paulo Zanoni56feca92016-09-22 18:00:28 -03003663static bool
3664intel_has_sagv(struct drm_i915_private *dev_priv)
3665{
Matt Roper70bfb302021-04-07 13:39:45 -07003666 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003667 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003668}
3669
James Ausmusb068a862019-10-09 10:23:14 -07003670static void
3671skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3672{
Matt Roper7dadd282021-03-19 21:42:43 -07003673 if (DISPLAY_VER(dev_priv) >= 12) {
James Ausmusda80f042019-10-09 10:23:15 -07003674 u32 val = 0;
3675 int ret;
3676
3677 ret = sandybridge_pcode_read(dev_priv,
3678 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3679 &val, NULL);
3680 if (!ret) {
3681 dev_priv->sagv_block_time_us = val;
3682 return;
3683 }
3684
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003685 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003686 } else if (DISPLAY_VER(dev_priv) == 11) {
James Ausmusb068a862019-10-09 10:23:14 -07003687 dev_priv->sagv_block_time_us = 10;
3688 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003689 } else if (DISPLAY_VER(dev_priv) == 10) {
James Ausmusb068a862019-10-09 10:23:14 -07003690 dev_priv->sagv_block_time_us = 20;
3691 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003692 } else if (DISPLAY_VER(dev_priv) == 9) {
James Ausmusb068a862019-10-09 10:23:14 -07003693 dev_priv->sagv_block_time_us = 30;
3694 return;
3695 } else {
Matt Roper7dadd282021-03-19 21:42:43 -07003696 MISSING_CASE(DISPLAY_VER(dev_priv));
James Ausmusb068a862019-10-09 10:23:14 -07003697 }
3698
3699 /* Default to an unusable block time */
3700 dev_priv->sagv_block_time_us = -1;
3701}
3702
Lyude656d1b82016-08-17 15:55:54 -04003703/*
3704 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3705 * depending on power and performance requirements. The display engine access
3706 * to system memory is blocked during the adjustment time. Because of the
3707 * blocking time, having this enabled can cause full system hangs and/or pipe
3708 * underruns if we don't meet all of the following requirements:
3709 *
3710 * - <= 1 pipe enabled
3711 * - All planes can enable watermarks for latencies >= SAGV engine block time
3712 * - We're not using an interlaced display configuration
3713 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003714static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003715intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003716{
3717 int ret;
3718
Paulo Zanoni56feca92016-09-22 18:00:28 -03003719 if (!intel_has_sagv(dev_priv))
3720 return 0;
3721
3722 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003723 return 0;
3724
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003725 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003726 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3727 GEN9_SAGV_ENABLE);
3728
Ville Syrjäläff61a972018-12-21 19:14:34 +02003729 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003730
3731 /*
3732 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003733 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003734 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003735 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003736 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003737 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003738 return 0;
3739 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003740 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003741 return ret;
3742 }
3743
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003744 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003745 return 0;
3746}
3747
Ville Syrjälä71024042020-09-25 15:17:48 +03003748static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003749intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003750{
Imre Deakb3b8e992016-12-05 18:27:38 +02003751 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003752
Paulo Zanoni56feca92016-09-22 18:00:28 -03003753 if (!intel_has_sagv(dev_priv))
3754 return 0;
3755
3756 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003757 return 0;
3758
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003759 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003760 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003761 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3762 GEN9_SAGV_DISABLE,
3763 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3764 1);
Lyude656d1b82016-08-17 15:55:54 -04003765 /*
3766 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003767 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003768 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003769 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003770 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003771 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003772 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003773 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003774 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003775 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003776 }
3777
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003778 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003779 return 0;
3780}
3781
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003782void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3783{
3784 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003785 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003786 const struct intel_bw_state *old_bw_state;
3787 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003788
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003789 /*
3790 * Just return if we can't control SAGV or don't have it.
3791 * This is different from situation when we have SAGV but just can't
3792 * afford it due to DBuf limitation - in case if SAGV is completely
3793 * disabled in a BIOS, we are not even allowed to send a PCode request,
3794 * as it will throw an error. So have to check it here.
3795 */
3796 if (!intel_has_sagv(dev_priv))
3797 return;
3798
3799 new_bw_state = intel_atomic_get_new_bw_state(state);
3800 if (!new_bw_state)
3801 return;
3802
Matt Roper7dadd282021-03-19 21:42:43 -07003803 if (DISPLAY_VER(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003804 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003805 return;
3806 }
3807
3808 old_bw_state = intel_atomic_get_old_bw_state(state);
3809 /*
3810 * Nothing to mask
3811 */
3812 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3813 return;
3814
3815 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3816
3817 /*
3818 * If new mask is zero - means there is nothing to mask,
3819 * we can only unmask, which should be done in unmask.
3820 */
3821 if (!new_mask)
3822 return;
3823
3824 /*
3825 * Restrict required qgv points before updating the configuration.
3826 * According to BSpec we can't mask and unmask qgv points at the same
3827 * time. Also masking should be done before updating the configuration
3828 * and unmasking afterwards.
3829 */
3830 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003831}
3832
3833void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3834{
3835 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003836 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003837 const struct intel_bw_state *old_bw_state;
3838 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003839
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003840 /*
3841 * Just return if we can't control SAGV or don't have it.
3842 * This is different from situation when we have SAGV but just can't
3843 * afford it due to DBuf limitation - in case if SAGV is completely
3844 * disabled in a BIOS, we are not even allowed to send a PCode request,
3845 * as it will throw an error. So have to check it here.
3846 */
3847 if (!intel_has_sagv(dev_priv))
3848 return;
3849
3850 new_bw_state = intel_atomic_get_new_bw_state(state);
3851 if (!new_bw_state)
3852 return;
3853
Matt Roper7dadd282021-03-19 21:42:43 -07003854 if (DISPLAY_VER(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003855 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003856 return;
3857 }
3858
3859 old_bw_state = intel_atomic_get_old_bw_state(state);
3860 /*
3861 * Nothing to unmask
3862 */
3863 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3864 return;
3865
3866 new_mask = new_bw_state->qgv_points_mask;
3867
3868 /*
3869 * Allow required qgv points after updating the configuration.
3870 * According to BSpec we can't mask and unmask qgv points at the same
3871 * time. Also masking should be done before updating the configuration
3872 * and unmasking afterwards.
3873 */
3874 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003875}
3876
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003877static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003878{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003879 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003880 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä9c312122020-11-06 19:30:40 +02003881 enum plane_id plane_id;
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003882 int max_level = INT_MAX;
Lyude656d1b82016-08-17 15:55:54 -04003883
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003884 if (!intel_has_sagv(dev_priv))
3885 return false;
3886
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003887 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003888 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003889
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02003890 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003891 return false;
3892
Ville Syrjälä9c312122020-11-06 19:30:40 +02003893 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003894 const struct skl_plane_wm *wm =
Ville Syrjälä9c312122020-11-06 19:30:40 +02003895 &crtc_state->wm.skl.optimal.planes[plane_id];
3896 int level;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003897
Lyude656d1b82016-08-17 15:55:54 -04003898 /* Skip this plane if it's not enabled */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003899 if (!wm->wm[0].enable)
Lyude656d1b82016-08-17 15:55:54 -04003900 continue;
3901
3902 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003903 for (level = ilk_wm_max_level(dev_priv);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003904 !wm->wm[level].enable; --level)
Lyude656d1b82016-08-17 15:55:54 -04003905 { }
3906
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003907 /* Highest common enabled wm level for all planes */
3908 max_level = min(level, max_level);
3909 }
3910
3911 /* No enabled planes? */
3912 if (max_level == INT_MAX)
3913 return true;
3914
3915 for_each_plane_id_on_crtc(crtc, plane_id) {
3916 const struct skl_plane_wm *wm =
3917 &crtc_state->wm.skl.optimal.planes[plane_id];
3918
Lyude656d1b82016-08-17 15:55:54 -04003919 /*
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003920 * All enabled planes must have enabled a common wm level that
3921 * can tolerate memory latencies higher than sagv_block_time_us
Lyude656d1b82016-08-17 15:55:54 -04003922 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003923 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
Lyude656d1b82016-08-17 15:55:54 -04003924 return false;
3925 }
3926
3927 return true;
3928}
3929
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003930static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3931{
3932 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3933 enum plane_id plane_id;
3934
3935 if (!crtc_state->hw.active)
3936 return true;
3937
3938 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003939 const struct skl_plane_wm *wm =
3940 &crtc_state->wm.skl.optimal.planes[plane_id];
3941
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003942 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003943 return false;
3944 }
3945
3946 return true;
3947}
3948
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003949static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3950{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003951 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3952 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3953
Matt Roper7dadd282021-03-19 21:42:43 -07003954 if (DISPLAY_VER(dev_priv) >= 12)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003955 return tgl_crtc_can_enable_sagv(crtc_state);
3956 else
3957 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003958}
3959
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003960bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3961 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003962{
Matt Roper7dadd282021-03-19 21:42:43 -07003963 if (DISPLAY_VER(dev_priv) < 11 &&
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003964 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003965 return false;
3966
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003967 return bw_state->pipe_sagv_reject == 0;
3968}
3969
3970static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3971{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003972 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003973 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003974 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003975 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003976 struct intel_bw_state *new_bw_state = NULL;
3977 const struct intel_bw_state *old_bw_state = NULL;
3978 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003979
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003980 for_each_new_intel_crtc_in_state(state, crtc,
3981 new_crtc_state, i) {
3982 new_bw_state = intel_atomic_get_bw_state(state);
3983 if (IS_ERR(new_bw_state))
3984 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003985
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003986 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003987
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003988 if (intel_crtc_can_enable_sagv(new_crtc_state))
3989 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
3990 else
3991 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
3992 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003993
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003994 if (!new_bw_state)
3995 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003996
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003997 new_bw_state->active_pipes =
3998 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003999
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004000 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4001 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4002 if (ret)
4003 return ret;
4004 }
4005
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004006 for_each_new_intel_crtc_in_state(state, crtc,
4007 new_crtc_state, i) {
4008 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4009
4010 /*
4011 * We store use_sagv_wm in the crtc state rather than relying on
4012 * that bw state since we have no convenient way to get at the
4013 * latter from the plane commit hooks (especially in the legacy
4014 * cursor case)
4015 */
Matt Roper7959ffe2021-05-18 17:06:11 -07004016 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4017 DISPLAY_VER(dev_priv) >= 12 &&
4018 intel_can_enable_sagv(dev_priv, new_bw_state);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004019 }
4020
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004021 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4022 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004023 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4024 if (ret)
4025 return ret;
4026 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4027 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4028 if (ret)
4029 return ret;
4030 }
4031
4032 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004033}
4034
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004035static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4036{
Ville Syrjäläb88da662021-04-16 20:10:09 +03004037 return INTEL_INFO(dev_priv)->dbuf.size /
4038 hweight8(INTEL_INFO(dev_priv)->dbuf.slice_mask);
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004039}
4040
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004041static void
4042skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4043 struct skl_ddb_entry *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304044{
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004045 int slice_size = intel_dbuf_slice_size(dev_priv);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004046
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004047 if (!slice_mask) {
4048 ddb->start = 0;
4049 ddb->end = 0;
4050 return;
4051 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004052
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004053 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4054 ddb->end = fls(slice_mask) * slice_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004055
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004056 WARN_ON(ddb->start >= ddb->end);
Ville Syrjäläb88da662021-04-16 20:10:09 +03004057 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->dbuf.size);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004058}
4059
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004060u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4061 const struct skl_ddb_entry *entry)
4062{
Ville Syrjälä6390e5a2021-04-16 20:10:07 +03004063 int slice_size = intel_dbuf_slice_size(dev_priv);
4064 enum dbuf_slice start_slice, end_slice;
4065 u8 slice_mask = 0;
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004066
4067 if (!skl_ddb_entry_size(entry))
4068 return 0;
4069
4070 start_slice = entry->start / slice_size;
4071 end_slice = (entry->end - 1) / slice_size;
4072
4073 /*
4074 * Per plane DDB entry can in a really worst case be on multiple slices
4075 * but single entry is anyway contigious.
4076 */
4077 while (start_slice <= end_slice) {
4078 slice_mask |= BIT(start_slice);
4079 start_slice++;
4080 }
4081
4082 return slice_mask;
4083}
4084
Ville Syrjälä2791a402021-01-22 22:56:26 +02004085static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4086{
4087 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4088 int hdisplay, vdisplay;
4089
4090 if (!crtc_state->hw.active)
4091 return 0;
4092
4093 /*
4094 * Watermark/ddb requirement highly depends upon width of the
4095 * framebuffer, So instead of allocating DDB equally among pipes
4096 * distribute DDB based on resolution/width of the display.
4097 */
4098 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4099
4100 return hdisplay;
4101}
4102
Ville Syrjäläef79d622021-01-22 22:56:32 +02004103static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4104 enum pipe for_pipe,
4105 unsigned int *weight_start,
4106 unsigned int *weight_end,
4107 unsigned int *weight_total)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004108{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004109 struct drm_i915_private *dev_priv =
4110 to_i915(dbuf_state->base.state->base.dev);
4111 enum pipe pipe;
Ville Syrjälä53630962021-01-22 22:56:31 +02004112
4113 *weight_start = 0;
4114 *weight_end = 0;
4115 *weight_total = 0;
4116
Ville Syrjäläef79d622021-01-22 22:56:32 +02004117 for_each_pipe(dev_priv, pipe) {
4118 int weight = dbuf_state->weight[pipe];
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004119
4120 /*
4121 * Do not account pipes using other slice sets
4122 * luckily as of current BSpec slice sets do not partially
4123 * intersect(pipes share either same one slice or same slice set
4124 * i.e no partial intersection), so it is enough to check for
4125 * equality for now.
4126 */
Ville Syrjäläef79d622021-01-22 22:56:32 +02004127 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304128 continue;
4129
Ville Syrjälä53630962021-01-22 22:56:31 +02004130 *weight_total += weight;
Ville Syrjälä53630962021-01-22 22:56:31 +02004131 if (pipe < for_pipe) {
4132 *weight_start += weight;
4133 *weight_end += weight;
4134 } else if (pipe == for_pipe) {
4135 *weight_end += weight;
4136 }
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304137 }
Ville Syrjälä53630962021-01-22 22:56:31 +02004138}
4139
4140static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02004141skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
Ville Syrjälä53630962021-01-22 22:56:31 +02004142{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004143 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4144 unsigned int weight_total, weight_start, weight_end;
Ville Syrjälä53630962021-01-22 22:56:31 +02004145 const struct intel_dbuf_state *old_dbuf_state =
4146 intel_atomic_get_old_dbuf_state(state);
4147 struct intel_dbuf_state *new_dbuf_state =
4148 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004149 struct intel_crtc_state *crtc_state;
Ville Syrjälä53630962021-01-22 22:56:31 +02004150 struct skl_ddb_entry ddb_slices;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004151 enum pipe pipe = crtc->pipe;
Ville Syrjälä53630962021-01-22 22:56:31 +02004152 u32 ddb_range_size;
4153 u32 dbuf_slice_mask;
4154 u32 start, end;
4155 int ret;
4156
Ville Syrjäläef79d622021-01-22 22:56:32 +02004157 if (new_dbuf_state->weight[pipe] == 0) {
4158 new_dbuf_state->ddb[pipe].start = 0;
4159 new_dbuf_state->ddb[pipe].end = 0;
4160 goto out;
Ville Syrjälä53630962021-01-22 22:56:31 +02004161 }
4162
Ville Syrjäläef79d622021-01-22 22:56:32 +02004163 dbuf_slice_mask = new_dbuf_state->slices[pipe];
Ville Syrjälä53630962021-01-22 22:56:31 +02004164
4165 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
4166 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4167
Ville Syrjäläef79d622021-01-22 22:56:32 +02004168 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4169 &weight_start, &weight_end, &weight_total);
Ville Syrjälä53630962021-01-22 22:56:31 +02004170
4171 start = ddb_range_size * weight_start / weight_total;
4172 end = ddb_range_size * weight_end / weight_total;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004173
Ville Syrjäläef79d622021-01-22 22:56:32 +02004174 new_dbuf_state->ddb[pipe].start = ddb_slices.start + start;
4175 new_dbuf_state->ddb[pipe].end = ddb_slices.start + end;
4176
4177out:
4178 if (skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
4179 &new_dbuf_state->ddb[pipe]))
4180 return 0;
4181
4182 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4183 if (ret)
4184 return ret;
4185
4186 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4187 if (IS_ERR(crtc_state))
4188 return PTR_ERR(crtc_state);
4189
4190 crtc_state->wm.skl.ddb = new_dbuf_state->ddb[pipe];
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004191
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004192 drm_dbg_kms(&dev_priv->drm,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004193 "[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 +02004194 crtc->base.base.id, crtc->base.name,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004195 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4196 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4197 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4198 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004199
4200 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004201}
4202
Ville Syrjälädf331de2019-03-19 18:03:11 +02004203static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4204 int width, const struct drm_format_info *format,
4205 u64 modifier, unsigned int rotation,
4206 u32 plane_pixel_rate, struct skl_wm_params *wp,
4207 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004208static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004209 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004210 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004211 const struct skl_wm_params *wp,
4212 const struct skl_wm_level *result_prev,
4213 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004214
Ville Syrjälädf331de2019-03-19 18:03:11 +02004215static unsigned int
4216skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4217 int num_active)
4218{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004219 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004220 int level, max_level = ilk_wm_max_level(dev_priv);
4221 struct skl_wm_level wm = {};
4222 int ret, min_ddb_alloc = 0;
4223 struct skl_wm_params wp;
4224
4225 ret = skl_compute_wm_params(crtc_state, 256,
4226 drm_format_info(DRM_FORMAT_ARGB8888),
4227 DRM_FORMAT_MOD_LINEAR,
4228 DRM_MODE_ROTATE_0,
4229 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304230 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004231
4232 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004233 unsigned int latency = dev_priv->wm.skl_latency[level];
4234
4235 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004236 if (wm.min_ddb_alloc == U16_MAX)
4237 break;
4238
4239 min_ddb_alloc = wm.min_ddb_alloc;
4240 }
4241
4242 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004243}
4244
Mahesh Kumar37cde112018-04-26 19:55:17 +05304245static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4246 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004247{
Mahesh Kumar37cde112018-04-26 19:55:17 +05304248
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004249 entry->start = reg & DDB_ENTRY_MASK;
4250 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304251
Damien Lespiau16160e32014-11-04 17:06:53 +00004252 if (entry->end)
4253 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004254}
4255
Mahesh Kumarddf34312018-04-09 09:11:03 +05304256static void
4257skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4258 const enum pipe pipe,
4259 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004260 struct skl_ddb_entry *ddb_y,
4261 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304262{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004263 u32 val, val2;
4264 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304265
4266 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4267 if (plane_id == PLANE_CURSOR) {
Jani Nikula5f461662020-11-30 13:15:58 +02004268 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004269 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304270 return;
4271 }
4272
Jani Nikula5f461662020-11-30 13:15:58 +02004273 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304274
4275 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004276 if (val & PLANE_CTL_ENABLE)
4277 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4278 val & PLANE_CTL_ORDER_RGBX,
4279 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304280
Matt Roper7dadd282021-03-19 21:42:43 -07004281 if (DISPLAY_VER(dev_priv) >= 11) {
Jani Nikula5f461662020-11-30 13:15:58 +02004282 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004283 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4284 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02004285 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4286 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304287
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004288 if (fourcc &&
4289 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004290 swap(val, val2);
4291
4292 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4293 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304294 }
4295}
4296
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004297void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4298 struct skl_ddb_entry *ddb_y,
4299 struct skl_ddb_entry *ddb_uv)
4300{
4301 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4302 enum intel_display_power_domain power_domain;
4303 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004304 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004305 enum plane_id plane_id;
4306
4307 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004308 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4309 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004310 return;
4311
4312 for_each_plane_id_on_crtc(crtc, plane_id)
4313 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4314 plane_id,
4315 &ddb_y[plane_id],
4316 &ddb_uv[plane_id]);
4317
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004318 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004319}
4320
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004321/*
4322 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4323 * The bspec defines downscale amount as:
4324 *
4325 * """
4326 * Horizontal down scale amount = maximum[1, Horizontal source size /
4327 * Horizontal destination size]
4328 * Vertical down scale amount = maximum[1, Vertical source size /
4329 * Vertical destination size]
4330 * Total down scale amount = Horizontal down scale amount *
4331 * Vertical down scale amount
4332 * """
4333 *
4334 * Return value is provided in 16.16 fixed point form to retain fractional part.
4335 * Caller should take care of dividing & rounding off the value.
4336 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304337static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004338skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4339 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004340{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304341 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004342 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304343 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4344 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004345
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304346 if (drm_WARN_ON(&dev_priv->drm,
4347 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304348 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004349
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004350 /*
4351 * Src coordinates are already rotated by 270 degrees for
4352 * the 90/270 degree plane rotation cases (to match the
4353 * GTT mapping), hence no need to account for rotation here.
4354 *
4355 * n.b., src is 16.16 fixed point, dst is whole integer.
4356 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004357 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4358 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4359 dst_w = drm_rect_width(&plane_state->uapi.dst);
4360 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004361
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304362 fp_w_ratio = div_fixed16(src_w, dst_w);
4363 fp_h_ratio = div_fixed16(src_h, dst_h);
4364 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4365 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004366
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304367 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004368}
4369
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004370struct dbuf_slice_conf_entry {
4371 u8 active_pipes;
4372 u8 dbuf_mask[I915_MAX_PIPES];
4373};
4374
4375/*
4376 * Table taken from Bspec 12716
4377 * Pipes do have some preferred DBuf slice affinity,
4378 * plus there are some hardcoded requirements on how
4379 * those should be distributed for multipipe scenarios.
4380 * For more DBuf slices algorithm can get even more messy
4381 * and less readable, so decided to use a table almost
4382 * as is from BSpec itself - that way it is at least easier
4383 * to compare, change and check.
4384 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004385static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004386/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4387{
4388 {
4389 .active_pipes = BIT(PIPE_A),
4390 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004391 [PIPE_A] = BIT(DBUF_S1),
4392 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004393 },
4394 {
4395 .active_pipes = BIT(PIPE_B),
4396 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004397 [PIPE_B] = BIT(DBUF_S1),
4398 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004399 },
4400 {
4401 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4402 .dbuf_mask = {
4403 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004404 [PIPE_B] = BIT(DBUF_S2),
4405 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004406 },
4407 {
4408 .active_pipes = BIT(PIPE_C),
4409 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004410 [PIPE_C] = BIT(DBUF_S2),
4411 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004412 },
4413 {
4414 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4415 .dbuf_mask = {
4416 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004417 [PIPE_C] = BIT(DBUF_S2),
4418 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004419 },
4420 {
4421 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4422 .dbuf_mask = {
4423 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004424 [PIPE_C] = BIT(DBUF_S2),
4425 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004426 },
4427 {
4428 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4429 .dbuf_mask = {
4430 [PIPE_A] = BIT(DBUF_S1),
4431 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004432 [PIPE_C] = BIT(DBUF_S2),
4433 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004434 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004435 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004436};
4437
4438/*
4439 * Table taken from Bspec 49255
4440 * Pipes do have some preferred DBuf slice affinity,
4441 * plus there are some hardcoded requirements on how
4442 * those should be distributed for multipipe scenarios.
4443 * For more DBuf slices algorithm can get even more messy
4444 * and less readable, so decided to use a table almost
4445 * as is from BSpec itself - that way it is at least easier
4446 * to compare, change and check.
4447 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004448static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004449/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4450{
4451 {
4452 .active_pipes = BIT(PIPE_A),
4453 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004454 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4455 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004456 },
4457 {
4458 .active_pipes = BIT(PIPE_B),
4459 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004460 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4461 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004462 },
4463 {
4464 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4465 .dbuf_mask = {
4466 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004467 [PIPE_B] = BIT(DBUF_S1),
4468 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004469 },
4470 {
4471 .active_pipes = BIT(PIPE_C),
4472 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004473 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4474 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004475 },
4476 {
4477 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4478 .dbuf_mask = {
4479 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004480 [PIPE_C] = BIT(DBUF_S2),
4481 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004482 },
4483 {
4484 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4485 .dbuf_mask = {
4486 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004487 [PIPE_C] = BIT(DBUF_S2),
4488 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004489 },
4490 {
4491 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4492 .dbuf_mask = {
4493 [PIPE_A] = BIT(DBUF_S1),
4494 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004495 [PIPE_C] = BIT(DBUF_S2),
4496 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004497 },
4498 {
4499 .active_pipes = BIT(PIPE_D),
4500 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004501 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4502 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004503 },
4504 {
4505 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4506 .dbuf_mask = {
4507 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004508 [PIPE_D] = BIT(DBUF_S2),
4509 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004510 },
4511 {
4512 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4513 .dbuf_mask = {
4514 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004515 [PIPE_D] = BIT(DBUF_S2),
4516 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004517 },
4518 {
4519 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4520 .dbuf_mask = {
4521 [PIPE_A] = BIT(DBUF_S1),
4522 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004523 [PIPE_D] = BIT(DBUF_S2),
4524 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004525 },
4526 {
4527 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4528 .dbuf_mask = {
4529 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004530 [PIPE_D] = BIT(DBUF_S2),
4531 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004532 },
4533 {
4534 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4535 .dbuf_mask = {
4536 [PIPE_A] = BIT(DBUF_S1),
4537 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004538 [PIPE_D] = BIT(DBUF_S2),
4539 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004540 },
4541 {
4542 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4543 .dbuf_mask = {
4544 [PIPE_B] = BIT(DBUF_S1),
4545 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004546 [PIPE_D] = BIT(DBUF_S2),
4547 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004548 },
4549 {
4550 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4551 .dbuf_mask = {
4552 [PIPE_A] = BIT(DBUF_S1),
4553 [PIPE_B] = BIT(DBUF_S1),
4554 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004555 [PIPE_D] = BIT(DBUF_S2),
4556 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004557 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004558 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004559};
4560
Ville Syrjälä05e81552020-02-25 19:11:09 +02004561static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4562 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004563{
4564 int i;
4565
Ville Syrjälä05e81552020-02-25 19:11:09 +02004566 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004567 if (dbuf_slices[i].active_pipes == active_pipes)
4568 return dbuf_slices[i].dbuf_mask[pipe];
4569 }
4570 return 0;
4571}
4572
4573/*
4574 * This function finds an entry with same enabled pipe configuration and
4575 * returns correspondent DBuf slice mask as stated in BSpec for particular
4576 * platform.
4577 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004578static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004579{
4580 /*
4581 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4582 * required calculating "pipe ratio" in order to determine
4583 * if one or two slices can be used for single pipe configurations
4584 * as additional constraint to the existing table.
4585 * However based on recent info, it should be not "pipe ratio"
4586 * but rather ratio between pixel_rate and cdclk with additional
4587 * constants, so for now we are using only table until this is
4588 * clarified. Also this is the reason why crtc_state param is
4589 * still here - we will need it once those additional constraints
4590 * pop up.
4591 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004592 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004593}
4594
Ville Syrjälä05e81552020-02-25 19:11:09 +02004595static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004596{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004597 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004598}
4599
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004600static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004601{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004602 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4603 enum pipe pipe = crtc->pipe;
4604
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004605 if (DISPLAY_VER(dev_priv) == 12)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004606 return tgl_compute_dbuf_slices(pipe, active_pipes);
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004607 else if (DISPLAY_VER(dev_priv) == 11)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004608 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004609 /*
4610 * For anything else just return one slice yet.
4611 * Should be extended for other platforms.
4612 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004613 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004614}
4615
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004616static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004617skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4618 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004619 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004620{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004621 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004622 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004623 u32 data_rate;
4624 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304625 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004626 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004627
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004628 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004629 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004630
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004631 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004632 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004633
4634 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004635 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004636 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004637
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004638 /*
4639 * Src coordinates are already rotated by 270 degrees for
4640 * the 90/270 degree plane rotation cases (to match the
4641 * GTT mapping), hence no need to account for rotation here.
4642 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004643 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4644 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004645
Mahesh Kumarb879d582018-04-09 09:11:01 +05304646 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004647 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304648 width /= 2;
4649 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004650 }
4651
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004652 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304653
Maarten Lankhorstec193642019-06-28 10:55:17 +02004654 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004655
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004656 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4657
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004658 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004659 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004660}
4661
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004662static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004663skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4664 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004665{
Ville Syrjäläab016302020-11-06 19:30:41 +02004666 struct intel_crtc_state *crtc_state =
4667 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004668 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004669 struct intel_plane *plane;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004670 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004671 enum plane_id plane_id;
4672 int i;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004673
Matt Ropera1de91e2016-05-12 07:05:57 -07004674 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004675 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4676 if (plane->pipe != crtc->pipe)
4677 continue;
4678
4679 plane_id = plane->id;
Matt Roper024c9042015-09-24 15:53:11 -07004680
Mahesh Kumarb879d582018-04-09 09:11:01 +05304681 /* packed/y */
Ville Syrjäläab016302020-11-06 19:30:41 +02004682 crtc_state->plane_data_rate[plane_id] =
4683 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Matt Roper9c74d822016-05-12 07:05:58 -07004684
Mahesh Kumarb879d582018-04-09 09:11:01 +05304685 /* uv-plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004686 crtc_state->uv_plane_data_rate[plane_id] =
4687 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4688 }
4689
4690 for_each_plane_id_on_crtc(crtc, plane_id) {
4691 total_data_rate += crtc_state->plane_data_rate[plane_id];
4692 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004693 }
4694
4695 return total_data_rate;
4696}
4697
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004698static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004699icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4700 struct intel_crtc *crtc)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004701{
Ville Syrjäläab016302020-11-06 19:30:41 +02004702 struct intel_crtc_state *crtc_state =
4703 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004704 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004705 struct intel_plane *plane;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004706 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004707 enum plane_id plane_id;
4708 int i;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004709
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004710 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004711 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4712 if (plane->pipe != crtc->pipe)
4713 continue;
4714
4715 plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004716
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004717 if (!plane_state->planar_linked_plane) {
Ville Syrjäläab016302020-11-06 19:30:41 +02004718 crtc_state->plane_data_rate[plane_id] =
4719 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004720 } else {
4721 enum plane_id y_plane_id;
4722
4723 /*
4724 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004725 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004726 * and needs the master plane state which may be
4727 * NULL if we try get_new_plane_state(), so we
4728 * always calculate from the master.
4729 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004730 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004731 continue;
4732
4733 /* Y plane rate is calculated on the slave */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004734 y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjäläab016302020-11-06 19:30:41 +02004735 crtc_state->plane_data_rate[y_plane_id] =
4736 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004737
Ville Syrjäläab016302020-11-06 19:30:41 +02004738 crtc_state->plane_data_rate[plane_id] =
4739 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004740 }
4741 }
4742
Ville Syrjäläab016302020-11-06 19:30:41 +02004743 for_each_plane_id_on_crtc(crtc, plane_id)
4744 total_data_rate += crtc_state->plane_data_rate[plane_id];
4745
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004746 return total_data_rate;
4747}
4748
Ville Syrjälä5516e892021-02-26 17:32:03 +02004749const struct skl_wm_level *
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02004750skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004751 enum plane_id plane_id,
4752 int level)
4753{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004754 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4755
4756 if (level == 0 && pipe_wm->use_sagv_wm)
Ville Syrjäläa68aa482021-02-26 17:32:01 +02004757 return &wm->sagv.wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004758
4759 return &wm->wm[level];
4760}
4761
Ville Syrjälä5516e892021-02-26 17:32:03 +02004762const struct skl_wm_level *
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02004763skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
4764 enum plane_id plane_id)
4765{
4766 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4767
4768 if (pipe_wm->use_sagv_wm)
4769 return &wm->sagv.trans_wm;
4770
4771 return &wm->trans_wm;
4772}
4773
Ville Syrjäläa5941b42021-03-05 17:36:09 +02004774/*
4775 * We only disable the watermarks for each plane if
4776 * they exceed the ddb allocation of said plane. This
4777 * is done so that we don't end up touching cursor
4778 * watermarks needlessly when some other plane reduces
4779 * our max possible watermark level.
4780 *
4781 * Bspec has this to say about the PLANE_WM enable bit:
4782 * "All the watermarks at this level for all enabled
4783 * planes must be enabled before the level will be used."
4784 * So this is actually safe to do.
4785 */
4786static void
4787skl_check_wm_level(struct skl_wm_level *wm, u64 total)
4788{
4789 if (wm->min_ddb_alloc > total)
4790 memset(wm, 0, sizeof(*wm));
4791}
4792
4793static void
4794skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
4795 u64 total, u64 uv_total)
4796{
4797 if (wm->min_ddb_alloc > total ||
4798 uv_wm->min_ddb_alloc > uv_total) {
4799 memset(wm, 0, sizeof(*wm));
4800 memset(uv_wm, 0, sizeof(*uv_wm));
4801 }
4802}
4803
Matt Roperc107acf2016-05-12 07:06:01 -07004804static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02004805skl_allocate_plane_ddb(struct intel_atomic_state *state,
4806 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004807{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004808 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläffc90032020-11-06 19:30:37 +02004809 struct intel_crtc_state *crtc_state =
4810 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004811 const struct intel_dbuf_state *dbuf_state =
Ville Syrjälä47a14952021-01-22 22:56:30 +02004812 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004813 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
4814 int num_active = hweight8(dbuf_state->active_pipes);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004815 u16 alloc_size, start = 0;
4816 u16 total[I915_MAX_PLANES] = {};
4817 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004818 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004819 enum plane_id plane_id;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004820 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004821 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004822
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004823 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004824 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4825 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004826
Ville Syrjäläef79d622021-01-22 22:56:32 +02004827 if (!crtc_state->hw.active)
Matt Roperc107acf2016-05-12 07:06:01 -07004828 return 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004829
Matt Roper7dadd282021-03-19 21:42:43 -07004830 if (DISPLAY_VER(dev_priv) >= 11)
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004831 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02004832 icl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004833 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004834 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02004835 skl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004836
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004837 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304838 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004839 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004840
Matt Roperd8e87492018-12-11 09:31:07 -08004841 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004842 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004843 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004844 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004845 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004846 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004847
Matt Ropera1de91e2016-05-12 07:05:57 -07004848 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004849 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004850
Matt Roperd8e87492018-12-11 09:31:07 -08004851 /*
4852 * Find the highest watermark level for which we can satisfy the block
4853 * requirement of active planes.
4854 */
4855 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004856 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004857 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004858 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004859 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004860
4861 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05304862 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304863 drm_WARN_ON(&dev_priv->drm,
4864 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004865 blocks = U32_MAX;
4866 break;
4867 }
4868 continue;
4869 }
4870
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004871 blocks += wm->wm[level].min_ddb_alloc;
4872 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004873 }
4874
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004875 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004876 alloc_size -= blocks;
4877 break;
4878 }
4879 }
4880
4881 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03004882 drm_dbg_kms(&dev_priv->drm,
4883 "Requested display configuration exceeds system DDB limitations");
4884 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
4885 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08004886 return -EINVAL;
4887 }
4888
4889 /*
4890 * Grant each plane the blocks it requires at the highest achievable
4891 * watermark level, plus an extra share of the leftover blocks
4892 * proportional to its relative data rate.
4893 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004894 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004895 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004896 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004897 u64 rate;
4898 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004899
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004900 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004901 continue;
4902
Damien Lespiaub9cec072014-11-04 17:06:43 +00004903 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004904 * We've accounted for all active planes; remaining planes are
4905 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004906 */
Matt Roperd8e87492018-12-11 09:31:07 -08004907 if (total_data_rate == 0)
4908 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004909
Ville Syrjäläab016302020-11-06 19:30:41 +02004910 rate = crtc_state->plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004911 extra = min_t(u16, alloc_size,
4912 DIV64_U64_ROUND_UP(alloc_size * rate,
4913 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004914 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004915 alloc_size -= extra;
4916 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004917
Matt Roperd8e87492018-12-11 09:31:07 -08004918 if (total_data_rate == 0)
4919 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004920
Ville Syrjäläab016302020-11-06 19:30:41 +02004921 rate = crtc_state->uv_plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004922 extra = min_t(u16, alloc_size,
4923 DIV64_U64_ROUND_UP(alloc_size * rate,
4924 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004925 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004926 alloc_size -= extra;
4927 total_data_rate -= rate;
4928 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304929 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08004930
4931 /* Set the actual DDB start/end points for each plane */
4932 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004933 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004934 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004935 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004936 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004937 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004938
4939 if (plane_id == PLANE_CURSOR)
4940 continue;
4941
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004942 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304943 drm_WARN_ON(&dev_priv->drm,
Matt Roper7dadd282021-03-19 21:42:43 -07004944 DISPLAY_VER(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004945
Matt Roperd8e87492018-12-11 09:31:07 -08004946 /* Leave disabled planes at (0,0) */
4947 if (total[plane_id]) {
4948 plane_alloc->start = start;
4949 start += total[plane_id];
4950 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004951 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004952
Matt Roperd8e87492018-12-11 09:31:07 -08004953 if (uv_total[plane_id]) {
4954 uv_plane_alloc->start = start;
4955 start += uv_total[plane_id];
4956 uv_plane_alloc->end = start;
4957 }
4958 }
4959
4960 /*
4961 * When we calculated watermark values we didn't know how high
4962 * of a level we'd actually be able to hit, so we just marked
4963 * all levels as "enabled." Go back now and disable the ones
4964 * that aren't actually possible.
4965 */
4966 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004967 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004968 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004969 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004970
Ville Syrjäläa5941b42021-03-05 17:36:09 +02004971 skl_check_nv12_wm_level(&wm->wm[level], &wm->uv_wm[level],
4972 total[plane_id], uv_total[plane_id]);
Ville Syrjälä290248c2019-02-13 18:54:24 +02004973
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004974 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004975 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004976 * Underruns with WM1+ disabled
4977 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004978 if (DISPLAY_VER(dev_priv) == 11 &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02004979 level == 1 && wm->wm[0].enable) {
4980 wm->wm[level].blocks = wm->wm[0].blocks;
4981 wm->wm[level].lines = wm->wm[0].lines;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004982 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004983 }
Matt Roperd8e87492018-12-11 09:31:07 -08004984 }
4985 }
4986
4987 /*
Ville Syrjälädf4a50a2021-02-26 17:31:59 +02004988 * Go back and disable the transition and SAGV watermarks
4989 * if it turns out we don't have enough DDB blocks for them.
Matt Roperd8e87492018-12-11 09:31:07 -08004990 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004991 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004992 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004993 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004994
Ville Syrjäläa5941b42021-03-05 17:36:09 +02004995 skl_check_wm_level(&wm->trans_wm, total[plane_id]);
4996 skl_check_wm_level(&wm->sagv.wm0, total[plane_id]);
4997 skl_check_wm_level(&wm->sagv.trans_wm, total[plane_id]);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004998 }
4999
Matt Roperc107acf2016-05-12 07:06:01 -07005000 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005001}
5002
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005003/*
5004 * 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 +02005005 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005006 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5007 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5008*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005009static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005010skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5011 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005012{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005013 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305014 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005015
5016 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305017 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005018
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305019 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005020 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005021
Matt Roper2b5a4562021-03-22 16:38:40 -07005022 if (DISPLAY_VER(dev_priv) >= 10)
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005023 ret = add_fixed16_u32(ret, 1);
5024
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005025 return ret;
5026}
5027
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005028static uint_fixed_16_16_t
5029skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5030 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005031{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005032 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305033 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005034
5035 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305036 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005037
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005038 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305039 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5040 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305041 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005042 return ret;
5043}
5044
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305045static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005046intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305047{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305048 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005049 u32 pixel_rate;
5050 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305051 uint_fixed_16_16_t linetime_us;
5052
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005053 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305054 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305055
Maarten Lankhorstec193642019-06-28 10:55:17 +02005056 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305057
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305058 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305059 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305060
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005061 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305062 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305063
5064 return linetime_us;
5065}
5066
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305067static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005068skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5069 int width, const struct drm_format_info *format,
5070 u64 modifier, unsigned int rotation,
5071 u32 plane_pixel_rate, struct skl_wm_params *wp,
5072 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305073{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005074 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005076 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305077
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305078 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005079 if (color_plane == 1 &&
5080 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005081 drm_dbg_kms(&dev_priv->drm,
5082 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305083 return -EINVAL;
5084 }
5085
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005086 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5087 modifier == I915_FORMAT_MOD_Yf_TILED ||
5088 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5089 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5090 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5091 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5092 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005093 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305094
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005095 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005096 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305097 wp->width /= 2;
5098
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005099 wp->cpp = format->cpp[color_plane];
5100 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305101
Matt Roper7dadd282021-03-19 21:42:43 -07005102 if (DISPLAY_VER(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005103 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005104 wp->dbuf_block_size = 256;
5105 else
5106 wp->dbuf_block_size = 512;
5107
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005108 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305109 switch (wp->cpp) {
5110 case 1:
5111 wp->y_min_scanlines = 16;
5112 break;
5113 case 2:
5114 wp->y_min_scanlines = 8;
5115 break;
5116 case 4:
5117 wp->y_min_scanlines = 4;
5118 break;
5119 default:
5120 MISSING_CASE(wp->cpp);
5121 return -EINVAL;
5122 }
5123 } else {
5124 wp->y_min_scanlines = 4;
5125 }
5126
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005127 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305128 wp->y_min_scanlines *= 2;
5129
5130 wp->plane_bytes_per_line = wp->width * wp->cpp;
5131 if (wp->y_tiled) {
5132 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005133 wp->y_min_scanlines,
5134 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305135
Matt Roper2b5a4562021-03-22 16:38:40 -07005136 if (DISPLAY_VER(dev_priv) >= 10)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305137 interm_pbpl++;
5138
5139 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5140 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305141 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005142 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005143 wp->dbuf_block_size);
5144
Matt Roper2b5a4562021-03-22 16:38:40 -07005145 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005146 interm_pbpl++;
5147
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305148 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5149 }
5150
5151 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5152 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005153
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305154 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005155 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305156
5157 return 0;
5158}
5159
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005160static int
5161skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5162 const struct intel_plane_state *plane_state,
5163 struct skl_wm_params *wp, int color_plane)
5164{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005165 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005166 int width;
5167
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005168 /*
5169 * Src coordinates are already rotated by 270 degrees for
5170 * the 90/270 degree plane rotation cases (to match the
5171 * GTT mapping), hence no need to account for rotation here.
5172 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005173 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005174
5175 return skl_compute_wm_params(crtc_state, width,
5176 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005177 plane_state->hw.rotation,
Ville Syrjälä3df3fe22020-11-06 19:30:42 +02005178 intel_plane_pixel_rate(crtc_state, plane_state),
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005179 wp, color_plane);
5180}
5181
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005182static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5183{
Matt Roper2b5a4562021-03-22 16:38:40 -07005184 if (DISPLAY_VER(dev_priv) >= 10)
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005185 return true;
5186
5187 /* The number of lines are ignored for the level 0 watermark. */
5188 return level > 0;
5189}
5190
Matt Roper1003cee2021-05-14 08:36:54 -07005191static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5192{
5193 if (DISPLAY_VER(dev_priv) >= 13)
5194 return 255;
5195 else
5196 return 31;
5197}
5198
Maarten Lankhorstec193642019-06-28 10:55:17 +02005199static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005200 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005201 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005202 const struct skl_wm_params *wp,
5203 const struct skl_wm_level *result_prev,
5204 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005205{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005206 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305207 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305208 uint_fixed_16_16_t selected_result;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005209 u32 blocks, lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005210
Ville Syrjälä0aded172019-02-05 17:50:53 +02005211 if (latency == 0) {
5212 /* reject it */
5213 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005214 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005215 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005216
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005217 /*
5218 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5219 * Display WA #1141: kbl,cfl
5220 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005221 if ((IS_KABYLAKE(dev_priv) ||
5222 IS_COFFEELAKE(dev_priv) ||
5223 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005224 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305225 latency += 4;
5226
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005227 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005228 latency += 15;
5229
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305230 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005231 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305232 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005233 crtc_state->hw.pipe_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005234 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305235 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005236
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305237 if (wp->y_tiled) {
5238 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005239 } else {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005240 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005241 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005242 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005243 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005244 } else if (latency >= wp->linetime_us) {
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005245 if (DISPLAY_VER(dev_priv) == 9)
Paulo Zanoni077b5822018-10-04 16:15:57 -07005246 selected_result = min_fixed16(method1, method2);
5247 else
5248 selected_result = method2;
5249 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005250 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005251 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005252 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005253
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005254 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5255 lines = div_round_up_fixed16(selected_result,
5256 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005257
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005258 if (DISPLAY_VER(dev_priv) == 9) {
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005259 /* Display WA #1125: skl,bxt,kbl */
5260 if (level == 0 && wp->rc_surface)
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005261 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005262
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005263 /* Display WA #1126: skl,bxt,kbl */
5264 if (level >= 1 && level <= 7) {
5265 if (wp->y_tiled) {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005266 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5267 lines += wp->y_min_scanlines;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005268 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005269 blocks++;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005270 }
5271
5272 /*
5273 * Make sure result blocks for higher latency levels are
5274 * atleast as high as level below the current level.
5275 * Assumption in DDB algorithm optimization for special
5276 * cases. Also covers Display WA #1125 for RC.
5277 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005278 if (result_prev->blocks > blocks)
5279 blocks = result_prev->blocks;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005280 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005281 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005282
Matt Roper7dadd282021-03-19 21:42:43 -07005283 if (DISPLAY_VER(dev_priv) >= 11) {
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005284 if (wp->y_tiled) {
5285 int extra_lines;
5286
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005287 if (lines % wp->y_min_scanlines == 0)
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005288 extra_lines = wp->y_min_scanlines;
5289 else
5290 extra_lines = wp->y_min_scanlines * 2 -
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005291 lines % wp->y_min_scanlines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005292
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005293 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005294 wp->plane_blocks_per_line);
5295 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005296 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005297 }
5298 }
5299
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005300 if (!skl_wm_has_lines(dev_priv, level))
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005301 lines = 0;
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005302
Matt Roper1003cee2021-05-14 08:36:54 -07005303 if (lines > skl_wm_max_lines(dev_priv)) {
Ville Syrjälä0aded172019-02-05 17:50:53 +02005304 /* reject it */
5305 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005306 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005307 }
Matt Roperd8e87492018-12-11 09:31:07 -08005308
5309 /*
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005310 * If lines is valid, assume we can use this watermark level
Matt Roperd8e87492018-12-11 09:31:07 -08005311 * for now. We'll come back and disable it after we calculate the
5312 * DDB allocation if it turns out we don't actually have enough
5313 * blocks to satisfy it.
5314 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005315 result->blocks = blocks;
5316 result->lines = lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005317 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005318 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5319 result->enable = true;
Ville Syrjälä9c312122020-11-06 19:30:40 +02005320
Matt Roper7dadd282021-03-19 21:42:43 -07005321 if (DISPLAY_VER(dev_priv) < 12)
Ville Syrjälä9c312122020-11-06 19:30:40 +02005322 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005323}
5324
Matt Roperd8e87492018-12-11 09:31:07 -08005325static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005326skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305327 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005328 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005329{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005330 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305331 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005332 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005333
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305334 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005335 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005336 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305337
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005338 skl_compute_plane_wm(crtc_state, level, latency,
5339 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005340
5341 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305342 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005343}
5344
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005345static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5346 const struct skl_wm_params *wm_params,
5347 struct skl_plane_wm *plane_wm)
5348{
5349 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005350 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005351 struct skl_wm_level *levels = plane_wm->wm;
5352 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5353
5354 skl_compute_plane_wm(crtc_state, 0, latency,
5355 wm_params, &levels[0],
5356 sagv_wm);
5357}
5358
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005359static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5360 struct skl_wm_level *trans_wm,
5361 const struct skl_wm_level *wm0,
5362 const struct skl_wm_params *wp)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005363{
Ville Syrjäläc834d032020-02-28 22:35:52 +02005364 u16 trans_min, trans_amount, trans_y_tile_min;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005365 u16 wm0_blocks, trans_offset, blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005366
Kumar, Maheshca476672017-08-17 19:15:24 +05305367 /* Transition WM don't make any sense if ipc is disabled */
5368 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005369 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305370
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005371 /*
5372 * WaDisableTWM:skl,kbl,cfl,bxt
5373 * Transition WM are not recommended by HW team for GEN9
5374 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005375 if (DISPLAY_VER(dev_priv) == 9)
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005376 return;
5377
Matt Roper7dadd282021-03-19 21:42:43 -07005378 if (DISPLAY_VER(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305379 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005380 else
5381 trans_min = 14;
5382
5383 /* Display WA #1140: glk,cnl */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005384 if (DISPLAY_VER(dev_priv) == 10)
Ville Syrjäläc834d032020-02-28 22:35:52 +02005385 trans_amount = 0;
5386 else
5387 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305388
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005389 trans_offset = trans_min + trans_amount;
Kumar, Maheshca476672017-08-17 19:15:24 +05305390
Paulo Zanonicbacc792018-10-04 16:15:58 -07005391 /*
5392 * The spec asks for Selected Result Blocks for wm0 (the real value),
5393 * not Result Blocks (the integer value). Pay attention to the capital
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005394 * letters. The value wm_l0->blocks is actually Result Blocks, but
Paulo Zanonicbacc792018-10-04 16:15:58 -07005395 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5396 * and since we later will have to get the ceiling of the sum in the
5397 * transition watermarks calculation, we can just pretend Selected
5398 * Result Blocks is Result Blocks minus 1 and it should work for the
5399 * current platforms.
5400 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005401 wm0_blocks = wm0->blocks - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005402
Kumar, Maheshca476672017-08-17 19:15:24 +05305403 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005404 trans_y_tile_min =
5405 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005406 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305407 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005408 blocks = wm0_blocks + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305409 }
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005410 blocks++;
Kumar, Maheshca476672017-08-17 19:15:24 +05305411
Matt Roperd8e87492018-12-11 09:31:07 -08005412 /*
5413 * Just assume we can enable the transition watermark. After
5414 * computing the DDB we'll come back and disable it if that
5415 * assumption turns out to be false.
5416 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005417 trans_wm->blocks = blocks;
5418 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5419 trans_wm->enable = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005420}
5421
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005422static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005423 const struct intel_plane_state *plane_state,
5424 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005425{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005426 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5427 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälädbf71382020-11-06 19:30:38 +02005428 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005429 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005430 int ret;
5431
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005432 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005433 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005434 if (ret)
5435 return ret;
5436
Ville Syrjälä67155a62019-03-12 22:58:37 +02005437 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005438
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005439 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5440 &wm->wm[0], &wm_params);
5441
Matt Roper7dadd282021-03-19 21:42:43 -07005442 if (DISPLAY_VER(dev_priv) >= 12) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005443 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5444
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005445 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5446 &wm->sagv.wm0, &wm_params);
5447 }
Ville Syrjälä83158472018-11-27 18:57:26 +02005448
5449 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005450}
5451
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005452static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005453 const struct intel_plane_state *plane_state,
5454 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005455{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005456 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005457 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005458 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005459
Ville Syrjälä83158472018-11-27 18:57:26 +02005460 wm->is_planar = true;
5461
5462 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005463 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005464 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005465 if (ret)
5466 return ret;
5467
Ville Syrjälä67155a62019-03-12 22:58:37 +02005468 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005469
5470 return 0;
5471}
5472
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005473static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005474 const struct intel_plane_state *plane_state)
5475{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005476 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä83158472018-11-27 18:57:26 +02005477 enum plane_id plane_id = plane->id;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005478 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5479 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005480 int ret;
5481
Ville Syrjälädbf71382020-11-06 19:30:38 +02005482 memset(wm, 0, sizeof(*wm));
5483
Ville Syrjälä83158472018-11-27 18:57:26 +02005484 if (!intel_wm_plane_visible(crtc_state, plane_state))
5485 return 0;
5486
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005487 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005488 plane_id, 0);
5489 if (ret)
5490 return ret;
5491
5492 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005493 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005494 plane_id);
5495 if (ret)
5496 return ret;
5497 }
5498
5499 return 0;
5500}
5501
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005502static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005503 const struct intel_plane_state *plane_state)
5504{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005505 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5506 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5507 enum plane_id plane_id = plane->id;
5508 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005509 int ret;
5510
5511 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005512 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005513 return 0;
5514
Ville Syrjäläf99b8052021-03-27 02:59:45 +02005515 memset(wm, 0, sizeof(*wm));
5516
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005517 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005518 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005519 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005520
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305521 drm_WARN_ON(&dev_priv->drm,
5522 !intel_wm_plane_visible(crtc_state, plane_state));
5523 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5524 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005525
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005526 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005527 y_plane_id, 0);
5528 if (ret)
5529 return ret;
5530
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005531 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005532 plane_id, 1);
5533 if (ret)
5534 return ret;
5535 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005536 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005537 plane_id, 0);
5538 if (ret)
5539 return ret;
5540 }
5541
5542 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005543}
5544
Ville Syrjäläffc90032020-11-06 19:30:37 +02005545static int skl_build_pipe_wm(struct intel_atomic_state *state,
5546 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005547{
Ville Syrjäläffc90032020-11-06 19:30:37 +02005548 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5549 struct intel_crtc_state *crtc_state =
5550 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005551 const struct intel_plane_state *plane_state;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005552 struct intel_plane *plane;
5553 int ret, i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005554
Ville Syrjälädbf71382020-11-06 19:30:38 +02005555 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5556 /*
5557 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5558 * instead but we don't populate that correctly for NV12 Y
5559 * planes so for now hack this.
5560 */
5561 if (plane->pipe != crtc->pipe)
5562 continue;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305563
Matt Roper7dadd282021-03-19 21:42:43 -07005564 if (DISPLAY_VER(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005565 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005566 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005567 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305568 if (ret)
5569 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005570 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305571
Ville Syrjälädbf71382020-11-06 19:30:38 +02005572 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5573
Matt Roper55994c22016-05-12 07:06:08 -07005574 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005575}
5576
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005577static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5578 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005579 const struct skl_ddb_entry *entry)
5580{
5581 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005582 intel_de_write_fw(dev_priv, reg,
5583 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005584 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005585 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005586}
5587
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005588static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5589 i915_reg_t reg,
5590 const struct skl_wm_level *level)
5591{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005592 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005593
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005594 if (level->enable)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005595 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005596 if (level->ignore_lines)
5597 val |= PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005598 val |= level->blocks;
Matt Roper1003cee2021-05-14 08:36:54 -07005599 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005600
Jani Nikula9b6320a2020-01-23 16:00:04 +02005601 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005602}
5603
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005604void skl_write_plane_wm(struct intel_plane *plane,
5605 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005606{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005607 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005608 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005609 enum plane_id plane_id = plane->id;
5610 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005611 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5612 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005613 const struct skl_ddb_entry *ddb_y =
5614 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5615 const struct skl_ddb_entry *ddb_uv =
5616 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005617
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005618 for (level = 0; level <= max_level; level++)
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005619 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005620 skl_plane_wm_level(pipe_wm, plane_id, level));
5621
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005622 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005623 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005624
Matt Roper7959ffe2021-05-18 17:06:11 -07005625 if (HAS_HW_SAGV_WM(dev_priv)) {
5626 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5627 &wm->sagv.wm0);
5628 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5629 &wm->sagv.trans_wm);
5630 }
5631
Matt Roper7dadd282021-03-19 21:42:43 -07005632 if (DISPLAY_VER(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005633 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005634 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5635 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305636 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005637
5638 if (wm->is_planar)
5639 swap(ddb_y, ddb_uv);
5640
5641 skl_ddb_entry_write(dev_priv,
5642 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5643 skl_ddb_entry_write(dev_priv,
5644 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005645}
5646
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005647void skl_write_cursor_wm(struct intel_plane *plane,
5648 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005649{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005650 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005651 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005652 enum plane_id plane_id = plane->id;
5653 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005654 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005655 const struct skl_ddb_entry *ddb =
5656 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005657
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005658 for (level = 0; level <= max_level; level++)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005659 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005660 skl_plane_wm_level(pipe_wm, plane_id, level));
5661
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005662 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5663 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005664
Matt Roper7959ffe2021-05-18 17:06:11 -07005665 if (HAS_HW_SAGV_WM(dev_priv)) {
5666 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5667
5668 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5669 &wm->sagv.wm0);
5670 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5671 &wm->sagv.trans_wm);
5672 }
5673
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005674 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005675}
5676
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005677bool skl_wm_level_equals(const struct skl_wm_level *l1,
5678 const struct skl_wm_level *l2)
5679{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005680 return l1->enable == l2->enable &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005681 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005682 l1->lines == l2->lines &&
5683 l1->blocks == l2->blocks;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005684}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005685
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005686static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5687 const struct skl_plane_wm *wm1,
5688 const struct skl_plane_wm *wm2)
5689{
5690 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005691
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005692 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005693 /*
5694 * We don't check uv_wm as the hardware doesn't actually
5695 * use it. It only gets used for calculating the required
5696 * ddb allocation.
5697 */
5698 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005699 return false;
5700 }
5701
Ville Syrjäläf11449d2021-02-26 17:32:00 +02005702 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005703 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5704 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005705}
5706
Jani Nikula81b55ef2020-04-20 17:04:38 +03005707static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5708 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005709{
Lyude27082492016-08-24 07:48:10 +02005710 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005711}
5712
Ville Syrjälä33c9c502021-01-22 22:56:33 +02005713static void skl_ddb_entry_union(struct skl_ddb_entry *a,
5714 const struct skl_ddb_entry *b)
5715{
5716 if (a->end && b->end) {
5717 a->start = min(a->start, b->start);
5718 a->end = max(a->end, b->end);
5719 } else if (b->end) {
5720 a->start = b->start;
5721 a->end = b->end;
5722 }
5723}
5724
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005725bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005726 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005727 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005728{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005729 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005730
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005731 for (i = 0; i < num_entries; i++) {
5732 if (i != ignore_idx &&
5733 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005734 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005735 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005736
Lyude27082492016-08-24 07:48:10 +02005737 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005738}
5739
Jani Nikulabb7791b2016-10-04 12:29:17 +03005740static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005741skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5742 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005743{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005744 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
5745 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005746 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5747 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005748
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005749 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5750 struct intel_plane_state *plane_state;
5751 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005752
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005753 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5754 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5755 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5756 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005757 continue;
5758
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005759 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005760 if (IS_ERR(plane_state))
5761 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005762
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005763 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005764 }
5765
5766 return 0;
5767}
5768
Ville Syrjäläef79d622021-01-22 22:56:32 +02005769static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
5770{
5771 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
5772 u8 enabled_slices;
5773 enum pipe pipe;
5774
5775 /*
5776 * FIXME: For now we always enable slice S1 as per
5777 * the Bspec display initialization sequence.
5778 */
5779 enabled_slices = BIT(DBUF_S1);
5780
5781 for_each_pipe(dev_priv, pipe)
5782 enabled_slices |= dbuf_state->slices[pipe];
5783
5784 return enabled_slices;
5785}
5786
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005787static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005788skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005789{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005790 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5791 const struct intel_dbuf_state *old_dbuf_state;
Ville Syrjäläef79d622021-01-22 22:56:32 +02005792 struct intel_dbuf_state *new_dbuf_state = NULL;
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005793 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005794 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305795 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305796 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005797
Ville Syrjäläef79d622021-01-22 22:56:32 +02005798 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
5799 new_dbuf_state = intel_atomic_get_dbuf_state(state);
5800 if (IS_ERR(new_dbuf_state))
5801 return PTR_ERR(new_dbuf_state);
5802
5803 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
5804 break;
5805 }
5806
5807 if (!new_dbuf_state)
5808 return 0;
5809
5810 new_dbuf_state->active_pipes =
5811 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
5812
5813 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
5814 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
5815 if (ret)
5816 return ret;
5817 }
5818
5819 for_each_intel_crtc(&dev_priv->drm, crtc) {
5820 enum pipe pipe = crtc->pipe;
5821
5822 new_dbuf_state->slices[pipe] =
5823 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes);
5824
5825 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
5826 continue;
5827
5828 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
5829 if (ret)
5830 return ret;
5831 }
5832
5833 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
5834
5835 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices) {
5836 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
5837 if (ret)
5838 return ret;
5839
5840 drm_dbg_kms(&dev_priv->drm,
Ville Syrjäläb88da662021-04-16 20:10:09 +03005841 "Enabled dbuf slices 0x%x -> 0x%x (total dbuf slices 0x%x)\n",
Ville Syrjäläef79d622021-01-22 22:56:32 +02005842 old_dbuf_state->enabled_slices,
5843 new_dbuf_state->enabled_slices,
Ville Syrjäläb88da662021-04-16 20:10:09 +03005844 INTEL_INFO(dev_priv)->dbuf.slice_mask);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005845 }
5846
5847 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
5848 enum pipe pipe = crtc->pipe;
5849
5850 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
5851
5852 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
5853 continue;
5854
5855 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
5856 if (ret)
5857 return ret;
5858 }
5859
5860 for_each_intel_crtc(&dev_priv->drm, crtc) {
5861 ret = skl_crtc_allocate_ddb(state, crtc);
5862 if (ret)
5863 return ret;
5864 }
5865
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005866 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005867 new_crtc_state, i) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02005868 ret = skl_allocate_plane_ddb(state, crtc);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005869 if (ret)
5870 return ret;
5871
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005872 ret = skl_ddb_add_affected_planes(old_crtc_state,
5873 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005874 if (ret)
5875 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005876 }
5877
5878 return 0;
5879}
5880
Ville Syrjäläab98e942019-02-08 22:05:27 +02005881static char enast(bool enable)
5882{
5883 return enable ? '*' : ' ';
5884}
5885
Matt Roper2722efb2016-08-17 15:55:55 -04005886static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005887skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005888{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005889 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5890 const struct intel_crtc_state *old_crtc_state;
5891 const struct intel_crtc_state *new_crtc_state;
5892 struct intel_plane *plane;
5893 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005894 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005895
Jani Nikulabdbf43d2019-10-28 12:38:15 +02005896 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02005897 return;
5898
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005899 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5900 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005901 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5902
5903 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5904 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5905
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005906 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5907 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005908 const struct skl_ddb_entry *old, *new;
5909
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005910 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5911 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005912
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005913 if (skl_ddb_entry_equal(old, new))
5914 continue;
5915
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005916 drm_dbg_kms(&dev_priv->drm,
5917 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
5918 plane->base.base.id, plane->base.name,
5919 old->start, old->end, new->start, new->end,
5920 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005921 }
5922
5923 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5924 enum plane_id plane_id = plane->id;
5925 const struct skl_plane_wm *old_wm, *new_wm;
5926
5927 old_wm = &old_pipe_wm->planes[plane_id];
5928 new_wm = &new_pipe_wm->planes[plane_id];
5929
5930 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5931 continue;
5932
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005933 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005934 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
5935 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005936 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005937 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
5938 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
5939 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
5940 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
5941 enast(old_wm->trans_wm.enable),
5942 enast(old_wm->sagv.wm0.enable),
5943 enast(old_wm->sagv.trans_wm.enable),
5944 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
5945 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
5946 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
5947 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
5948 enast(new_wm->trans_wm.enable),
5949 enast(new_wm->sagv.wm0.enable),
5950 enast(new_wm->sagv.trans_wm.enable));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005951
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005952 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005953 "[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"
5954 " -> %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 +03005955 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005956 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
5957 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
5958 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
5959 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
5960 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
5961 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
5962 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
5963 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
5964 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
5965 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
5966 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
5967 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
5968 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
5969 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
5970 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
5971 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
5972 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
5973 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
5974 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
5975 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
5976 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
5977 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005978
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005979 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005980 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
5981 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005982 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005983 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
5984 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
5985 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
5986 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
5987 old_wm->trans_wm.blocks,
5988 old_wm->sagv.wm0.blocks,
5989 old_wm->sagv.trans_wm.blocks,
5990 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
5991 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
5992 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
5993 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
5994 new_wm->trans_wm.blocks,
5995 new_wm->sagv.wm0.blocks,
5996 new_wm->sagv.trans_wm.blocks);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005997
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005998 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005999 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6000 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006001 plane->base.base.id, plane->base.name,
6002 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6003 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6004 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6005 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6006 old_wm->trans_wm.min_ddb_alloc,
Ville Syrjäläa68aa482021-02-26 17:32:01 +02006007 old_wm->sagv.wm0.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006008 old_wm->sagv.trans_wm.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006009 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6010 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6011 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6012 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006013 new_wm->trans_wm.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006014 new_wm->sagv.wm0.min_ddb_alloc,
6015 new_wm->sagv.trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006016 }
6017 }
6018}
6019
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006020static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6021 const struct skl_pipe_wm *old_pipe_wm,
6022 const struct skl_pipe_wm *new_pipe_wm)
6023{
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006024 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6025 int level, max_level = ilk_wm_max_level(i915);
6026
6027 for (level = 0; level <= max_level; level++) {
6028 /*
6029 * We don't check uv_wm as the hardware doesn't actually
6030 * use it. It only gets used for calculating the required
6031 * ddb allocation.
6032 */
Ville Syrjälä93fe8622021-03-25 02:44:14 +02006033 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6034 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006035 return false;
6036 }
6037
Matt Roper7959ffe2021-05-18 17:06:11 -07006038 if (HAS_HW_SAGV_WM(i915)) {
6039 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6040 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6041
6042 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6043 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6044 return false;
6045 }
6046
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006047 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6048 skl_plane_trans_wm(new_pipe_wm, plane->id));
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006049}
6050
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006051/*
6052 * To make sure the cursor watermark registers are always consistent
6053 * with our computed state the following scenario needs special
6054 * treatment:
6055 *
6056 * 1. enable cursor
6057 * 2. move cursor entirely offscreen
6058 * 3. disable cursor
6059 *
6060 * Step 2. does call .disable_plane() but does not zero the watermarks
6061 * (since we consider an offscreen cursor still active for the purposes
6062 * of watermarks). Step 3. would not normally call .disable_plane()
6063 * because the actual plane visibility isn't changing, and we don't
6064 * deallocate the cursor ddb until the pipe gets disabled. So we must
6065 * force step 3. to call .disable_plane() to update the watermark
6066 * registers properly.
6067 *
6068 * Other planes do not suffer from this issues as their watermarks are
6069 * calculated based on the actual plane visibility. The only time this
6070 * can trigger for the other planes is during the initial readout as the
6071 * default value of the watermarks registers is not zero.
6072 */
6073static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6074 struct intel_crtc *crtc)
6075{
6076 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6077 const struct intel_crtc_state *old_crtc_state =
6078 intel_atomic_get_old_crtc_state(state, crtc);
6079 struct intel_crtc_state *new_crtc_state =
6080 intel_atomic_get_new_crtc_state(state, crtc);
6081 struct intel_plane *plane;
6082
6083 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6084 struct intel_plane_state *plane_state;
6085 enum plane_id plane_id = plane->id;
6086
6087 /*
6088 * Force a full wm update for every plane on modeset.
6089 * Required because the reset value of the wm registers
6090 * is non-zero, whereas we want all disabled planes to
6091 * have zero watermarks. So if we turn off the relevant
6092 * power well the hardware state will go out of sync
6093 * with the software state.
6094 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006095 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006096 skl_plane_selected_wm_equals(plane,
6097 &old_crtc_state->wm.skl.optimal,
6098 &new_crtc_state->wm.skl.optimal))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006099 continue;
6100
6101 plane_state = intel_atomic_get_plane_state(state, plane);
6102 if (IS_ERR(plane_state))
6103 return PTR_ERR(plane_state);
6104
6105 new_crtc_state->update_planes |= BIT(plane_id);
6106 }
6107
6108 return 0;
6109}
6110
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306111static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006112skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306113{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006114 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006115 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306116 int ret, i;
6117
Ville Syrjäläffc90032020-11-06 19:30:37 +02006118 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6119 ret = skl_build_pipe_wm(state, crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006120 if (ret)
6121 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006122 }
6123
Matt Roperd8e87492018-12-11 09:31:07 -08006124 ret = skl_compute_ddb(state);
6125 if (ret)
6126 return ret;
6127
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006128 ret = intel_compute_sagv_mask(state);
6129 if (ret)
6130 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006131
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006132 /*
6133 * skl_compute_ddb() will have adjusted the final watermarks
6134 * based on how much ddb is available. Now we can actually
6135 * check if the final watermarks changed.
6136 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006137 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006138 ret = skl_wm_add_affected_planes(state, crtc);
6139 if (ret)
6140 return ret;
6141 }
6142
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006143 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006144
Matt Roper98d39492016-05-12 07:06:03 -07006145 return 0;
6146}
6147
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006148static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006149 struct intel_wm_config *config)
6150{
6151 struct intel_crtc *crtc;
6152
6153 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006154 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006155 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6156
6157 if (!wm->pipe_enabled)
6158 continue;
6159
6160 config->sprites_enabled |= wm->sprites_enabled;
6161 config->sprites_scaled |= wm->sprites_scaled;
6162 config->num_pipes_active++;
6163 }
6164}
6165
Matt Ropered4a6a72016-02-23 17:20:13 -08006166static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006167{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006168 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006169 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006170 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006171 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006172 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006173
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006174 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006175
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006176 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6177 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006178
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006179 /* 5/6 split only in single pipe config on IVB+ */
Matt Roper7dadd282021-03-19 21:42:43 -07006180 if (DISPLAY_VER(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006181 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006182 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6183 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006184
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006185 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006186 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006187 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006188 }
6189
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006190 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006191 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006192
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006193 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006194
Imre Deak820c1982013-12-17 14:46:36 +02006195 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006196}
6197
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006198static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006199 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006200{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006201 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6202 const struct intel_crtc_state *crtc_state =
6203 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006204
Matt Ropered4a6a72016-02-23 17:20:13 -08006205 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006206 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006207 ilk_program_watermarks(dev_priv);
6208 mutex_unlock(&dev_priv->wm.wm_mutex);
6209}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006210
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006211static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006212 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006213{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006214 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6215 const struct intel_crtc_state *crtc_state =
6216 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006217
6218 if (!crtc_state->wm.need_postvbl_update)
6219 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006220
6221 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006222 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6223 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006224 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006225}
6226
Jani Nikula81b55ef2020-04-20 17:04:38 +03006227static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006228{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006229 level->enable = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006230 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006231 level->blocks = val & PLANE_WM_BLOCKS_MASK;
Matt Roper1003cee2021-05-14 08:36:54 -07006232 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
Pradeep Bhat30789992014-11-04 17:06:45 +00006233}
6234
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006235void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006236 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006237{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006238 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6239 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006240 int level, max_level;
6241 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006242 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006243
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006244 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006245
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006246 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006247 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006248
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006249 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006250 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006251 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006252 else
Jani Nikula5f461662020-11-30 13:15:58 +02006253 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006254
6255 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6256 }
6257
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006258 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006259 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006260 else
Jani Nikula5f461662020-11-30 13:15:58 +02006261 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006262
6263 skl_wm_level_from_reg_val(val, &wm->trans_wm);
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006264
Matt Roper7959ffe2021-05-18 17:06:11 -07006265 if (HAS_HW_SAGV_WM(dev_priv)) {
6266 if (plane_id != PLANE_CURSOR)
6267 val = intel_uncore_read(&dev_priv->uncore,
6268 PLANE_WM_SAGV(pipe, plane_id));
6269 else
6270 val = intel_uncore_read(&dev_priv->uncore,
6271 CUR_WM_SAGV(pipe));
6272
6273 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6274
6275 if (plane_id != PLANE_CURSOR)
6276 val = intel_uncore_read(&dev_priv->uncore,
6277 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6278 else
6279 val = intel_uncore_read(&dev_priv->uncore,
6280 CUR_WM_SAGV_TRANS(pipe));
6281
6282 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6283 } else if (DISPLAY_VER(dev_priv) >= 12) {
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006284 wm->sagv.wm0 = wm->wm[0];
6285 wm->sagv.trans_wm = wm->trans_wm;
6286 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006287 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006288}
6289
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006290void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006291{
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006292 struct intel_dbuf_state *dbuf_state =
6293 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006294 struct intel_crtc *crtc;
Pradeep Bhat30789992014-11-04 17:06:45 +00006295
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006296 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006297 struct intel_crtc_state *crtc_state =
6298 to_intel_crtc_state(crtc->base.state);
6299 enum pipe pipe = crtc->pipe;
6300 enum plane_id plane_id;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006301
Maarten Lankhorstec193642019-06-28 10:55:17 +02006302 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
Ville Syrjälädbf71382020-11-06 19:30:38 +02006303 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006304
6305 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6306
6307 for_each_plane_id_on_crtc(crtc, plane_id) {
6308 struct skl_ddb_entry *ddb_y =
6309 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6310 struct skl_ddb_entry *ddb_uv =
6311 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
6312
6313 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6314 plane_id, ddb_y, ddb_uv);
6315
6316 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6317 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_uv);
6318 }
6319
6320 dbuf_state->slices[pipe] =
6321 skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes);
6322
6323 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6324
6325 crtc_state->wm.skl.ddb = dbuf_state->ddb[pipe];
6326
6327 drm_dbg_kms(&dev_priv->drm,
6328 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x\n",
6329 crtc->base.base.id, crtc->base.name,
6330 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
6331 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006332 }
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006333
6334 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
Pradeep Bhat30789992014-11-04 17:06:45 +00006335}
6336
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006337static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006338{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006339 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006340 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006341 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006342 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6343 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006344 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006345
Jani Nikula5f461662020-11-30 13:15:58 +02006346 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006347
Ville Syrjälä15606532016-05-13 17:55:17 +03006348 memset(active, 0, sizeof(*active));
6349
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006350 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006351
6352 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006353 u32 tmp = hw->wm_pipe[pipe];
6354
6355 /*
6356 * For active pipes LP0 watermark is marked as
6357 * enabled, and LP1+ watermaks as disabled since
6358 * we can't really reverse compute them in case
6359 * multiple pipes are active.
6360 */
6361 active->wm[0].enable = true;
6362 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6363 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6364 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006365 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006366 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006367
6368 /*
6369 * For inactive pipes, all watermark levels
6370 * should be marked as enabled but zeroed,
6371 * which is what we'd compute them to.
6372 */
6373 for (level = 0; level <= max_level; level++)
6374 active->wm[level].enable = true;
6375 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006376
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006377 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006378}
6379
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006380#define _FW_WM(value, plane) \
6381 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6382#define _FW_WM_VLV(value, plane) \
6383 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6384
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006385static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6386 struct g4x_wm_values *wm)
6387{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006388 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006389
Jani Nikula5f461662020-11-30 13:15:58 +02006390 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006391 wm->sr.plane = _FW_WM(tmp, SR);
6392 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6393 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6394 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6395
Jani Nikula5f461662020-11-30 13:15:58 +02006396 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006397 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6398 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6399 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6400 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6401 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6402 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6403
Jani Nikula5f461662020-11-30 13:15:58 +02006404 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006405 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6406 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6407 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6408 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6409}
6410
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006411static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6412 struct vlv_wm_values *wm)
6413{
6414 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006415 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006416
6417 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006418 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006419
Ville Syrjälä1b313892016-11-28 19:37:08 +02006420 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006421 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006422 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006423 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006424 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006425 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006426 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006427 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6428 }
6429
Jani Nikula5f461662020-11-30 13:15:58 +02006430 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006431 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006432 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6433 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6434 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006435
Jani Nikula5f461662020-11-30 13:15:58 +02006436 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006437 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6438 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6439 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006440
Jani Nikula5f461662020-11-30 13:15:58 +02006441 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006442 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6443
6444 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02006445 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006446 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6447 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006448
Jani Nikula5f461662020-11-30 13:15:58 +02006449 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006450 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6451 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006452
Jani Nikula5f461662020-11-30 13:15:58 +02006453 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006454 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6455 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006456
Jani Nikula5f461662020-11-30 13:15:58 +02006457 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006458 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006459 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6460 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6461 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6462 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6463 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6464 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6465 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6466 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6467 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006468 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02006469 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006470 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6471 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006472
Jani Nikula5f461662020-11-30 13:15:58 +02006473 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006474 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006475 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6476 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6477 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6478 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6479 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6480 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006481 }
6482}
6483
6484#undef _FW_WM
6485#undef _FW_WM_VLV
6486
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006487void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006488{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006489 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6490 struct intel_crtc *crtc;
6491
6492 g4x_read_wm_values(dev_priv, wm);
6493
Jani Nikula5f461662020-11-30 13:15:58 +02006494 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006495
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006496 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006497 struct intel_crtc_state *crtc_state =
6498 to_intel_crtc_state(crtc->base.state);
6499 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6500 struct g4x_pipe_wm *raw;
6501 enum pipe pipe = crtc->pipe;
6502 enum plane_id plane_id;
6503 int level, max_level;
6504
6505 active->cxsr = wm->cxsr;
6506 active->hpll_en = wm->hpll_en;
6507 active->fbc_en = wm->fbc_en;
6508
6509 active->sr = wm->sr;
6510 active->hpll = wm->hpll;
6511
6512 for_each_plane_id_on_crtc(crtc, plane_id) {
6513 active->wm.plane[plane_id] =
6514 wm->pipe[pipe].plane[plane_id];
6515 }
6516
6517 if (wm->cxsr && wm->hpll_en)
6518 max_level = G4X_WM_LEVEL_HPLL;
6519 else if (wm->cxsr)
6520 max_level = G4X_WM_LEVEL_SR;
6521 else
6522 max_level = G4X_WM_LEVEL_NORMAL;
6523
6524 level = G4X_WM_LEVEL_NORMAL;
6525 raw = &crtc_state->wm.g4x.raw[level];
6526 for_each_plane_id_on_crtc(crtc, plane_id)
6527 raw->plane[plane_id] = active->wm.plane[plane_id];
6528
6529 if (++level > max_level)
6530 goto out;
6531
6532 raw = &crtc_state->wm.g4x.raw[level];
6533 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6534 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6535 raw->plane[PLANE_SPRITE0] = 0;
6536 raw->fbc = active->sr.fbc;
6537
6538 if (++level > max_level)
6539 goto out;
6540
6541 raw = &crtc_state->wm.g4x.raw[level];
6542 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6543 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6544 raw->plane[PLANE_SPRITE0] = 0;
6545 raw->fbc = active->hpll.fbc;
6546
6547 out:
6548 for_each_plane_id_on_crtc(crtc, plane_id)
6549 g4x_raw_plane_wm_set(crtc_state, level,
6550 plane_id, USHRT_MAX);
6551 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6552
6553 crtc_state->wm.g4x.optimal = *active;
6554 crtc_state->wm.g4x.intermediate = *active;
6555
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006556 drm_dbg_kms(&dev_priv->drm,
6557 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6558 pipe_name(pipe),
6559 wm->pipe[pipe].plane[PLANE_PRIMARY],
6560 wm->pipe[pipe].plane[PLANE_CURSOR],
6561 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006562 }
6563
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006564 drm_dbg_kms(&dev_priv->drm,
6565 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6566 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6567 drm_dbg_kms(&dev_priv->drm,
6568 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6569 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6570 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6571 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006572}
6573
6574void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6575{
6576 struct intel_plane *plane;
6577 struct intel_crtc *crtc;
6578
6579 mutex_lock(&dev_priv->wm.wm_mutex);
6580
6581 for_each_intel_plane(&dev_priv->drm, plane) {
6582 struct intel_crtc *crtc =
6583 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6584 struct intel_crtc_state *crtc_state =
6585 to_intel_crtc_state(crtc->base.state);
6586 struct intel_plane_state *plane_state =
6587 to_intel_plane_state(plane->base.state);
6588 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6589 enum plane_id plane_id = plane->id;
6590 int level;
6591
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006592 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006593 continue;
6594
6595 for (level = 0; level < 3; level++) {
6596 struct g4x_pipe_wm *raw =
6597 &crtc_state->wm.g4x.raw[level];
6598
6599 raw->plane[plane_id] = 0;
6600 wm_state->wm.plane[plane_id] = 0;
6601 }
6602
6603 if (plane_id == PLANE_PRIMARY) {
6604 for (level = 0; level < 3; level++) {
6605 struct g4x_pipe_wm *raw =
6606 &crtc_state->wm.g4x.raw[level];
6607 raw->fbc = 0;
6608 }
6609
6610 wm_state->sr.fbc = 0;
6611 wm_state->hpll.fbc = 0;
6612 wm_state->fbc_en = false;
6613 }
6614 }
6615
6616 for_each_intel_crtc(&dev_priv->drm, crtc) {
6617 struct intel_crtc_state *crtc_state =
6618 to_intel_crtc_state(crtc->base.state);
6619
6620 crtc_state->wm.g4x.intermediate =
6621 crtc_state->wm.g4x.optimal;
6622 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6623 }
6624
6625 g4x_program_watermarks(dev_priv);
6626
6627 mutex_unlock(&dev_priv->wm.wm_mutex);
6628}
6629
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006630void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006631{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006632 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006633 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006634 u32 val;
6635
6636 vlv_read_wm_values(dev_priv, wm);
6637
Jani Nikula5f461662020-11-30 13:15:58 +02006638 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006639 wm->level = VLV_WM_LEVEL_PM2;
6640
6641 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006642 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006643
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006644 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006645 if (val & DSP_MAXFIFO_PM5_ENABLE)
6646 wm->level = VLV_WM_LEVEL_PM5;
6647
Ville Syrjälä58590c12015-09-08 21:05:12 +03006648 /*
6649 * If DDR DVFS is disabled in the BIOS, Punit
6650 * will never ack the request. So if that happens
6651 * assume we don't have to enable/disable DDR DVFS
6652 * dynamically. To test that just set the REQ_ACK
6653 * bit to poke the Punit, but don't change the
6654 * HIGH/LOW bits so that we don't actually change
6655 * the current state.
6656 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006657 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006658 val |= FORCE_DDR_FREQ_REQ_ACK;
6659 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6660
6661 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6662 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006663 drm_dbg_kms(&dev_priv->drm,
6664 "Punit not acking DDR DVFS request, "
6665 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006666 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6667 } else {
6668 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6669 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6670 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6671 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006672
Chris Wilson337fa6e2019-04-26 09:17:20 +01006673 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006674 }
6675
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006676 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006677 struct intel_crtc_state *crtc_state =
6678 to_intel_crtc_state(crtc->base.state);
6679 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6680 const struct vlv_fifo_state *fifo_state =
6681 &crtc_state->wm.vlv.fifo_state;
6682 enum pipe pipe = crtc->pipe;
6683 enum plane_id plane_id;
6684 int level;
6685
6686 vlv_get_fifo_size(crtc_state);
6687
6688 active->num_levels = wm->level + 1;
6689 active->cxsr = wm->cxsr;
6690
Ville Syrjäläff32c542017-03-02 19:14:57 +02006691 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006692 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006693 &crtc_state->wm.vlv.raw[level];
6694
6695 active->sr[level].plane = wm->sr.plane;
6696 active->sr[level].cursor = wm->sr.cursor;
6697
6698 for_each_plane_id_on_crtc(crtc, plane_id) {
6699 active->wm[level].plane[plane_id] =
6700 wm->pipe[pipe].plane[plane_id];
6701
6702 raw->plane[plane_id] =
6703 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6704 fifo_state->plane[plane_id]);
6705 }
6706 }
6707
6708 for_each_plane_id_on_crtc(crtc, plane_id)
6709 vlv_raw_plane_wm_set(crtc_state, level,
6710 plane_id, USHRT_MAX);
6711 vlv_invalidate_wms(crtc, active, level);
6712
6713 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006714 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006715
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006716 drm_dbg_kms(&dev_priv->drm,
6717 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
6718 pipe_name(pipe),
6719 wm->pipe[pipe].plane[PLANE_PRIMARY],
6720 wm->pipe[pipe].plane[PLANE_CURSOR],
6721 wm->pipe[pipe].plane[PLANE_SPRITE0],
6722 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006723 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006724
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006725 drm_dbg_kms(&dev_priv->drm,
6726 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6727 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006728}
6729
Ville Syrjälä602ae832017-03-02 19:15:02 +02006730void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6731{
6732 struct intel_plane *plane;
6733 struct intel_crtc *crtc;
6734
6735 mutex_lock(&dev_priv->wm.wm_mutex);
6736
6737 for_each_intel_plane(&dev_priv->drm, plane) {
6738 struct intel_crtc *crtc =
6739 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6740 struct intel_crtc_state *crtc_state =
6741 to_intel_crtc_state(crtc->base.state);
6742 struct intel_plane_state *plane_state =
6743 to_intel_plane_state(plane->base.state);
6744 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6745 const struct vlv_fifo_state *fifo_state =
6746 &crtc_state->wm.vlv.fifo_state;
6747 enum plane_id plane_id = plane->id;
6748 int level;
6749
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006750 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02006751 continue;
6752
6753 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006754 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006755 &crtc_state->wm.vlv.raw[level];
6756
6757 raw->plane[plane_id] = 0;
6758
6759 wm_state->wm[level].plane[plane_id] =
6760 vlv_invert_wm_value(raw->plane[plane_id],
6761 fifo_state->plane[plane_id]);
6762 }
6763 }
6764
6765 for_each_intel_crtc(&dev_priv->drm, crtc) {
6766 struct intel_crtc_state *crtc_state =
6767 to_intel_crtc_state(crtc->base.state);
6768
6769 crtc_state->wm.vlv.intermediate =
6770 crtc_state->wm.vlv.optimal;
6771 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6772 }
6773
6774 vlv_program_watermarks(dev_priv);
6775
6776 mutex_unlock(&dev_priv->wm.wm_mutex);
6777}
6778
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006779/*
6780 * FIXME should probably kill this and improve
6781 * the real watermark readout/sanitation instead
6782 */
6783static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6784{
Jani Nikula5f461662020-11-30 13:15:58 +02006785 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM1_LP_SR_EN);
6786 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM1_LP_SR_EN);
6787 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 +02006788
6789 /*
6790 * Don't touch WM1S_LP_EN here.
6791 * Doing so could cause underruns.
6792 */
6793}
6794
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006795void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006796{
Imre Deak820c1982013-12-17 14:46:36 +02006797 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006798 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006799
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006800 ilk_init_lp_watermarks(dev_priv);
6801
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006802 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006803 ilk_pipe_wm_get_hw_state(crtc);
6804
Jani Nikula5f461662020-11-30 13:15:58 +02006805 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
6806 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
6807 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006808
Jani Nikula5f461662020-11-30 13:15:58 +02006809 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
Matt Roper7dadd282021-03-19 21:42:43 -07006810 if (DISPLAY_VER(dev_priv) >= 7) {
Jani Nikula5f461662020-11-30 13:15:58 +02006811 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
6812 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006813 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006814
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006815 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02006816 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 +02006817 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006818 else if (IS_IVYBRIDGE(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02006819 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 +02006820 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006821
6822 hw->enable_fbc_wm =
Jani Nikula5f461662020-11-30 13:15:58 +02006823 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006824}
6825
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006826/**
6827 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006828 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006829 *
6830 * Calculate watermark values for the various WM regs based on current mode
6831 * and plane configuration.
6832 *
6833 * There are several cases to deal with here:
6834 * - normal (i.e. non-self-refresh)
6835 * - self-refresh (SR) mode
6836 * - lines are large relative to FIFO size (buffer can hold up to 2)
6837 * - lines are small relative to FIFO size (buffer can hold more than 2
6838 * lines), so need to account for TLB latency
6839 *
6840 * The normal calculation is:
6841 * watermark = dotclock * bytes per pixel * latency
6842 * where latency is platform & configuration dependent (we assume pessimal
6843 * values here).
6844 *
6845 * The SR calculation is:
6846 * watermark = (trunc(latency/line time)+1) * surface width *
6847 * bytes per pixel
6848 * where
6849 * line time = htotal / dotclock
6850 * surface width = hdisplay for normal plane and 64 for cursor
6851 * and latency is assumed to be high, as above.
6852 *
6853 * The final value programmed to the register should always be rounded up,
6854 * and include an extra 2 entries to account for clock crossings.
6855 *
6856 * We don't use the sprite, so we can ignore that. And on Crestline we have
6857 * to set the non-SR watermarks to 8.
6858 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006859void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006860{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006861 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006862
6863 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006864 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006865}
6866
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306867void intel_enable_ipc(struct drm_i915_private *dev_priv)
6868{
6869 u32 val;
6870
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006871 if (!HAS_IPC(dev_priv))
6872 return;
6873
Jani Nikula5f461662020-11-30 13:15:58 +02006874 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306875
6876 if (dev_priv->ipc_enabled)
6877 val |= DISP_IPC_ENABLE;
6878 else
6879 val &= ~DISP_IPC_ENABLE;
6880
Jani Nikula5f461662020-11-30 13:15:58 +02006881 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306882}
6883
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006884static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6885{
6886 /* Display WA #0477 WaDisableIPC: skl */
6887 if (IS_SKYLAKE(dev_priv))
6888 return false;
6889
6890 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01006891 if (IS_KABYLAKE(dev_priv) ||
6892 IS_COFFEELAKE(dev_priv) ||
6893 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006894 return dev_priv->dram_info.symmetric_memory;
6895
6896 return true;
6897}
6898
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306899void intel_init_ipc(struct drm_i915_private *dev_priv)
6900{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306901 if (!HAS_IPC(dev_priv))
6902 return;
6903
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006904 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006905
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306906 intel_enable_ipc(dev_priv);
6907}
6908
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006909static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006910{
Daniel Vetter3107bd42012-10-31 22:52:31 +01006911 /*
6912 * On Ibex Peak and Cougar Point, we need to disable clock
6913 * gating for the panel power sequencer or it will fail to
6914 * start up when no ports are active.
6915 */
Jani Nikula5f461662020-11-30 13:15:58 +02006916 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006917}
6918
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006919static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006920{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006921 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006922
Damien Lespiau055e3932014-08-18 13:49:10 +01006923 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006924 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
6925 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006926 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006927
Jani Nikula5f461662020-11-30 13:15:58 +02006928 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
6929 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006930 }
6931}
6932
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006933static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006934{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006935 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006936
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006937 /*
6938 * Required for FBC
6939 * WaFbcDisableDpfcClockGating:ilk
6940 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006941 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6942 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6943 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006944
Jani Nikula5f461662020-11-30 13:15:58 +02006945 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006946 MARIUNIT_CLOCK_GATE_DISABLE |
6947 SVSMUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02006948 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006949 VFMUNIT_CLOCK_GATE_DISABLE);
6950
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006951 /*
6952 * According to the spec the following bits should be set in
6953 * order to enable memory self-refresh
6954 * The bit 22/21 of 0x42004
6955 * The bit 5 of 0x42020
6956 * The bit 15 of 0x45000
6957 */
Jani Nikula5f461662020-11-30 13:15:58 +02006958 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
6959 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006960 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006961 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02006962 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
6963 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006964 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006965
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006966 /*
6967 * Based on the document from hardware guys the following bits
6968 * should be set unconditionally in order to enable FBC.
6969 * The bit 22 of 0x42000
6970 * The bit 22 of 0x42004
6971 * The bit 7,8,9 of 0x42020.
6972 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006973 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006974 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Jani Nikula5f461662020-11-30 13:15:58 +02006975 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
6976 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006977 ILK_FBCQ_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02006978 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
6979 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006980 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006981 }
6982
Jani Nikula5f461662020-11-30 13:15:58 +02006983 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006984
Jani Nikula5f461662020-11-30 13:15:58 +02006985 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
6986 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006987 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05306988
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006989 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006990
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006991 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006992}
6993
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006994static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006995{
Ville Syrjäläd048a262019-08-21 20:30:31 +03006996 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006997 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006998
6999 /*
7000 * On Ibex Peak and Cougar Point, we need to disable clock
7001 * gating for the panel power sequencer or it will fail to
7002 * start up when no ports are active.
7003 */
Jani Nikula5f461662020-11-30 13:15:58 +02007004 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnescd664072013-10-02 10:34:19 -07007005 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7006 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007007 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
Daniel Vetter3107bd42012-10-31 22:52:31 +01007008 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007009 /* The below fixes the weird display corruption, a few pixels shifted
7010 * downward, on (only) LVDS of some HP laptops with IVY.
7011 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007012 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007013 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007014 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7015 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007016 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007017 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007018 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7019 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Jani Nikula5f461662020-11-30 13:15:58 +02007020 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007021 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007022 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007023 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007024 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
Daniel Vetter3107bd42012-10-31 22:52:31 +01007025 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7026 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007027}
7028
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007029static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007030{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007031 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007032
Jani Nikula5f461662020-11-30 13:15:58 +02007033 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007034 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007035 drm_dbg_kms(&dev_priv->drm,
7036 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7037 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007038}
7039
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007040static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007041{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007042 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007043
Jani Nikula5f461662020-11-30 13:15:58 +02007044 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007045
Jani Nikula5f461662020-11-30 13:15:58 +02007046 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7047 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007048 ILK_ELPIN_409_SELECT);
7049
Jani Nikula5f461662020-11-30 13:15:58 +02007050 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7051 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007052 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7053 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7054
7055 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7056 * gating disable must be set. Failure to set it results in
7057 * flickering pixels due to Z write ordering failures after
7058 * some amount of runtime in the Mesa "fire" demo, and Unigine
7059 * Sanctuary and Tropics, and apparently anything else with
7060 * alpha test or pixel discard.
7061 *
7062 * According to the spec, bit 11 (RCCUNIT) must also be set,
7063 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007064 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007065 * WaDisableRCCUnitClockGating:snb
7066 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007067 */
Jani Nikula5f461662020-11-30 13:15:58 +02007068 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007069 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7070 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7071
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007072 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007073 * According to the spec the following bits should be
7074 * set in order to enable memory self-refresh and fbc:
7075 * The bit21 and bit22 of 0x42000
7076 * The bit21 and bit22 of 0x42004
7077 * The bit5 and bit7 of 0x42020
7078 * The bit14 of 0x70180
7079 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007080 *
7081 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007082 */
Jani Nikula5f461662020-11-30 13:15:58 +02007083 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7084 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007085 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007086 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7087 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007088 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Jani Nikula5f461662020-11-30 13:15:58 +02007089 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7090 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
Damien Lespiau231e54f2012-10-19 17:55:41 +01007091 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7092 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007093
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007094 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007095
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007096 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007097
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007098 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007099}
7100
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007101static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007102{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007103 /*
7104 * TODO: this bit should only be enabled when really needed, then
7105 * disabled when not needed anymore in order to save power.
7106 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007107 if (HAS_PCH_LPT_LP(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007108 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7109 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007110 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007111
7112 /* WADPOClockGatingDisable:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007113 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7114 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007115 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007116}
7117
Ville Syrjälä712bf362016-10-31 22:37:23 +02007118static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007119{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007120 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02007121 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007122
7123 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007124 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
Imre Deak7d708ee2013-04-17 14:04:50 +03007125 }
7126}
7127
Imre Deak450174f2016-05-03 15:54:21 +03007128static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7129 int general_prio_credits,
7130 int high_prio_credits)
7131{
7132 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007133 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007134
7135 /* WaTempDisableDOPClkGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007136 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7137 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
Imre Deak450174f2016-05-03 15:54:21 +03007138
Jani Nikula5f461662020-11-30 13:15:58 +02007139 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Oscar Mateo930a7842017-10-17 13:25:45 -07007140 val &= ~L3_PRIO_CREDITS_MASK;
7141 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7142 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
Jani Nikula5f461662020-11-30 13:15:58 +02007143 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007144
7145 /*
7146 * Wait at least 100 clocks before re-enabling clock gating.
7147 * See the definition of L3SQCREG1 in BSpec.
7148 */
Jani Nikula5f461662020-11-30 13:15:58 +02007149 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Imre Deak450174f2016-05-03 15:54:21 +03007150 udelay(1);
Jani Nikula5f461662020-11-30 13:15:58 +02007151 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
Imre Deak450174f2016-05-03 15:54:21 +03007152}
7153
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007154static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7155{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007156 /* Wa_1409120013:icl,ehl */
Jani Nikula5f461662020-11-30 13:15:58 +02007157 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä885f1822020-07-08 16:12:20 +03007158 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7159
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007160 /* This is not an Wa. Enable to reduce Sampler power */
Jani Nikula5f461662020-11-30 13:15:58 +02007161 intel_uncore_write(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7162 intel_uncore_read(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07007163
Matt Atwood6f4194c2020-01-13 23:11:28 -05007164 /*Wa_14010594013:icl, ehl */
7165 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7166 0, CNL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007167}
7168
José Roberto de Souza35f08372021-01-13 05:37:59 -08007169static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
Michel Thierry5d869232019-08-23 01:20:34 -07007170{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007171 /* Wa_1409120013:tgl,rkl,adl_s,dg1 */
Jani Nikula5f461662020-11-30 13:15:58 +02007172 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
José Roberto de Souza35f08372021-01-13 05:37:59 -08007173 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007174
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007175 /* Wa_1409825376:tgl (pre-prod)*/
Jani Nikulacd0fcf52021-03-26 15:21:36 +02007176 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B1))
Jani Nikula5f461662020-11-30 13:15:58 +02007177 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 -08007178 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007179
José Roberto de Souza35f08372021-01-13 05:37:59 -08007180 /* Wa_14011059788:tgl,rkl,adl_s,dg1 */
Matt Atwoodf9d77422020-04-15 15:35:35 -04007181 intel_uncore_rmw(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7182 0, DFR_DISABLE);
José Roberto de Souza41c70d22021-04-08 13:49:16 -07007183
7184 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7185 if (DISPLAY_VER(dev_priv) == 12)
7186 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7187 CLKREQ_POLICY_MEM_UP_OVRD, 0);
Michel Thierry5d869232019-08-23 01:20:34 -07007188}
7189
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007190static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7191{
7192 gen12lp_init_clock_gating(dev_priv);
7193
7194 /* Wa_22011091694:adlp */
7195 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7196}
7197
Stuart Summersda9427502020-10-14 12:19:34 -07007198static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7199{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007200 gen12lp_init_clock_gating(dev_priv);
7201
Stuart Summersda9427502020-10-14 12:19:34 -07007202 /* Wa_1409836686:dg1[a0] */
7203 if (IS_DG1_REVID(dev_priv, DG1_REVID_A0, DG1_REVID_A0))
Jani Nikula5f461662020-11-30 13:15:58 +02007204 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 -07007205 DPT_GATING_DIS);
7206}
7207
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007208static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7209{
7210 if (!HAS_PCH_CNP(dev_priv))
7211 return;
7212
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007213 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Jani Nikula5f461662020-11-30 13:15:58 +02007214 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 -07007215 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007216}
7217
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007218static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007219{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07007220 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007221 cnp_init_clock_gating(dev_priv);
7222
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07007223 /* This is not an Wa. Enable for better image quality */
Jani Nikula5f461662020-11-30 13:15:58 +02007224 intel_uncore_write(&dev_priv->uncore, _3D_CHICKEN3,
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07007225 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
7226
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007227 /* WaEnableChickenDCPR:cnl */
Jani Nikula5f461662020-11-30 13:15:58 +02007228 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7229 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007230
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007231 /*
7232 * WaFbcWakeMemOn:cnl
7233 * Display WA #0859: cnl
7234 */
Jani Nikula5f461662020-11-30 13:15:58 +02007235 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007236 DISP_FBC_MEMORY_WAKE);
7237
Jani Nikula5f461662020-11-30 13:15:58 +02007238 val = intel_uncore_read(&dev_priv->uncore, SLICE_UNIT_LEVEL_CLKGATE);
Chris Wilson34991bd2017-11-11 10:03:36 +00007239 /* ReadHitWriteOnlyDisable:cnl */
7240 val |= RCCUNIT_CLKGATE_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02007241 intel_uncore_write(&dev_priv->uncore, SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007242
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007243 /* Wa_2201832410:cnl */
Jani Nikula5f461662020-11-30 13:15:58 +02007244 val = intel_uncore_read(&dev_priv->uncore, SUBSLICE_UNIT_LEVEL_CLKGATE);
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007245 val |= GWUNIT_CLKGATE_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02007246 intel_uncore_write(&dev_priv->uncore, SUBSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007247
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007248 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08007249 /* WaVFUnitClockGatingDisable:cnl */
Jani Nikula5f461662020-11-30 13:15:58 +02007250 val = intel_uncore_read(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007251 val |= VFUNIT_CLKGATE_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02007252 intel_uncore_write(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007253}
7254
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007255static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7256{
7257 cnp_init_clock_gating(dev_priv);
7258 gen9_init_clock_gating(dev_priv);
7259
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007260 /* WAC6entrylatency:cfl */
Jani Nikula5f461662020-11-30 13:15:58 +02007261 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 +03007262 FBC_LLC_FULLY_OPEN);
7263
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007264 /*
7265 * WaFbcTurnOffFbcWatermark:cfl
7266 * Display WA #0562: cfl
7267 */
Jani Nikula5f461662020-11-30 13:15:58 +02007268 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 +03007269 DISP_FBC_WM_DIS);
7270
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007271 /*
7272 * WaFbcNukeOnHostModify:cfl
7273 * Display WA #0873: cfl
7274 */
Jani Nikula5f461662020-11-30 13:15:58 +02007275 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 -07007276 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7277}
7278
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007279static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007280{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007281 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007282
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007283 /* WAC6entrylatency:kbl */
Jani Nikula5f461662020-11-30 13:15:58 +02007284 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 +03007285 FBC_LLC_FULLY_OPEN);
7286
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007287 /* WaDisableSDEUnitClockGating:kbl */
Jani Nikulaef47b7a2021-03-26 15:21:34 +02007288 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007289 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007290 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007291
7292 /* WaDisableGamClockGating:kbl */
Jani Nikulaef47b7a2021-03-26 15:21:34 +02007293 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007294 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007295 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007296
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007297 /*
7298 * WaFbcTurnOffFbcWatermark:kbl
7299 * Display WA #0562: kbl
7300 */
Jani Nikula5f461662020-11-30 13:15:58 +02007301 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 +03007302 DISP_FBC_WM_DIS);
7303
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007304 /*
7305 * WaFbcNukeOnHostModify:kbl
7306 * Display WA #0873: kbl
7307 */
Jani Nikula5f461662020-11-30 13:15:58 +02007308 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 +03007309 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007310}
7311
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007312static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007313{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007314 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007315
Ville Syrjäläf1421192020-07-16 22:04:25 +03007316 /* WaDisableDopClockGating:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007317 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 +03007318 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7319
Mika Kuoppala44fff992016-06-07 17:19:09 +03007320 /* WAC6entrylatency:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007321 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 +03007322 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007323
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007324 /*
7325 * WaFbcTurnOffFbcWatermark:skl
7326 * Display WA #0562: skl
7327 */
Jani Nikula5f461662020-11-30 13:15:58 +02007328 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 +03007329 DISP_FBC_WM_DIS);
7330
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007331 /*
7332 * WaFbcNukeOnHostModify:skl
7333 * Display WA #0873: skl
7334 */
Jani Nikula5f461662020-11-30 13:15:58 +02007335 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 +03007336 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007337
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007338 /*
7339 * WaFbcHighMemBwCorruptionAvoidance:skl
7340 * Display WA #0883: skl
7341 */
Jani Nikula5f461662020-11-30 13:15:58 +02007342 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 +03007343 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007344}
7345
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007346static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007347{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007348 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007349
Ville Syrjälä885f1822020-07-08 16:12:20 +03007350 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007351 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7352 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007353 HSW_FBCQ_DIS);
7354
Ben Widawskyab57fff2013-12-12 15:28:04 -08007355 /* WaSwitchSolVfFArbitrationPriority:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007356 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 -07007357
Ben Widawskyab57fff2013-12-12 15:28:04 -08007358 /* WaPsrDPAMaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007359 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7360 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007361
Damien Lespiau055e3932014-08-18 13:49:10 +01007362 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007363 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007364 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7365 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007366 BDW_DPRS_MASK_VBLANK_SRD);
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007367
7368 /* Undocumented but fixes async flip + VT-d corruption */
7369 if (intel_vtd_active())
7370 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7371 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007372 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007373
Ben Widawskyab57fff2013-12-12 15:28:04 -08007374 /* WaVSRefCountFullforceMissDisable:bdw */
7375 /* WaDSRefCountFullforceMissDisable:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007376 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7377 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ben Widawskyab57fff2013-12-12 15:28:04 -08007378 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007379
Jani Nikula5f461662020-11-30 13:15:58 +02007380 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007381 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007382
7383 /* WaDisableSDEUnitClockGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007384 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 +02007385 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007386
Imre Deak450174f2016-05-03 15:54:21 +03007387 /* WaProgramL3SqcReg1Default:bdw */
7388 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007389
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007390 /* WaKVMNotificationOnConfigChange:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007391 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 +03007392 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7393
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007394 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007395
7396 /* WaDisableDopClockGating:bdw
7397 *
7398 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7399 * clock gating.
7400 */
Jani Nikula5f461662020-11-30 13:15:58 +02007401 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7402 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007403}
7404
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007405static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007406{
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007407 enum pipe pipe;
7408
Ville Syrjälä885f1822020-07-08 16:12:20 +03007409 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007410 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7411 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007412 HSW_FBCQ_DIS);
7413
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007414 for_each_pipe(dev_priv, pipe) {
7415 /* Undocumented but fixes async flip + VT-d corruption */
7416 if (intel_vtd_active())
7417 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7418 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
7419 }
7420
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007421 /* This is required by WaCatErrorRejectionIssue:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007422 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7423 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007424 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007425
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007426 /* WaSwitchSolVfFArbitrationPriority:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007427 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 -07007428
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007429 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007430}
7431
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007432static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007433{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007434 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007435
Jani Nikula5f461662020-11-30 13:15:58 +02007436 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007437
Ville Syrjälä885f1822020-07-08 16:12:20 +03007438 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007439 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7440 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007441 ILK_FBCQ_DIS);
7442
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007443 /* WaDisableBackToBackFlipFix:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007444 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007445 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7446 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7447
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007448 if (IS_IVB_GT1(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007449 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007450 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007451 else {
7452 /* must write both registers */
Jani Nikula5f461662020-11-30 13:15:58 +02007453 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Ville Syrjälä412236c2014-01-22 21:32:44 +02007454 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jani Nikula5f461662020-11-30 13:15:58 +02007455 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007456 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007457 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007458
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007459 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007460 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007461 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007462 */
Jani Nikula5f461662020-11-30 13:15:58 +02007463 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007464 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007465
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007466 /* This is required by WaCatErrorRejectionIssue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007467 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7468 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007469 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7470
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007471 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007472
Jani Nikula5f461662020-11-30 13:15:58 +02007473 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
Ben Widawsky20848222012-05-04 18:58:59 -07007474 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7475 snpcr |= GEN6_MBC_SNPCR_MED;
Jani Nikula5f461662020-11-30 13:15:58 +02007476 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007477
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007478 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007479 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007480
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007481 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007482}
7483
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007484static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007485{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007486 /* WaDisableBackToBackFlipFix:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007487 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007488 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7489 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7490
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007491 /* WaDisableDopClockGating:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007492 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007493 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7494
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007495 /* This is required by WaCatErrorRejectionIssue:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007496 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7497 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007498 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7499
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007500 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007501 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007502 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007503 */
Jani Nikula5f461662020-11-30 13:15:58 +02007504 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007505 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007506
Akash Goelc98f5062014-03-24 23:00:07 +05307507 /* WaDisableL3Bank2xClockGate:vlv
7508 * Disabling L3 clock gating- MMIO 940c[25] = 1
7509 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
Jani Nikula5f461662020-11-30 13:15:58 +02007510 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7511 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007512
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007513 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007514 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007515 * Disable clock gating on th GCFG unit to prevent a delay
7516 * in the reporting of vblank events.
7517 */
Jani Nikula5f461662020-11-30 13:15:58 +02007518 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007519}
7520
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007521static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007522{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007523 /* WaVSRefCountFullforceMissDisable:chv */
7524 /* WaDSRefCountFullforceMissDisable:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007525 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7526 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ville Syrjälä232ce332014-04-09 13:28:35 +03007527 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007528
7529 /* WaDisableSemaphoreAndSyncFlipWait:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007530 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007531 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007532
7533 /* WaDisableCSUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007534 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 +03007535 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007536
7537 /* WaDisableSDEUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007538 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 +03007539 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007540
7541 /*
Imre Deak450174f2016-05-03 15:54:21 +03007542 * WaProgramL3SqcReg1Default:chv
7543 * See gfxspecs/Related Documents/Performance Guide/
7544 * LSQC Setting Recommendations.
7545 */
7546 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007547}
7548
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007549static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007550{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007551 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007552
Jani Nikula5f461662020-11-30 13:15:58 +02007553 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7554 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007555 GS_UNIT_CLOCK_GATE_DISABLE |
7556 CL_UNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007557 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007558 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7559 OVRUNIT_CLOCK_GATE_DISABLE |
7560 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007561 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007562 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007563 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007564
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007565 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007566}
7567
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007568static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007569{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007570 struct intel_uncore *uncore = &dev_priv->uncore;
7571
7572 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7573 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7574 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7575 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7576 intel_uncore_write16(uncore, DEUC, 0);
7577 intel_uncore_write(uncore,
7578 MI_ARB_STATE,
7579 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007580}
7581
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007582static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007583{
Jani Nikula5f461662020-11-30 13:15:58 +02007584 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007585 I965_RCC_CLOCK_GATE_DISABLE |
7586 I965_RCPB_CLOCK_GATE_DISABLE |
7587 I965_ISC_CLOCK_GATE_DISABLE |
7588 I965_FBC_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007589 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7590 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä20f94962013-06-07 10:47:02 +03007591 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007592}
7593
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007594static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007595{
Jani Nikula5f461662020-11-30 13:15:58 +02007596 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007597
7598 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7599 DSTATE_DOT_CLOCK_GATING;
Jani Nikula5f461662020-11-30 13:15:58 +02007600 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007601
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007602 if (IS_PINEVIEW(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007603 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007604
7605 /* IIR "flip pending" means done if this bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007606 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007607
7608 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007609 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007610
7611 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007612 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 +03007613
Jani Nikula5f461662020-11-30 13:15:58 +02007614 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007615 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007616}
7617
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007618static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007619{
Jani Nikula5f461662020-11-30 13:15:58 +02007620 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007621
7622 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007623 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007624 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007625
Jani Nikula5f461662020-11-30 13:15:58 +02007626 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007627 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007628
7629 /*
7630 * Have FBC ignore 3D activity since we use software
7631 * render tracking, and otherwise a pure 3D workload
7632 * (even if it just renders a single frame and then does
7633 * abosultely nothing) would not allow FBC to recompress
7634 * until a 2D blit occurs.
7635 */
Jani Nikula5f461662020-11-30 13:15:58 +02007636 intel_uncore_write(&dev_priv->uncore, SCPD0,
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007637 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007638}
7639
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007640static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007641{
Jani Nikula5f461662020-11-30 13:15:58 +02007642 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007643 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7644 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007645}
7646
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007647void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007648{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007649 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007650}
7651
Ville Syrjälä712bf362016-10-31 22:37:23 +02007652void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007653{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007654 if (HAS_PCH_LPT(dev_priv))
7655 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007656}
7657
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007658static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007659{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007660 drm_dbg_kms(&dev_priv->drm,
7661 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007662}
7663
7664/**
7665 * intel_init_clock_gating_hooks - setup the clock gating hooks
7666 * @dev_priv: device private
7667 *
7668 * Setup the hooks that configure which clocks of a given platform can be
7669 * gated and also apply various GT and display specific workarounds for these
7670 * platforms. Note that some GT specific workarounds are applied separately
7671 * when GPU contexts or batchbuffers start their execution.
7672 */
7673void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7674{
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007675 if (IS_ALDERLAKE_P(dev_priv))
7676 dev_priv->display.init_clock_gating = adlp_init_clock_gating;
7677 else if (IS_DG1(dev_priv))
Stuart Summersda9427502020-10-14 12:19:34 -07007678 dev_priv->display.init_clock_gating = dg1_init_clock_gating;
7679 else if (IS_GEN(dev_priv, 12))
José Roberto de Souza35f08372021-01-13 05:37:59 -08007680 dev_priv->display.init_clock_gating = gen12lp_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07007681 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007682 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07007683 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007684 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007685 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007686 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007687 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007688 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007689 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007690 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007691 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007692 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007693 else if (IS_GEMINILAKE(dev_priv))
7694 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007695 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007696 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007697 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007698 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007699 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007700 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007701 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007702 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007703 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007704 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007705 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02007706 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007707 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007708 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007709 else if (IS_G4X(dev_priv))
7710 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007711 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007712 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007713 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007714 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007715 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02007716 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7717 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7718 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007719 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02007720 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7721 else {
7722 MISSING_CASE(INTEL_DEVID(dev_priv));
7723 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7724 }
7725}
7726
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007727/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007728void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007729{
Daniel Vetterc921aba2012-04-26 23:28:17 +02007730 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007731 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08007732 pnv_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007733 else if (IS_GEN(dev_priv, 5))
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08007734 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007735
James Ausmusb068a862019-10-09 10:23:14 -07007736 if (intel_has_sagv(dev_priv))
7737 skl_setup_sagv_block_time(dev_priv);
7738
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007739 /* For FIFO watermark updates */
Matt Roper7dadd282021-03-19 21:42:43 -07007740 if (DISPLAY_VER(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007741 skl_setup_wm_latency(dev_priv);
Matt Roper98d39492016-05-12 07:06:03 -07007742 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007743 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007744 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007745
Lucas De Marchi93e7e612021-04-12 22:09:53 -07007746 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007747 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchi93e7e612021-04-12 22:09:53 -07007748 (DISPLAY_VER(dev_priv) != 5 && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007749 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007750 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007751 dev_priv->display.compute_intermediate_wm =
7752 ilk_compute_intermediate_wm;
7753 dev_priv->display.initial_watermarks =
7754 ilk_initial_watermarks;
7755 dev_priv->display.optimize_watermarks =
7756 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007757 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007758 drm_dbg_kms(&dev_priv->drm,
7759 "Failed to read display plane latency. "
7760 "Disable CxSR\n");
Ville Syrjäläbd602542014-01-07 16:14:10 +02007761 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007762 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007763 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007764 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007765 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007766 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007767 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007768 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03007769 } else if (IS_G4X(dev_priv)) {
7770 g4x_setup_wm_latency(dev_priv);
7771 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
7772 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
7773 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
7774 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007775 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00007776 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007777 dev_priv->is_ddr3,
7778 dev_priv->fsb_freq,
7779 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007780 drm_info(&dev_priv->drm,
7781 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007782 "(found ddr%s fsb freq %d, mem freq %d), "
7783 "disabling CxSR\n",
7784 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7785 dev_priv->fsb_freq, dev_priv->mem_freq);
7786 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007787 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007788 dev_priv->display.update_wm = NULL;
7789 } else
Lucas De Marchi1d218222019-12-24 00:40:04 -08007790 dev_priv->display.update_wm = pnv_update_wm;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07007791 } else if (DISPLAY_VER(dev_priv) == 4) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007792 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07007793 } else if (DISPLAY_VER(dev_priv) == 3) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007794 dev_priv->display.update_wm = i9xx_update_wm;
7795 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07007796 } else if (DISPLAY_VER(dev_priv) == 2) {
Jani Nikula24977872019-09-11 12:26:08 +03007797 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007798 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007799 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007800 } else {
7801 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007802 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007803 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007804 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007805 drm_err(&dev_priv->drm,
7806 "unexpected fall-through in %s\n", __func__);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007807 }
7808}
7809
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00007810void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01007811{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01007812 dev_priv->runtime_pm.suspended = false;
7813 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007814}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02007815
7816static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
7817{
7818 struct intel_dbuf_state *dbuf_state;
7819
7820 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
7821 if (!dbuf_state)
7822 return NULL;
7823
7824 return &dbuf_state->base;
7825}
7826
7827static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
7828 struct intel_global_state *state)
7829{
7830 kfree(state);
7831}
7832
7833static const struct intel_global_state_funcs intel_dbuf_funcs = {
7834 .atomic_duplicate_state = intel_dbuf_duplicate_state,
7835 .atomic_destroy_state = intel_dbuf_destroy_state,
7836};
7837
7838struct intel_dbuf_state *
7839intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
7840{
7841 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7842 struct intel_global_state *dbuf_state;
7843
7844 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
7845 if (IS_ERR(dbuf_state))
7846 return ERR_CAST(dbuf_state);
7847
7848 return to_intel_dbuf_state(dbuf_state);
7849}
7850
7851int intel_dbuf_init(struct drm_i915_private *dev_priv)
7852{
7853 struct intel_dbuf_state *dbuf_state;
7854
7855 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
7856 if (!dbuf_state)
7857 return -ENOMEM;
7858
7859 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
7860 &dbuf_state->base, &intel_dbuf_funcs);
7861
7862 return 0;
7863}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02007864
7865void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
7866{
7867 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7868 const struct intel_dbuf_state *new_dbuf_state =
7869 intel_atomic_get_new_dbuf_state(state);
7870 const struct intel_dbuf_state *old_dbuf_state =
7871 intel_atomic_get_old_dbuf_state(state);
7872
7873 if (!new_dbuf_state ||
7874 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7875 return;
7876
7877 WARN_ON(!new_dbuf_state->base.changed);
7878
7879 gen9_dbuf_slices_update(dev_priv,
7880 old_dbuf_state->enabled_slices |
7881 new_dbuf_state->enabled_slices);
7882}
7883
7884void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
7885{
7886 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7887 const struct intel_dbuf_state *new_dbuf_state =
7888 intel_atomic_get_new_dbuf_state(state);
7889 const struct intel_dbuf_state *old_dbuf_state =
7890 intel_atomic_get_old_dbuf_state(state);
7891
7892 if (!new_dbuf_state ||
7893 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7894 return;
7895
7896 WARN_ON(!new_dbuf_state->base.changed);
7897
7898 gen9_dbuf_slices_update(dev_priv,
7899 new_dbuf_state->enabled_slices);
7900}