blob: 2796f32d72ad0f71e075c37297810367457dccbe [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"
Imre Deak0f2922e2021-10-20 22:51:33 +030040#include "display/intel_fb.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030041#include "display/intel_fbc.h"
42#include "display/intel_sprite.h"
Dave Airlie46d12f92021-02-05 16:48:36 +020043#include "display/skl_universal_plane.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030044
Andi Shyti0dc3c562019-10-20 19:41:39 +010045#include "gt/intel_llc.h"
46
Eugeni Dodonov85208be2012-04-16 22:20:34 -030047#include "i915_drv.h"
Jani Nikulaa10510a2020-02-27 19:00:47 +020048#include "i915_fixed.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030049#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030050#include "i915_trace.h"
Jani Nikula4dd43752021-10-14 13:28:57 +030051#include "intel_pcode.h"
Jani Nikula696173b2019-04-05 14:00:15 +030052#include "intel_pm.h"
Jani Nikula1eecf31e2021-10-13 13:11:59 +030053#include "vlv_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020054#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030055
Jani Nikulaa10510a2020-02-27 19:00:47 +020056/* Stores plane specific WM parameters */
57struct skl_wm_params {
58 bool x_tiled, y_tiled;
59 bool rc_surface;
60 bool is_planar;
61 u32 width;
62 u8 cpp;
63 u32 plane_pixel_rate;
64 u32 y_min_scanlines;
65 u32 plane_bytes_per_line;
66 uint_fixed_16_16_t plane_blocks_per_line;
67 uint_fixed_16_16_t y_tile_minimum;
68 u32 linetime_us;
69 u32 dbuf_block_size;
70};
71
72/* used in computing the new watermarks state */
73struct intel_wm_config {
74 unsigned int num_pipes_active;
75 bool sprites_enabled;
76 bool sprites_scaled;
77};
78
Ville Syrjälä46f16e62016-10-31 22:37:22 +020079static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030080{
Ville Syrjäläb2d73de2021-09-30 22:09:42 +030081 enum pipe pipe;
82
Ville Syrjälä93564042017-08-24 22:10:51 +030083 if (HAS_LLC(dev_priv)) {
84 /*
85 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080086 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030087 *
88 * Must match Sampler, Pixel Back End, and Media. See
89 * WaCompressedResourceSamplerPbeMediaNewHashMode.
90 */
Jani Nikula5f461662020-11-30 13:15:58 +020091 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
92 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
Ville Syrjälä93564042017-08-24 22:10:51 +030093 SKL_DE_COMPRESSED_HASH_MODE);
94 }
95
Ville Syrjäläb2d73de2021-09-30 22:09:42 +030096 for_each_pipe(dev_priv, pipe) {
97 /*
98 * "Plane N strech max must be programmed to 11b (x1)
99 * when Async flips are enabled on that plane."
100 */
101 if (!IS_GEMINILAKE(dev_priv) && intel_vtd_active())
102 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
103 SKL_PLANE1_STRETCH_MAX_MASK, SKL_PLANE1_STRETCH_MAX_X1);
104 }
105
Rodrigo Vivi82525c12017-06-08 08:50:00 -0700106 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +0200107 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
108 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300109
Rodrigo Vivi82525c12017-06-08 08:50:00 -0700110 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +0200111 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
112 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +0300113
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300114 /*
115 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
116 * Display WA #0859: skl,bxt,kbl,glk,cfl
117 */
Jani Nikula5f461662020-11-30 13:15:58 +0200118 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 +0300119 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300120}
121
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200122static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200123{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200124 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200125
Nick Hoatha7546152015-06-29 14:07:32 +0100126 /* WaDisableSDEUnitClockGating:bxt */
Jani Nikula5f461662020-11-30 13:15:58 +0200127 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Nick Hoatha7546152015-06-29 14:07:32 +0100128 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
129
Imre Deak32608ca2015-03-11 11:10:27 +0200130 /*
131 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200132 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200133 */
Jani Nikula5f461662020-11-30 13:15:58 +0200134 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200135 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200136
137 /*
138 * Wa: Backlight PWM may stop in the asserted state, causing backlight
139 * to stay fully on.
140 */
Jani Nikula5f461662020-11-30 13:15:58 +0200141 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 +0200142 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530143
144 /*
145 * Lower the display internal timeout.
146 * This is needed to avoid any hard hangs when DSI port PLL
147 * is off and a MMIO access is attempted by any privilege
148 * application, using batch buffers or any other means.
149 */
Jani Nikula5f461662020-11-30 13:15:58 +0200150 intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300151
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300152 /*
153 * WaFbcTurnOffFbcWatermark:bxt
154 * Display WA #0562: bxt
155 */
Jani Nikula5f461662020-11-30 13:15:58 +0200156 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 +0300157 DISP_FBC_WM_DIS);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300158
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300159 /*
160 * WaFbcHighMemBwCorruptionAvoidance:bxt
161 * Display WA #0883: bxt
162 */
Jani Nikula5f461662020-11-30 13:15:58 +0200163 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjälä73ab6ec2021-11-04 16:45:15 +0200164 DPFC_DISABLE_DUMMY0);
Imre Deaka82abe42015-03-27 14:00:04 +0200165}
166
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200167static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
168{
169 gen9_init_clock_gating(dev_priv);
170
171 /*
172 * WaDisablePWMClockGating:glk
173 * Backlight PWM may stop in the asserted state, causing backlight
174 * to stay fully on.
175 */
Jani Nikula5f461662020-11-30 13:15:58 +0200176 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 +0200177 PWM1_GATING_DIS | PWM2_GATING_DIS);
178}
179
Lucas De Marchi1d218222019-12-24 00:40:04 -0800180static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200181{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200182 u32 tmp;
183
Jani Nikula5f461662020-11-30 13:15:58 +0200184 tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200185
186 switch (tmp & CLKCFG_FSB_MASK) {
187 case CLKCFG_FSB_533:
188 dev_priv->fsb_freq = 533; /* 133*4 */
189 break;
190 case CLKCFG_FSB_800:
191 dev_priv->fsb_freq = 800; /* 200*4 */
192 break;
193 case CLKCFG_FSB_667:
194 dev_priv->fsb_freq = 667; /* 167*4 */
195 break;
196 case CLKCFG_FSB_400:
197 dev_priv->fsb_freq = 400; /* 100*4 */
198 break;
199 }
200
201 switch (tmp & CLKCFG_MEM_MASK) {
202 case CLKCFG_MEM_533:
203 dev_priv->mem_freq = 533;
204 break;
205 case CLKCFG_MEM_667:
206 dev_priv->mem_freq = 667;
207 break;
208 case CLKCFG_MEM_800:
209 dev_priv->mem_freq = 800;
210 break;
211 }
212
213 /* detect pineview DDR3 setting */
Jani Nikula5f461662020-11-30 13:15:58 +0200214 tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200215 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
216}
217
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800218static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200219{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200220 u16 ddrpll, csipll;
221
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100222 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
223 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200224
225 switch (ddrpll & 0xff) {
226 case 0xc:
227 dev_priv->mem_freq = 800;
228 break;
229 case 0x10:
230 dev_priv->mem_freq = 1066;
231 break;
232 case 0x14:
233 dev_priv->mem_freq = 1333;
234 break;
235 case 0x18:
236 dev_priv->mem_freq = 1600;
237 break;
238 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300239 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
240 ddrpll & 0xff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200241 dev_priv->mem_freq = 0;
242 break;
243 }
244
Daniel Vetterc921aba2012-04-26 23:28:17 +0200245 switch (csipll & 0x3ff) {
246 case 0x00c:
247 dev_priv->fsb_freq = 3200;
248 break;
249 case 0x00e:
250 dev_priv->fsb_freq = 3733;
251 break;
252 case 0x010:
253 dev_priv->fsb_freq = 4266;
254 break;
255 case 0x012:
256 dev_priv->fsb_freq = 4800;
257 break;
258 case 0x014:
259 dev_priv->fsb_freq = 5333;
260 break;
261 case 0x016:
262 dev_priv->fsb_freq = 5866;
263 break;
264 case 0x018:
265 dev_priv->fsb_freq = 6400;
266 break;
267 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300268 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
269 csipll & 0x3ff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200270 dev_priv->fsb_freq = 0;
271 break;
272 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200273}
274
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300275static const struct cxsr_latency cxsr_latency_table[] = {
276 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
277 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
278 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
279 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
280 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
281
282 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
283 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
284 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
285 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
286 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
287
288 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
289 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
290 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
291 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
292 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
293
294 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
295 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
296 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
297 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
298 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
299
300 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
301 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
302 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
303 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
304 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
305
306 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
307 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
308 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
309 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
310 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
311};
312
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100313static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
314 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300315 int fsb,
316 int mem)
317{
318 const struct cxsr_latency *latency;
319 int i;
320
321 if (fsb == 0 || mem == 0)
322 return NULL;
323
324 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
325 latency = &cxsr_latency_table[i];
326 if (is_desktop == latency->is_desktop &&
327 is_ddr3 == latency->is_ddr3 &&
328 fsb == latency->fsb_freq && mem == latency->mem_freq)
329 return latency;
330 }
331
332 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
333
334 return NULL;
335}
336
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200337static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
338{
339 u32 val;
340
Chris Wilson337fa6e2019-04-26 09:17:20 +0100341 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200342
343 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
344 if (enable)
345 val &= ~FORCE_DDR_HIGH_FREQ;
346 else
347 val |= FORCE_DDR_HIGH_FREQ;
348 val &= ~FORCE_DDR_LOW_FREQ;
349 val |= FORCE_DDR_FREQ_REQ_ACK;
350 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
351
352 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
353 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300354 drm_err(&dev_priv->drm,
355 "timed out waiting for Punit DDR DVFS request\n");
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200356
Chris Wilson337fa6e2019-04-26 09:17:20 +0100357 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200358}
359
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200360static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
361{
362 u32 val;
363
Chris Wilson337fa6e2019-04-26 09:17:20 +0100364 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200365
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200366 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200367 if (enable)
368 val |= DSP_MAXFIFO_PM5_ENABLE;
369 else
370 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200371 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200372
Chris Wilson337fa6e2019-04-26 09:17:20 +0100373 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200374}
375
Ville Syrjäläf4998962015-03-10 17:02:21 +0200376#define FW_WM(value, plane) \
377 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
378
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200379static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300380{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200381 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300382 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300383
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100384 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200385 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
386 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
387 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200388 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200389 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
390 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
391 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200392 } else if (IS_PINEVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200393 val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200394 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
395 if (enable)
396 val |= PINEVIEW_SELF_REFRESH_EN;
397 else
398 val &= ~PINEVIEW_SELF_REFRESH_EN;
Jani Nikula5f461662020-11-30 13:15:58 +0200399 intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
400 intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100401 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200402 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300403 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
404 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200405 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
406 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100407 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300408 /*
409 * FIXME can't find a bit like this for 915G, and
410 * and yet it does have the related watermark in
411 * FW_BLC_SELF. What's going on?
412 */
Jani Nikula5f461662020-11-30 13:15:58 +0200413 was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300414 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
415 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200416 intel_uncore_write(&dev_priv->uncore, INSTPM, val);
417 intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300418 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200419 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300420 }
421
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200422 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
423
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300424 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
425 enableddisabled(enable),
426 enableddisabled(was_enabled));
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200427
428 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300429}
430
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300431/**
432 * intel_set_memory_cxsr - Configure CxSR state
433 * @dev_priv: i915 device
434 * @enable: Allow vs. disallow CxSR
435 *
436 * Allow or disallow the system to enter a special CxSR
437 * (C-state self refresh) state. What typically happens in CxSR mode
438 * is that several display FIFOs may get combined into a single larger
439 * FIFO for a particular plane (so called max FIFO mode) to allow the
440 * system to defer memory fetches longer, and the memory will enter
441 * self refresh.
442 *
443 * Note that enabling CxSR does not guarantee that the system enter
444 * this special mode, nor does it guarantee that the system stays
445 * in that mode once entered. So this just allows/disallows the system
446 * to autonomously utilize the CxSR mode. Other factors such as core
447 * C-states will affect when/if the system actually enters/exits the
448 * CxSR mode.
449 *
450 * Note that on VLV/CHV this actually only controls the max FIFO mode,
451 * and the system is free to enter/exit memory self refresh at any time
452 * even when the use of CxSR has been disallowed.
453 *
454 * While the system is actually in the CxSR/max FIFO mode, some plane
455 * control registers will not get latched on vblank. Thus in order to
456 * guarantee the system will respond to changes in the plane registers
457 * we must always disallow CxSR prior to making changes to those registers.
458 * Unfortunately the system will re-evaluate the CxSR conditions at
459 * frame start which happens after vblank start (which is when the plane
460 * registers would get latched), so we can't proceed with the plane update
461 * during the same frame where we disallowed CxSR.
462 *
463 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
464 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
465 * the hardware w.r.t. HPLL SR when writing to plane registers.
466 * Disallowing just CxSR is sufficient.
467 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200468bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200469{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200470 bool ret;
471
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200472 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200473 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300474 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
475 dev_priv->wm.vlv.cxsr = enable;
476 else if (IS_G4X(dev_priv))
477 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200478 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200479
480 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200481}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200482
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300483/*
484 * Latency for FIFO fetches is dependent on several factors:
485 * - memory configuration (speed, channels)
486 * - chipset
487 * - current MCH state
488 * It can be fairly high in some situations, so here we assume a fairly
489 * pessimal value. It's a tradeoff between extra memory fetches (if we
490 * set this value too high, the FIFO will fetch frequently to stay full)
491 * and power consumption (set it too low to save power and we might see
492 * FIFO underruns and display "flicker").
493 *
494 * A value of 5us seems to be a good balance; safe for very low end
495 * platforms but not overly aggressive on lower latency configs.
496 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100497static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300498
Ville Syrjäläb5004722015-03-05 21:19:47 +0200499#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
500 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
501
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200502static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200503{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100504 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200505 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200506 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200507 enum pipe pipe = crtc->pipe;
508 int sprite0_start, sprite1_start;
Kees Cook2713eb42020-02-20 16:05:17 -0800509 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200510
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200511 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200512 case PIPE_A:
Jani Nikula5f461662020-11-30 13:15:58 +0200513 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
514 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200515 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
516 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
517 break;
518 case PIPE_B:
Jani Nikula5f461662020-11-30 13:15:58 +0200519 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
520 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200521 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
522 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
523 break;
524 case PIPE_C:
Jani Nikula5f461662020-11-30 13:15:58 +0200525 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
526 dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200527 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
528 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
529 break;
530 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200531 MISSING_CASE(pipe);
532 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200533 }
534
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200535 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
536 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
537 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
538 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200539}
540
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200541static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
542 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300543{
Jani Nikula5f461662020-11-30 13:15:58 +0200544 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545 int size;
546
547 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200548 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300549 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
550
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300551 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
552 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300553
554 return size;
555}
556
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200557static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
558 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559{
Jani Nikula5f461662020-11-30 13:15:58 +0200560 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300561 int size;
562
563 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200564 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300565 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
566 size >>= 1; /* Convert to cachelines */
567
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300568 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
569 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300570
571 return size;
572}
573
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200574static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
575 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300576{
Jani Nikula5f461662020-11-30 13:15:58 +0200577 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300578 int size;
579
580 size = dsparb & 0x7f;
581 size >>= 2; /* Convert to cachelines */
582
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300583 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
584 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300585
586 return size;
587}
588
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300589/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800590static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300591 .fifo_size = PINEVIEW_DISPLAY_FIFO,
592 .max_wm = PINEVIEW_MAX_WM,
593 .default_wm = PINEVIEW_DFT_WM,
594 .guard_size = PINEVIEW_GUARD_WM,
595 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300596};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800597
598static const struct intel_watermark_params pnv_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300599 .fifo_size = PINEVIEW_DISPLAY_FIFO,
600 .max_wm = PINEVIEW_MAX_WM,
601 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
602 .guard_size = PINEVIEW_GUARD_WM,
603 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300604};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800605
606static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300607 .fifo_size = PINEVIEW_CURSOR_FIFO,
608 .max_wm = PINEVIEW_CURSOR_MAX_WM,
609 .default_wm = PINEVIEW_CURSOR_DFT_WM,
610 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
611 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300612};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800613
614static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300615 .fifo_size = PINEVIEW_CURSOR_FIFO,
616 .max_wm = PINEVIEW_CURSOR_MAX_WM,
617 .default_wm = PINEVIEW_CURSOR_DFT_WM,
618 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
619 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300620};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800621
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300622static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300623 .fifo_size = I965_CURSOR_FIFO,
624 .max_wm = I965_CURSOR_MAX_WM,
625 .default_wm = I965_CURSOR_DFT_WM,
626 .guard_size = 2,
627 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300628};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800629
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300630static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300631 .fifo_size = I945_FIFO_SIZE,
632 .max_wm = I915_MAX_WM,
633 .default_wm = 1,
634 .guard_size = 2,
635 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300636};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800637
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300638static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300639 .fifo_size = I915_FIFO_SIZE,
640 .max_wm = I915_MAX_WM,
641 .default_wm = 1,
642 .guard_size = 2,
643 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300644};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800645
Ville Syrjälä9d539102014-08-15 01:21:53 +0300646static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300647 .fifo_size = I855GM_FIFO_SIZE,
648 .max_wm = I915_MAX_WM,
649 .default_wm = 1,
650 .guard_size = 2,
651 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300652};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800653
Ville Syrjälä9d539102014-08-15 01:21:53 +0300654static const struct intel_watermark_params i830_bc_wm_info = {
655 .fifo_size = I855GM_FIFO_SIZE,
656 .max_wm = I915_MAX_WM/2,
657 .default_wm = 1,
658 .guard_size = 2,
659 .cacheline_size = I830_FIFO_LINE_SIZE,
660};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800661
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200662static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300663 .fifo_size = I830_FIFO_SIZE,
664 .max_wm = I915_MAX_WM,
665 .default_wm = 1,
666 .guard_size = 2,
667 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300668};
669
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300670/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300671 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
672 * @pixel_rate: Pipe pixel rate in kHz
673 * @cpp: Plane bytes per pixel
674 * @latency: Memory wakeup latency in 0.1us units
675 *
676 * Compute the watermark using the method 1 or "small buffer"
677 * formula. The caller may additonally add extra cachelines
678 * to account for TLB misses and clock crossings.
679 *
680 * This method is concerned with the short term drain rate
681 * of the FIFO, ie. it does not account for blanking periods
682 * which would effectively reduce the average drain rate across
683 * a longer period. The name "small" refers to the fact the
684 * FIFO is relatively small compared to the amount of data
685 * fetched.
686 *
687 * The FIFO level vs. time graph might look something like:
688 *
689 * |\ |\
690 * | \ | \
691 * __---__---__ (- plane active, _ blanking)
692 * -> time
693 *
694 * or perhaps like this:
695 *
696 * |\|\ |\|\
697 * __----__----__ (- plane active, _ blanking)
698 * -> time
699 *
700 * Returns:
701 * The watermark in bytes
702 */
703static unsigned int intel_wm_method1(unsigned int pixel_rate,
704 unsigned int cpp,
705 unsigned int latency)
706{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200707 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300708
Ville Syrjäläd492a292019-04-08 18:27:01 +0300709 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300710 ret = DIV_ROUND_UP_ULL(ret, 10000);
711
712 return ret;
713}
714
715/**
716 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
717 * @pixel_rate: Pipe pixel rate in kHz
718 * @htotal: Pipe horizontal total
719 * @width: Plane width in pixels
720 * @cpp: Plane bytes per pixel
721 * @latency: Memory wakeup latency in 0.1us units
722 *
723 * Compute the watermark using the method 2 or "large buffer"
724 * formula. The caller may additonally add extra cachelines
725 * to account for TLB misses and clock crossings.
726 *
727 * This method is concerned with the long term drain rate
728 * of the FIFO, ie. it does account for blanking periods
729 * which effectively reduce the average drain rate across
730 * a longer period. The name "large" refers to the fact the
731 * FIFO is relatively large compared to the amount of data
732 * fetched.
733 *
734 * The FIFO level vs. time graph might look something like:
735 *
736 * |\___ |\___
737 * | \___ | \___
738 * | \ | \
739 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
740 * -> time
741 *
742 * Returns:
743 * The watermark in bytes
744 */
745static unsigned int intel_wm_method2(unsigned int pixel_rate,
746 unsigned int htotal,
747 unsigned int width,
748 unsigned int cpp,
749 unsigned int latency)
750{
751 unsigned int ret;
752
753 /*
754 * FIXME remove once all users are computing
755 * watermarks in the correct place.
756 */
757 if (WARN_ON_ONCE(htotal == 0))
758 htotal = 1;
759
760 ret = (latency * pixel_rate) / (htotal * 10000);
761 ret = (ret + 1) * width * cpp;
762
763 return ret;
764}
765
766/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300767 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300768 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300769 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000770 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200771 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300772 * @latency_ns: memory latency for the platform
773 *
774 * Calculate the watermark level (the level at which the display plane will
775 * start fetching from memory again). Each chip has a different display
776 * FIFO size and allocation, so the caller needs to figure that out and pass
777 * in the correct intel_watermark_params structure.
778 *
779 * As the pixel clock runs, the FIFO will be drained at a rate that depends
780 * on the pixel size. When it reaches the watermark level, it'll start
781 * fetching FIFO line sized based chunks from memory until the FIFO fills
782 * past the watermark point. If the FIFO drains completely, a FIFO underrun
783 * will occur, and a display engine hang could result.
784 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300785static unsigned int intel_calculate_wm(int pixel_rate,
786 const struct intel_watermark_params *wm,
787 int fifo_size, int cpp,
788 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300789{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300790 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300791
792 /*
793 * Note: we need to make sure we don't overflow for various clock &
794 * latency values.
795 * clocks go from a few thousand to several hundred thousand.
796 * latency is usually a few thousand
797 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300798 entries = intel_wm_method1(pixel_rate, cpp,
799 latency_ns / 100);
800 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
801 wm->guard_size;
802 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300803
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300804 wm_size = fifo_size - entries;
805 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300806
807 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300808 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300809 wm_size = wm->max_wm;
810 if (wm_size <= 0)
811 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300812
813 /*
814 * Bspec seems to indicate that the value shouldn't be lower than
815 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
816 * Lets go for 8 which is the burst size since certain platforms
817 * already use a hardcoded 8 (which is what the spec says should be
818 * done).
819 */
820 if (wm_size <= 8)
821 wm_size = 8;
822
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300823 return wm_size;
824}
825
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300826static bool is_disabling(int old, int new, int threshold)
827{
828 return old >= threshold && new < threshold;
829}
830
831static bool is_enabling(int old, int new, int threshold)
832{
833 return old < threshold && new >= threshold;
834}
835
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300836static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
837{
838 return dev_priv->wm.max_level + 1;
839}
840
Ville Syrjälä24304d812017-03-14 17:10:49 +0200841static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
842 const struct intel_plane_state *plane_state)
843{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100844 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200845
846 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100847 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200848 return false;
849
850 /*
851 * Treat cursor with fb as always visible since cursor updates
852 * can happen faster than the vrefresh rate, and the current
853 * watermark code doesn't handle that correctly. Cursor updates
854 * which set/clear the fb or change the cursor size are going
855 * to get throttled by intel_legacy_cursor_update() to work
856 * around this problem with the watermark code.
857 */
858 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100859 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200860 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100861 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200862}
863
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200864static bool intel_crtc_active(struct intel_crtc *crtc)
865{
866 /* Be paranoid as we can arrive here with only partial
867 * state retrieved from the hardware during setup.
868 *
869 * We can ditch the adjusted_mode.crtc_clock check as soon
870 * as Haswell has gained clock readout/fastboot support.
871 *
872 * We can ditch the crtc->primary->state->fb check as soon as we can
873 * properly reconstruct framebuffers.
874 *
875 * FIXME: The intel_crtc->active here should be switched to
876 * crtc->state->active once we have proper CRTC states wired up
877 * for atomic.
878 */
879 return crtc->active && crtc->base.primary->state->fb &&
880 crtc->config->hw.adjusted_mode.crtc_clock;
881}
882
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200883static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200885 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300886
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200887 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200888 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300889 if (enabled)
890 return NULL;
891 enabled = crtc;
892 }
893 }
894
895 return enabled;
896}
897
Dave Airlieef9c66a2021-09-29 01:57:47 +0300898static void pnv_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300899{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200900 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300901 const struct cxsr_latency *latency;
902 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300903 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300904
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000905 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100906 dev_priv->is_ddr3,
907 dev_priv->fsb_freq,
908 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300909 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300910 drm_dbg_kms(&dev_priv->drm,
911 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300912 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913 return;
914 }
915
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200916 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300917 if (crtc) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200918 const struct drm_display_mode *pipe_mode =
919 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200920 const struct drm_framebuffer *fb =
921 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200922 int cpp = fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200923 int clock = pipe_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300924
925 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800926 wm = intel_calculate_wm(clock, &pnv_display_wm,
927 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200928 cpp, latency->display_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200929 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300930 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200931 reg |= FW_WM(wm, SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200932 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300933 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300934
935 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800936 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
937 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300938 4, latency->cursor_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200939 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300940 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200941 reg |= FW_WM(wm, CURSOR_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200942 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300943
944 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800945 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
946 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200947 cpp, latency->display_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200948 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300949 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200950 reg |= FW_WM(wm, HPLL_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200951 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300952
953 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800954 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
955 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300956 4, latency->cursor_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200957 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300958 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200959 reg |= FW_WM(wm, HPLL_CURSOR);
Jani Nikula5f461662020-11-30 13:15:58 +0200960 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300961 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300962
Imre Deak5209b1f2014-07-01 12:36:17 +0300963 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300964 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300965 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300966 }
967}
968
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300969/*
970 * Documentation says:
971 * "If the line size is small, the TLB fetches can get in the way of the
972 * data fetches, causing some lag in the pixel data return which is not
973 * accounted for in the above formulas. The following adjustment only
974 * needs to be applied if eight whole lines fit in the buffer at once.
975 * The WM is adjusted upwards by the difference between the FIFO size
976 * and the size of 8 whole lines. This adjustment is always performed
977 * in the actual pixel depth regardless of whether FBC is enabled or not."
978 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000979static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300980{
981 int tlb_miss = fifo_size * 64 - width * cpp * 8;
982
983 return max(0, tlb_miss);
984}
985
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300986static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
987 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300988{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300989 enum pipe pipe;
990
991 for_each_pipe(dev_priv, pipe)
992 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
993
Jani Nikula5f461662020-11-30 13:15:58 +0200994 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300995 FW_WM(wm->sr.plane, SR) |
996 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
997 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
998 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200999 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001000 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
1001 FW_WM(wm->sr.fbc, FBC_SR) |
1002 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
1003 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
1004 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1005 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001006 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001007 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
1008 FW_WM(wm->sr.cursor, CURSOR_SR) |
1009 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
1010 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001011
Jani Nikula5f461662020-11-30 13:15:58 +02001012 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001013}
1014
Ville Syrjälä15665972015-03-10 16:16:28 +02001015#define FW_WM_VLV(value, plane) \
1016 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1017
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001018static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001019 const struct vlv_wm_values *wm)
1020{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001021 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001022
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001023 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001024 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1025
Jani Nikula5f461662020-11-30 13:15:58 +02001026 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001027 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1028 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1029 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1030 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1031 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001032
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001033 /*
1034 * Zero the (unused) WM1 watermarks, and also clear all the
1035 * high order bits so that there are no out of bounds values
1036 * present in the registers during the reprogramming.
1037 */
Jani Nikula5f461662020-11-30 13:15:58 +02001038 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1039 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1040 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1041 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1042 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001043
Jani Nikula5f461662020-11-30 13:15:58 +02001044 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001045 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001046 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1047 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1048 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001049 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001050 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1051 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1052 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001053 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001054 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001055
1056 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02001057 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001058 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1059 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001060 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001061 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1062 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Jani Nikula5f461662020-11-30 13:15:58 +02001063 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001064 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1065 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Jani Nikula5f461662020-11-30 13:15:58 +02001066 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001067 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001068 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1069 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1070 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1071 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1072 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1073 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1074 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1075 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1076 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001077 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02001078 intel_uncore_write(&dev_priv->uncore, DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001079 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1080 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001081 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001082 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001083 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1084 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1085 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1086 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1087 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1088 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001089 }
1090
Jani Nikula5f461662020-11-30 13:15:58 +02001091 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001092}
1093
Ville Syrjälä15665972015-03-10 16:16:28 +02001094#undef FW_WM_VLV
1095
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001096static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1097{
1098 /* all latencies in usec */
1099 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1100 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001101 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001102
Ville Syrjälä79d94302017-04-21 21:14:30 +03001103 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001104}
1105
1106static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1107{
1108 /*
1109 * DSPCNTR[13] supposedly controls whether the
1110 * primary plane can use the FIFO space otherwise
1111 * reserved for the sprite plane. It's not 100% clear
1112 * what the actual FIFO size is, but it looks like we
1113 * can happily set both primary and sprite watermarks
1114 * up to 127 cachelines. So that would seem to mean
1115 * that either DSPCNTR[13] doesn't do anything, or that
1116 * the total FIFO is >= 256 cachelines in size. Either
1117 * way, we don't seem to have to worry about this
1118 * repartitioning as the maximum watermark value the
1119 * register can hold for each plane is lower than the
1120 * minimum FIFO size.
1121 */
1122 switch (plane_id) {
1123 case PLANE_CURSOR:
1124 return 63;
1125 case PLANE_PRIMARY:
1126 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1127 case PLANE_SPRITE0:
1128 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1129 default:
1130 MISSING_CASE(plane_id);
1131 return 0;
1132 }
1133}
1134
1135static int g4x_fbc_fifo_size(int level)
1136{
1137 switch (level) {
1138 case G4X_WM_LEVEL_SR:
1139 return 7;
1140 case G4X_WM_LEVEL_HPLL:
1141 return 15;
1142 default:
1143 MISSING_CASE(level);
1144 return 0;
1145 }
1146}
1147
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001148static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1149 const struct intel_plane_state *plane_state,
1150 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001151{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001152 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001153 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001154 const struct drm_display_mode *pipe_mode =
1155 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001156 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1157 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001158
1159 if (latency == 0)
1160 return USHRT_MAX;
1161
1162 if (!intel_wm_plane_visible(crtc_state, plane_state))
1163 return 0;
1164
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001165 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001166
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001167 /*
Ville Syrjälä52913622021-05-14 15:57:41 +03001168 * WaUse32BppForSRWM:ctg,elk
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001169 *
Ville Syrjälä52913622021-05-14 15:57:41 +03001170 * The spec fails to list this restriction for the
1171 * HPLL watermark, which seems a little strange.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001172 * Let's use 32bpp for the HPLL watermark as well.
1173 */
Ville Syrjälä52913622021-05-14 15:57:41 +03001174 if (plane->id == PLANE_PRIMARY &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001175 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001176 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001177
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001178 clock = pipe_mode->crtc_clock;
1179 htotal = pipe_mode->crtc_htotal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001180
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001181 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001182
1183 if (plane->id == PLANE_CURSOR) {
1184 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1185 } else if (plane->id == PLANE_PRIMARY &&
1186 level == G4X_WM_LEVEL_NORMAL) {
1187 wm = intel_wm_method1(clock, cpp, latency);
1188 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001189 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001190
1191 small = intel_wm_method1(clock, cpp, latency);
1192 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1193
1194 wm = min(small, large);
1195 }
1196
1197 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1198 width, cpp);
1199
1200 wm = DIV_ROUND_UP(wm, 64) + 2;
1201
Chris Wilson1a1f1282017-11-07 14:03:38 +00001202 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001203}
1204
1205static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1206 int level, enum plane_id plane_id, u16 value)
1207{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001208 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001209 bool dirty = false;
1210
1211 for (; level < intel_wm_num_levels(dev_priv); level++) {
1212 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1213
1214 dirty |= raw->plane[plane_id] != value;
1215 raw->plane[plane_id] = value;
1216 }
1217
1218 return dirty;
1219}
1220
1221static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1222 int level, u16 value)
1223{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001224 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001225 bool dirty = false;
1226
1227 /* NORMAL level doesn't have an FBC watermark */
1228 level = max(level, G4X_WM_LEVEL_SR);
1229
1230 for (; level < intel_wm_num_levels(dev_priv); level++) {
1231 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1232
1233 dirty |= raw->fbc != value;
1234 raw->fbc = value;
1235 }
1236
1237 return dirty;
1238}
1239
Maarten Lankhorstec193642019-06-28 10:55:17 +02001240static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1241 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001242 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001243
1244static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1245 const struct intel_plane_state *plane_state)
1246{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001247 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001248 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001249 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1250 enum plane_id plane_id = plane->id;
1251 bool dirty = false;
1252 int level;
1253
1254 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1255 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1256 if (plane_id == PLANE_PRIMARY)
1257 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1258 goto out;
1259 }
1260
1261 for (level = 0; level < num_levels; level++) {
1262 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1263 int wm, max_wm;
1264
1265 wm = g4x_compute_wm(crtc_state, plane_state, level);
1266 max_wm = g4x_plane_fifo_size(plane_id, level);
1267
1268 if (wm > max_wm)
1269 break;
1270
1271 dirty |= raw->plane[plane_id] != wm;
1272 raw->plane[plane_id] = wm;
1273
1274 if (plane_id != PLANE_PRIMARY ||
1275 level == G4X_WM_LEVEL_NORMAL)
1276 continue;
1277
1278 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1279 raw->plane[plane_id]);
1280 max_wm = g4x_fbc_fifo_size(level);
1281
1282 /*
1283 * FBC wm is not mandatory as we
1284 * can always just disable its use.
1285 */
1286 if (wm > max_wm)
1287 wm = USHRT_MAX;
1288
1289 dirty |= raw->fbc != wm;
1290 raw->fbc = wm;
1291 }
1292
1293 /* mark watermarks as invalid */
1294 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1295
1296 if (plane_id == PLANE_PRIMARY)
1297 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1298
1299 out:
1300 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001301 drm_dbg_kms(&dev_priv->drm,
1302 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1303 plane->base.name,
1304 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1305 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1306 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001307
1308 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001309 drm_dbg_kms(&dev_priv->drm,
1310 "FBC watermarks: SR=%d, HPLL=%d\n",
1311 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1312 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001313 }
1314
1315 return dirty;
1316}
1317
1318static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1319 enum plane_id plane_id, int level)
1320{
1321 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1322
1323 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1324}
1325
1326static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1327 int level)
1328{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001329 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001330
1331 if (level > dev_priv->wm.max_level)
1332 return false;
1333
1334 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1335 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1336 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1337}
1338
1339/* mark all levels starting from 'level' as invalid */
1340static void g4x_invalidate_wms(struct intel_crtc *crtc,
1341 struct g4x_wm_state *wm_state, int level)
1342{
1343 if (level <= G4X_WM_LEVEL_NORMAL) {
1344 enum plane_id plane_id;
1345
1346 for_each_plane_id_on_crtc(crtc, plane_id)
1347 wm_state->wm.plane[plane_id] = USHRT_MAX;
1348 }
1349
1350 if (level <= G4X_WM_LEVEL_SR) {
1351 wm_state->cxsr = false;
1352 wm_state->sr.cursor = USHRT_MAX;
1353 wm_state->sr.plane = USHRT_MAX;
1354 wm_state->sr.fbc = USHRT_MAX;
1355 }
1356
1357 if (level <= G4X_WM_LEVEL_HPLL) {
1358 wm_state->hpll_en = false;
1359 wm_state->hpll.cursor = USHRT_MAX;
1360 wm_state->hpll.plane = USHRT_MAX;
1361 wm_state->hpll.fbc = USHRT_MAX;
1362 }
1363}
1364
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001365static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1366 int level)
1367{
1368 if (level < G4X_WM_LEVEL_SR)
1369 return false;
1370
1371 if (level >= G4X_WM_LEVEL_SR &&
1372 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1373 return false;
1374
1375 if (level >= G4X_WM_LEVEL_HPLL &&
1376 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1377 return false;
1378
1379 return true;
1380}
1381
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001382static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1383 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001384{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001385 struct intel_crtc_state *crtc_state =
1386 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001387 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001388 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001389 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001390 const struct intel_plane_state *old_plane_state;
1391 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001392 struct intel_plane *plane;
1393 enum plane_id plane_id;
1394 int i, level;
1395 unsigned int dirty = 0;
1396
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001397 for_each_oldnew_intel_plane_in_state(state, plane,
1398 old_plane_state,
1399 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001400 if (new_plane_state->hw.crtc != &crtc->base &&
1401 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001402 continue;
1403
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001404 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001405 dirty |= BIT(plane->id);
1406 }
1407
1408 if (!dirty)
1409 return 0;
1410
1411 level = G4X_WM_LEVEL_NORMAL;
1412 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1413 goto out;
1414
1415 raw = &crtc_state->wm.g4x.raw[level];
1416 for_each_plane_id_on_crtc(crtc, plane_id)
1417 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1418
1419 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001420 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1421 goto out;
1422
1423 raw = &crtc_state->wm.g4x.raw[level];
1424 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1425 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1426 wm_state->sr.fbc = raw->fbc;
1427
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001428 wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001429
1430 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001431 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1432 goto out;
1433
1434 raw = &crtc_state->wm.g4x.raw[level];
1435 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1436 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1437 wm_state->hpll.fbc = raw->fbc;
1438
1439 wm_state->hpll_en = wm_state->cxsr;
1440
1441 level++;
1442
1443 out:
1444 if (level == G4X_WM_LEVEL_NORMAL)
1445 return -EINVAL;
1446
1447 /* invalidate the higher levels */
1448 g4x_invalidate_wms(crtc, wm_state, level);
1449
1450 /*
1451 * Determine if the FBC watermark(s) can be used. IF
1452 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001453 * watermark(s) rather than disable the SR/HPLL
1454 * level(s) entirely. 'level-1' is the highest valid
1455 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001456 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001457 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001458
1459 return 0;
1460}
1461
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001462static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1463 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001464{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301465 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001466 struct intel_crtc_state *new_crtc_state =
1467 intel_atomic_get_new_crtc_state(state, crtc);
1468 const struct intel_crtc_state *old_crtc_state =
1469 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001470 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1471 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001472 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001473 enum plane_id plane_id;
1474
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001475 if (!new_crtc_state->hw.active ||
1476 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001477 *intermediate = *optimal;
1478
1479 intermediate->cxsr = false;
1480 intermediate->hpll_en = false;
1481 goto out;
1482 }
1483
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001484 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001485 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001486 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001487 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001488 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1489
1490 for_each_plane_id_on_crtc(crtc, plane_id) {
1491 intermediate->wm.plane[plane_id] =
1492 max(optimal->wm.plane[plane_id],
1493 active->wm.plane[plane_id]);
1494
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301495 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1496 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001497 }
1498
1499 intermediate->sr.plane = max(optimal->sr.plane,
1500 active->sr.plane);
1501 intermediate->sr.cursor = max(optimal->sr.cursor,
1502 active->sr.cursor);
1503 intermediate->sr.fbc = max(optimal->sr.fbc,
1504 active->sr.fbc);
1505
1506 intermediate->hpll.plane = max(optimal->hpll.plane,
1507 active->hpll.plane);
1508 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1509 active->hpll.cursor);
1510 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1511 active->hpll.fbc);
1512
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301513 drm_WARN_ON(&dev_priv->drm,
1514 (intermediate->sr.plane >
1515 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1516 intermediate->sr.cursor >
1517 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1518 intermediate->cxsr);
1519 drm_WARN_ON(&dev_priv->drm,
1520 (intermediate->sr.plane >
1521 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1522 intermediate->sr.cursor >
1523 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1524 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001525
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301526 drm_WARN_ON(&dev_priv->drm,
1527 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1528 intermediate->fbc_en && intermediate->cxsr);
1529 drm_WARN_ON(&dev_priv->drm,
1530 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1531 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001532
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001533out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001534 /*
1535 * If our intermediate WM are identical to the final WM, then we can
1536 * omit the post-vblank programming; only update if it's different.
1537 */
1538 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001539 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001540
1541 return 0;
1542}
1543
1544static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1545 struct g4x_wm_values *wm)
1546{
1547 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001548 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001549
1550 wm->cxsr = true;
1551 wm->hpll_en = true;
1552 wm->fbc_en = true;
1553
1554 for_each_intel_crtc(&dev_priv->drm, crtc) {
1555 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1556
1557 if (!crtc->active)
1558 continue;
1559
1560 if (!wm_state->cxsr)
1561 wm->cxsr = false;
1562 if (!wm_state->hpll_en)
1563 wm->hpll_en = false;
1564 if (!wm_state->fbc_en)
1565 wm->fbc_en = false;
1566
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001567 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001568 }
1569
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001570 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001571 wm->cxsr = false;
1572 wm->hpll_en = false;
1573 wm->fbc_en = false;
1574 }
1575
1576 for_each_intel_crtc(&dev_priv->drm, crtc) {
1577 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1578 enum pipe pipe = crtc->pipe;
1579
1580 wm->pipe[pipe] = wm_state->wm;
1581 if (crtc->active && wm->cxsr)
1582 wm->sr = wm_state->sr;
1583 if (crtc->active && wm->hpll_en)
1584 wm->hpll = wm_state->hpll;
1585 }
1586}
1587
1588static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1589{
1590 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1591 struct g4x_wm_values new_wm = {};
1592
1593 g4x_merge_wm(dev_priv, &new_wm);
1594
1595 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1596 return;
1597
1598 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1599 _intel_set_memory_cxsr(dev_priv, false);
1600
1601 g4x_write_wm_values(dev_priv, &new_wm);
1602
1603 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1604 _intel_set_memory_cxsr(dev_priv, true);
1605
1606 *old_wm = new_wm;
1607}
1608
1609static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001610 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001611{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001612 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1613 const struct intel_crtc_state *crtc_state =
1614 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001615
1616 mutex_lock(&dev_priv->wm.wm_mutex);
1617 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1618 g4x_program_watermarks(dev_priv);
1619 mutex_unlock(&dev_priv->wm.wm_mutex);
1620}
1621
1622static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001623 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001624{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001625 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1626 const struct intel_crtc_state *crtc_state =
1627 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001628
1629 if (!crtc_state->wm.need_postvbl_update)
1630 return;
1631
1632 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001633 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001634 g4x_program_watermarks(dev_priv);
1635 mutex_unlock(&dev_priv->wm.wm_mutex);
1636}
1637
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001638/* latency must be in 0.1us units. */
1639static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001640 unsigned int htotal,
1641 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001642 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001643 unsigned int latency)
1644{
1645 unsigned int ret;
1646
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001647 ret = intel_wm_method2(pixel_rate, htotal,
1648 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001649 ret = DIV_ROUND_UP(ret, 64);
1650
1651 return ret;
1652}
1653
Ville Syrjäläbb726512016-10-31 22:37:24 +02001654static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001655{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001656 /* all latencies in usec */
1657 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1658
Ville Syrjälä58590c12015-09-08 21:05:12 +03001659 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1660
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001661 if (IS_CHERRYVIEW(dev_priv)) {
1662 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1663 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001664
1665 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001666 }
1667}
1668
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001669static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1670 const struct intel_plane_state *plane_state,
1671 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001672{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001673 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001674 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001675 const struct drm_display_mode *pipe_mode =
1676 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001677 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001678
1679 if (dev_priv->wm.pri_latency[level] == 0)
1680 return USHRT_MAX;
1681
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001682 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001683 return 0;
1684
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001685 cpp = plane_state->hw.fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001686 clock = pipe_mode->crtc_clock;
1687 htotal = pipe_mode->crtc_htotal;
Ville Syrjäläe339d672016-11-28 19:37:17 +02001688 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001689
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001690 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001691 /*
1692 * FIXME the formula gives values that are
1693 * too big for the cursor FIFO, and hence we
1694 * would never be able to use cursors. For
1695 * now just hardcode the watermark.
1696 */
1697 wm = 63;
1698 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001699 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001700 dev_priv->wm.pri_latency[level] * 10);
1701 }
1702
Chris Wilson1a1f1282017-11-07 14:03:38 +00001703 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001704}
1705
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001706static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1707{
1708 return (active_planes & (BIT(PLANE_SPRITE0) |
1709 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1710}
1711
Ville Syrjälä5012e602017-03-02 19:14:56 +02001712static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001713{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001714 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301715 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001716 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001717 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001718 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001719 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001720 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001721 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001722 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001723 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001724 unsigned int total_rate;
1725 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001726
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001727 /*
1728 * When enabling sprite0 after sprite1 has already been enabled
1729 * we tend to get an underrun unless sprite0 already has some
1730 * FIFO space allcoated. Hence we always allocate at least one
1731 * cacheline for sprite0 whenever sprite1 is enabled.
1732 *
1733 * All other plane enable sequences appear immune to this problem.
1734 */
1735 if (vlv_need_sprite0_fifo_workaround(active_planes))
1736 sprite0_fifo_extra = 1;
1737
Ville Syrjälä5012e602017-03-02 19:14:56 +02001738 total_rate = raw->plane[PLANE_PRIMARY] +
1739 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001740 raw->plane[PLANE_SPRITE1] +
1741 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001742
Ville Syrjälä5012e602017-03-02 19:14:56 +02001743 if (total_rate > fifo_size)
1744 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001745
Ville Syrjälä5012e602017-03-02 19:14:56 +02001746 if (total_rate == 0)
1747 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001748
Ville Syrjälä5012e602017-03-02 19:14:56 +02001749 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001750 unsigned int rate;
1751
Ville Syrjälä5012e602017-03-02 19:14:56 +02001752 if ((active_planes & BIT(plane_id)) == 0) {
1753 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001754 continue;
1755 }
1756
Ville Syrjälä5012e602017-03-02 19:14:56 +02001757 rate = raw->plane[plane_id];
1758 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1759 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001760 }
1761
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001762 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1763 fifo_left -= sprite0_fifo_extra;
1764
Ville Syrjälä5012e602017-03-02 19:14:56 +02001765 fifo_state->plane[PLANE_CURSOR] = 63;
1766
1767 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001768
1769 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001770 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001771 int plane_extra;
1772
1773 if (fifo_left == 0)
1774 break;
1775
Ville Syrjälä5012e602017-03-02 19:14:56 +02001776 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001777 continue;
1778
1779 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001780 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001781 fifo_left -= plane_extra;
1782 }
1783
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301784 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001785
1786 /* give it all to the first plane if none are active */
1787 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301788 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001789 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1790 }
1791
1792 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001793}
1794
Ville Syrjäläff32c542017-03-02 19:14:57 +02001795/* mark all levels starting from 'level' as invalid */
1796static void vlv_invalidate_wms(struct intel_crtc *crtc,
1797 struct vlv_wm_state *wm_state, int level)
1798{
1799 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1800
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001801 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001802 enum plane_id plane_id;
1803
1804 for_each_plane_id_on_crtc(crtc, plane_id)
1805 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1806
1807 wm_state->sr[level].cursor = USHRT_MAX;
1808 wm_state->sr[level].plane = USHRT_MAX;
1809 }
1810}
1811
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001812static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1813{
1814 if (wm > fifo_size)
1815 return USHRT_MAX;
1816 else
1817 return fifo_size - wm;
1818}
1819
Ville Syrjäläff32c542017-03-02 19:14:57 +02001820/*
1821 * Starting from 'level' set all higher
1822 * levels to 'value' in the "raw" watermarks.
1823 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001824static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001825 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001826{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001827 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001828 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001829 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001830
Ville Syrjäläff32c542017-03-02 19:14:57 +02001831 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001832 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001833
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001834 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001836 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001837
1838 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001839}
1840
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001841static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1842 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001843{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001844 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001845 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001846 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001847 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001848 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001849 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001850
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001851 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001852 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1853 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001854 }
1855
1856 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001857 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001858 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1859 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1860
Ville Syrjäläff32c542017-03-02 19:14:57 +02001861 if (wm > max_wm)
1862 break;
1863
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001864 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001865 raw->plane[plane_id] = wm;
1866 }
1867
1868 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001869 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001870
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001871out:
1872 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001873 drm_dbg_kms(&dev_priv->drm,
1874 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1875 plane->base.name,
1876 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1877 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1878 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001879
1880 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001881}
1882
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001883static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1884 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001885{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001886 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001887 &crtc_state->wm.vlv.raw[level];
1888 const struct vlv_fifo_state *fifo_state =
1889 &crtc_state->wm.vlv.fifo_state;
1890
1891 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1892}
1893
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001894static 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 +02001895{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001896 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1897 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1898 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1899 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001900}
1901
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001902static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1903 struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001904{
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001905 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001906 struct intel_crtc_state *crtc_state =
1907 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001908 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001909 const struct vlv_fifo_state *fifo_state =
1910 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001911 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1912 int num_active_planes = hweight8(active_planes);
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001913 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001914 const struct intel_plane_state *old_plane_state;
1915 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001916 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001917 enum plane_id plane_id;
1918 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001919 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001920
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001921 for_each_oldnew_intel_plane_in_state(state, plane,
1922 old_plane_state,
1923 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001924 if (new_plane_state->hw.crtc != &crtc->base &&
1925 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001926 continue;
1927
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001928 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001929 dirty |= BIT(plane->id);
1930 }
1931
1932 /*
1933 * DSPARB registers may have been reset due to the
1934 * power well being turned off. Make sure we restore
1935 * them to a consistent state even if no primary/sprite
1936 * planes are initially active.
1937 */
1938 if (needs_modeset)
1939 crtc_state->fifo_changed = true;
1940
1941 if (!dirty)
1942 return 0;
1943
1944 /* cursor changes don't warrant a FIFO recompute */
1945 if (dirty & ~BIT(PLANE_CURSOR)) {
1946 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001947 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001948 const struct vlv_fifo_state *old_fifo_state =
1949 &old_crtc_state->wm.vlv.fifo_state;
1950
1951 ret = vlv_compute_fifo(crtc_state);
1952 if (ret)
1953 return ret;
1954
1955 if (needs_modeset ||
1956 memcmp(old_fifo_state, fifo_state,
1957 sizeof(*fifo_state)) != 0)
1958 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001959 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001960
Ville Syrjäläff32c542017-03-02 19:14:57 +02001961 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001962 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001963 /*
1964 * Note that enabling cxsr with no primary/sprite planes
1965 * enabled can wedge the pipe. Hence we only allow cxsr
1966 * with exactly one enabled primary/sprite plane.
1967 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001968 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001969
Ville Syrjälä5012e602017-03-02 19:14:56 +02001970 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001971 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001972 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001973
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001974 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001975 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001976
Ville Syrjäläff32c542017-03-02 19:14:57 +02001977 for_each_plane_id_on_crtc(crtc, plane_id) {
1978 wm_state->wm[level].plane[plane_id] =
1979 vlv_invert_wm_value(raw->plane[plane_id],
1980 fifo_state->plane[plane_id]);
1981 }
1982
1983 wm_state->sr[level].plane =
1984 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001985 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001986 raw->plane[PLANE_SPRITE1]),
1987 sr_fifo_size);
1988
1989 wm_state->sr[level].cursor =
1990 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1991 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001992 }
1993
Ville Syrjäläff32c542017-03-02 19:14:57 +02001994 if (level == 0)
1995 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001996
Ville Syrjäläff32c542017-03-02 19:14:57 +02001997 /* limit to only levels we can actually handle */
1998 wm_state->num_levels = level;
1999
2000 /* invalidate the higher levels */
2001 vlv_invalidate_wms(crtc, wm_state, level);
2002
2003 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002004}
2005
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002006#define VLV_FIFO(plane, value) \
2007 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
2008
Ville Syrjäläff32c542017-03-02 19:14:57 +02002009static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002010 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002011{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002012 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002013 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002014 const struct intel_crtc_state *crtc_state =
2015 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002016 const struct vlv_fifo_state *fifo_state =
2017 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002018 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002019 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002020
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002021 if (!crtc_state->fifo_changed)
2022 return;
2023
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002024 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2025 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2026 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002027
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302028 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2029 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002030
Ville Syrjäläc137d662017-03-02 19:15:06 +02002031 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2032
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002033 /*
2034 * uncore.lock serves a double purpose here. It allows us to
2035 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2036 * it protects the DSPARB registers from getting clobbered by
2037 * parallel updates from multiple pipes.
2038 *
2039 * intel_pipe_update_start() has already disabled interrupts
2040 * for us, so a plain spin_lock() is sufficient here.
2041 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002042 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002043
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002044 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002045 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002046 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2047 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002048
2049 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2050 VLV_FIFO(SPRITEB, 0xff));
2051 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2052 VLV_FIFO(SPRITEB, sprite1_start));
2053
2054 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2055 VLV_FIFO(SPRITEB_HI, 0x1));
2056 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2057 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2058
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002059 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2060 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002061 break;
2062 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002063 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2064 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002065
2066 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2067 VLV_FIFO(SPRITED, 0xff));
2068 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2069 VLV_FIFO(SPRITED, sprite1_start));
2070
2071 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2072 VLV_FIFO(SPRITED_HI, 0xff));
2073 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2074 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2075
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002076 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2077 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002078 break;
2079 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002080 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2081 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002082
2083 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2084 VLV_FIFO(SPRITEF, 0xff));
2085 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2086 VLV_FIFO(SPRITEF, sprite1_start));
2087
2088 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2089 VLV_FIFO(SPRITEF_HI, 0xff));
2090 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2091 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2092
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002093 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2094 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002095 break;
2096 default:
2097 break;
2098 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002099
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002100 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002101
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002102 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002103}
2104
2105#undef VLV_FIFO
2106
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002107static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
2108 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002109{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002110 struct intel_crtc_state *new_crtc_state =
2111 intel_atomic_get_new_crtc_state(state, crtc);
2112 const struct intel_crtc_state *old_crtc_state =
2113 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002114 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2115 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002116 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002117 int level;
2118
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002119 if (!new_crtc_state->hw.active ||
2120 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002121 *intermediate = *optimal;
2122
2123 intermediate->cxsr = false;
2124 goto out;
2125 }
2126
Ville Syrjälä4841da52017-03-02 19:14:59 +02002127 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002128 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002129 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002130
2131 for (level = 0; level < intermediate->num_levels; level++) {
2132 enum plane_id plane_id;
2133
2134 for_each_plane_id_on_crtc(crtc, plane_id) {
2135 intermediate->wm[level].plane[plane_id] =
2136 min(optimal->wm[level].plane[plane_id],
2137 active->wm[level].plane[plane_id]);
2138 }
2139
2140 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2141 active->sr[level].plane);
2142 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2143 active->sr[level].cursor);
2144 }
2145
2146 vlv_invalidate_wms(crtc, intermediate, level);
2147
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002148out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002149 /*
2150 * If our intermediate WM are identical to the final WM, then we can
2151 * omit the post-vblank programming; only update if it's different.
2152 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002153 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002154 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002155
2156 return 0;
2157}
2158
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002159static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002160 struct vlv_wm_values *wm)
2161{
2162 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002163 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002164
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002165 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002166 wm->cxsr = true;
2167
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002168 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002169 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002170
2171 if (!crtc->active)
2172 continue;
2173
2174 if (!wm_state->cxsr)
2175 wm->cxsr = false;
2176
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002177 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002178 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2179 }
2180
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002181 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002182 wm->cxsr = false;
2183
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002184 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002185 wm->level = VLV_WM_LEVEL_PM2;
2186
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002187 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002188 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002189 enum pipe pipe = crtc->pipe;
2190
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002191 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002192 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002193 wm->sr = wm_state->sr[wm->level];
2194
Ville Syrjälä1b313892016-11-28 19:37:08 +02002195 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2196 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2197 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2198 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002199 }
2200}
2201
Ville Syrjäläff32c542017-03-02 19:14:57 +02002202static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002203{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002204 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2205 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002206
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002207 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002208
Ville Syrjäläff32c542017-03-02 19:14:57 +02002209 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002210 return;
2211
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002212 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002213 chv_set_memory_dvfs(dev_priv, false);
2214
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002215 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002216 chv_set_memory_pm5(dev_priv, false);
2217
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002218 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002219 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002220
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002221 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002222
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002223 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002224 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002225
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002226 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002227 chv_set_memory_pm5(dev_priv, true);
2228
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002229 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002230 chv_set_memory_dvfs(dev_priv, true);
2231
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002232 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002233}
2234
Ville Syrjäläff32c542017-03-02 19:14:57 +02002235static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002236 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002237{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002238 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2239 const struct intel_crtc_state *crtc_state =
2240 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002241
2242 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002243 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2244 vlv_program_watermarks(dev_priv);
2245 mutex_unlock(&dev_priv->wm.wm_mutex);
2246}
2247
2248static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002249 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002250{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002251 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2252 const struct intel_crtc_state *crtc_state =
2253 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002254
2255 if (!crtc_state->wm.need_postvbl_update)
2256 return;
2257
2258 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002259 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002260 vlv_program_watermarks(dev_priv);
2261 mutex_unlock(&dev_priv->wm.wm_mutex);
2262}
2263
Dave Airlieef9c66a2021-09-29 01:57:47 +03002264static void i965_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002265{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002266 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002267 int srwm = 1;
2268 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002269 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002270
2271 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002272 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002273 if (crtc) {
2274 /* self-refresh has much higher latency */
2275 static const int sr_latency_ns = 12000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002276 const struct drm_display_mode *pipe_mode =
2277 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002278 const struct drm_framebuffer *fb =
2279 crtc->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002280 int clock = pipe_mode->crtc_clock;
2281 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002282 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002283 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002284 int entries;
2285
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002286 entries = intel_wm_method2(clock, htotal,
2287 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002288 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2289 srwm = I965_FIFO_SIZE - entries;
2290 if (srwm < 0)
2291 srwm = 1;
2292 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002293 drm_dbg_kms(&dev_priv->drm,
2294 "self-refresh entries: %d, wm: %d\n",
2295 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002296
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002297 entries = intel_wm_method2(clock, htotal,
2298 crtc->base.cursor->state->crtc_w, 4,
2299 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002300 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002301 i965_cursor_wm_info.cacheline_size) +
2302 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002303
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002304 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002305 if (cursor_sr > i965_cursor_wm_info.max_wm)
2306 cursor_sr = i965_cursor_wm_info.max_wm;
2307
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002308 drm_dbg_kms(&dev_priv->drm,
2309 "self-refresh watermark: display plane %d "
2310 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002311
Imre Deak98584252014-06-13 14:54:20 +03002312 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002313 } else {
Imre Deak98584252014-06-13 14:54:20 +03002314 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002315 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002316 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002317 }
2318
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002319 drm_dbg_kms(&dev_priv->drm,
2320 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2321 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002322
2323 /* 965 has limitations... */
Jani Nikula5f461662020-11-30 13:15:58 +02002324 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002325 FW_WM(8, CURSORB) |
2326 FW_WM(8, PLANEB) |
2327 FW_WM(8, PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02002328 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002329 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002330 /* update cursor SR watermark */
Jani Nikula5f461662020-11-30 13:15:58 +02002331 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002332
2333 if (cxsr_enabled)
2334 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002335}
2336
Ville Syrjäläf4998962015-03-10 17:02:21 +02002337#undef FW_WM
2338
Dave Airlieef9c66a2021-09-29 01:57:47 +03002339static void i9xx_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002340{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002342 u32 fwater_lo;
2343 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344 int cwm, srwm = 1;
2345 int fifo_size;
2346 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002347 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002348
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002349 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002350 wm_info = &i945_wm_info;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002351 else if (DISPLAY_VER(dev_priv) != 2)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002352 wm_info = &i915_wm_info;
2353 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002354 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002355
Dave Airlie758b2fc2021-09-29 01:57:46 +03002356 if (DISPLAY_VER(dev_priv) == 2)
2357 fifo_size = i830_get_fifo_size(dev_priv, PLANE_A);
2358 else
2359 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_A);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002360 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002361 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002362 const struct drm_display_mode *pipe_mode =
2363 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002364 const struct drm_framebuffer *fb =
2365 crtc->base.primary->state->fb;
2366 int cpp;
2367
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002368 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002369 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002370 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002371 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002372
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002373 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002374 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002375 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002376 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002377 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002378 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002379 if (planea_wm > (long)wm_info->max_wm)
2380 planea_wm = wm_info->max_wm;
2381 }
2382
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002383 if (DISPLAY_VER(dev_priv) == 2)
Ville Syrjälä9d539102014-08-15 01:21:53 +03002384 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002385
Dave Airlie758b2fc2021-09-29 01:57:46 +03002386 if (DISPLAY_VER(dev_priv) == 2)
2387 fifo_size = i830_get_fifo_size(dev_priv, PLANE_B);
2388 else
2389 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_B);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002390 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002391 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002392 const struct drm_display_mode *pipe_mode =
2393 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002394 const struct drm_framebuffer *fb =
2395 crtc->base.primary->state->fb;
2396 int cpp;
2397
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002398 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002399 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002400 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002401 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002402
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002403 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002404 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002405 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002406 if (enabled == NULL)
2407 enabled = crtc;
2408 else
2409 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002410 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002411 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002412 if (planeb_wm > (long)wm_info->max_wm)
2413 planeb_wm = wm_info->max_wm;
2414 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002415
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002416 drm_dbg_kms(&dev_priv->drm,
2417 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002418
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002419 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002420 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002421
Ville Syrjäläefc26112016-10-31 22:37:04 +02002422 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002423
2424 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002425 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002426 enabled = NULL;
2427 }
2428
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002429 /*
2430 * Overlay gets an aggressive default since video jitter is bad.
2431 */
2432 cwm = 2;
2433
2434 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002435 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002436
2437 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002438 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002439 /* self-refresh has much higher latency */
2440 static const int sr_latency_ns = 6000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002441 const struct drm_display_mode *pipe_mode =
2442 &enabled->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002443 const struct drm_framebuffer *fb =
2444 enabled->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002445 int clock = pipe_mode->crtc_clock;
2446 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002447 int hdisplay = enabled->config->pipe_src_w;
2448 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002449 int entries;
2450
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002451 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002452 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002453 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002454 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002455
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002456 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2457 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002458 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002459 drm_dbg_kms(&dev_priv->drm,
2460 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002461 srwm = wm_info->fifo_size - entries;
2462 if (srwm < 0)
2463 srwm = 1;
2464
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002465 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02002466 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002467 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002468 else
Jani Nikula5f461662020-11-30 13:15:58 +02002469 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002470 }
2471
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002472 drm_dbg_kms(&dev_priv->drm,
2473 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2474 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002475
2476 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2477 fwater_hi = (cwm & 0x1f);
2478
2479 /* Set request length to 8 cachelines per fetch */
2480 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2481 fwater_hi = fwater_hi | (1 << 8);
2482
Jani Nikula5f461662020-11-30 13:15:58 +02002483 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2484 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002485
Imre Deak5209b1f2014-07-01 12:36:17 +03002486 if (enabled)
2487 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002488}
2489
Dave Airlieef9c66a2021-09-29 01:57:47 +03002490static void i845_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002491{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002492 struct intel_crtc *crtc;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002493 const struct drm_display_mode *pipe_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002494 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002495 int planea_wm;
2496
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002497 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002498 if (crtc == NULL)
2499 return;
2500
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002501 pipe_mode = &crtc->config->hw.pipe_mode;
2502 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002503 &i845_wm_info,
Dave Airlie758b2fc2021-09-29 01:57:46 +03002504 i845_get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002505 4, pessimal_latency_ns);
Jani Nikula5f461662020-11-30 13:15:58 +02002506 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002507 fwater_lo |= (3<<8) | planea_wm;
2508
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002509 drm_dbg_kms(&dev_priv->drm,
2510 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002511
Jani Nikula5f461662020-11-30 13:15:58 +02002512 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002513}
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_method1(unsigned int pixel_rate,
2517 unsigned int cpp,
2518 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002520 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002522 ret = intel_wm_method1(pixel_rate, cpp, latency);
2523 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002524
2525 return ret;
2526}
2527
Ville Syrjälä37126462013-08-01 16:18:55 +03002528/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002529static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2530 unsigned int htotal,
2531 unsigned int width,
2532 unsigned int cpp,
2533 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002534{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002535 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002536
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002537 ret = intel_wm_method2(pixel_rate, htotal,
2538 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002539 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002540
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002541 return ret;
2542}
2543
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002544static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002545{
Matt Roper15126882015-12-03 11:37:40 -08002546 /*
2547 * Neither of these should be possible since this function shouldn't be
2548 * called if the CRTC is off or the plane is invisible. But let's be
2549 * extra paranoid to avoid a potential divide-by-zero if we screw up
2550 * elsewhere in the driver.
2551 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002552 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002553 return 0;
2554 if (WARN_ON(!horiz_pixels))
2555 return 0;
2556
Ville Syrjäläac484962016-01-20 21:05:26 +02002557 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002558}
2559
Imre Deak820c1982013-12-17 14:46:36 +02002560struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002561 u16 pri;
2562 u16 spr;
2563 u16 cur;
2564 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002565};
2566
Ville Syrjälä37126462013-08-01 16:18:55 +03002567/*
2568 * For both WM_PIPE and WM_LP.
2569 * mem_value must be in 0.1us units.
2570 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002571static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2572 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002573 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002574{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002575 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002576 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002577
Ville Syrjälä03981c62018-11-14 19:34:40 +02002578 if (mem_value == 0)
2579 return U32_MAX;
2580
Maarten Lankhorstec193642019-06-28 10:55:17 +02002581 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002582 return 0;
2583
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002584 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002585
Maarten Lankhorstec193642019-06-28 10:55:17 +02002586 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002587
2588 if (!is_lp)
2589 return method1;
2590
Maarten Lankhorstec193642019-06-28 10:55:17 +02002591 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002592 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002593 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002594 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002595
2596 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002597}
2598
Ville Syrjälä37126462013-08-01 16:18:55 +03002599/*
2600 * For both WM_PIPE and WM_LP.
2601 * mem_value must be in 0.1us units.
2602 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002603static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2604 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002605 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002606{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002607 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002608 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002609
Ville Syrjälä03981c62018-11-14 19:34:40 +02002610 if (mem_value == 0)
2611 return U32_MAX;
2612
Maarten Lankhorstec193642019-06-28 10:55:17 +02002613 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002614 return 0;
2615
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002616 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002617
Maarten Lankhorstec193642019-06-28 10:55:17 +02002618 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2619 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002620 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002621 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002622 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002623 return min(method1, method2);
2624}
2625
Ville Syrjälä37126462013-08-01 16:18:55 +03002626/*
2627 * For both WM_PIPE and WM_LP.
2628 * mem_value must be in 0.1us units.
2629 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002630static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2631 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002632 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002633{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002634 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002635
Ville Syrjälä03981c62018-11-14 19:34:40 +02002636 if (mem_value == 0)
2637 return U32_MAX;
2638
Maarten Lankhorstec193642019-06-28 10:55:17 +02002639 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002640 return 0;
2641
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002642 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002643
Maarten Lankhorstec193642019-06-28 10:55:17 +02002644 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002645 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002646 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002647 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002648}
2649
Paulo Zanonicca32e92013-05-31 11:45:06 -03002650/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002651static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2652 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002653 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002654{
Ville Syrjälä83054942016-11-18 21:53:00 +02002655 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002656
Maarten Lankhorstec193642019-06-28 10:55:17 +02002657 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002658 return 0;
2659
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002660 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002661
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002662 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2663 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002664}
2665
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002666static unsigned int
2667ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002668{
Matt Roper7dadd282021-03-19 21:42:43 -07002669 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002670 return 3072;
Matt Roper7dadd282021-03-19 21:42:43 -07002671 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002672 return 768;
2673 else
2674 return 512;
2675}
2676
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002677static unsigned int
2678ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2679 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002680{
Matt Roper7dadd282021-03-19 21:42:43 -07002681 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002682 /* BDW primary/sprite plane watermarks */
2683 return level == 0 ? 255 : 2047;
Matt Roper7dadd282021-03-19 21:42:43 -07002684 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002685 /* IVB/HSW primary/sprite plane watermarks */
2686 return level == 0 ? 127 : 1023;
2687 else if (!is_sprite)
2688 /* ILK/SNB primary plane watermarks */
2689 return level == 0 ? 127 : 511;
2690 else
2691 /* ILK/SNB sprite plane watermarks */
2692 return level == 0 ? 63 : 255;
2693}
2694
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002695static unsigned int
2696ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002697{
Matt Roper7dadd282021-03-19 21:42:43 -07002698 if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002699 return level == 0 ? 63 : 255;
2700 else
2701 return level == 0 ? 31 : 63;
2702}
2703
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002704static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002705{
Matt Roper7dadd282021-03-19 21:42:43 -07002706 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002707 return 31;
2708 else
2709 return 15;
2710}
2711
Ville Syrjälä158ae642013-08-07 13:28:19 +03002712/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002713static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002714 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002715 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002716 enum intel_ddb_partitioning ddb_partitioning,
2717 bool is_sprite)
2718{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002719 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002720
2721 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002722 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002723 return 0;
2724
2725 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002726 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002727 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002728
2729 /*
2730 * For some reason the non self refresh
2731 * FIFO size is only half of the self
2732 * refresh FIFO size on ILK/SNB.
2733 */
Matt Roper7dadd282021-03-19 21:42:43 -07002734 if (DISPLAY_VER(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002735 fifo_size /= 2;
2736 }
2737
Ville Syrjälä240264f2013-08-07 13:29:12 +03002738 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002739 /* level 0 is always calculated with 1:1 split */
2740 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2741 if (is_sprite)
2742 fifo_size *= 5;
2743 fifo_size /= 6;
2744 } else {
2745 fifo_size /= 2;
2746 }
2747 }
2748
2749 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002750 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002751}
2752
2753/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002754static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002755 int level,
2756 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002757{
2758 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002759 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002760 return 64;
2761
2762 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002763 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002764}
2765
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002766static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002767 int level,
2768 const struct intel_wm_config *config,
2769 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002770 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002771{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002772 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2773 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2774 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2775 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002776}
2777
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002778static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002779 int level,
2780 struct ilk_wm_maximums *max)
2781{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002782 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2783 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2784 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2785 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002786}
2787
Ville Syrjäläd9395652013-10-09 19:18:10 +03002788static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002789 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002790 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002791{
2792 bool ret;
2793
2794 /* already determined to be invalid? */
2795 if (!result->enable)
2796 return false;
2797
2798 result->enable = result->pri_val <= max->pri &&
2799 result->spr_val <= max->spr &&
2800 result->cur_val <= max->cur;
2801
2802 ret = result->enable;
2803
2804 /*
2805 * HACK until we can pre-compute everything,
2806 * and thus fail gracefully if LP0 watermarks
2807 * are exceeded...
2808 */
2809 if (level == 0 && !result->enable) {
2810 if (result->pri_val > max->pri)
2811 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2812 level, result->pri_val, max->pri);
2813 if (result->spr_val > max->spr)
2814 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2815 level, result->spr_val, max->spr);
2816 if (result->cur_val > max->cur)
2817 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2818 level, result->cur_val, max->cur);
2819
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002820 result->pri_val = min_t(u32, result->pri_val, max->pri);
2821 result->spr_val = min_t(u32, result->spr_val, max->spr);
2822 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002823 result->enable = true;
2824 }
2825
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002826 return ret;
2827}
2828
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002829static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002830 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002831 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002832 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002833 const struct intel_plane_state *pristate,
2834 const struct intel_plane_state *sprstate,
2835 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002836 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002837{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002838 u16 pri_latency = dev_priv->wm.pri_latency[level];
2839 u16 spr_latency = dev_priv->wm.spr_latency[level];
2840 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002841
2842 /* WM1+ latency values stored in 0.5us units */
2843 if (level > 0) {
2844 pri_latency *= 5;
2845 spr_latency *= 5;
2846 cur_latency *= 5;
2847 }
2848
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002849 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002850 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002851 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002852 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002853 }
2854
2855 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002856 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002857
2858 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002859 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002860
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002861 result->enable = true;
2862}
2863
Ville Syrjäläbb726512016-10-31 22:37:24 +02002864static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002865 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002866{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002867 struct intel_uncore *uncore = &dev_priv->uncore;
2868
Matt Roper7dadd282021-03-19 21:42:43 -07002869 if (DISPLAY_VER(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002870 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002871 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002872 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roperd3252e12021-08-20 15:57:10 -07002873 int mult = IS_DG2(dev_priv) ? 2 : 1;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002874
2875 /* read the first set of memory latencies[0:3] */
2876 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002877 ret = sandybridge_pcode_read(dev_priv,
2878 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002879 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002880
2881 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002882 drm_err(&dev_priv->drm,
2883 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002884 return;
2885 }
2886
Matt Roperd3252e12021-08-20 15:57:10 -07002887 wm[0] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2888 wm[1] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2889 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2890 wm[2] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2891 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2892 wm[3] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2893 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002894
2895 /* read the second set of memory latencies[4:7] */
2896 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002897 ret = sandybridge_pcode_read(dev_priv,
2898 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002899 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002900 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002901 drm_err(&dev_priv->drm,
2902 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002903 return;
2904 }
2905
Matt Roperd3252e12021-08-20 15:57:10 -07002906 wm[4] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2907 wm[5] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2908 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2909 wm[6] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2910 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2911 wm[7] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2912 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002913
Vandana Kannan367294b2014-11-04 17:06:46 +00002914 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002915 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2916 * need to be disabled. We make sure to sanitize the values out
2917 * of the punit to satisfy this requirement.
2918 */
2919 for (level = 1; level <= max_level; level++) {
2920 if (wm[level] == 0) {
2921 for (i = level + 1; i <= max_level; i++)
2922 wm[i] = 0;
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002923
2924 max_level = level - 1;
2925
Paulo Zanoni0727e402016-09-22 18:00:30 -03002926 break;
2927 }
2928 }
2929
2930 /*
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002931 * WaWmMemoryReadLatency
Damien Lespiau6f972352015-02-09 19:33:07 +00002932 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002933 * punit doesn't take into account the read latency so we need
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002934 * to add proper adjustement to each valid level we retrieve
2935 * from the punit when level 0 response data is 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002936 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002937 if (wm[0] == 0) {
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002938 u8 adjust = DISPLAY_VER(dev_priv) >= 12 ? 3 : 2;
2939
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002940 for (level = 0; level <= max_level; level++)
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002941 wm[level] += adjust;
Paulo Zanoni0727e402016-09-22 18:00:30 -03002942 }
2943
Mahesh Kumar86b59282018-08-31 16:39:42 +05302944 /*
2945 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2946 * If we could not get dimm info enable this WA to prevent from
2947 * any underrun. If not able to get Dimm info assume 16GB dimm
2948 * to avoid any underrun.
2949 */
José Roberto de Souza66a24502021-01-28 08:43:12 -08002950 if (dev_priv->dram_info.wm_lv_0_adjust_needed)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302951 wm[0] += 1;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002952 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002953 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002954
2955 wm[0] = (sskpd >> 56) & 0xFF;
2956 if (wm[0] == 0)
2957 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002958 wm[1] = (sskpd >> 4) & 0xFF;
2959 wm[2] = (sskpd >> 12) & 0xFF;
2960 wm[3] = (sskpd >> 20) & 0x1FF;
2961 wm[4] = (sskpd >> 32) & 0x1FF;
Matt Roper7dadd282021-03-19 21:42:43 -07002962 } else if (DISPLAY_VER(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002963 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002964
2965 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2966 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2967 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2968 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Matt Roper7dadd282021-03-19 21:42:43 -07002969 } else if (DISPLAY_VER(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002970 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002971
2972 /* ILK primary LP0 latency is 700 ns */
2973 wm[0] = 7;
2974 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2975 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002976 } else {
2977 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002978 }
2979}
2980
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002981static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002982 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002983{
2984 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002985 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002986 wm[0] = 13;
2987}
2988
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002989static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002990 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002991{
2992 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002993 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002994 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002995}
2996
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002997int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002998{
2999 /* how many WM levels are we expecting */
Matt Roper7959ffe2021-05-18 17:06:11 -07003000 if (HAS_HW_SAGV_WM(dev_priv))
3001 return 5;
3002 else if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003003 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003004 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03003005 return 4;
Matt Roper7dadd282021-03-19 21:42:43 -07003006 else if (DISPLAY_VER(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03003007 return 3;
3008 else
3009 return 2;
3010}
Daniel Vetter7526ed72014-09-29 15:07:19 +02003011
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003012static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003013 const char *name,
Linus Torvaldse7c6e402021-04-27 17:05:53 -07003014 const u16 wm[])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003015{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003016 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003017
3018 for (level = 0; level <= max_level; level++) {
3019 unsigned int latency = wm[level];
3020
3021 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003022 drm_dbg_kms(&dev_priv->drm,
3023 "%s WM%d latency not provided\n",
3024 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003025 continue;
3026 }
3027
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003028 /*
3029 * - latencies are in us on gen9.
3030 * - before then, WM1+ latency values are in 0.5us units
3031 */
Matt Roper7dadd282021-03-19 21:42:43 -07003032 if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003033 latency *= 10;
3034 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003035 latency *= 5;
3036
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003037 drm_dbg_kms(&dev_priv->drm,
3038 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3039 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003040 }
3041}
3042
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003043static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003044 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003045{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003046 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003047
3048 if (wm[0] >= min)
3049 return false;
3050
3051 wm[0] = max(wm[0], min);
3052 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003053 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003054
3055 return true;
3056}
3057
Ville Syrjäläbb726512016-10-31 22:37:24 +02003058static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003059{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003060 bool changed;
3061
3062 /*
3063 * The BIOS provided WM memory latency values are often
3064 * inadequate for high resolution displays. Adjust them.
3065 */
Nathan Chancellor2e705702021-10-14 14:19:16 -07003066 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12);
3067 changed |= ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12);
3068 changed |= ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003069
3070 if (!changed)
3071 return;
3072
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003073 drm_dbg_kms(&dev_priv->drm,
3074 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003075 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3076 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3077 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003078}
3079
Ville Syrjälä03981c62018-11-14 19:34:40 +02003080static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3081{
3082 /*
3083 * On some SNB machines (Thinkpad X220 Tablet at least)
3084 * LP3 usage can cause vblank interrupts to be lost.
3085 * The DEIIR bit will go high but it looks like the CPU
3086 * never gets interrupted.
3087 *
3088 * It's not clear whether other interrupt source could
3089 * be affected or if this is somehow limited to vblank
3090 * interrupts only. To play it safe we disable LP3
3091 * watermarks entirely.
3092 */
3093 if (dev_priv->wm.pri_latency[3] == 0 &&
3094 dev_priv->wm.spr_latency[3] == 0 &&
3095 dev_priv->wm.cur_latency[3] == 0)
3096 return;
3097
3098 dev_priv->wm.pri_latency[3] = 0;
3099 dev_priv->wm.spr_latency[3] = 0;
3100 dev_priv->wm.cur_latency[3] = 0;
3101
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003102 drm_dbg_kms(&dev_priv->drm,
3103 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003104 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3105 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3106 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3107}
3108
Ville Syrjäläbb726512016-10-31 22:37:24 +02003109static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003110{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003111 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003112
3113 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3114 sizeof(dev_priv->wm.pri_latency));
3115 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3116 sizeof(dev_priv->wm.pri_latency));
3117
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003118 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003119 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003120
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003121 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3122 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3123 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003124
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003125 if (DISPLAY_VER(dev_priv) == 6) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003126 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003127 snb_wm_lp3_irq_quirk(dev_priv);
3128 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003129}
3130
Ville Syrjäläbb726512016-10-31 22:37:24 +02003131static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003132{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003133 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003134 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003135}
3136
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003137static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003138 struct intel_pipe_wm *pipe_wm)
3139{
3140 /* LP0 watermark maximums depend on this pipe alone */
3141 const struct intel_wm_config config = {
3142 .num_pipes_active = 1,
3143 .sprites_enabled = pipe_wm->sprites_enabled,
3144 .sprites_scaled = pipe_wm->sprites_scaled,
3145 };
3146 struct ilk_wm_maximums max;
3147
3148 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003149 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003150
3151 /* At least LP0 must be valid */
3152 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003153 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003154 return false;
3155 }
3156
3157 return true;
3158}
3159
Matt Roper261a27d2015-10-08 15:28:25 -07003160/* Compute new watermarks for the pipe */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003161static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
3162 struct intel_crtc *crtc)
Matt Roper261a27d2015-10-08 15:28:25 -07003163{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003164 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3165 struct intel_crtc_state *crtc_state =
3166 intel_atomic_get_new_crtc_state(state, crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003167 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003168 struct intel_plane *plane;
3169 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003170 const struct intel_plane_state *pristate = NULL;
3171 const struct intel_plane_state *sprstate = NULL;
3172 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003173 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003174 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003175
Maarten Lankhorstec193642019-06-28 10:55:17 +02003176 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003177
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003178 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3179 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3180 pristate = plane_state;
3181 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3182 sprstate = plane_state;
3183 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3184 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003185 }
3186
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003187 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003188 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003189 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3190 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3191 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3192 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003193 }
3194
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003195 usable_level = max_level;
3196
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003197 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Matt Roper7dadd282021-03-19 21:42:43 -07003198 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003199 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003200
3201 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003202 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003203 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003204
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003205 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003206 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003207 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003208
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003209 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003210 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003211
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003212 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003213
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003214 for (level = 1; level <= usable_level; level++) {
3215 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003216
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003217 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003218 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003219
3220 /*
3221 * Disable any watermark level that exceeds the
3222 * register maximums since such watermarks are
3223 * always invalid.
3224 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003225 if (!ilk_validate_wm_level(level, &max, wm)) {
3226 memset(wm, 0, sizeof(*wm));
3227 break;
3228 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003229 }
3230
Matt Roper86c8bbb2015-09-24 15:53:16 -07003231 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003232}
3233
3234/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003235 * Build a set of 'intermediate' watermark values that satisfy both the old
3236 * state and the new state. These can be programmed to the hardware
3237 * immediately.
3238 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003239static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
3240 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08003241{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003242 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3243 struct intel_crtc_state *new_crtc_state =
3244 intel_atomic_get_new_crtc_state(state, crtc);
3245 const struct intel_crtc_state *old_crtc_state =
3246 intel_atomic_get_old_crtc_state(state, crtc);
3247 struct intel_pipe_wm *a = &new_crtc_state->wm.ilk.intermediate;
3248 const struct intel_pipe_wm *b = &old_crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003249 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003250
3251 /*
3252 * Start with the final, target watermarks, then combine with the
3253 * currently active watermarks to get values that are safe both before
3254 * and after the vblank.
3255 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003256 *a = new_crtc_state->wm.ilk.optimal;
3257 if (!new_crtc_state->hw.active ||
3258 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) ||
3259 state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003260 return 0;
3261
Matt Ropered4a6a72016-02-23 17:20:13 -08003262 a->pipe_enabled |= b->pipe_enabled;
3263 a->sprites_enabled |= b->sprites_enabled;
3264 a->sprites_scaled |= b->sprites_scaled;
3265
3266 for (level = 0; level <= max_level; level++) {
3267 struct intel_wm_level *a_wm = &a->wm[level];
3268 const struct intel_wm_level *b_wm = &b->wm[level];
3269
3270 a_wm->enable &= b_wm->enable;
3271 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3272 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3273 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3274 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3275 }
3276
3277 /*
3278 * We need to make sure that these merged watermark values are
3279 * actually a valid configuration themselves. If they're not,
3280 * there's no safe way to transition from the old state to
3281 * the new state, so we need to fail the atomic transaction.
3282 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003283 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003284 return -EINVAL;
3285
3286 /*
3287 * If our intermediate WM are identical to the final WM, then we can
3288 * omit the post-vblank programming; only update if it's different.
3289 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003290 if (memcmp(a, &new_crtc_state->wm.ilk.optimal, sizeof(*a)) != 0)
3291 new_crtc_state->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003292
3293 return 0;
3294}
3295
3296/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003297 * Merge the watermarks from all active pipes for a specific level.
3298 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003299static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003300 int level,
3301 struct intel_wm_level *ret_wm)
3302{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003303 const struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003304
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003305 ret_wm->enable = true;
3306
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003307 for_each_intel_crtc(&dev_priv->drm, crtc) {
3308 const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003309 const struct intel_wm_level *wm = &active->wm[level];
3310
3311 if (!active->pipe_enabled)
3312 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003313
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003314 /*
3315 * The watermark values may have been used in the past,
3316 * so we must maintain them in the registers for some
3317 * time even if the level is now disabled.
3318 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003319 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003320 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003321
3322 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3323 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3324 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3325 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3326 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003327}
3328
3329/*
3330 * Merge all low power watermarks for all active pipes.
3331 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003332static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003333 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003334 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003335 struct intel_pipe_wm *merged)
3336{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003337 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003338 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003339
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003340 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Matt Roper7dadd282021-03-19 21:42:43 -07003341 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003342 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003343 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003344
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003345 /* ILK: FBC WM must be disabled always */
Matt Roper7dadd282021-03-19 21:42:43 -07003346 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003347
3348 /* merge each WM1+ level */
3349 for (level = 1; level <= max_level; level++) {
3350 struct intel_wm_level *wm = &merged->wm[level];
3351
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003352 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003353
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003354 if (level > last_enabled_level)
3355 wm->enable = false;
3356 else if (!ilk_validate_wm_level(level, max, wm))
3357 /* make sure all following levels get disabled */
3358 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003359
3360 /*
3361 * The spec says it is preferred to disable
3362 * FBC WMs instead of disabling a WM level.
3363 */
3364 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003365 if (wm->enable)
3366 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003367 wm->fbc_val = 0;
3368 }
3369 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003370
3371 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3372 /*
3373 * FIXME this is racy. FBC might get enabled later.
3374 * What we should check here is whether FBC can be
3375 * enabled sometime later.
3376 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003377 if (DISPLAY_VER(dev_priv) == 5 && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003378 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003379 for (level = 2; level <= max_level; level++) {
3380 struct intel_wm_level *wm = &merged->wm[level];
3381
3382 wm->enable = false;
3383 }
3384 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003385}
3386
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003387static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3388{
3389 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3390 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3391}
3392
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003393/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003394static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3395 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003396{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003397 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003398 return 2 * level;
3399 else
3400 return dev_priv->wm.pri_latency[level];
3401}
3402
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003403static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003404 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003405 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003406 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003407{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003408 struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003409 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003410
Ville Syrjälä0362c782013-10-09 19:17:57 +03003411 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003412 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003413
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003414 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003415 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003416 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003417
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003418 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003419
Ville Syrjälä0362c782013-10-09 19:17:57 +03003420 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003421
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003422 /*
3423 * Maintain the watermark values even if the level is
3424 * disabled. Doing otherwise could cause underruns.
3425 */
3426 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003427 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003428 (r->pri_val << WM1_LP_SR_SHIFT) |
3429 r->cur_val;
3430
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003431 if (r->enable)
3432 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3433
Matt Roper7dadd282021-03-19 21:42:43 -07003434 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003435 results->wm_lp[wm_lp - 1] |=
3436 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3437 else
3438 results->wm_lp[wm_lp - 1] |=
3439 r->fbc_val << WM1_LP_FBC_SHIFT;
3440
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003441 /*
3442 * Always set WM1S_LP_EN when spr_val != 0, even if the
3443 * level is disabled. Doing otherwise could cause underruns.
3444 */
Matt Roper7dadd282021-03-19 21:42:43 -07003445 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303446 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003447 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3448 } else
3449 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003450 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003451
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003452 /* LP0 register values */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003453 for_each_intel_crtc(&dev_priv->drm, crtc) {
3454 enum pipe pipe = crtc->pipe;
3455 const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003456 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003457
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303458 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003459 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003460
3461 results->wm_pipe[pipe] =
3462 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3463 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3464 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003465 }
3466}
3467
Paulo Zanoni861f3382013-05-31 10:19:21 -03003468/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3469 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003470static struct intel_pipe_wm *
3471ilk_find_best_result(struct drm_i915_private *dev_priv,
3472 struct intel_pipe_wm *r1,
3473 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003474{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003475 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003476 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003477
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003478 for (level = 1; level <= max_level; level++) {
3479 if (r1->wm[level].enable)
3480 level1 = level;
3481 if (r2->wm[level].enable)
3482 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003483 }
3484
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003485 if (level1 == level2) {
3486 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003487 return r2;
3488 else
3489 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003490 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003491 return r1;
3492 } else {
3493 return r2;
3494 }
3495}
3496
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003497/* dirty bits used to track which watermarks need changes */
3498#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003499#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3500#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3501#define WM_DIRTY_FBC (1 << 24)
3502#define WM_DIRTY_DDB (1 << 25)
3503
Damien Lespiau055e3932014-08-18 13:49:10 +01003504static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003505 const struct ilk_wm_values *old,
3506 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003507{
3508 unsigned int dirty = 0;
3509 enum pipe pipe;
3510 int wm_lp;
3511
Damien Lespiau055e3932014-08-18 13:49:10 +01003512 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003513 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3514 dirty |= WM_DIRTY_PIPE(pipe);
3515 /* Must disable LP1+ watermarks too */
3516 dirty |= WM_DIRTY_LP_ALL;
3517 }
3518 }
3519
3520 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3521 dirty |= WM_DIRTY_FBC;
3522 /* Must disable LP1+ watermarks too */
3523 dirty |= WM_DIRTY_LP_ALL;
3524 }
3525
3526 if (old->partitioning != new->partitioning) {
3527 dirty |= WM_DIRTY_DDB;
3528 /* Must disable LP1+ watermarks too */
3529 dirty |= WM_DIRTY_LP_ALL;
3530 }
3531
3532 /* LP1+ watermarks already deemed dirty, no need to continue */
3533 if (dirty & WM_DIRTY_LP_ALL)
3534 return dirty;
3535
3536 /* Find the lowest numbered LP1+ watermark in need of an update... */
3537 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3538 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3539 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3540 break;
3541 }
3542
3543 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3544 for (; wm_lp <= 3; wm_lp++)
3545 dirty |= WM_DIRTY_LP(wm_lp);
3546
3547 return dirty;
3548}
3549
Ville Syrjälä8553c182013-12-05 15:51:39 +02003550static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3551 unsigned int dirty)
3552{
Imre Deak820c1982013-12-17 14:46:36 +02003553 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003554 bool changed = false;
3555
3556 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3557 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003558 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003559 changed = true;
3560 }
3561 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3562 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003563 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003564 changed = true;
3565 }
3566 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3567 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003568 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003569 changed = true;
3570 }
3571
3572 /*
3573 * Don't touch WM1S_LP_EN here.
3574 * Doing so could cause underruns.
3575 */
3576
3577 return changed;
3578}
3579
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003580/*
3581 * The spec says we shouldn't write when we don't need, because every write
3582 * causes WMs to be re-evaluated, expending some power.
3583 */
Imre Deak820c1982013-12-17 14:46:36 +02003584static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3585 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003586{
Imre Deak820c1982013-12-17 14:46:36 +02003587 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003588 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003589 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003590
Damien Lespiau055e3932014-08-18 13:49:10 +01003591 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003592 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003593 return;
3594
Ville Syrjälä8553c182013-12-05 15:51:39 +02003595 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003596
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003597 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Jani Nikula5f461662020-11-30 13:15:58 +02003598 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003599 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Jani Nikula5f461662020-11-30 13:15:58 +02003600 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003601 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Jani Nikula5f461662020-11-30 13:15:58 +02003602 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003603
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003604 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003605 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02003606 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003607 if (results->partitioning == INTEL_DDB_PART_1_2)
3608 val &= ~WM_MISC_DATA_PARTITION_5_6;
3609 else
3610 val |= WM_MISC_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003611 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003612 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02003613 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003614 if (results->partitioning == INTEL_DDB_PART_1_2)
3615 val &= ~DISP_DATA_PARTITION_5_6;
3616 else
3617 val |= DISP_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003618 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003619 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003620 }
3621
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003622 if (dirty & WM_DIRTY_FBC) {
Jani Nikula5f461662020-11-30 13:15:58 +02003623 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003624 if (results->enable_fbc_wm)
3625 val &= ~DISP_FBC_WM_DIS;
3626 else
3627 val |= DISP_FBC_WM_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02003628 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003629 }
3630
Imre Deak954911e2013-12-17 14:46:34 +02003631 if (dirty & WM_DIRTY_LP(1) &&
3632 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003633 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
Imre Deak954911e2013-12-17 14:46:34 +02003634
Matt Roper7dadd282021-03-19 21:42:43 -07003635 if (DISPLAY_VER(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003636 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003637 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003638 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003639 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003640 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003641
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003642 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003643 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003644 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003645 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003646 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003647 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003648
3649 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003650}
3651
Ville Syrjälä60aca572019-11-27 21:05:51 +02003652bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003653{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003654 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3655}
3656
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003657u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303658{
Ville Syrjäläb88da662021-04-16 20:10:09 +03003659 u8 enabled_slices = 0;
3660 enum dbuf_slice slice;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303661
Ville Syrjäläb88da662021-04-16 20:10:09 +03003662 for_each_dbuf_slice(dev_priv, slice) {
3663 if (intel_uncore_read(&dev_priv->uncore,
3664 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3665 enabled_slices |= BIT(slice);
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003666 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303667
Ville Syrjäläb88da662021-04-16 20:10:09 +03003668 return enabled_slices;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303669}
3670
Matt Roper024c9042015-09-24 15:53:11 -07003671/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003672 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3673 * so assume we'll always need it in order to avoid underruns.
3674 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003675static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003676{
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003677 return DISPLAY_VER(dev_priv) == 9;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003678}
3679
Paulo Zanoni56feca92016-09-22 18:00:28 -03003680static bool
3681intel_has_sagv(struct drm_i915_private *dev_priv)
3682{
Matt Roper70bfb302021-04-07 13:39:45 -07003683 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003684 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003685}
3686
James Ausmusb068a862019-10-09 10:23:14 -07003687static void
3688skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3689{
Matt Roper7dadd282021-03-19 21:42:43 -07003690 if (DISPLAY_VER(dev_priv) >= 12) {
James Ausmusda80f042019-10-09 10:23:15 -07003691 u32 val = 0;
3692 int ret;
3693
3694 ret = sandybridge_pcode_read(dev_priv,
3695 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3696 &val, NULL);
3697 if (!ret) {
3698 dev_priv->sagv_block_time_us = val;
3699 return;
3700 }
3701
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003702 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003703 } else if (DISPLAY_VER(dev_priv) == 11) {
James Ausmusb068a862019-10-09 10:23:14 -07003704 dev_priv->sagv_block_time_us = 10;
3705 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003706 } else if (DISPLAY_VER(dev_priv) == 10) {
James Ausmusb068a862019-10-09 10:23:14 -07003707 dev_priv->sagv_block_time_us = 20;
3708 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003709 } else if (DISPLAY_VER(dev_priv) == 9) {
James Ausmusb068a862019-10-09 10:23:14 -07003710 dev_priv->sagv_block_time_us = 30;
3711 return;
3712 } else {
Matt Roper7dadd282021-03-19 21:42:43 -07003713 MISSING_CASE(DISPLAY_VER(dev_priv));
James Ausmusb068a862019-10-09 10:23:14 -07003714 }
3715
3716 /* Default to an unusable block time */
3717 dev_priv->sagv_block_time_us = -1;
3718}
3719
Lyude656d1b82016-08-17 15:55:54 -04003720/*
3721 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3722 * depending on power and performance requirements. The display engine access
3723 * to system memory is blocked during the adjustment time. Because of the
3724 * blocking time, having this enabled can cause full system hangs and/or pipe
3725 * underruns if we don't meet all of the following requirements:
3726 *
3727 * - <= 1 pipe enabled
3728 * - All planes can enable watermarks for latencies >= SAGV engine block time
3729 * - We're not using an interlaced display configuration
3730 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003731static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003732intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003733{
3734 int ret;
3735
Paulo Zanoni56feca92016-09-22 18:00:28 -03003736 if (!intel_has_sagv(dev_priv))
3737 return 0;
3738
3739 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003740 return 0;
3741
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003742 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003743 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3744 GEN9_SAGV_ENABLE);
3745
Ville Syrjäläff61a972018-12-21 19:14:34 +02003746 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003747
3748 /*
3749 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003750 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003751 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003752 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003753 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003754 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003755 return 0;
3756 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003757 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003758 return ret;
3759 }
3760
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003761 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003762 return 0;
3763}
3764
Ville Syrjälä71024042020-09-25 15:17:48 +03003765static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003766intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003767{
Imre Deakb3b8e992016-12-05 18:27:38 +02003768 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003769
Paulo Zanoni56feca92016-09-22 18:00:28 -03003770 if (!intel_has_sagv(dev_priv))
3771 return 0;
3772
3773 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003774 return 0;
3775
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003776 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003777 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003778 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3779 GEN9_SAGV_DISABLE,
3780 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3781 1);
Lyude656d1b82016-08-17 15:55:54 -04003782 /*
3783 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003784 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003785 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003786 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003787 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003788 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003789 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003790 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003791 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003792 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003793 }
3794
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003795 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003796 return 0;
3797}
3798
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003799void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3800{
3801 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003802 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003803 const struct intel_bw_state *old_bw_state;
3804 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003805
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003806 /*
3807 * Just return if we can't control SAGV or don't have it.
3808 * This is different from situation when we have SAGV but just can't
3809 * afford it due to DBuf limitation - in case if SAGV is completely
3810 * disabled in a BIOS, we are not even allowed to send a PCode request,
3811 * as it will throw an error. So have to check it here.
3812 */
3813 if (!intel_has_sagv(dev_priv))
3814 return;
3815
3816 new_bw_state = intel_atomic_get_new_bw_state(state);
3817 if (!new_bw_state)
3818 return;
3819
Matt Roper7dadd282021-03-19 21:42:43 -07003820 if (DISPLAY_VER(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003821 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003822 return;
3823 }
3824
3825 old_bw_state = intel_atomic_get_old_bw_state(state);
3826 /*
3827 * Nothing to mask
3828 */
3829 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3830 return;
3831
3832 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3833
3834 /*
3835 * If new mask is zero - means there is nothing to mask,
3836 * we can only unmask, which should be done in unmask.
3837 */
3838 if (!new_mask)
3839 return;
3840
3841 /*
3842 * Restrict required qgv points before updating the configuration.
3843 * According to BSpec we can't mask and unmask qgv points at the same
3844 * time. Also masking should be done before updating the configuration
3845 * and unmasking afterwards.
3846 */
3847 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003848}
3849
3850void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3851{
3852 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003853 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003854 const struct intel_bw_state *old_bw_state;
3855 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003856
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003857 /*
3858 * Just return if we can't control SAGV or don't have it.
3859 * This is different from situation when we have SAGV but just can't
3860 * afford it due to DBuf limitation - in case if SAGV is completely
3861 * disabled in a BIOS, we are not even allowed to send a PCode request,
3862 * as it will throw an error. So have to check it here.
3863 */
3864 if (!intel_has_sagv(dev_priv))
3865 return;
3866
3867 new_bw_state = intel_atomic_get_new_bw_state(state);
3868 if (!new_bw_state)
3869 return;
3870
Matt Roper7dadd282021-03-19 21:42:43 -07003871 if (DISPLAY_VER(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003872 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003873 return;
3874 }
3875
3876 old_bw_state = intel_atomic_get_old_bw_state(state);
3877 /*
3878 * Nothing to unmask
3879 */
3880 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3881 return;
3882
3883 new_mask = new_bw_state->qgv_points_mask;
3884
3885 /*
3886 * Allow required qgv points after updating the configuration.
3887 * According to BSpec we can't mask and unmask qgv points at the same
3888 * time. Also masking should be done before updating the configuration
3889 * and unmasking afterwards.
3890 */
3891 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003892}
3893
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003894static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003895{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003896 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003897 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä9c312122020-11-06 19:30:40 +02003898 enum plane_id plane_id;
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003899 int max_level = INT_MAX;
Lyude656d1b82016-08-17 15:55:54 -04003900
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003901 if (!intel_has_sagv(dev_priv))
3902 return false;
3903
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003904 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003905 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003906
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02003907 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003908 return false;
3909
Ville Syrjälä9c312122020-11-06 19:30:40 +02003910 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003911 const struct skl_plane_wm *wm =
Ville Syrjälä9c312122020-11-06 19:30:40 +02003912 &crtc_state->wm.skl.optimal.planes[plane_id];
3913 int level;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003914
Lyude656d1b82016-08-17 15:55:54 -04003915 /* Skip this plane if it's not enabled */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003916 if (!wm->wm[0].enable)
Lyude656d1b82016-08-17 15:55:54 -04003917 continue;
3918
3919 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003920 for (level = ilk_wm_max_level(dev_priv);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003921 !wm->wm[level].enable; --level)
Lyude656d1b82016-08-17 15:55:54 -04003922 { }
3923
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003924 /* Highest common enabled wm level for all planes */
3925 max_level = min(level, max_level);
3926 }
3927
3928 /* No enabled planes? */
3929 if (max_level == INT_MAX)
3930 return true;
3931
3932 for_each_plane_id_on_crtc(crtc, plane_id) {
3933 const struct skl_plane_wm *wm =
3934 &crtc_state->wm.skl.optimal.planes[plane_id];
3935
Lyude656d1b82016-08-17 15:55:54 -04003936 /*
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003937 * All enabled planes must have enabled a common wm level that
3938 * can tolerate memory latencies higher than sagv_block_time_us
Lyude656d1b82016-08-17 15:55:54 -04003939 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003940 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
Lyude656d1b82016-08-17 15:55:54 -04003941 return false;
3942 }
3943
3944 return true;
3945}
3946
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003947static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3948{
3949 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3950 enum plane_id plane_id;
3951
3952 if (!crtc_state->hw.active)
3953 return true;
3954
3955 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003956 const struct skl_plane_wm *wm =
3957 &crtc_state->wm.skl.optimal.planes[plane_id];
3958
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003959 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003960 return false;
3961 }
3962
3963 return true;
3964}
3965
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003966static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3967{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003968 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3969 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3970
Matt Roper7dadd282021-03-19 21:42:43 -07003971 if (DISPLAY_VER(dev_priv) >= 12)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003972 return tgl_crtc_can_enable_sagv(crtc_state);
3973 else
3974 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003975}
3976
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003977bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3978 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003979{
Matt Roper7dadd282021-03-19 21:42:43 -07003980 if (DISPLAY_VER(dev_priv) < 11 &&
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003981 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003982 return false;
3983
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003984 return bw_state->pipe_sagv_reject == 0;
3985}
3986
3987static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3988{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003989 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003990 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003991 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003992 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003993 struct intel_bw_state *new_bw_state = NULL;
3994 const struct intel_bw_state *old_bw_state = NULL;
3995 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003996
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003997 for_each_new_intel_crtc_in_state(state, crtc,
3998 new_crtc_state, i) {
3999 new_bw_state = intel_atomic_get_bw_state(state);
4000 if (IS_ERR(new_bw_state))
4001 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004002
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004003 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004004
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004005 if (intel_crtc_can_enable_sagv(new_crtc_state))
4006 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
4007 else
4008 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
4009 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004010
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004011 if (!new_bw_state)
4012 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004013
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004014 new_bw_state->active_pipes =
4015 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03004016
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004017 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4018 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4019 if (ret)
4020 return ret;
4021 }
4022
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004023 for_each_new_intel_crtc_in_state(state, crtc,
4024 new_crtc_state, i) {
4025 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4026
4027 /*
4028 * We store use_sagv_wm in the crtc state rather than relying on
4029 * that bw state since we have no convenient way to get at the
4030 * latter from the plane commit hooks (especially in the legacy
4031 * cursor case)
4032 */
Matt Roper7959ffe2021-05-18 17:06:11 -07004033 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4034 DISPLAY_VER(dev_priv) >= 12 &&
4035 intel_can_enable_sagv(dev_priv, new_bw_state);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004036 }
4037
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004038 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4039 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004040 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4041 if (ret)
4042 return ret;
4043 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4044 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4045 if (ret)
4046 return ret;
4047 }
4048
4049 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004050}
4051
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004052static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4053{
Ville Syrjäläb88da662021-04-16 20:10:09 +03004054 return INTEL_INFO(dev_priv)->dbuf.size /
4055 hweight8(INTEL_INFO(dev_priv)->dbuf.slice_mask);
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004056}
4057
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004058static void
4059skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4060 struct skl_ddb_entry *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304061{
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004062 int slice_size = intel_dbuf_slice_size(dev_priv);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004063
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004064 if (!slice_mask) {
4065 ddb->start = 0;
4066 ddb->end = 0;
4067 return;
4068 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004069
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004070 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4071 ddb->end = fls(slice_mask) * slice_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004072
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004073 WARN_ON(ddb->start >= ddb->end);
Ville Syrjäläb88da662021-04-16 20:10:09 +03004074 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->dbuf.size);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004075}
4076
Ville Syrjälä835c1762021-05-18 17:06:16 -07004077static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
4078{
4079 struct skl_ddb_entry ddb;
4080
4081 if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
4082 slice_mask = BIT(DBUF_S1);
4083 else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
4084 slice_mask = BIT(DBUF_S3);
4085
4086 skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
4087
4088 return ddb.start;
4089}
4090
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004091u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4092 const struct skl_ddb_entry *entry)
4093{
Ville Syrjälä6390e5a2021-04-16 20:10:07 +03004094 int slice_size = intel_dbuf_slice_size(dev_priv);
4095 enum dbuf_slice start_slice, end_slice;
4096 u8 slice_mask = 0;
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004097
4098 if (!skl_ddb_entry_size(entry))
4099 return 0;
4100
4101 start_slice = entry->start / slice_size;
4102 end_slice = (entry->end - 1) / slice_size;
4103
4104 /*
4105 * Per plane DDB entry can in a really worst case be on multiple slices
4106 * but single entry is anyway contigious.
4107 */
4108 while (start_slice <= end_slice) {
4109 slice_mask |= BIT(start_slice);
4110 start_slice++;
4111 }
4112
4113 return slice_mask;
4114}
4115
Ville Syrjälä2791a402021-01-22 22:56:26 +02004116static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4117{
4118 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4119 int hdisplay, vdisplay;
4120
4121 if (!crtc_state->hw.active)
4122 return 0;
4123
4124 /*
4125 * Watermark/ddb requirement highly depends upon width of the
4126 * framebuffer, So instead of allocating DDB equally among pipes
4127 * distribute DDB based on resolution/width of the display.
4128 */
4129 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4130
4131 return hdisplay;
4132}
4133
Ville Syrjäläef79d622021-01-22 22:56:32 +02004134static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4135 enum pipe for_pipe,
4136 unsigned int *weight_start,
4137 unsigned int *weight_end,
4138 unsigned int *weight_total)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004139{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004140 struct drm_i915_private *dev_priv =
4141 to_i915(dbuf_state->base.state->base.dev);
4142 enum pipe pipe;
Ville Syrjälä53630962021-01-22 22:56:31 +02004143
4144 *weight_start = 0;
4145 *weight_end = 0;
4146 *weight_total = 0;
4147
Ville Syrjäläef79d622021-01-22 22:56:32 +02004148 for_each_pipe(dev_priv, pipe) {
4149 int weight = dbuf_state->weight[pipe];
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004150
4151 /*
4152 * Do not account pipes using other slice sets
4153 * luckily as of current BSpec slice sets do not partially
4154 * intersect(pipes share either same one slice or same slice set
4155 * i.e no partial intersection), so it is enough to check for
4156 * equality for now.
4157 */
Ville Syrjäläef79d622021-01-22 22:56:32 +02004158 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304159 continue;
4160
Ville Syrjälä53630962021-01-22 22:56:31 +02004161 *weight_total += weight;
Ville Syrjälä53630962021-01-22 22:56:31 +02004162 if (pipe < for_pipe) {
4163 *weight_start += weight;
4164 *weight_end += weight;
4165 } else if (pipe == for_pipe) {
4166 *weight_end += weight;
4167 }
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304168 }
Ville Syrjälä53630962021-01-22 22:56:31 +02004169}
4170
4171static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02004172skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
Ville Syrjälä53630962021-01-22 22:56:31 +02004173{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004174 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4175 unsigned int weight_total, weight_start, weight_end;
Ville Syrjälä53630962021-01-22 22:56:31 +02004176 const struct intel_dbuf_state *old_dbuf_state =
4177 intel_atomic_get_old_dbuf_state(state);
4178 struct intel_dbuf_state *new_dbuf_state =
4179 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004180 struct intel_crtc_state *crtc_state;
Ville Syrjälä53630962021-01-22 22:56:31 +02004181 struct skl_ddb_entry ddb_slices;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004182 enum pipe pipe = crtc->pipe;
Manasi Navaree2bebb92021-06-03 14:53:38 -07004183 unsigned int mbus_offset = 0;
Ville Syrjälä53630962021-01-22 22:56:31 +02004184 u32 ddb_range_size;
4185 u32 dbuf_slice_mask;
4186 u32 start, end;
4187 int ret;
4188
Ville Syrjäläef79d622021-01-22 22:56:32 +02004189 if (new_dbuf_state->weight[pipe] == 0) {
4190 new_dbuf_state->ddb[pipe].start = 0;
4191 new_dbuf_state->ddb[pipe].end = 0;
4192 goto out;
Ville Syrjälä53630962021-01-22 22:56:31 +02004193 }
4194
Ville Syrjäläef79d622021-01-22 22:56:32 +02004195 dbuf_slice_mask = new_dbuf_state->slices[pipe];
Ville Syrjälä53630962021-01-22 22:56:31 +02004196
4197 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
Ville Syrjälä835c1762021-05-18 17:06:16 -07004198 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_slice_mask);
Ville Syrjälä53630962021-01-22 22:56:31 +02004199 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4200
Ville Syrjäläef79d622021-01-22 22:56:32 +02004201 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4202 &weight_start, &weight_end, &weight_total);
Ville Syrjälä53630962021-01-22 22:56:31 +02004203
4204 start = ddb_range_size * weight_start / weight_total;
4205 end = ddb_range_size * weight_end / weight_total;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004206
Ville Syrjälä835c1762021-05-18 17:06:16 -07004207 new_dbuf_state->ddb[pipe].start = ddb_slices.start - mbus_offset + start;
4208 new_dbuf_state->ddb[pipe].end = ddb_slices.start - mbus_offset + end;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004209out:
Ville Syrjälä835c1762021-05-18 17:06:16 -07004210 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
4211 skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
Ville Syrjäläef79d622021-01-22 22:56:32 +02004212 &new_dbuf_state->ddb[pipe]))
4213 return 0;
4214
4215 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4216 if (ret)
4217 return ret;
4218
4219 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4220 if (IS_ERR(crtc_state))
4221 return PTR_ERR(crtc_state);
4222
Ville Syrjälä835c1762021-05-18 17:06:16 -07004223 /*
4224 * Used for checking overlaps, so we need absolute
4225 * offsets instead of MBUS relative offsets.
4226 */
4227 crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
4228 crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004229
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004230 drm_dbg_kms(&dev_priv->drm,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004231 "[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 +02004232 crtc->base.base.id, crtc->base.name,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004233 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4234 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4235 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4236 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004237
4238 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004239}
4240
Ville Syrjälädf331de2019-03-19 18:03:11 +02004241static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4242 int width, const struct drm_format_info *format,
4243 u64 modifier, unsigned int rotation,
4244 u32 plane_pixel_rate, struct skl_wm_params *wp,
4245 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004246static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004247 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004248 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004249 const struct skl_wm_params *wp,
4250 const struct skl_wm_level *result_prev,
4251 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004252
Ville Syrjälädf331de2019-03-19 18:03:11 +02004253static unsigned int
4254skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4255 int num_active)
4256{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004257 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004258 int level, max_level = ilk_wm_max_level(dev_priv);
4259 struct skl_wm_level wm = {};
4260 int ret, min_ddb_alloc = 0;
4261 struct skl_wm_params wp;
4262
4263 ret = skl_compute_wm_params(crtc_state, 256,
4264 drm_format_info(DRM_FORMAT_ARGB8888),
4265 DRM_FORMAT_MOD_LINEAR,
4266 DRM_MODE_ROTATE_0,
4267 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304268 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004269
4270 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004271 unsigned int latency = dev_priv->wm.skl_latency[level];
4272
4273 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004274 if (wm.min_ddb_alloc == U16_MAX)
4275 break;
4276
4277 min_ddb_alloc = wm.min_ddb_alloc;
4278 }
4279
4280 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004281}
4282
Mahesh Kumar37cde112018-04-26 19:55:17 +05304283static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4284 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004285{
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004286 entry->start = reg & DDB_ENTRY_MASK;
4287 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304288
Damien Lespiau16160e32014-11-04 17:06:53 +00004289 if (entry->end)
4290 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004291}
4292
Mahesh Kumarddf34312018-04-09 09:11:03 +05304293static void
4294skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4295 const enum pipe pipe,
4296 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004297 struct skl_ddb_entry *ddb_y,
4298 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304299{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004300 u32 val, val2;
4301 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304302
4303 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4304 if (plane_id == PLANE_CURSOR) {
Jani Nikula5f461662020-11-30 13:15:58 +02004305 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004306 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304307 return;
4308 }
4309
Jani Nikula5f461662020-11-30 13:15:58 +02004310 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304311
4312 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004313 if (val & PLANE_CTL_ENABLE)
4314 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4315 val & PLANE_CTL_ORDER_RGBX,
4316 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304317
Matt Roper7dadd282021-03-19 21:42:43 -07004318 if (DISPLAY_VER(dev_priv) >= 11) {
Jani Nikula5f461662020-11-30 13:15:58 +02004319 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004320 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4321 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02004322 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4323 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304324
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004325 if (fourcc &&
4326 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004327 swap(val, val2);
4328
4329 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4330 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304331 }
4332}
4333
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004334void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4335 struct skl_ddb_entry *ddb_y,
4336 struct skl_ddb_entry *ddb_uv)
4337{
4338 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4339 enum intel_display_power_domain power_domain;
4340 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004341 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004342 enum plane_id plane_id;
4343
4344 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004345 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4346 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004347 return;
4348
4349 for_each_plane_id_on_crtc(crtc, plane_id)
4350 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4351 plane_id,
4352 &ddb_y[plane_id],
4353 &ddb_uv[plane_id]);
4354
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004355 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004356}
4357
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004358/*
4359 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4360 * The bspec defines downscale amount as:
4361 *
4362 * """
4363 * Horizontal down scale amount = maximum[1, Horizontal source size /
4364 * Horizontal destination size]
4365 * Vertical down scale amount = maximum[1, Vertical source size /
4366 * Vertical destination size]
4367 * Total down scale amount = Horizontal down scale amount *
4368 * Vertical down scale amount
4369 * """
4370 *
4371 * Return value is provided in 16.16 fixed point form to retain fractional part.
4372 * Caller should take care of dividing & rounding off the value.
4373 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304374static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004375skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4376 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004377{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304378 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004379 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304380 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4381 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004382
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304383 if (drm_WARN_ON(&dev_priv->drm,
4384 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304385 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004386
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004387 /*
4388 * Src coordinates are already rotated by 270 degrees for
4389 * the 90/270 degree plane rotation cases (to match the
4390 * GTT mapping), hence no need to account for rotation here.
4391 *
4392 * n.b., src is 16.16 fixed point, dst is whole integer.
4393 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004394 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4395 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4396 dst_w = drm_rect_width(&plane_state->uapi.dst);
4397 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004398
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304399 fp_w_ratio = div_fixed16(src_w, dst_w);
4400 fp_h_ratio = div_fixed16(src_h, dst_h);
4401 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4402 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004403
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304404 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004405}
4406
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004407struct dbuf_slice_conf_entry {
4408 u8 active_pipes;
4409 u8 dbuf_mask[I915_MAX_PIPES];
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004410 bool join_mbus;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004411};
4412
4413/*
4414 * Table taken from Bspec 12716
4415 * Pipes do have some preferred DBuf slice affinity,
4416 * plus there are some hardcoded requirements on how
4417 * those should be distributed for multipipe scenarios.
4418 * For more DBuf slices algorithm can get even more messy
4419 * and less readable, so decided to use a table almost
4420 * as is from BSpec itself - that way it is at least easier
4421 * to compare, change and check.
4422 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004423static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004424/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4425{
4426 {
4427 .active_pipes = BIT(PIPE_A),
4428 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004429 [PIPE_A] = BIT(DBUF_S1),
4430 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004431 },
4432 {
4433 .active_pipes = BIT(PIPE_B),
4434 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004435 [PIPE_B] = BIT(DBUF_S1),
4436 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004437 },
4438 {
4439 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4440 .dbuf_mask = {
4441 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004442 [PIPE_B] = BIT(DBUF_S2),
4443 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004444 },
4445 {
4446 .active_pipes = BIT(PIPE_C),
4447 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004448 [PIPE_C] = BIT(DBUF_S2),
4449 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004450 },
4451 {
4452 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4453 .dbuf_mask = {
4454 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004455 [PIPE_C] = BIT(DBUF_S2),
4456 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004457 },
4458 {
4459 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4460 .dbuf_mask = {
4461 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004462 [PIPE_C] = BIT(DBUF_S2),
4463 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004464 },
4465 {
4466 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4467 .dbuf_mask = {
4468 [PIPE_A] = BIT(DBUF_S1),
4469 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004470 [PIPE_C] = BIT(DBUF_S2),
4471 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004472 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004473 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004474};
4475
4476/*
4477 * Table taken from Bspec 49255
4478 * Pipes do have some preferred DBuf slice affinity,
4479 * plus there are some hardcoded requirements on how
4480 * those should be distributed for multipipe scenarios.
4481 * For more DBuf slices algorithm can get even more messy
4482 * and less readable, so decided to use a table almost
4483 * as is from BSpec itself - that way it is at least easier
4484 * to compare, change and check.
4485 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004486static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004487/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4488{
4489 {
4490 .active_pipes = BIT(PIPE_A),
4491 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004492 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4493 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004494 },
4495 {
4496 .active_pipes = BIT(PIPE_B),
4497 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004498 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4499 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004500 },
4501 {
4502 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4503 .dbuf_mask = {
4504 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004505 [PIPE_B] = BIT(DBUF_S1),
4506 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004507 },
4508 {
4509 .active_pipes = BIT(PIPE_C),
4510 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004511 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4512 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004513 },
4514 {
4515 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4516 .dbuf_mask = {
4517 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004518 [PIPE_C] = BIT(DBUF_S2),
4519 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004520 },
4521 {
4522 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4523 .dbuf_mask = {
4524 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004525 [PIPE_C] = BIT(DBUF_S2),
4526 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004527 },
4528 {
4529 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4530 .dbuf_mask = {
4531 [PIPE_A] = BIT(DBUF_S1),
4532 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004533 [PIPE_C] = BIT(DBUF_S2),
4534 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004535 },
4536 {
4537 .active_pipes = BIT(PIPE_D),
4538 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004539 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4540 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004541 },
4542 {
4543 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4544 .dbuf_mask = {
4545 [PIPE_A] = BIT(DBUF_S1),
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_B) | BIT(PIPE_D),
4551 .dbuf_mask = {
4552 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004553 [PIPE_D] = BIT(DBUF_S2),
4554 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004555 },
4556 {
4557 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4558 .dbuf_mask = {
4559 [PIPE_A] = BIT(DBUF_S1),
4560 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004561 [PIPE_D] = BIT(DBUF_S2),
4562 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004563 },
4564 {
4565 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4566 .dbuf_mask = {
4567 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004568 [PIPE_D] = BIT(DBUF_S2),
4569 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004570 },
4571 {
4572 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4573 .dbuf_mask = {
4574 [PIPE_A] = BIT(DBUF_S1),
4575 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004576 [PIPE_D] = BIT(DBUF_S2),
4577 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004578 },
4579 {
4580 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4581 .dbuf_mask = {
4582 [PIPE_B] = BIT(DBUF_S1),
4583 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004584 [PIPE_D] = BIT(DBUF_S2),
4585 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004586 },
4587 {
4588 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4589 .dbuf_mask = {
4590 [PIPE_A] = BIT(DBUF_S1),
4591 [PIPE_B] = BIT(DBUF_S1),
4592 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004593 [PIPE_D] = BIT(DBUF_S2),
4594 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004595 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004596 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004597};
4598
Matt Roper49f75632021-07-21 15:30:40 -07004599static const struct dbuf_slice_conf_entry dg2_allowed_dbufs[] = {
4600 {
4601 .active_pipes = BIT(PIPE_A),
4602 .dbuf_mask = {
4603 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4604 },
4605 },
4606 {
4607 .active_pipes = BIT(PIPE_B),
4608 .dbuf_mask = {
4609 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4610 },
4611 },
4612 {
4613 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4614 .dbuf_mask = {
4615 [PIPE_A] = BIT(DBUF_S1),
4616 [PIPE_B] = BIT(DBUF_S2),
4617 },
4618 },
4619 {
4620 .active_pipes = BIT(PIPE_C),
4621 .dbuf_mask = {
4622 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4623 },
4624 },
4625 {
4626 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4627 .dbuf_mask = {
4628 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4629 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4630 },
4631 },
4632 {
4633 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4634 .dbuf_mask = {
4635 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4636 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4637 },
4638 },
4639 {
4640 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4641 .dbuf_mask = {
4642 [PIPE_A] = BIT(DBUF_S1),
4643 [PIPE_B] = BIT(DBUF_S2),
4644 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4645 },
4646 },
4647 {
4648 .active_pipes = BIT(PIPE_D),
4649 .dbuf_mask = {
4650 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4651 },
4652 },
4653 {
4654 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4655 .dbuf_mask = {
4656 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4657 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4658 },
4659 },
4660 {
4661 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4662 .dbuf_mask = {
4663 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4664 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4665 },
4666 },
4667 {
4668 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4669 .dbuf_mask = {
4670 [PIPE_A] = BIT(DBUF_S1),
4671 [PIPE_B] = BIT(DBUF_S2),
4672 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4673 },
4674 },
4675 {
4676 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4677 .dbuf_mask = {
4678 [PIPE_C] = BIT(DBUF_S3),
4679 [PIPE_D] = BIT(DBUF_S4),
4680 },
4681 },
4682 {
4683 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4684 .dbuf_mask = {
4685 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4686 [PIPE_C] = BIT(DBUF_S3),
4687 [PIPE_D] = BIT(DBUF_S4),
4688 },
4689 },
4690 {
4691 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4692 .dbuf_mask = {
4693 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4694 [PIPE_C] = BIT(DBUF_S3),
4695 [PIPE_D] = BIT(DBUF_S4),
4696 },
4697 },
4698 {
4699 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4700 .dbuf_mask = {
4701 [PIPE_A] = BIT(DBUF_S1),
4702 [PIPE_B] = BIT(DBUF_S2),
4703 [PIPE_C] = BIT(DBUF_S3),
4704 [PIPE_D] = BIT(DBUF_S4),
4705 },
4706 },
4707 {}
4708};
4709
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004710static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
4711 {
4712 .active_pipes = BIT(PIPE_A),
4713 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004714 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004715 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004716 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004717 },
4718 {
4719 .active_pipes = BIT(PIPE_B),
4720 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004721 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004722 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004723 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004724 },
4725 {
4726 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4727 .dbuf_mask = {
4728 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4729 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4730 },
4731 },
4732 {
4733 .active_pipes = BIT(PIPE_C),
4734 .dbuf_mask = {
4735 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4736 },
4737 },
4738 {
4739 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4740 .dbuf_mask = {
4741 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4742 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4743 },
4744 },
4745 {
4746 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4747 .dbuf_mask = {
4748 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4749 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4750 },
4751 },
4752 {
4753 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4754 .dbuf_mask = {
4755 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4756 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4757 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4758 },
4759 },
4760 {
4761 .active_pipes = BIT(PIPE_D),
4762 .dbuf_mask = {
4763 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4764 },
4765 },
4766 {
4767 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4768 .dbuf_mask = {
4769 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4770 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4771 },
4772 },
4773 {
4774 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4775 .dbuf_mask = {
4776 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4777 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4778 },
4779 },
4780 {
4781 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4782 .dbuf_mask = {
4783 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4784 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4785 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4786 },
4787 },
4788 {
4789 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4790 .dbuf_mask = {
4791 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4792 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4793 },
4794 },
4795 {
4796 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4797 .dbuf_mask = {
4798 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4799 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4800 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4801 },
4802 },
4803 {
4804 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4805 .dbuf_mask = {
4806 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4807 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4808 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4809 },
4810 },
4811 {
4812 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4813 .dbuf_mask = {
4814 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4815 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4816 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4817 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4818 },
4819 },
4820 {}
4821
4822};
4823
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004824static bool check_mbus_joined(u8 active_pipes,
4825 const struct dbuf_slice_conf_entry *dbuf_slices)
4826{
4827 int i;
4828
4829 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
4830 if (dbuf_slices[i].active_pipes == active_pipes)
4831 return dbuf_slices[i].join_mbus;
4832 }
4833 return false;
4834}
4835
4836static bool adlp_check_mbus_joined(u8 active_pipes)
4837{
4838 return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
4839}
4840
Ville Syrjälä05e81552020-02-25 19:11:09 +02004841static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4842 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004843{
4844 int i;
4845
Ville Syrjälä05e81552020-02-25 19:11:09 +02004846 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004847 if (dbuf_slices[i].active_pipes == active_pipes)
4848 return dbuf_slices[i].dbuf_mask[pipe];
4849 }
4850 return 0;
4851}
4852
4853/*
4854 * This function finds an entry with same enabled pipe configuration and
4855 * returns correspondent DBuf slice mask as stated in BSpec for particular
4856 * platform.
4857 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004858static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004859{
4860 /*
4861 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4862 * required calculating "pipe ratio" in order to determine
4863 * if one or two slices can be used for single pipe configurations
4864 * as additional constraint to the existing table.
4865 * However based on recent info, it should be not "pipe ratio"
4866 * but rather ratio between pixel_rate and cdclk with additional
4867 * constants, so for now we are using only table until this is
4868 * clarified. Also this is the reason why crtc_state param is
4869 * still here - we will need it once those additional constraints
4870 * pop up.
4871 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004872 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004873}
4874
Ville Syrjälä05e81552020-02-25 19:11:09 +02004875static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004876{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004877 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004878}
4879
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004880static u32 adlp_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4881{
4882 return compute_dbuf_slices(pipe, active_pipes, adlp_allowed_dbufs);
4883}
4884
Matt Roper49f75632021-07-21 15:30:40 -07004885static u32 dg2_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4886{
4887 return compute_dbuf_slices(pipe, active_pipes, dg2_allowed_dbufs);
4888}
4889
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004890static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004891{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004892 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4893 enum pipe pipe = crtc->pipe;
4894
Matt Roper49f75632021-07-21 15:30:40 -07004895 if (IS_DG2(dev_priv))
4896 return dg2_compute_dbuf_slices(pipe, active_pipes);
4897 else if (IS_ALDERLAKE_P(dev_priv))
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004898 return adlp_compute_dbuf_slices(pipe, active_pipes);
4899 else if (DISPLAY_VER(dev_priv) == 12)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004900 return tgl_compute_dbuf_slices(pipe, active_pipes);
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004901 else if (DISPLAY_VER(dev_priv) == 11)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004902 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004903 /*
4904 * For anything else just return one slice yet.
4905 * Should be extended for other platforms.
4906 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004907 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004908}
4909
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004910static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004911skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4912 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004913 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004914{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004915 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004916 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004917 u32 data_rate;
4918 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304919 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004920 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004921
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004922 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004923 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004924
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004925 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004926 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004927
4928 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004929 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004930 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004931
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004932 /*
4933 * Src coordinates are already rotated by 270 degrees for
4934 * the 90/270 degree plane rotation cases (to match the
4935 * GTT mapping), hence no need to account for rotation here.
4936 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004937 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4938 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004939
Mahesh Kumarb879d582018-04-09 09:11:01 +05304940 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004941 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304942 width /= 2;
4943 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004944 }
4945
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004946 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304947
Maarten Lankhorstec193642019-06-28 10:55:17 +02004948 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004949
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004950 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4951
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004952 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004953 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004954}
4955
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004956static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004957skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4958 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004959{
Ville Syrjäläab016302020-11-06 19:30:41 +02004960 struct intel_crtc_state *crtc_state =
4961 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004962 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004963 struct intel_plane *plane;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004964 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004965 enum plane_id plane_id;
4966 int i;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004967
Matt Ropera1de91e2016-05-12 07:05:57 -07004968 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004969 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4970 if (plane->pipe != crtc->pipe)
4971 continue;
4972
4973 plane_id = plane->id;
Matt Roper024c9042015-09-24 15:53:11 -07004974
Mahesh Kumarb879d582018-04-09 09:11:01 +05304975 /* packed/y */
Ville Syrjäläab016302020-11-06 19:30:41 +02004976 crtc_state->plane_data_rate[plane_id] =
4977 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Matt Roper9c74d822016-05-12 07:05:58 -07004978
Mahesh Kumarb879d582018-04-09 09:11:01 +05304979 /* uv-plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004980 crtc_state->uv_plane_data_rate[plane_id] =
4981 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4982 }
4983
4984 for_each_plane_id_on_crtc(crtc, plane_id) {
4985 total_data_rate += crtc_state->plane_data_rate[plane_id];
4986 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004987 }
4988
4989 return total_data_rate;
4990}
4991
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004992static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004993icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4994 struct intel_crtc *crtc)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004995{
Ville Syrjäläab016302020-11-06 19:30:41 +02004996 struct intel_crtc_state *crtc_state =
4997 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004998 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004999 struct intel_plane *plane;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005000 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02005001 enum plane_id plane_id;
5002 int i;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005003
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005004 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02005005 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5006 if (plane->pipe != crtc->pipe)
5007 continue;
5008
5009 plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005010
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005011 if (!plane_state->planar_linked_plane) {
Ville Syrjäläab016302020-11-06 19:30:41 +02005012 crtc_state->plane_data_rate[plane_id] =
5013 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005014 } else {
5015 enum plane_id y_plane_id;
5016
5017 /*
5018 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005019 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005020 * and needs the master plane state which may be
5021 * NULL if we try get_new_plane_state(), so we
5022 * always calculate from the master.
5023 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005024 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005025 continue;
5026
5027 /* Y plane rate is calculated on the slave */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005028 y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjäläab016302020-11-06 19:30:41 +02005029 crtc_state->plane_data_rate[y_plane_id] =
5030 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005031
Ville Syrjäläab016302020-11-06 19:30:41 +02005032 crtc_state->plane_data_rate[plane_id] =
5033 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005034 }
5035 }
5036
Ville Syrjäläab016302020-11-06 19:30:41 +02005037 for_each_plane_id_on_crtc(crtc, plane_id)
5038 total_data_rate += crtc_state->plane_data_rate[plane_id];
5039
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005040 return total_data_rate;
5041}
5042
Ville Syrjälä5516e892021-02-26 17:32:03 +02005043const struct skl_wm_level *
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005044skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005045 enum plane_id plane_id,
5046 int level)
5047{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005048 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5049
5050 if (level == 0 && pipe_wm->use_sagv_wm)
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005051 return &wm->sagv.wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005052
5053 return &wm->wm[level];
5054}
5055
Ville Syrjälä5516e892021-02-26 17:32:03 +02005056const struct skl_wm_level *
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005057skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
5058 enum plane_id plane_id)
5059{
5060 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5061
5062 if (pipe_wm->use_sagv_wm)
5063 return &wm->sagv.trans_wm;
5064
5065 return &wm->trans_wm;
5066}
5067
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005068/*
5069 * We only disable the watermarks for each plane if
5070 * they exceed the ddb allocation of said plane. This
5071 * is done so that we don't end up touching cursor
5072 * watermarks needlessly when some other plane reduces
5073 * our max possible watermark level.
5074 *
5075 * Bspec has this to say about the PLANE_WM enable bit:
5076 * "All the watermarks at this level for all enabled
5077 * planes must be enabled before the level will be used."
5078 * So this is actually safe to do.
5079 */
5080static void
5081skl_check_wm_level(struct skl_wm_level *wm, u64 total)
5082{
5083 if (wm->min_ddb_alloc > total)
5084 memset(wm, 0, sizeof(*wm));
5085}
5086
5087static void
5088skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
5089 u64 total, u64 uv_total)
5090{
5091 if (wm->min_ddb_alloc > total ||
5092 uv_wm->min_ddb_alloc > uv_total) {
5093 memset(wm, 0, sizeof(*wm));
5094 memset(uv_wm, 0, sizeof(*uv_wm));
5095 }
5096}
5097
Matt Roperc107acf2016-05-12 07:06:01 -07005098static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02005099skl_allocate_plane_ddb(struct intel_atomic_state *state,
5100 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00005101{
Ville Syrjäläef79d622021-01-22 22:56:32 +02005102 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläffc90032020-11-06 19:30:37 +02005103 struct intel_crtc_state *crtc_state =
5104 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005105 const struct intel_dbuf_state *dbuf_state =
Ville Syrjälä47a14952021-01-22 22:56:30 +02005106 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005107 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
5108 int num_active = hweight8(dbuf_state->active_pipes);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005109 u16 alloc_size, start = 0;
5110 u16 total[I915_MAX_PLANES] = {};
5111 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02005112 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005113 enum plane_id plane_id;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005114 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08005115 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005116
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005117 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005118 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
5119 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005120
Ville Syrjäläef79d622021-01-22 22:56:32 +02005121 if (!crtc_state->hw.active)
Matt Roperc107acf2016-05-12 07:06:01 -07005122 return 0;
Matt Roperc107acf2016-05-12 07:06:01 -07005123
Matt Roper7dadd282021-03-19 21:42:43 -07005124 if (DISPLAY_VER(dev_priv) >= 11)
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005125 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005126 icl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005127 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005128 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005129 skl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005130
Damien Lespiau34bb56a2014-11-04 17:07:01 +00005131 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05305132 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005133 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005134
Matt Roperd8e87492018-12-11 09:31:07 -08005135 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005136 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08005137 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005138 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08005139 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005140 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005141
Matt Ropera1de91e2016-05-12 07:05:57 -07005142 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005143 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005144
Matt Roperd8e87492018-12-11 09:31:07 -08005145 /*
5146 * Find the highest watermark level for which we can satisfy the block
5147 * requirement of active planes.
5148 */
5149 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08005150 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005151 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005152 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005153 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005154
5155 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05305156 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305157 drm_WARN_ON(&dev_priv->drm,
5158 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005159 blocks = U32_MAX;
5160 break;
5161 }
5162 continue;
5163 }
5164
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005165 blocks += wm->wm[level].min_ddb_alloc;
5166 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08005167 }
5168
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02005169 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08005170 alloc_size -= blocks;
5171 break;
5172 }
5173 }
5174
5175 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005176 drm_dbg_kms(&dev_priv->drm,
5177 "Requested display configuration exceeds system DDB limitations");
5178 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
5179 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08005180 return -EINVAL;
5181 }
5182
5183 /*
5184 * Grant each plane the blocks it requires at the highest achievable
5185 * watermark level, plus an extra share of the leftover blocks
5186 * proportional to its relative data rate.
5187 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005188 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005189 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005190 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005191 u64 rate;
5192 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005193
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005194 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005195 continue;
5196
Damien Lespiaub9cec072014-11-04 17:06:43 +00005197 /*
Matt Roperd8e87492018-12-11 09:31:07 -08005198 * We've accounted for all active planes; remaining planes are
5199 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00005200 */
Matt Roperd8e87492018-12-11 09:31:07 -08005201 if (total_data_rate == 0)
5202 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005203
Ville Syrjäläab016302020-11-06 19:30:41 +02005204 rate = crtc_state->plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005205 extra = min_t(u16, alloc_size,
5206 DIV64_U64_ROUND_UP(alloc_size * rate,
5207 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005208 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005209 alloc_size -= extra;
5210 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005211
Matt Roperd8e87492018-12-11 09:31:07 -08005212 if (total_data_rate == 0)
5213 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07005214
Ville Syrjäläab016302020-11-06 19:30:41 +02005215 rate = crtc_state->uv_plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005216 extra = min_t(u16, alloc_size,
5217 DIV64_U64_ROUND_UP(alloc_size * rate,
5218 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005219 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005220 alloc_size -= extra;
5221 total_data_rate -= rate;
5222 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305223 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08005224
5225 /* Set the actual DDB start/end points for each plane */
5226 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005227 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005228 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005229 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005230 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005231 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005232
5233 if (plane_id == PLANE_CURSOR)
5234 continue;
5235
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005236 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305237 drm_WARN_ON(&dev_priv->drm,
Matt Roper7dadd282021-03-19 21:42:43 -07005238 DISPLAY_VER(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005239
Matt Roperd8e87492018-12-11 09:31:07 -08005240 /* Leave disabled planes at (0,0) */
5241 if (total[plane_id]) {
5242 plane_alloc->start = start;
5243 start += total[plane_id];
5244 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07005245 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005246
Matt Roperd8e87492018-12-11 09:31:07 -08005247 if (uv_total[plane_id]) {
5248 uv_plane_alloc->start = start;
5249 start += uv_total[plane_id];
5250 uv_plane_alloc->end = start;
5251 }
5252 }
5253
5254 /*
5255 * When we calculated watermark values we didn't know how high
5256 * of a level we'd actually be able to hit, so we just marked
5257 * all levels as "enabled." Go back now and disable the ones
5258 * that aren't actually possible.
5259 */
5260 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005261 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005262 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005263 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02005264
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005265 skl_check_nv12_wm_level(&wm->wm[level], &wm->uv_wm[level],
5266 total[plane_id], uv_total[plane_id]);
Ville Syrjälä290248c2019-02-13 18:54:24 +02005267
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005268 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005269 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005270 * Underruns with WM1+ disabled
5271 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005272 if (DISPLAY_VER(dev_priv) == 11 &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005273 level == 1 && wm->wm[0].enable) {
5274 wm->wm[level].blocks = wm->wm[0].blocks;
5275 wm->wm[level].lines = wm->wm[0].lines;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005276 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005277 }
Matt Roperd8e87492018-12-11 09:31:07 -08005278 }
5279 }
5280
5281 /*
Ville Syrjälädf4a50a2021-02-26 17:31:59 +02005282 * Go back and disable the transition and SAGV watermarks
5283 * if it turns out we don't have enough DDB blocks for them.
Matt Roperd8e87492018-12-11 09:31:07 -08005284 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005285 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005286 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005287 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005288
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005289 skl_check_wm_level(&wm->trans_wm, total[plane_id]);
5290 skl_check_wm_level(&wm->sagv.wm0, total[plane_id]);
5291 skl_check_wm_level(&wm->sagv.trans_wm, total[plane_id]);
Damien Lespiaub9cec072014-11-04 17:06:43 +00005292 }
5293
Matt Roperc107acf2016-05-12 07:06:01 -07005294 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005295}
5296
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005297/*
5298 * 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 +02005299 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005300 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5301 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5302*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005303static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005304skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5305 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005306{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005307 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305308 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005309
5310 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305311 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005312
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305313 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005314 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005315
Matt Roper2b5a4562021-03-22 16:38:40 -07005316 if (DISPLAY_VER(dev_priv) >= 10)
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005317 ret = add_fixed16_u32(ret, 1);
5318
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005319 return ret;
5320}
5321
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005322static uint_fixed_16_16_t
5323skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5324 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005325{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005326 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305327 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005328
5329 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305330 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005331
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005332 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305333 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5334 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305335 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005336 return ret;
5337}
5338
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305339static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005340intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305341{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305342 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005343 u32 pixel_rate;
5344 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305345 uint_fixed_16_16_t linetime_us;
5346
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005347 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305348 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305349
Maarten Lankhorstec193642019-06-28 10:55:17 +02005350 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305351
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305352 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305353 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305354
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005355 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305356 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305357
5358 return linetime_us;
5359}
5360
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305361static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005362skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5363 int width, const struct drm_format_info *format,
5364 u64 modifier, unsigned int rotation,
5365 u32 plane_pixel_rate, struct skl_wm_params *wp,
5366 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305367{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005368 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005369 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005370 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305371
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305372 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005373 if (color_plane == 1 &&
5374 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005375 drm_dbg_kms(&dev_priv->drm,
5376 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305377 return -EINVAL;
5378 }
5379
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005380 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5381 modifier == I915_FORMAT_MOD_Yf_TILED ||
5382 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5383 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5384 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5385 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5386 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005387 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305388
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005389 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005390 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305391 wp->width /= 2;
5392
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005393 wp->cpp = format->cpp[color_plane];
5394 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305395
Matt Roper7dadd282021-03-19 21:42:43 -07005396 if (DISPLAY_VER(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005397 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005398 wp->dbuf_block_size = 256;
5399 else
5400 wp->dbuf_block_size = 512;
5401
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005402 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305403 switch (wp->cpp) {
5404 case 1:
5405 wp->y_min_scanlines = 16;
5406 break;
5407 case 2:
5408 wp->y_min_scanlines = 8;
5409 break;
5410 case 4:
5411 wp->y_min_scanlines = 4;
5412 break;
5413 default:
5414 MISSING_CASE(wp->cpp);
5415 return -EINVAL;
5416 }
5417 } else {
5418 wp->y_min_scanlines = 4;
5419 }
5420
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005421 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305422 wp->y_min_scanlines *= 2;
5423
5424 wp->plane_bytes_per_line = wp->width * wp->cpp;
5425 if (wp->y_tiled) {
5426 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005427 wp->y_min_scanlines,
5428 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305429
Matt Roper2b5a4562021-03-22 16:38:40 -07005430 if (DISPLAY_VER(dev_priv) >= 10)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305431 interm_pbpl++;
5432
5433 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5434 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305435 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005436 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005437 wp->dbuf_block_size);
5438
Matt Roper2b5a4562021-03-22 16:38:40 -07005439 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005440 interm_pbpl++;
5441
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305442 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5443 }
5444
5445 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5446 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005447
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305448 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005449 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305450
5451 return 0;
5452}
5453
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005454static int
5455skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5456 const struct intel_plane_state *plane_state,
5457 struct skl_wm_params *wp, int color_plane)
5458{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005459 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005460 int width;
5461
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005462 /*
5463 * Src coordinates are already rotated by 270 degrees for
5464 * the 90/270 degree plane rotation cases (to match the
5465 * GTT mapping), hence no need to account for rotation here.
5466 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005467 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005468
5469 return skl_compute_wm_params(crtc_state, width,
5470 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005471 plane_state->hw.rotation,
Ville Syrjälä3df3fe22020-11-06 19:30:42 +02005472 intel_plane_pixel_rate(crtc_state, plane_state),
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005473 wp, color_plane);
5474}
5475
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005476static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5477{
Matt Roper2b5a4562021-03-22 16:38:40 -07005478 if (DISPLAY_VER(dev_priv) >= 10)
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005479 return true;
5480
5481 /* The number of lines are ignored for the level 0 watermark. */
5482 return level > 0;
5483}
5484
Matt Roper1003cee2021-05-14 08:36:54 -07005485static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5486{
5487 if (DISPLAY_VER(dev_priv) >= 13)
5488 return 255;
5489 else
5490 return 31;
5491}
5492
Maarten Lankhorstec193642019-06-28 10:55:17 +02005493static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005494 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005495 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005496 const struct skl_wm_params *wp,
5497 const struct skl_wm_level *result_prev,
5498 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005499{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005500 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305501 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305502 uint_fixed_16_16_t selected_result;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005503 u32 blocks, lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005504
Ville Syrjälä0aded172019-02-05 17:50:53 +02005505 if (latency == 0) {
5506 /* reject it */
5507 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005508 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005509 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005510
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005511 /*
5512 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5513 * Display WA #1141: kbl,cfl
5514 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005515 if ((IS_KABYLAKE(dev_priv) ||
5516 IS_COFFEELAKE(dev_priv) ||
5517 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005518 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305519 latency += 4;
5520
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005521 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005522 latency += 15;
5523
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305524 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005525 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305526 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005527 crtc_state->hw.pipe_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005528 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305529 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005530
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305531 if (wp->y_tiled) {
5532 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005533 } else {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005534 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005535 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005536 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005537 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005538 } else if (latency >= wp->linetime_us) {
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005539 if (DISPLAY_VER(dev_priv) == 9)
Paulo Zanoni077b5822018-10-04 16:15:57 -07005540 selected_result = min_fixed16(method1, method2);
5541 else
5542 selected_result = method2;
5543 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005544 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005545 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005546 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005547
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005548 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5549 lines = div_round_up_fixed16(selected_result,
5550 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005551
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005552 if (DISPLAY_VER(dev_priv) == 9) {
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005553 /* Display WA #1125: skl,bxt,kbl */
5554 if (level == 0 && wp->rc_surface)
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005555 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005556
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005557 /* Display WA #1126: skl,bxt,kbl */
5558 if (level >= 1 && level <= 7) {
5559 if (wp->y_tiled) {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005560 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5561 lines += wp->y_min_scanlines;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005562 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005563 blocks++;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005564 }
5565
5566 /*
5567 * Make sure result blocks for higher latency levels are
5568 * atleast as high as level below the current level.
5569 * Assumption in DDB algorithm optimization for special
5570 * cases. Also covers Display WA #1125 for RC.
5571 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005572 if (result_prev->blocks > blocks)
5573 blocks = result_prev->blocks;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005574 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005575 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005576
Matt Roper7dadd282021-03-19 21:42:43 -07005577 if (DISPLAY_VER(dev_priv) >= 11) {
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005578 if (wp->y_tiled) {
5579 int extra_lines;
5580
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005581 if (lines % wp->y_min_scanlines == 0)
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005582 extra_lines = wp->y_min_scanlines;
5583 else
5584 extra_lines = wp->y_min_scanlines * 2 -
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005585 lines % wp->y_min_scanlines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005586
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005587 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005588 wp->plane_blocks_per_line);
5589 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005590 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005591 }
5592 }
5593
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005594 if (!skl_wm_has_lines(dev_priv, level))
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005595 lines = 0;
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005596
Matt Roper1003cee2021-05-14 08:36:54 -07005597 if (lines > skl_wm_max_lines(dev_priv)) {
Ville Syrjälä0aded172019-02-05 17:50:53 +02005598 /* reject it */
5599 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005600 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005601 }
Matt Roperd8e87492018-12-11 09:31:07 -08005602
5603 /*
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005604 * If lines is valid, assume we can use this watermark level
Matt Roperd8e87492018-12-11 09:31:07 -08005605 * for now. We'll come back and disable it after we calculate the
5606 * DDB allocation if it turns out we don't actually have enough
5607 * blocks to satisfy it.
5608 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005609 result->blocks = blocks;
5610 result->lines = lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005611 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005612 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5613 result->enable = true;
Ville Syrjälä9c312122020-11-06 19:30:40 +02005614
Matt Roper7dadd282021-03-19 21:42:43 -07005615 if (DISPLAY_VER(dev_priv) < 12)
Ville Syrjälä9c312122020-11-06 19:30:40 +02005616 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005617}
5618
Matt Roperd8e87492018-12-11 09:31:07 -08005619static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005620skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305621 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005622 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005623{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005624 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305625 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005626 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005627
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305628 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005629 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005630 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305631
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005632 skl_compute_plane_wm(crtc_state, level, latency,
5633 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005634
5635 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305636 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005637}
5638
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005639static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5640 const struct skl_wm_params *wm_params,
5641 struct skl_plane_wm *plane_wm)
5642{
5643 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005644 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005645 struct skl_wm_level *levels = plane_wm->wm;
5646 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5647
5648 skl_compute_plane_wm(crtc_state, 0, latency,
5649 wm_params, &levels[0],
5650 sagv_wm);
5651}
5652
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005653static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5654 struct skl_wm_level *trans_wm,
5655 const struct skl_wm_level *wm0,
5656 const struct skl_wm_params *wp)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005657{
Ville Syrjäläc834d032020-02-28 22:35:52 +02005658 u16 trans_min, trans_amount, trans_y_tile_min;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005659 u16 wm0_blocks, trans_offset, blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005660
Kumar, Maheshca476672017-08-17 19:15:24 +05305661 /* Transition WM don't make any sense if ipc is disabled */
5662 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005663 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305664
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005665 /*
5666 * WaDisableTWM:skl,kbl,cfl,bxt
5667 * Transition WM are not recommended by HW team for GEN9
5668 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005669 if (DISPLAY_VER(dev_priv) == 9)
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005670 return;
5671
Matt Roper7dadd282021-03-19 21:42:43 -07005672 if (DISPLAY_VER(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305673 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005674 else
5675 trans_min = 14;
5676
5677 /* Display WA #1140: glk,cnl */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005678 if (DISPLAY_VER(dev_priv) == 10)
Ville Syrjäläc834d032020-02-28 22:35:52 +02005679 trans_amount = 0;
5680 else
5681 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305682
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005683 trans_offset = trans_min + trans_amount;
Kumar, Maheshca476672017-08-17 19:15:24 +05305684
Paulo Zanonicbacc792018-10-04 16:15:58 -07005685 /*
5686 * The spec asks for Selected Result Blocks for wm0 (the real value),
5687 * not Result Blocks (the integer value). Pay attention to the capital
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005688 * letters. The value wm_l0->blocks is actually Result Blocks, but
Paulo Zanonicbacc792018-10-04 16:15:58 -07005689 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5690 * and since we later will have to get the ceiling of the sum in the
5691 * transition watermarks calculation, we can just pretend Selected
5692 * Result Blocks is Result Blocks minus 1 and it should work for the
5693 * current platforms.
5694 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005695 wm0_blocks = wm0->blocks - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005696
Kumar, Maheshca476672017-08-17 19:15:24 +05305697 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005698 trans_y_tile_min =
5699 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005700 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305701 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005702 blocks = wm0_blocks + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305703 }
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005704 blocks++;
Kumar, Maheshca476672017-08-17 19:15:24 +05305705
Matt Roperd8e87492018-12-11 09:31:07 -08005706 /*
5707 * Just assume we can enable the transition watermark. After
5708 * computing the DDB we'll come back and disable it if that
5709 * assumption turns out to be false.
5710 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005711 trans_wm->blocks = blocks;
5712 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5713 trans_wm->enable = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005714}
5715
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005716static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005717 const struct intel_plane_state *plane_state,
5718 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005719{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005720 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5721 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälädbf71382020-11-06 19:30:38 +02005722 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005723 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005724 int ret;
5725
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005726 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005727 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005728 if (ret)
5729 return ret;
5730
Ville Syrjälä67155a62019-03-12 22:58:37 +02005731 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005732
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005733 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5734 &wm->wm[0], &wm_params);
5735
Matt Roper7dadd282021-03-19 21:42:43 -07005736 if (DISPLAY_VER(dev_priv) >= 12) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005737 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5738
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005739 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5740 &wm->sagv.wm0, &wm_params);
5741 }
Ville Syrjälä83158472018-11-27 18:57:26 +02005742
5743 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005744}
5745
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005746static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005747 const struct intel_plane_state *plane_state,
5748 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005749{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005750 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005751 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005752 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005753
Ville Syrjälä83158472018-11-27 18:57:26 +02005754 wm->is_planar = true;
5755
5756 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005757 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005758 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005759 if (ret)
5760 return ret;
5761
Ville Syrjälä67155a62019-03-12 22:58:37 +02005762 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005763
5764 return 0;
5765}
5766
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005767static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005768 const struct intel_plane_state *plane_state)
5769{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005770 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä83158472018-11-27 18:57:26 +02005771 enum plane_id plane_id = plane->id;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005772 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5773 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005774 int ret;
5775
Ville Syrjälädbf71382020-11-06 19:30:38 +02005776 memset(wm, 0, sizeof(*wm));
5777
Ville Syrjälä83158472018-11-27 18:57:26 +02005778 if (!intel_wm_plane_visible(crtc_state, plane_state))
5779 return 0;
5780
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005781 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005782 plane_id, 0);
5783 if (ret)
5784 return ret;
5785
5786 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005787 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005788 plane_id);
5789 if (ret)
5790 return ret;
5791 }
5792
5793 return 0;
5794}
5795
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005796static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005797 const struct intel_plane_state *plane_state)
5798{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005799 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5800 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5801 enum plane_id plane_id = plane->id;
5802 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005803 int ret;
5804
5805 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005806 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005807 return 0;
5808
Ville Syrjäläf99b8052021-03-27 02:59:45 +02005809 memset(wm, 0, sizeof(*wm));
5810
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005811 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005812 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005813 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005814
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305815 drm_WARN_ON(&dev_priv->drm,
5816 !intel_wm_plane_visible(crtc_state, plane_state));
5817 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5818 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005819
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005820 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005821 y_plane_id, 0);
5822 if (ret)
5823 return ret;
5824
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005825 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005826 plane_id, 1);
5827 if (ret)
5828 return ret;
5829 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005830 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005831 plane_id, 0);
5832 if (ret)
5833 return ret;
5834 }
5835
5836 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005837}
5838
Ville Syrjäläffc90032020-11-06 19:30:37 +02005839static int skl_build_pipe_wm(struct intel_atomic_state *state,
5840 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005841{
Ville Syrjäläffc90032020-11-06 19:30:37 +02005842 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5843 struct intel_crtc_state *crtc_state =
5844 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005845 const struct intel_plane_state *plane_state;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005846 struct intel_plane *plane;
5847 int ret, i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005848
Ville Syrjälädbf71382020-11-06 19:30:38 +02005849 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5850 /*
5851 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5852 * instead but we don't populate that correctly for NV12 Y
5853 * planes so for now hack this.
5854 */
5855 if (plane->pipe != crtc->pipe)
5856 continue;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305857
Matt Roper7dadd282021-03-19 21:42:43 -07005858 if (DISPLAY_VER(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005859 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005860 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005861 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305862 if (ret)
5863 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005864 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305865
Ville Syrjälädbf71382020-11-06 19:30:38 +02005866 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5867
Matt Roper55994c22016-05-12 07:06:08 -07005868 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005869}
5870
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005871static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5872 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005873 const struct skl_ddb_entry *entry)
5874{
5875 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005876 intel_de_write_fw(dev_priv, reg,
5877 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005878 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005879 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005880}
5881
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005882static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5883 i915_reg_t reg,
5884 const struct skl_wm_level *level)
5885{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005886 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005887
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005888 if (level->enable)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005889 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005890 if (level->ignore_lines)
5891 val |= PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005892 val |= level->blocks;
Matt Roper1003cee2021-05-14 08:36:54 -07005893 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005894
Jani Nikula9b6320a2020-01-23 16:00:04 +02005895 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005896}
5897
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005898void skl_write_plane_wm(struct intel_plane *plane,
5899 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005900{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005901 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005902 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005903 enum plane_id plane_id = plane->id;
5904 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005905 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5906 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005907 const struct skl_ddb_entry *ddb_y =
5908 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5909 const struct skl_ddb_entry *ddb_uv =
5910 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005911
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005912 for (level = 0; level <= max_level; level++)
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005913 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005914 skl_plane_wm_level(pipe_wm, plane_id, level));
5915
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005916 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005917 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005918
Matt Roper7959ffe2021-05-18 17:06:11 -07005919 if (HAS_HW_SAGV_WM(dev_priv)) {
5920 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5921 &wm->sagv.wm0);
5922 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5923 &wm->sagv.trans_wm);
5924 }
5925
Matt Roper7dadd282021-03-19 21:42:43 -07005926 if (DISPLAY_VER(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005927 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005928 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5929 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305930 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005931
5932 if (wm->is_planar)
5933 swap(ddb_y, ddb_uv);
5934
5935 skl_ddb_entry_write(dev_priv,
5936 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5937 skl_ddb_entry_write(dev_priv,
5938 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005939}
5940
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005941void skl_write_cursor_wm(struct intel_plane *plane,
5942 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005943{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005944 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005945 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005946 enum plane_id plane_id = plane->id;
5947 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005948 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005949 const struct skl_ddb_entry *ddb =
5950 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005951
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005952 for (level = 0; level <= max_level; level++)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005953 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005954 skl_plane_wm_level(pipe_wm, plane_id, level));
5955
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005956 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5957 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005958
Matt Roper7959ffe2021-05-18 17:06:11 -07005959 if (HAS_HW_SAGV_WM(dev_priv)) {
5960 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5961
5962 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5963 &wm->sagv.wm0);
5964 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5965 &wm->sagv.trans_wm);
5966 }
5967
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005968 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005969}
5970
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005971bool skl_wm_level_equals(const struct skl_wm_level *l1,
5972 const struct skl_wm_level *l2)
5973{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005974 return l1->enable == l2->enable &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005975 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005976 l1->lines == l2->lines &&
5977 l1->blocks == l2->blocks;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005978}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005979
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005980static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5981 const struct skl_plane_wm *wm1,
5982 const struct skl_plane_wm *wm2)
5983{
5984 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005985
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005986 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005987 /*
5988 * We don't check uv_wm as the hardware doesn't actually
5989 * use it. It only gets used for calculating the required
5990 * ddb allocation.
5991 */
5992 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005993 return false;
5994 }
5995
Ville Syrjäläf11449d2021-02-26 17:32:00 +02005996 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005997 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5998 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005999}
6000
Jani Nikula81b55ef2020-04-20 17:04:38 +03006001static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
6002 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006003{
Lyude27082492016-08-24 07:48:10 +02006004 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006005}
6006
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006007static void skl_ddb_entry_union(struct skl_ddb_entry *a,
6008 const struct skl_ddb_entry *b)
6009{
6010 if (a->end && b->end) {
6011 a->start = min(a->start, b->start);
6012 a->end = max(a->end, b->end);
6013 } else if (b->end) {
6014 a->start = b->start;
6015 a->end = b->end;
6016 }
6017}
6018
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006019bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03006020 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006021 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006022{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006023 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006024
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006025 for (i = 0; i < num_entries; i++) {
6026 if (i != ignore_idx &&
6027 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02006028 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03006029 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006030
Lyude27082492016-08-24 07:48:10 +02006031 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006032}
6033
Jani Nikulabb7791b2016-10-04 12:29:17 +03006034static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006035skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
6036 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006037{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006038 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
6039 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006040 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6041 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006042
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006043 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6044 struct intel_plane_state *plane_state;
6045 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006046
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006047 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
6048 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
6049 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
6050 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006051 continue;
6052
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006053 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006054 if (IS_ERR(plane_state))
6055 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02006056
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006057 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006058 }
6059
6060 return 0;
6061}
6062
Ville Syrjäläef79d622021-01-22 22:56:32 +02006063static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
6064{
6065 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
6066 u8 enabled_slices;
6067 enum pipe pipe;
6068
6069 /*
6070 * FIXME: For now we always enable slice S1 as per
6071 * the Bspec display initialization sequence.
6072 */
6073 enabled_slices = BIT(DBUF_S1);
6074
6075 for_each_pipe(dev_priv, pipe)
6076 enabled_slices |= dbuf_state->slices[pipe];
6077
6078 return enabled_slices;
6079}
6080
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006081static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006082skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07006083{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006084 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6085 const struct intel_dbuf_state *old_dbuf_state;
Ville Syrjäläef79d622021-01-22 22:56:32 +02006086 struct intel_dbuf_state *new_dbuf_state = NULL;
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006087 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006088 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306089 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306090 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07006091
Ville Syrjäläef79d622021-01-22 22:56:32 +02006092 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6093 new_dbuf_state = intel_atomic_get_dbuf_state(state);
6094 if (IS_ERR(new_dbuf_state))
6095 return PTR_ERR(new_dbuf_state);
6096
6097 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6098 break;
6099 }
6100
6101 if (!new_dbuf_state)
6102 return 0;
6103
6104 new_dbuf_state->active_pipes =
6105 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6106
6107 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
6108 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6109 if (ret)
6110 return ret;
6111 }
6112
6113 for_each_intel_crtc(&dev_priv->drm, crtc) {
6114 enum pipe pipe = crtc->pipe;
6115
6116 new_dbuf_state->slices[pipe] =
6117 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes);
6118
6119 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
6120 continue;
6121
6122 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6123 if (ret)
6124 return ret;
6125 }
6126
6127 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
6128
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006129 if (IS_ALDERLAKE_P(dev_priv))
6130 new_dbuf_state->joined_mbus = adlp_check_mbus_joined(new_dbuf_state->active_pipes);
6131
6132 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
6133 old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006134 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
6135 if (ret)
6136 return ret;
6137
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006138 if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6139 /* TODO: Implement vblank synchronized MBUS joining changes */
6140 ret = intel_modeset_all_pipes(state);
6141 if (ret)
6142 return ret;
6143 }
6144
Ville Syrjäläef79d622021-01-22 22:56:32 +02006145 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006146 "Enabled dbuf slices 0x%x -> 0x%x (total dbuf slices 0x%x), mbus joined? %s->%s\n",
Ville Syrjäläef79d622021-01-22 22:56:32 +02006147 old_dbuf_state->enabled_slices,
6148 new_dbuf_state->enabled_slices,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006149 INTEL_INFO(dev_priv)->dbuf.slice_mask,
6150 yesno(old_dbuf_state->joined_mbus),
6151 yesno(new_dbuf_state->joined_mbus));
Ville Syrjäläef79d622021-01-22 22:56:32 +02006152 }
6153
6154 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6155 enum pipe pipe = crtc->pipe;
6156
6157 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
6158
6159 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
6160 continue;
6161
6162 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6163 if (ret)
6164 return ret;
6165 }
6166
6167 for_each_intel_crtc(&dev_priv->drm, crtc) {
6168 ret = skl_crtc_allocate_ddb(state, crtc);
6169 if (ret)
6170 return ret;
6171 }
6172
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006173 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006174 new_crtc_state, i) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006175 ret = skl_allocate_plane_ddb(state, crtc);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006176 if (ret)
6177 return ret;
6178
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006179 ret = skl_ddb_add_affected_planes(old_crtc_state,
6180 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006181 if (ret)
6182 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07006183 }
6184
6185 return 0;
6186}
6187
Ville Syrjäläab98e942019-02-08 22:05:27 +02006188static char enast(bool enable)
6189{
6190 return enable ? '*' : ' ';
6191}
6192
Matt Roper2722efb2016-08-17 15:55:55 -04006193static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006194skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006195{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006196 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6197 const struct intel_crtc_state *old_crtc_state;
6198 const struct intel_crtc_state *new_crtc_state;
6199 struct intel_plane *plane;
6200 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01006201 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006202
Jani Nikulabdbf43d2019-10-28 12:38:15 +02006203 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02006204 return;
6205
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006206 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6207 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02006208 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
6209
6210 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
6211 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
6212
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006213 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6214 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006215 const struct skl_ddb_entry *old, *new;
6216
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006217 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
6218 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006219
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006220 if (skl_ddb_entry_equal(old, new))
6221 continue;
6222
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006223 drm_dbg_kms(&dev_priv->drm,
6224 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
6225 plane->base.base.id, plane->base.name,
6226 old->start, old->end, new->start, new->end,
6227 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006228 }
6229
6230 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6231 enum plane_id plane_id = plane->id;
6232 const struct skl_plane_wm *old_wm, *new_wm;
6233
6234 old_wm = &old_pipe_wm->planes[plane_id];
6235 new_wm = &new_pipe_wm->planes[plane_id];
6236
6237 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
6238 continue;
6239
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006240 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006241 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
6242 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006243 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006244 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
6245 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
6246 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
6247 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
6248 enast(old_wm->trans_wm.enable),
6249 enast(old_wm->sagv.wm0.enable),
6250 enast(old_wm->sagv.trans_wm.enable),
6251 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
6252 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
6253 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
6254 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
6255 enast(new_wm->trans_wm.enable),
6256 enast(new_wm->sagv.wm0.enable),
6257 enast(new_wm->sagv.trans_wm.enable));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006258
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006259 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006260 "[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"
6261 " -> %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 +03006262 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006263 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
6264 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
6265 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
6266 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
6267 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
6268 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
6269 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
6270 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
6271 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
6272 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
6273 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
6274 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
6275 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
6276 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
6277 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
6278 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
6279 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
6280 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
6281 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
6282 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
6283 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
6284 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006285
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006286 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006287 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6288 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006289 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006290 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
6291 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
6292 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
6293 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
6294 old_wm->trans_wm.blocks,
6295 old_wm->sagv.wm0.blocks,
6296 old_wm->sagv.trans_wm.blocks,
6297 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
6298 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
6299 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
6300 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
6301 new_wm->trans_wm.blocks,
6302 new_wm->sagv.wm0.blocks,
6303 new_wm->sagv.trans_wm.blocks);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006304
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006305 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006306 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6307 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006308 plane->base.base.id, plane->base.name,
6309 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6310 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6311 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6312 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6313 old_wm->trans_wm.min_ddb_alloc,
Ville Syrjäläa68aa482021-02-26 17:32:01 +02006314 old_wm->sagv.wm0.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006315 old_wm->sagv.trans_wm.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006316 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6317 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6318 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6319 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006320 new_wm->trans_wm.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006321 new_wm->sagv.wm0.min_ddb_alloc,
6322 new_wm->sagv.trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006323 }
6324 }
6325}
6326
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006327static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6328 const struct skl_pipe_wm *old_pipe_wm,
6329 const struct skl_pipe_wm *new_pipe_wm)
6330{
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006331 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6332 int level, max_level = ilk_wm_max_level(i915);
6333
6334 for (level = 0; level <= max_level; level++) {
6335 /*
6336 * We don't check uv_wm as the hardware doesn't actually
6337 * use it. It only gets used for calculating the required
6338 * ddb allocation.
6339 */
Ville Syrjälä93fe8622021-03-25 02:44:14 +02006340 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6341 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006342 return false;
6343 }
6344
Matt Roper7959ffe2021-05-18 17:06:11 -07006345 if (HAS_HW_SAGV_WM(i915)) {
6346 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6347 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6348
6349 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6350 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6351 return false;
6352 }
6353
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006354 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6355 skl_plane_trans_wm(new_pipe_wm, plane->id));
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006356}
6357
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006358/*
6359 * To make sure the cursor watermark registers are always consistent
6360 * with our computed state the following scenario needs special
6361 * treatment:
6362 *
6363 * 1. enable cursor
6364 * 2. move cursor entirely offscreen
6365 * 3. disable cursor
6366 *
6367 * Step 2. does call .disable_plane() but does not zero the watermarks
6368 * (since we consider an offscreen cursor still active for the purposes
6369 * of watermarks). Step 3. would not normally call .disable_plane()
6370 * because the actual plane visibility isn't changing, and we don't
6371 * deallocate the cursor ddb until the pipe gets disabled. So we must
6372 * force step 3. to call .disable_plane() to update the watermark
6373 * registers properly.
6374 *
6375 * Other planes do not suffer from this issues as their watermarks are
6376 * calculated based on the actual plane visibility. The only time this
6377 * can trigger for the other planes is during the initial readout as the
6378 * default value of the watermarks registers is not zero.
6379 */
6380static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6381 struct intel_crtc *crtc)
6382{
6383 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6384 const struct intel_crtc_state *old_crtc_state =
6385 intel_atomic_get_old_crtc_state(state, crtc);
6386 struct intel_crtc_state *new_crtc_state =
6387 intel_atomic_get_new_crtc_state(state, crtc);
6388 struct intel_plane *plane;
6389
6390 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6391 struct intel_plane_state *plane_state;
6392 enum plane_id plane_id = plane->id;
6393
6394 /*
6395 * Force a full wm update for every plane on modeset.
6396 * Required because the reset value of the wm registers
6397 * is non-zero, whereas we want all disabled planes to
6398 * have zero watermarks. So if we turn off the relevant
6399 * power well the hardware state will go out of sync
6400 * with the software state.
6401 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006402 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006403 skl_plane_selected_wm_equals(plane,
6404 &old_crtc_state->wm.skl.optimal,
6405 &new_crtc_state->wm.skl.optimal))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006406 continue;
6407
6408 plane_state = intel_atomic_get_plane_state(state, plane);
6409 if (IS_ERR(plane_state))
6410 return PTR_ERR(plane_state);
6411
6412 new_crtc_state->update_planes |= BIT(plane_id);
6413 }
6414
6415 return 0;
6416}
6417
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306418static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006419skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306420{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006421 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006422 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306423 int ret, i;
6424
Ville Syrjäläffc90032020-11-06 19:30:37 +02006425 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6426 ret = skl_build_pipe_wm(state, crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006427 if (ret)
6428 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006429 }
6430
Matt Roperd8e87492018-12-11 09:31:07 -08006431 ret = skl_compute_ddb(state);
6432 if (ret)
6433 return ret;
6434
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006435 ret = intel_compute_sagv_mask(state);
6436 if (ret)
6437 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006438
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006439 /*
6440 * skl_compute_ddb() will have adjusted the final watermarks
6441 * based on how much ddb is available. Now we can actually
6442 * check if the final watermarks changed.
6443 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006444 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006445 ret = skl_wm_add_affected_planes(state, crtc);
6446 if (ret)
6447 return ret;
6448 }
6449
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006450 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006451
Matt Roper98d39492016-05-12 07:06:03 -07006452 return 0;
6453}
6454
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006455static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006456 struct intel_wm_config *config)
6457{
6458 struct intel_crtc *crtc;
6459
6460 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006461 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006462 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6463
6464 if (!wm->pipe_enabled)
6465 continue;
6466
6467 config->sprites_enabled |= wm->sprites_enabled;
6468 config->sprites_scaled |= wm->sprites_scaled;
6469 config->num_pipes_active++;
6470 }
6471}
6472
Matt Ropered4a6a72016-02-23 17:20:13 -08006473static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006474{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006475 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006476 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006477 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006478 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006479 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006480
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006481 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006482
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006483 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6484 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006485
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006486 /* 5/6 split only in single pipe config on IVB+ */
Matt Roper7dadd282021-03-19 21:42:43 -07006487 if (DISPLAY_VER(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006488 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006489 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6490 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006491
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006492 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006493 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006494 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006495 }
6496
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006497 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006498 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006499
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006500 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006501
Imre Deak820c1982013-12-17 14:46:36 +02006502 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006503}
6504
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006505static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006506 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006507{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006508 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6509 const struct intel_crtc_state *crtc_state =
6510 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006511
Matt Ropered4a6a72016-02-23 17:20:13 -08006512 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006513 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006514 ilk_program_watermarks(dev_priv);
6515 mutex_unlock(&dev_priv->wm.wm_mutex);
6516}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006517
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006518static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006519 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006520{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006521 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6522 const struct intel_crtc_state *crtc_state =
6523 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006524
6525 if (!crtc_state->wm.need_postvbl_update)
6526 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006527
6528 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006529 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6530 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006531 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006532}
6533
Jani Nikula81b55ef2020-04-20 17:04:38 +03006534static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006535{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006536 level->enable = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006537 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006538 level->blocks = val & PLANE_WM_BLOCKS_MASK;
Matt Roper1003cee2021-05-14 08:36:54 -07006539 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
Pradeep Bhat30789992014-11-04 17:06:45 +00006540}
6541
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006542void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006543 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006544{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006545 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6546 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006547 int level, max_level;
6548 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006549 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006550
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006551 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006552
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006553 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006554 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006555
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006556 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006557 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006558 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006559 else
Jani Nikula5f461662020-11-30 13:15:58 +02006560 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006561
6562 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6563 }
6564
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006565 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006566 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006567 else
Jani Nikula5f461662020-11-30 13:15:58 +02006568 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006569
6570 skl_wm_level_from_reg_val(val, &wm->trans_wm);
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006571
Matt Roper7959ffe2021-05-18 17:06:11 -07006572 if (HAS_HW_SAGV_WM(dev_priv)) {
6573 if (plane_id != PLANE_CURSOR)
6574 val = intel_uncore_read(&dev_priv->uncore,
6575 PLANE_WM_SAGV(pipe, plane_id));
6576 else
6577 val = intel_uncore_read(&dev_priv->uncore,
6578 CUR_WM_SAGV(pipe));
6579
6580 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6581
6582 if (plane_id != PLANE_CURSOR)
6583 val = intel_uncore_read(&dev_priv->uncore,
6584 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6585 else
6586 val = intel_uncore_read(&dev_priv->uncore,
6587 CUR_WM_SAGV_TRANS(pipe));
6588
6589 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6590 } else if (DISPLAY_VER(dev_priv) >= 12) {
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006591 wm->sagv.wm0 = wm->wm[0];
6592 wm->sagv.trans_wm = wm->trans_wm;
6593 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006594 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006595}
6596
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006597void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006598{
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006599 struct intel_dbuf_state *dbuf_state =
6600 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006601 struct intel_crtc *crtc;
Pradeep Bhat30789992014-11-04 17:06:45 +00006602
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006603 if (IS_ALDERLAKE_P(dev_priv))
6604 dbuf_state->joined_mbus = intel_de_read(dev_priv, MBUS_CTL) & MBUS_JOIN;
6605
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006606 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006607 struct intel_crtc_state *crtc_state =
6608 to_intel_crtc_state(crtc->base.state);
6609 enum pipe pipe = crtc->pipe;
Ville Syrjälä835c1762021-05-18 17:06:16 -07006610 unsigned int mbus_offset;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006611 enum plane_id plane_id;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006612
Maarten Lankhorstec193642019-06-28 10:55:17 +02006613 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
Ville Syrjälädbf71382020-11-06 19:30:38 +02006614 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006615
6616 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6617
6618 for_each_plane_id_on_crtc(crtc, plane_id) {
6619 struct skl_ddb_entry *ddb_y =
6620 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6621 struct skl_ddb_entry *ddb_uv =
6622 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
6623
6624 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6625 plane_id, ddb_y, ddb_uv);
6626
6627 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6628 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_uv);
6629 }
6630
6631 dbuf_state->slices[pipe] =
6632 skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes);
6633
6634 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6635
Ville Syrjälä835c1762021-05-18 17:06:16 -07006636 /*
6637 * Used for checking overlaps, so we need absolute
6638 * offsets instead of MBUS relative offsets.
6639 */
6640 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_state->slices[pipe]);
6641 crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
6642 crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006643
6644 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006645 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x, mbus joined: %s\n",
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006646 crtc->base.base.id, crtc->base.name,
6647 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006648 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
6649 yesno(dbuf_state->joined_mbus));
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006650 }
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006651
6652 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
Pradeep Bhat30789992014-11-04 17:06:45 +00006653}
6654
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006655static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006656{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006657 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006658 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006659 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006660 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6661 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006662 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006663
Jani Nikula5f461662020-11-30 13:15:58 +02006664 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006665
Ville Syrjälä15606532016-05-13 17:55:17 +03006666 memset(active, 0, sizeof(*active));
6667
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006668 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006669
6670 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006671 u32 tmp = hw->wm_pipe[pipe];
6672
6673 /*
6674 * For active pipes LP0 watermark is marked as
6675 * enabled, and LP1+ watermaks as disabled since
6676 * we can't really reverse compute them in case
6677 * multiple pipes are active.
6678 */
6679 active->wm[0].enable = true;
6680 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6681 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6682 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006683 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006684 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006685
6686 /*
6687 * For inactive pipes, all watermark levels
6688 * should be marked as enabled but zeroed,
6689 * which is what we'd compute them to.
6690 */
6691 for (level = 0; level <= max_level; level++)
6692 active->wm[level].enable = true;
6693 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006694
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006695 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006696}
6697
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006698#define _FW_WM(value, plane) \
6699 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6700#define _FW_WM_VLV(value, plane) \
6701 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6702
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006703static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6704 struct g4x_wm_values *wm)
6705{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006706 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006707
Jani Nikula5f461662020-11-30 13:15:58 +02006708 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006709 wm->sr.plane = _FW_WM(tmp, SR);
6710 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6711 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6712 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6713
Jani Nikula5f461662020-11-30 13:15:58 +02006714 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006715 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6716 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6717 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6718 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6719 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6720 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6721
Jani Nikula5f461662020-11-30 13:15:58 +02006722 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006723 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6724 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6725 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6726 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6727}
6728
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006729static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6730 struct vlv_wm_values *wm)
6731{
6732 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006733 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006734
6735 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006736 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006737
Ville Syrjälä1b313892016-11-28 19:37:08 +02006738 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006739 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006740 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006741 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006742 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006743 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006744 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006745 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6746 }
6747
Jani Nikula5f461662020-11-30 13:15:58 +02006748 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006749 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006750 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6751 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6752 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006753
Jani Nikula5f461662020-11-30 13:15:58 +02006754 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006755 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6756 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6757 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006758
Jani Nikula5f461662020-11-30 13:15:58 +02006759 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006760 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6761
6762 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02006763 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006764 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6765 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006766
Jani Nikula5f461662020-11-30 13:15:58 +02006767 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006768 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6769 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006770
Jani Nikula5f461662020-11-30 13:15:58 +02006771 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006772 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6773 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006774
Jani Nikula5f461662020-11-30 13:15:58 +02006775 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006776 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006777 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6778 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6779 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6780 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6781 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6782 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6783 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6784 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6785 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006786 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02006787 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006788 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6789 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006790
Jani Nikula5f461662020-11-30 13:15:58 +02006791 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006792 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006793 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6794 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6795 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6796 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6797 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6798 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006799 }
6800}
6801
6802#undef _FW_WM
6803#undef _FW_WM_VLV
6804
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006805void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006806{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006807 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6808 struct intel_crtc *crtc;
6809
6810 g4x_read_wm_values(dev_priv, wm);
6811
Jani Nikula5f461662020-11-30 13:15:58 +02006812 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006813
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006814 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006815 struct intel_crtc_state *crtc_state =
6816 to_intel_crtc_state(crtc->base.state);
6817 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6818 struct g4x_pipe_wm *raw;
6819 enum pipe pipe = crtc->pipe;
6820 enum plane_id plane_id;
6821 int level, max_level;
6822
6823 active->cxsr = wm->cxsr;
6824 active->hpll_en = wm->hpll_en;
6825 active->fbc_en = wm->fbc_en;
6826
6827 active->sr = wm->sr;
6828 active->hpll = wm->hpll;
6829
6830 for_each_plane_id_on_crtc(crtc, plane_id) {
6831 active->wm.plane[plane_id] =
6832 wm->pipe[pipe].plane[plane_id];
6833 }
6834
6835 if (wm->cxsr && wm->hpll_en)
6836 max_level = G4X_WM_LEVEL_HPLL;
6837 else if (wm->cxsr)
6838 max_level = G4X_WM_LEVEL_SR;
6839 else
6840 max_level = G4X_WM_LEVEL_NORMAL;
6841
6842 level = G4X_WM_LEVEL_NORMAL;
6843 raw = &crtc_state->wm.g4x.raw[level];
6844 for_each_plane_id_on_crtc(crtc, plane_id)
6845 raw->plane[plane_id] = active->wm.plane[plane_id];
6846
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006847 level = G4X_WM_LEVEL_SR;
6848 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006849 goto out;
6850
6851 raw = &crtc_state->wm.g4x.raw[level];
6852 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6853 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6854 raw->plane[PLANE_SPRITE0] = 0;
6855 raw->fbc = active->sr.fbc;
6856
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006857 level = G4X_WM_LEVEL_HPLL;
6858 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006859 goto out;
6860
6861 raw = &crtc_state->wm.g4x.raw[level];
6862 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6863 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6864 raw->plane[PLANE_SPRITE0] = 0;
6865 raw->fbc = active->hpll.fbc;
6866
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006867 level++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006868 out:
6869 for_each_plane_id_on_crtc(crtc, plane_id)
6870 g4x_raw_plane_wm_set(crtc_state, level,
6871 plane_id, USHRT_MAX);
6872 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6873
6874 crtc_state->wm.g4x.optimal = *active;
6875 crtc_state->wm.g4x.intermediate = *active;
6876
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006877 drm_dbg_kms(&dev_priv->drm,
6878 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6879 pipe_name(pipe),
6880 wm->pipe[pipe].plane[PLANE_PRIMARY],
6881 wm->pipe[pipe].plane[PLANE_CURSOR],
6882 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006883 }
6884
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006885 drm_dbg_kms(&dev_priv->drm,
6886 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6887 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6888 drm_dbg_kms(&dev_priv->drm,
6889 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6890 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6891 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6892 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006893}
6894
6895void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6896{
6897 struct intel_plane *plane;
6898 struct intel_crtc *crtc;
6899
6900 mutex_lock(&dev_priv->wm.wm_mutex);
6901
6902 for_each_intel_plane(&dev_priv->drm, plane) {
6903 struct intel_crtc *crtc =
6904 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6905 struct intel_crtc_state *crtc_state =
6906 to_intel_crtc_state(crtc->base.state);
6907 struct intel_plane_state *plane_state =
6908 to_intel_plane_state(plane->base.state);
6909 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6910 enum plane_id plane_id = plane->id;
6911 int level;
6912
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006913 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006914 continue;
6915
6916 for (level = 0; level < 3; level++) {
6917 struct g4x_pipe_wm *raw =
6918 &crtc_state->wm.g4x.raw[level];
6919
6920 raw->plane[plane_id] = 0;
6921 wm_state->wm.plane[plane_id] = 0;
6922 }
6923
6924 if (plane_id == PLANE_PRIMARY) {
6925 for (level = 0; level < 3; level++) {
6926 struct g4x_pipe_wm *raw =
6927 &crtc_state->wm.g4x.raw[level];
6928 raw->fbc = 0;
6929 }
6930
6931 wm_state->sr.fbc = 0;
6932 wm_state->hpll.fbc = 0;
6933 wm_state->fbc_en = false;
6934 }
6935 }
6936
6937 for_each_intel_crtc(&dev_priv->drm, crtc) {
6938 struct intel_crtc_state *crtc_state =
6939 to_intel_crtc_state(crtc->base.state);
6940
6941 crtc_state->wm.g4x.intermediate =
6942 crtc_state->wm.g4x.optimal;
6943 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6944 }
6945
6946 g4x_program_watermarks(dev_priv);
6947
6948 mutex_unlock(&dev_priv->wm.wm_mutex);
6949}
6950
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006951void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006952{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006953 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006954 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006955 u32 val;
6956
6957 vlv_read_wm_values(dev_priv, wm);
6958
Jani Nikula5f461662020-11-30 13:15:58 +02006959 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006960 wm->level = VLV_WM_LEVEL_PM2;
6961
6962 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006963 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006964
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006965 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006966 if (val & DSP_MAXFIFO_PM5_ENABLE)
6967 wm->level = VLV_WM_LEVEL_PM5;
6968
Ville Syrjälä58590c12015-09-08 21:05:12 +03006969 /*
6970 * If DDR DVFS is disabled in the BIOS, Punit
6971 * will never ack the request. So if that happens
6972 * assume we don't have to enable/disable DDR DVFS
6973 * dynamically. To test that just set the REQ_ACK
6974 * bit to poke the Punit, but don't change the
6975 * HIGH/LOW bits so that we don't actually change
6976 * the current state.
6977 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006978 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006979 val |= FORCE_DDR_FREQ_REQ_ACK;
6980 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6981
6982 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6983 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006984 drm_dbg_kms(&dev_priv->drm,
6985 "Punit not acking DDR DVFS request, "
6986 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006987 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6988 } else {
6989 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6990 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6991 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6992 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006993
Chris Wilson337fa6e2019-04-26 09:17:20 +01006994 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006995 }
6996
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006997 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006998 struct intel_crtc_state *crtc_state =
6999 to_intel_crtc_state(crtc->base.state);
7000 struct vlv_wm_state *active = &crtc->wm.active.vlv;
7001 const struct vlv_fifo_state *fifo_state =
7002 &crtc_state->wm.vlv.fifo_state;
7003 enum pipe pipe = crtc->pipe;
7004 enum plane_id plane_id;
7005 int level;
7006
7007 vlv_get_fifo_size(crtc_state);
7008
7009 active->num_levels = wm->level + 1;
7010 active->cxsr = wm->cxsr;
7011
Ville Syrjäläff32c542017-03-02 19:14:57 +02007012 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03007013 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02007014 &crtc_state->wm.vlv.raw[level];
7015
7016 active->sr[level].plane = wm->sr.plane;
7017 active->sr[level].cursor = wm->sr.cursor;
7018
7019 for_each_plane_id_on_crtc(crtc, plane_id) {
7020 active->wm[level].plane[plane_id] =
7021 wm->pipe[pipe].plane[plane_id];
7022
7023 raw->plane[plane_id] =
7024 vlv_invert_wm_value(active->wm[level].plane[plane_id],
7025 fifo_state->plane[plane_id]);
7026 }
7027 }
7028
7029 for_each_plane_id_on_crtc(crtc, plane_id)
7030 vlv_raw_plane_wm_set(crtc_state, level,
7031 plane_id, USHRT_MAX);
7032 vlv_invalidate_wms(crtc, active, level);
7033
7034 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007035 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007036
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007037 drm_dbg_kms(&dev_priv->drm,
7038 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
7039 pipe_name(pipe),
7040 wm->pipe[pipe].plane[PLANE_PRIMARY],
7041 wm->pipe[pipe].plane[PLANE_CURSOR],
7042 wm->pipe[pipe].plane[PLANE_SPRITE0],
7043 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007044 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007045
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007046 drm_dbg_kms(&dev_priv->drm,
7047 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
7048 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007049}
7050
Ville Syrjälä602ae832017-03-02 19:15:02 +02007051void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
7052{
7053 struct intel_plane *plane;
7054 struct intel_crtc *crtc;
7055
7056 mutex_lock(&dev_priv->wm.wm_mutex);
7057
7058 for_each_intel_plane(&dev_priv->drm, plane) {
7059 struct intel_crtc *crtc =
7060 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
7061 struct intel_crtc_state *crtc_state =
7062 to_intel_crtc_state(crtc->base.state);
7063 struct intel_plane_state *plane_state =
7064 to_intel_plane_state(plane->base.state);
7065 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
7066 const struct vlv_fifo_state *fifo_state =
7067 &crtc_state->wm.vlv.fifo_state;
7068 enum plane_id plane_id = plane->id;
7069 int level;
7070
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01007071 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02007072 continue;
7073
7074 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03007075 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02007076 &crtc_state->wm.vlv.raw[level];
7077
7078 raw->plane[plane_id] = 0;
7079
7080 wm_state->wm[level].plane[plane_id] =
7081 vlv_invert_wm_value(raw->plane[plane_id],
7082 fifo_state->plane[plane_id]);
7083 }
7084 }
7085
7086 for_each_intel_crtc(&dev_priv->drm, crtc) {
7087 struct intel_crtc_state *crtc_state =
7088 to_intel_crtc_state(crtc->base.state);
7089
7090 crtc_state->wm.vlv.intermediate =
7091 crtc_state->wm.vlv.optimal;
7092 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
7093 }
7094
7095 vlv_program_watermarks(dev_priv);
7096
7097 mutex_unlock(&dev_priv->wm.wm_mutex);
7098}
7099
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007100/*
7101 * FIXME should probably kill this and improve
7102 * the real watermark readout/sanitation instead
7103 */
7104static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
7105{
Jani Nikula5f461662020-11-30 13:15:58 +02007106 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM1_LP_SR_EN);
7107 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM1_LP_SR_EN);
7108 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 +02007109
7110 /*
7111 * Don't touch WM1S_LP_EN here.
7112 * Doing so could cause underruns.
7113 */
7114}
7115
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007116void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007117{
Imre Deak820c1982013-12-17 14:46:36 +02007118 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007119 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007120
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007121 ilk_init_lp_watermarks(dev_priv);
7122
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007123 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007124 ilk_pipe_wm_get_hw_state(crtc);
7125
Jani Nikula5f461662020-11-30 13:15:58 +02007126 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
7127 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
7128 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007129
Jani Nikula5f461662020-11-30 13:15:58 +02007130 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
Matt Roper7dadd282021-03-19 21:42:43 -07007131 if (DISPLAY_VER(dev_priv) >= 7) {
Jani Nikula5f461662020-11-30 13:15:58 +02007132 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
7133 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02007134 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007135
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007136 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007137 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 +02007138 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01007139 else if (IS_IVYBRIDGE(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007140 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 +02007141 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007142
7143 hw->enable_fbc_wm =
Jani Nikula5f461662020-11-30 13:15:58 +02007144 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007145}
7146
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307147void intel_enable_ipc(struct drm_i915_private *dev_priv)
7148{
7149 u32 val;
7150
José Roberto de Souzafd847b82018-09-18 13:47:11 -07007151 if (!HAS_IPC(dev_priv))
7152 return;
7153
Jani Nikula5f461662020-11-30 13:15:58 +02007154 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307155
7156 if (dev_priv->ipc_enabled)
7157 val |= DISP_IPC_ENABLE;
7158 else
7159 val &= ~DISP_IPC_ENABLE;
7160
Jani Nikula5f461662020-11-30 13:15:58 +02007161 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307162}
7163
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007164static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
7165{
7166 /* Display WA #0477 WaDisableIPC: skl */
7167 if (IS_SKYLAKE(dev_priv))
7168 return false;
7169
7170 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01007171 if (IS_KABYLAKE(dev_priv) ||
7172 IS_COFFEELAKE(dev_priv) ||
7173 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007174 return dev_priv->dram_info.symmetric_memory;
7175
7176 return true;
7177}
7178
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307179void intel_init_ipc(struct drm_i915_private *dev_priv)
7180{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307181 if (!HAS_IPC(dev_priv))
7182 return;
7183
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007184 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07007185
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307186 intel_enable_ipc(dev_priv);
7187}
7188
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007189static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007190{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007191 /*
7192 * On Ibex Peak and Cougar Point, we need to disable clock
7193 * gating for the panel power sequencer or it will fail to
7194 * start up when no ports are active.
7195 */
Jani Nikula5f461662020-11-30 13:15:58 +02007196 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007197}
7198
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007199static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007200{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007201 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007202
Damien Lespiau055e3932014-08-18 13:49:10 +01007203 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007204 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
7205 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007206 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007207
Jani Nikula5f461662020-11-30 13:15:58 +02007208 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
7209 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007210 }
7211}
7212
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007213static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007214{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007215 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007216
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007217 /*
7218 * Required for FBC
7219 * WaFbcDisableDpfcClockGating:ilk
7220 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007221 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7222 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7223 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007224
Jani Nikula5f461662020-11-30 13:15:58 +02007225 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007226 MARIUNIT_CLOCK_GATE_DISABLE |
7227 SVSMUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007228 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007229 VFMUNIT_CLOCK_GATE_DISABLE);
7230
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007231 /*
7232 * According to the spec the following bits should be set in
7233 * order to enable memory self-refresh
7234 * The bit 22/21 of 0x42004
7235 * The bit 5 of 0x42020
7236 * The bit 15 of 0x45000
7237 */
Jani Nikula5f461662020-11-30 13:15:58 +02007238 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7239 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007240 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007241 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007242 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
7243 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007244 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007245
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007246 /*
7247 * Based on the document from hardware guys the following bits
7248 * should be set unconditionally in order to enable FBC.
7249 * The bit 22 of 0x42000
7250 * The bit 22 of 0x42004
7251 * The bit 7,8,9 of 0x42020.
7252 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007253 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007254 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Jani Nikula5f461662020-11-30 13:15:58 +02007255 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7256 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007257 ILK_FBCQ_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007258 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7259 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007260 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007261 }
7262
Jani Nikula5f461662020-11-30 13:15:58 +02007263 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007264
Jani Nikula5f461662020-11-30 13:15:58 +02007265 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7266 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007267 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05307268
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007269 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007270
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007271 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007272}
7273
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007274static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007275{
Ville Syrjäläd048a262019-08-21 20:30:31 +03007276 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007277 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007278
7279 /*
7280 * On Ibex Peak and Cougar Point, we need to disable clock
7281 * gating for the panel power sequencer or it will fail to
7282 * start up when no ports are active.
7283 */
Jani Nikula5f461662020-11-30 13:15:58 +02007284 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnescd664072013-10-02 10:34:19 -07007285 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7286 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007287 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
Daniel Vetter3107bd42012-10-31 22:52:31 +01007288 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007289 /* The below fixes the weird display corruption, a few pixels shifted
7290 * downward, on (only) LVDS of some HP laptops with IVY.
7291 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007292 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007293 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007294 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7295 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007296 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007297 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007298 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7299 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Jani Nikula5f461662020-11-30 13:15:58 +02007300 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007301 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007302 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007303 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007304 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
Daniel Vetter3107bd42012-10-31 22:52:31 +01007305 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7306 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007307}
7308
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007309static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007310{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007311 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007312
Jani Nikula5f461662020-11-30 13:15:58 +02007313 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007314 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007315 drm_dbg_kms(&dev_priv->drm,
7316 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7317 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007318}
7319
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007320static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007321{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007322 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007323
Jani Nikula5f461662020-11-30 13:15:58 +02007324 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007325
Jani Nikula5f461662020-11-30 13:15:58 +02007326 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7327 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007328 ILK_ELPIN_409_SELECT);
7329
Jani Nikula5f461662020-11-30 13:15:58 +02007330 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7331 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007332 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7333 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7334
7335 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7336 * gating disable must be set. Failure to set it results in
7337 * flickering pixels due to Z write ordering failures after
7338 * some amount of runtime in the Mesa "fire" demo, and Unigine
7339 * Sanctuary and Tropics, and apparently anything else with
7340 * alpha test or pixel discard.
7341 *
7342 * According to the spec, bit 11 (RCCUNIT) must also be set,
7343 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007344 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007345 * WaDisableRCCUnitClockGating:snb
7346 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007347 */
Jani Nikula5f461662020-11-30 13:15:58 +02007348 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007349 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7350 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7351
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007352 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007353 * According to the spec the following bits should be
7354 * set in order to enable memory self-refresh and fbc:
7355 * The bit21 and bit22 of 0x42000
7356 * The bit21 and bit22 of 0x42004
7357 * The bit5 and bit7 of 0x42020
7358 * The bit14 of 0x70180
7359 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007360 *
7361 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007362 */
Jani Nikula5f461662020-11-30 13:15:58 +02007363 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7364 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007365 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007366 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7367 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007368 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Jani Nikula5f461662020-11-30 13:15:58 +02007369 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7370 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
Damien Lespiau231e54f2012-10-19 17:55:41 +01007371 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7372 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007373
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007374 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007375
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007376 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007377
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007378 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007379}
7380
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007381static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007382{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007383 /*
7384 * TODO: this bit should only be enabled when really needed, then
7385 * disabled when not needed anymore in order to save power.
7386 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007387 if (HAS_PCH_LPT_LP(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007388 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7389 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007390 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007391
7392 /* WADPOClockGatingDisable:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007393 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7394 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007395 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007396}
7397
Ville Syrjälä712bf362016-10-31 22:37:23 +02007398static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007399{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007400 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02007401 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007402
7403 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007404 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
Imre Deak7d708ee2013-04-17 14:04:50 +03007405 }
7406}
7407
Imre Deak450174f2016-05-03 15:54:21 +03007408static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7409 int general_prio_credits,
7410 int high_prio_credits)
7411{
7412 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007413 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007414
7415 /* WaTempDisableDOPClkGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007416 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7417 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
Imre Deak450174f2016-05-03 15:54:21 +03007418
Jani Nikula5f461662020-11-30 13:15:58 +02007419 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Oscar Mateo930a7842017-10-17 13:25:45 -07007420 val &= ~L3_PRIO_CREDITS_MASK;
7421 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7422 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
Jani Nikula5f461662020-11-30 13:15:58 +02007423 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007424
7425 /*
7426 * Wait at least 100 clocks before re-enabling clock gating.
7427 * See the definition of L3SQCREG1 in BSpec.
7428 */
Jani Nikula5f461662020-11-30 13:15:58 +02007429 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Imre Deak450174f2016-05-03 15:54:21 +03007430 udelay(1);
Jani Nikula5f461662020-11-30 13:15:58 +02007431 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
Imre Deak450174f2016-05-03 15:54:21 +03007432}
7433
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007434static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7435{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007436 /* Wa_1409120013:icl,ehl */
Jani Nikula5f461662020-11-30 13:15:58 +02007437 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä73ab6ec2021-11-04 16:45:15 +02007438 DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007439
Matt Atwood6f4194c2020-01-13 23:11:28 -05007440 /*Wa_14010594013:icl, ehl */
7441 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
Lucas De Marchidbac4f32021-07-28 14:59:38 -07007442 0, ICL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007443}
7444
José Roberto de Souza35f08372021-01-13 05:37:59 -08007445static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
Michel Thierry5d869232019-08-23 01:20:34 -07007446{
José Roberto de Souzac4924052021-07-12 17:38:50 -07007447 /* Wa_1409120013:tgl,rkl,adl-s,dg1 */
Clint Taylor8c209f42021-06-08 10:47:21 -07007448 if (IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv) ||
7449 IS_ALDERLAKE_S(dev_priv) || IS_DG1(dev_priv))
7450 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä73ab6ec2021-11-04 16:45:15 +02007451 DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007452
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007453 /* Wa_1409825376:tgl (pre-prod)*/
Matt Roper46b0d702021-07-16 22:14:26 -07007454 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007455 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 -08007456 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007457
José Roberto de Souza41c70d22021-04-08 13:49:16 -07007458 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7459 if (DISPLAY_VER(dev_priv) == 12)
7460 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7461 CLKREQ_POLICY_MEM_UP_OVRD, 0);
Michel Thierry5d869232019-08-23 01:20:34 -07007462}
7463
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007464static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7465{
7466 gen12lp_init_clock_gating(dev_priv);
7467
7468 /* Wa_22011091694:adlp */
7469 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7470}
7471
Stuart Summersda9427502020-10-14 12:19:34 -07007472static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7473{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007474 gen12lp_init_clock_gating(dev_priv);
7475
Stuart Summersda9427502020-10-14 12:19:34 -07007476 /* Wa_1409836686:dg1[a0] */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007477 if (IS_DG1_GT_STEP(dev_priv, STEP_A0, STEP_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007478 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 -07007479 DPT_GATING_DIS);
7480}
7481
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007482static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7483{
7484 if (!HAS_PCH_CNP(dev_priv))
7485 return;
7486
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007487 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Jani Nikula5f461662020-11-30 13:15:58 +02007488 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 -07007489 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007490}
7491
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007492static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7493{
7494 cnp_init_clock_gating(dev_priv);
7495 gen9_init_clock_gating(dev_priv);
7496
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007497 /* WAC6entrylatency:cfl */
Jani Nikula5f461662020-11-30 13:15:58 +02007498 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 +03007499 FBC_LLC_FULLY_OPEN);
7500
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007501 /*
7502 * WaFbcTurnOffFbcWatermark:cfl
7503 * Display WA #0562: cfl
7504 */
Jani Nikula5f461662020-11-30 13:15:58 +02007505 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 +03007506 DISP_FBC_WM_DIS);
7507
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007508 /*
7509 * WaFbcNukeOnHostModify:cfl
7510 * Display WA #0873: cfl
7511 */
Jani Nikula5f461662020-11-30 13:15:58 +02007512 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjälä73ab6ec2021-11-04 16:45:15 +02007513 DPFC_NUKE_ON_ANY_MODIFICATION);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007514}
7515
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007516static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007517{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007518 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007519
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007520 /* WAC6entrylatency:kbl */
Jani Nikula5f461662020-11-30 13:15:58 +02007521 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 +03007522 FBC_LLC_FULLY_OPEN);
7523
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007524 /* WaDisableSDEUnitClockGating:kbl */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007525 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007526 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007527 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007528
7529 /* WaDisableGamClockGating:kbl */
Matt Roper6b73a7f2021-07-16 22:14:26 -07007530 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007531 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007532 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007533
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007534 /*
7535 * WaFbcTurnOffFbcWatermark:kbl
7536 * Display WA #0562: kbl
7537 */
Jani Nikula5f461662020-11-30 13:15:58 +02007538 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 +03007539 DISP_FBC_WM_DIS);
7540
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007541 /*
7542 * WaFbcNukeOnHostModify:kbl
7543 * Display WA #0873: kbl
7544 */
Jani Nikula5f461662020-11-30 13:15:58 +02007545 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjälä73ab6ec2021-11-04 16:45:15 +02007546 DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007547}
7548
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007549static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007550{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007551 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007552
Ville Syrjäläf1421192020-07-16 22:04:25 +03007553 /* WaDisableDopClockGating:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007554 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 +03007555 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7556
Mika Kuoppala44fff992016-06-07 17:19:09 +03007557 /* WAC6entrylatency:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007558 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 +03007559 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007560
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007561 /*
7562 * WaFbcTurnOffFbcWatermark:skl
7563 * Display WA #0562: skl
7564 */
Jani Nikula5f461662020-11-30 13:15:58 +02007565 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007566 DISP_FBC_WM_DIS);
7567
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007568 /*
7569 * WaFbcNukeOnHostModify:skl
7570 * Display WA #0873: skl
7571 */
Jani Nikula5f461662020-11-30 13:15:58 +02007572 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjälä73ab6ec2021-11-04 16:45:15 +02007573 DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007574
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007575 /*
7576 * WaFbcHighMemBwCorruptionAvoidance:skl
7577 * Display WA #0883: skl
7578 */
Jani Nikula5f461662020-11-30 13:15:58 +02007579 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjälä73ab6ec2021-11-04 16:45:15 +02007580 DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007581}
7582
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007583static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007584{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007585 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007586
Ville Syrjälä885f1822020-07-08 16:12:20 +03007587 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007588 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7589 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007590 HSW_FBCQ_DIS);
7591
Ben Widawskyab57fff2013-12-12 15:28:04 -08007592 /* WaSwitchSolVfFArbitrationPriority:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007593 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 -07007594
Ben Widawskyab57fff2013-12-12 15:28:04 -08007595 /* WaPsrDPAMaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007596 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7597 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007598
Damien Lespiau055e3932014-08-18 13:49:10 +01007599 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007600 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007601 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7602 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007603 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007604 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007605
Ben Widawskyab57fff2013-12-12 15:28:04 -08007606 /* WaVSRefCountFullforceMissDisable:bdw */
7607 /* WaDSRefCountFullforceMissDisable:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007608 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7609 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ben Widawskyab57fff2013-12-12 15:28:04 -08007610 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007611
Jani Nikula5f461662020-11-30 13:15:58 +02007612 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007613 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007614
7615 /* WaDisableSDEUnitClockGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007616 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 +02007617 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007618
Imre Deak450174f2016-05-03 15:54:21 +03007619 /* WaProgramL3SqcReg1Default:bdw */
7620 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007621
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007622 /* WaKVMNotificationOnConfigChange:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007623 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 +03007624 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7625
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007626 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007627
7628 /* WaDisableDopClockGating:bdw
7629 *
7630 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7631 * clock gating.
7632 */
Jani Nikula5f461662020-11-30 13:15:58 +02007633 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7634 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007635}
7636
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007637static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007638{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007639 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007640 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7641 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007642 HSW_FBCQ_DIS);
7643
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007644 /* This is required by WaCatErrorRejectionIssue:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007645 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7646 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007647 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007648
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007649 /* WaSwitchSolVfFArbitrationPriority:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007650 intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskye3dff582013-03-20 14:49:14 -07007651
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007652 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007653}
7654
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007655static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007656{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007657 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007658
Jani Nikula5f461662020-11-30 13:15:58 +02007659 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007660
Ville Syrjälä885f1822020-07-08 16:12:20 +03007661 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007662 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7663 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007664 ILK_FBCQ_DIS);
7665
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007666 /* WaDisableBackToBackFlipFix:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007667 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007668 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7669 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7670
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007671 if (IS_IVB_GT1(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007672 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007673 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007674 else {
7675 /* must write both registers */
Jani Nikula5f461662020-11-30 13:15:58 +02007676 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Ville Syrjälä412236c2014-01-22 21:32:44 +02007677 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jani Nikula5f461662020-11-30 13:15:58 +02007678 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007679 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007680 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007681
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007682 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007683 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007684 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007685 */
Jani Nikula5f461662020-11-30 13:15:58 +02007686 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007687 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007688
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007689 /* This is required by WaCatErrorRejectionIssue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007690 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7691 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007692 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7693
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007694 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007695
Jani Nikula5f461662020-11-30 13:15:58 +02007696 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
Ben Widawsky20848222012-05-04 18:58:59 -07007697 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7698 snpcr |= GEN6_MBC_SNPCR_MED;
Jani Nikula5f461662020-11-30 13:15:58 +02007699 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007700
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007701 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007702 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007703
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007704 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007705}
7706
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007707static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007708{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007709 /* WaDisableBackToBackFlipFix:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007710 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007711 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7712 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7713
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007714 /* WaDisableDopClockGating:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007715 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007716 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7717
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007718 /* This is required by WaCatErrorRejectionIssue:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007719 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7720 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007721 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7722
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007723 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007724 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007725 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007726 */
Jani Nikula5f461662020-11-30 13:15:58 +02007727 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007728 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007729
Akash Goelc98f5062014-03-24 23:00:07 +05307730 /* WaDisableL3Bank2xClockGate:vlv
7731 * Disabling L3 clock gating- MMIO 940c[25] = 1
7732 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
Jani Nikula5f461662020-11-30 13:15:58 +02007733 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7734 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007735
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007736 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007737 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007738 * Disable clock gating on th GCFG unit to prevent a delay
7739 * in the reporting of vblank events.
7740 */
Jani Nikula5f461662020-11-30 13:15:58 +02007741 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007742}
7743
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007744static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007745{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007746 /* WaVSRefCountFullforceMissDisable:chv */
7747 /* WaDSRefCountFullforceMissDisable:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007748 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7749 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ville Syrjälä232ce332014-04-09 13:28:35 +03007750 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007751
7752 /* WaDisableSemaphoreAndSyncFlipWait:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007753 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007754 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007755
7756 /* WaDisableCSUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007757 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Ville Syrjälä08466972014-04-09 13:28:37 +03007758 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007759
7760 /* WaDisableSDEUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007761 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ville Syrjäläc6317802014-04-09 13:28:38 +03007762 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007763
7764 /*
Imre Deak450174f2016-05-03 15:54:21 +03007765 * WaProgramL3SqcReg1Default:chv
7766 * See gfxspecs/Related Documents/Performance Guide/
7767 * LSQC Setting Recommendations.
7768 */
7769 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007770}
7771
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007772static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007773{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007774 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007775
Jani Nikula5f461662020-11-30 13:15:58 +02007776 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7777 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007778 GS_UNIT_CLOCK_GATE_DISABLE |
7779 CL_UNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007780 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007781 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7782 OVRUNIT_CLOCK_GATE_DISABLE |
7783 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007784 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007785 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007786 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007787
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007788 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007789}
7790
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007791static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007792{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007793 struct intel_uncore *uncore = &dev_priv->uncore;
7794
7795 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7796 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7797 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7798 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7799 intel_uncore_write16(uncore, DEUC, 0);
7800 intel_uncore_write(uncore,
7801 MI_ARB_STATE,
7802 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007803}
7804
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007805static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007806{
Jani Nikula5f461662020-11-30 13:15:58 +02007807 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007808 I965_RCC_CLOCK_GATE_DISABLE |
7809 I965_RCPB_CLOCK_GATE_DISABLE |
7810 I965_ISC_CLOCK_GATE_DISABLE |
7811 I965_FBC_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007812 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7813 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä20f94962013-06-07 10:47:02 +03007814 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007815}
7816
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007817static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007818{
Jani Nikula5f461662020-11-30 13:15:58 +02007819 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007820
7821 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7822 DSTATE_DOT_CLOCK_GATING;
Jani Nikula5f461662020-11-30 13:15:58 +02007823 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007824
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007825 if (IS_PINEVIEW(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007826 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007827
7828 /* IIR "flip pending" means done if this bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007829 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007830
7831 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007832 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007833
7834 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007835 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007836
Jani Nikula5f461662020-11-30 13:15:58 +02007837 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007838 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007839}
7840
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007841static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007842{
Jani Nikula5f461662020-11-30 13:15:58 +02007843 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007844
7845 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007846 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007847 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007848
Jani Nikula5f461662020-11-30 13:15:58 +02007849 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007850 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007851
7852 /*
7853 * Have FBC ignore 3D activity since we use software
7854 * render tracking, and otherwise a pure 3D workload
7855 * (even if it just renders a single frame and then does
7856 * abosultely nothing) would not allow FBC to recompress
7857 * until a 2D blit occurs.
7858 */
Jani Nikula5f461662020-11-30 13:15:58 +02007859 intel_uncore_write(&dev_priv->uncore, SCPD0,
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007860 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007861}
7862
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007863static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007864{
Jani Nikula5f461662020-11-30 13:15:58 +02007865 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007866 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7867 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007868}
7869
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007870void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007871{
Dave Airlieeba4b792021-09-29 01:58:07 +03007872 dev_priv->clock_gating_funcs->init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007873}
7874
Ville Syrjälä712bf362016-10-31 22:37:23 +02007875void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007876{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007877 if (HAS_PCH_LPT(dev_priv))
7878 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007879}
7880
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007881static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007882{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007883 drm_dbg_kms(&dev_priv->drm,
7884 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007885}
7886
Dave Airlieeba4b792021-09-29 01:58:07 +03007887#define CG_FUNCS(platform) \
7888static const struct drm_i915_clock_gating_funcs platform##_clock_gating_funcs = { \
7889 .init_clock_gating = platform##_init_clock_gating, \
7890}
7891
7892CG_FUNCS(adlp);
7893CG_FUNCS(dg1);
7894CG_FUNCS(gen12lp);
7895CG_FUNCS(icl);
7896CG_FUNCS(cfl);
7897CG_FUNCS(skl);
7898CG_FUNCS(kbl);
7899CG_FUNCS(bxt);
7900CG_FUNCS(glk);
7901CG_FUNCS(bdw);
7902CG_FUNCS(chv);
7903CG_FUNCS(hsw);
7904CG_FUNCS(ivb);
7905CG_FUNCS(vlv);
7906CG_FUNCS(gen6);
7907CG_FUNCS(ilk);
7908CG_FUNCS(g4x);
7909CG_FUNCS(i965gm);
7910CG_FUNCS(i965g);
7911CG_FUNCS(gen3);
7912CG_FUNCS(i85x);
7913CG_FUNCS(i830);
7914CG_FUNCS(nop);
7915#undef CG_FUNCS
7916
Imre Deakbb400da2016-03-16 13:38:54 +02007917/**
7918 * intel_init_clock_gating_hooks - setup the clock gating hooks
7919 * @dev_priv: device private
7920 *
7921 * Setup the hooks that configure which clocks of a given platform can be
7922 * gated and also apply various GT and display specific workarounds for these
7923 * platforms. Note that some GT specific workarounds are applied separately
7924 * when GPU contexts or batchbuffers start their execution.
7925 */
7926void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7927{
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007928 if (IS_ALDERLAKE_P(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007929 dev_priv->clock_gating_funcs = &adlp_clock_gating_funcs;
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007930 else if (IS_DG1(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007931 dev_priv->clock_gating_funcs = &dg1_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007932 else if (GRAPHICS_VER(dev_priv) == 12)
Dave Airlieeba4b792021-09-29 01:58:07 +03007933 dev_priv->clock_gating_funcs = &gen12lp_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007934 else if (GRAPHICS_VER(dev_priv) == 11)
Dave Airlieeba4b792021-09-29 01:58:07 +03007935 dev_priv->clock_gating_funcs = &icl_clock_gating_funcs;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007936 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007937 dev_priv->clock_gating_funcs = &cfl_clock_gating_funcs;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007938 else if (IS_SKYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007939 dev_priv->clock_gating_funcs = &skl_clock_gating_funcs;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007940 else if (IS_KABYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007941 dev_priv->clock_gating_funcs = &kbl_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007942 else if (IS_BROXTON(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007943 dev_priv->clock_gating_funcs = &bxt_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007944 else if (IS_GEMINILAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007945 dev_priv->clock_gating_funcs = &glk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007946 else if (IS_BROADWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007947 dev_priv->clock_gating_funcs = &bdw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007948 else if (IS_CHERRYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007949 dev_priv->clock_gating_funcs = &chv_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007950 else if (IS_HASWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007951 dev_priv->clock_gating_funcs = &hsw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007952 else if (IS_IVYBRIDGE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007953 dev_priv->clock_gating_funcs = &ivb_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007954 else if (IS_VALLEYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007955 dev_priv->clock_gating_funcs = &vlv_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007956 else if (GRAPHICS_VER(dev_priv) == 6)
Dave Airlieeba4b792021-09-29 01:58:07 +03007957 dev_priv->clock_gating_funcs = &gen6_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007958 else if (GRAPHICS_VER(dev_priv) == 5)
Dave Airlieeba4b792021-09-29 01:58:07 +03007959 dev_priv->clock_gating_funcs = &ilk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007960 else if (IS_G4X(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007961 dev_priv->clock_gating_funcs = &g4x_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007962 else if (IS_I965GM(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007963 dev_priv->clock_gating_funcs = &i965gm_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007964 else if (IS_I965G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007965 dev_priv->clock_gating_funcs = &i965g_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007966 else if (GRAPHICS_VER(dev_priv) == 3)
Dave Airlieeba4b792021-09-29 01:58:07 +03007967 dev_priv->clock_gating_funcs = &gen3_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007968 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007969 dev_priv->clock_gating_funcs = &i85x_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007970 else if (GRAPHICS_VER(dev_priv) == 2)
Dave Airlieeba4b792021-09-29 01:58:07 +03007971 dev_priv->clock_gating_funcs = &i830_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007972 else {
7973 MISSING_CASE(INTEL_DEVID(dev_priv));
Dave Airlieeba4b792021-09-29 01:58:07 +03007974 dev_priv->clock_gating_funcs = &nop_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007975 }
7976}
7977
Dave Airliedde98a52021-09-29 01:58:08 +03007978static const struct drm_i915_wm_disp_funcs skl_wm_funcs = {
7979 .compute_global_watermarks = skl_compute_wm,
7980};
7981
7982static const struct drm_i915_wm_disp_funcs ilk_wm_funcs = {
7983 .compute_pipe_wm = ilk_compute_pipe_wm,
7984 .compute_intermediate_wm = ilk_compute_intermediate_wm,
7985 .initial_watermarks = ilk_initial_watermarks,
7986 .optimize_watermarks = ilk_optimize_watermarks,
7987};
7988
7989static const struct drm_i915_wm_disp_funcs vlv_wm_funcs = {
7990 .compute_pipe_wm = vlv_compute_pipe_wm,
7991 .compute_intermediate_wm = vlv_compute_intermediate_wm,
7992 .initial_watermarks = vlv_initial_watermarks,
7993 .optimize_watermarks = vlv_optimize_watermarks,
7994 .atomic_update_watermarks = vlv_atomic_update_fifo,
7995};
7996
7997static const struct drm_i915_wm_disp_funcs g4x_wm_funcs = {
7998 .compute_pipe_wm = g4x_compute_pipe_wm,
7999 .compute_intermediate_wm = g4x_compute_intermediate_wm,
8000 .initial_watermarks = g4x_initial_watermarks,
8001 .optimize_watermarks = g4x_optimize_watermarks,
8002};
8003
8004static const struct drm_i915_wm_disp_funcs pnv_wm_funcs = {
8005 .update_wm = pnv_update_wm,
8006};
8007
8008static const struct drm_i915_wm_disp_funcs i965_wm_funcs = {
8009 .update_wm = i965_update_wm,
8010};
8011
8012static const struct drm_i915_wm_disp_funcs i9xx_wm_funcs = {
8013 .update_wm = i9xx_update_wm,
8014};
8015
8016static const struct drm_i915_wm_disp_funcs i845_wm_funcs = {
8017 .update_wm = i845_update_wm,
8018};
8019
8020static const struct drm_i915_wm_disp_funcs nop_funcs = {
8021};
8022
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008023/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008024void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008025{
Daniel Vetterc921aba2012-04-26 23:28:17 +02008026 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008027 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08008028 pnv_get_mem_freq(dev_priv);
Lucas De Marchi651e7d42021-06-05 21:50:49 -07008029 else if (GRAPHICS_VER(dev_priv) == 5)
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08008030 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008031
James Ausmusb068a862019-10-09 10:23:14 -07008032 if (intel_has_sagv(dev_priv))
8033 skl_setup_sagv_block_time(dev_priv);
8034
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008035 /* For FIFO watermark updates */
Matt Roper7dadd282021-03-19 21:42:43 -07008036 if (DISPLAY_VER(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008037 skl_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008038 dev_priv->wm_disp = &skl_wm_funcs;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008039 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008040 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008041
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008042 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008043 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008044 (DISPLAY_VER(dev_priv) != 5 && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008045 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Dave Airliedde98a52021-09-29 01:58:08 +03008046 dev_priv->wm_disp = &ilk_wm_funcs;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008047 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008048 drm_dbg_kms(&dev_priv->drm,
8049 "Failed to read display plane latency. "
8050 "Disable CxSR\n");
Dave Airliedde98a52021-09-29 01:58:08 +03008051 dev_priv->wm_disp = &nop_funcs;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008052 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008053 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008054 vlv_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008055 dev_priv->wm_disp = &vlv_wm_funcs;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008056 } else if (IS_G4X(dev_priv)) {
8057 g4x_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008058 dev_priv->wm_disp = &g4x_wm_funcs;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008059 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00008060 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008061 dev_priv->is_ddr3,
8062 dev_priv->fsb_freq,
8063 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008064 drm_info(&dev_priv->drm,
8065 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008066 "(found ddr%s fsb freq %d, mem freq %d), "
8067 "disabling CxSR\n",
8068 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8069 dev_priv->fsb_freq, dev_priv->mem_freq);
8070 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008071 intel_set_memory_cxsr(dev_priv, false);
Dave Airliedde98a52021-09-29 01:58:08 +03008072 dev_priv->wm_disp = &nop_funcs;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008073 } else
Dave Airliedde98a52021-09-29 01:58:08 +03008074 dev_priv->wm_disp = &pnv_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008075 } else if (DISPLAY_VER(dev_priv) == 4) {
Dave Airliedde98a52021-09-29 01:58:08 +03008076 dev_priv->wm_disp = &i965_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008077 } else if (DISPLAY_VER(dev_priv) == 3) {
Dave Airliedde98a52021-09-29 01:58:08 +03008078 dev_priv->wm_disp = &i9xx_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008079 } else if (DISPLAY_VER(dev_priv) == 2) {
Dave Airlie758b2fc2021-09-29 01:57:46 +03008080 if (INTEL_NUM_PIPES(dev_priv) == 1)
Dave Airliedde98a52021-09-29 01:58:08 +03008081 dev_priv->wm_disp = &i845_wm_funcs;
Dave Airlie758b2fc2021-09-29 01:57:46 +03008082 else
Dave Airliedde98a52021-09-29 01:58:08 +03008083 dev_priv->wm_disp = &i9xx_wm_funcs;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008084 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008085 drm_err(&dev_priv->drm,
8086 "unexpected fall-through in %s\n", __func__);
Dave Airliedde98a52021-09-29 01:58:08 +03008087 dev_priv->wm_disp = &nop_funcs;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008088 }
8089}
8090
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008091void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008092{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01008093 dev_priv->runtime_pm.suspended = false;
8094 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008095}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02008096
8097static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
8098{
8099 struct intel_dbuf_state *dbuf_state;
8100
8101 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
8102 if (!dbuf_state)
8103 return NULL;
8104
8105 return &dbuf_state->base;
8106}
8107
8108static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
8109 struct intel_global_state *state)
8110{
8111 kfree(state);
8112}
8113
8114static const struct intel_global_state_funcs intel_dbuf_funcs = {
8115 .atomic_duplicate_state = intel_dbuf_duplicate_state,
8116 .atomic_destroy_state = intel_dbuf_destroy_state,
8117};
8118
8119struct intel_dbuf_state *
8120intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
8121{
8122 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8123 struct intel_global_state *dbuf_state;
8124
8125 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
8126 if (IS_ERR(dbuf_state))
8127 return ERR_CAST(dbuf_state);
8128
8129 return to_intel_dbuf_state(dbuf_state);
8130}
8131
8132int intel_dbuf_init(struct drm_i915_private *dev_priv)
8133{
8134 struct intel_dbuf_state *dbuf_state;
8135
8136 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
8137 if (!dbuf_state)
8138 return -ENOMEM;
8139
8140 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
8141 &dbuf_state->base, &intel_dbuf_funcs);
8142
8143 return 0;
8144}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008145
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008146/*
8147 * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
8148 * update the request state of all DBUS slices.
8149 */
8150static void update_mbus_pre_enable(struct intel_atomic_state *state)
8151{
8152 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8153 u32 mbus_ctl, dbuf_min_tracker_val;
8154 enum dbuf_slice slice;
8155 const struct intel_dbuf_state *dbuf_state =
8156 intel_atomic_get_new_dbuf_state(state);
8157
8158 if (!IS_ALDERLAKE_P(dev_priv))
8159 return;
8160
8161 /*
8162 * TODO: Implement vblank synchronized MBUS joining changes.
8163 * Must be properly coordinated with dbuf reprogramming.
8164 */
8165 if (dbuf_state->joined_mbus) {
8166 mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
8167 MBUS_JOIN_PIPE_SELECT_NONE;
8168 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
8169 } else {
8170 mbus_ctl = MBUS_HASHING_MODE_2x2 |
8171 MBUS_JOIN_PIPE_SELECT_NONE;
8172 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
8173 }
8174
8175 intel_de_rmw(dev_priv, MBUS_CTL,
8176 MBUS_HASHING_MODE_MASK | MBUS_JOIN |
8177 MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
8178
8179 for_each_dbuf_slice(dev_priv, slice)
8180 intel_de_rmw(dev_priv, DBUF_CTL_S(slice),
8181 DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
8182 dbuf_min_tracker_val);
8183}
8184
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008185void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
8186{
8187 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8188 const struct intel_dbuf_state *new_dbuf_state =
8189 intel_atomic_get_new_dbuf_state(state);
8190 const struct intel_dbuf_state *old_dbuf_state =
8191 intel_atomic_get_old_dbuf_state(state);
8192
8193 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008194 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8195 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008196 return;
8197
8198 WARN_ON(!new_dbuf_state->base.changed);
8199
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008200 update_mbus_pre_enable(state);
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008201 gen9_dbuf_slices_update(dev_priv,
8202 old_dbuf_state->enabled_slices |
8203 new_dbuf_state->enabled_slices);
8204}
8205
8206void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
8207{
8208 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8209 const struct intel_dbuf_state *new_dbuf_state =
8210 intel_atomic_get_new_dbuf_state(state);
8211 const struct intel_dbuf_state *old_dbuf_state =
8212 intel_atomic_get_old_dbuf_state(state);
8213
8214 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008215 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8216 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008217 return;
8218
8219 WARN_ON(!new_dbuf_state->base.changed);
8220
8221 gen9_dbuf_slices_update(dev_priv,
8222 new_dbuf_state->enabled_slices);
8223}