blob: a133419af998f0a3750afd1f51f144d9baf769fb [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Sam Ravnborgd0e93592019-01-26 13:25:24 +010028#include <linux/module.h>
Chris Wilson08ea70a2018-08-12 23:36:31 +010029#include <linux/pm_runtime.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010030
31#include <drm/drm_atomic_helper.h>
32#include <drm/drm_fourcc.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070033#include <drm/drm_plane_helper.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010034
Jani Nikuladf0566a2019-06-13 11:44:16 +030035#include "display/intel_atomic.h"
Ville Syrjälä3df3fe22020-11-06 19:30:42 +020036#include "display/intel_atomic_plane.h"
Stanislav Lisovskiycac91e62020-05-22 16:18:43 +030037#include "display/intel_bw.h"
Ville Syrjälä7785ae02021-04-30 17:39:44 +030038#include "display/intel_de.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030039#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030040#include "display/intel_fbc.h"
41#include "display/intel_sprite.h"
Dave Airlie46d12f92021-02-05 16:48:36 +020042#include "display/skl_universal_plane.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030043
Andi Shyti0dc3c562019-10-20 19:41:39 +010044#include "gt/intel_llc.h"
45
Eugeni Dodonov85208be2012-04-16 22:20:34 -030046#include "i915_drv.h"
Jani Nikulaa10510a2020-02-27 19:00:47 +020047#include "i915_fixed.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030048#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030049#include "i915_trace.h"
Jani Nikula4dd43752021-10-14 13:28:57 +030050#include "intel_pcode.h"
Jani Nikula696173b2019-04-05 14:00:15 +030051#include "intel_pm.h"
Jani Nikula1eecf31e2021-10-13 13:11:59 +030052#include "vlv_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020053#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030054
Jani Nikulaa10510a2020-02-27 19:00:47 +020055/* Stores plane specific WM parameters */
56struct skl_wm_params {
57 bool x_tiled, y_tiled;
58 bool rc_surface;
59 bool is_planar;
60 u32 width;
61 u8 cpp;
62 u32 plane_pixel_rate;
63 u32 y_min_scanlines;
64 u32 plane_bytes_per_line;
65 uint_fixed_16_16_t plane_blocks_per_line;
66 uint_fixed_16_16_t y_tile_minimum;
67 u32 linetime_us;
68 u32 dbuf_block_size;
69};
70
71/* used in computing the new watermarks state */
72struct intel_wm_config {
73 unsigned int num_pipes_active;
74 bool sprites_enabled;
75 bool sprites_scaled;
76};
77
Ville Syrjälä46f16e62016-10-31 22:37:22 +020078static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030079{
Ville Syrjälä93564042017-08-24 22:10:51 +030080 if (HAS_LLC(dev_priv)) {
81 /*
82 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080083 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030084 *
85 * Must match Sampler, Pixel Back End, and Media. See
86 * WaCompressedResourceSamplerPbeMediaNewHashMode.
87 */
Jani Nikula5f461662020-11-30 13:15:58 +020088 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
89 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
Ville Syrjälä93564042017-08-24 22:10:51 +030090 SKL_DE_COMPRESSED_HASH_MODE);
91 }
92
Rodrigo Vivi82525c12017-06-08 08:50:00 -070093 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +020094 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
95 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030096
Rodrigo Vivi82525c12017-06-08 08:50:00 -070097 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +020098 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
99 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +0300100
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300101 /*
102 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
103 * Display WA #0859: skl,bxt,kbl,glk,cfl
104 */
Jani Nikula5f461662020-11-30 13:15:58 +0200105 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 +0300106 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300107}
108
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200109static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200110{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200111 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200112
Nick Hoatha7546152015-06-29 14:07:32 +0100113 /* WaDisableSDEUnitClockGating:bxt */
Jani Nikula5f461662020-11-30 13:15:58 +0200114 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Nick Hoatha7546152015-06-29 14:07:32 +0100115 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
116
Imre Deak32608ca2015-03-11 11:10:27 +0200117 /*
118 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200119 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200120 */
Jani Nikula5f461662020-11-30 13:15:58 +0200121 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200122 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200123
124 /*
125 * Wa: Backlight PWM may stop in the asserted state, causing backlight
126 * to stay fully on.
127 */
Jani Nikula5f461662020-11-30 13:15:58 +0200128 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 +0200129 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530130
131 /*
132 * Lower the display internal timeout.
133 * This is needed to avoid any hard hangs when DSI port PLL
134 * is off and a MMIO access is attempted by any privilege
135 * application, using batch buffers or any other means.
136 */
Jani Nikula5f461662020-11-30 13:15:58 +0200137 intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300138
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300139 /*
140 * WaFbcTurnOffFbcWatermark:bxt
141 * Display WA #0562: bxt
142 */
Jani Nikula5f461662020-11-30 13:15:58 +0200143 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 +0300144 DISP_FBC_WM_DIS);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300145
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300146 /*
147 * WaFbcHighMemBwCorruptionAvoidance:bxt
148 * Display WA #0883: bxt
149 */
Jani Nikula5f461662020-11-30 13:15:58 +0200150 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300151 ILK_DPFC_DISABLE_DUMMY0);
Imre Deaka82abe42015-03-27 14:00:04 +0200152}
153
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200154static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
155{
156 gen9_init_clock_gating(dev_priv);
157
158 /*
159 * WaDisablePWMClockGating:glk
160 * Backlight PWM may stop in the asserted state, causing backlight
161 * to stay fully on.
162 */
Jani Nikula5f461662020-11-30 13:15:58 +0200163 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 +0200164 PWM1_GATING_DIS | PWM2_GATING_DIS);
165}
166
Lucas De Marchi1d218222019-12-24 00:40:04 -0800167static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200168{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200169 u32 tmp;
170
Jani Nikula5f461662020-11-30 13:15:58 +0200171 tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200172
173 switch (tmp & CLKCFG_FSB_MASK) {
174 case CLKCFG_FSB_533:
175 dev_priv->fsb_freq = 533; /* 133*4 */
176 break;
177 case CLKCFG_FSB_800:
178 dev_priv->fsb_freq = 800; /* 200*4 */
179 break;
180 case CLKCFG_FSB_667:
181 dev_priv->fsb_freq = 667; /* 167*4 */
182 break;
183 case CLKCFG_FSB_400:
184 dev_priv->fsb_freq = 400; /* 100*4 */
185 break;
186 }
187
188 switch (tmp & CLKCFG_MEM_MASK) {
189 case CLKCFG_MEM_533:
190 dev_priv->mem_freq = 533;
191 break;
192 case CLKCFG_MEM_667:
193 dev_priv->mem_freq = 667;
194 break;
195 case CLKCFG_MEM_800:
196 dev_priv->mem_freq = 800;
197 break;
198 }
199
200 /* detect pineview DDR3 setting */
Jani Nikula5f461662020-11-30 13:15:58 +0200201 tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200202 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
203}
204
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800205static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200206{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200207 u16 ddrpll, csipll;
208
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100209 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
210 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200211
212 switch (ddrpll & 0xff) {
213 case 0xc:
214 dev_priv->mem_freq = 800;
215 break;
216 case 0x10:
217 dev_priv->mem_freq = 1066;
218 break;
219 case 0x14:
220 dev_priv->mem_freq = 1333;
221 break;
222 case 0x18:
223 dev_priv->mem_freq = 1600;
224 break;
225 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300226 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
227 ddrpll & 0xff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200228 dev_priv->mem_freq = 0;
229 break;
230 }
231
Daniel Vetterc921aba2012-04-26 23:28:17 +0200232 switch (csipll & 0x3ff) {
233 case 0x00c:
234 dev_priv->fsb_freq = 3200;
235 break;
236 case 0x00e:
237 dev_priv->fsb_freq = 3733;
238 break;
239 case 0x010:
240 dev_priv->fsb_freq = 4266;
241 break;
242 case 0x012:
243 dev_priv->fsb_freq = 4800;
244 break;
245 case 0x014:
246 dev_priv->fsb_freq = 5333;
247 break;
248 case 0x016:
249 dev_priv->fsb_freq = 5866;
250 break;
251 case 0x018:
252 dev_priv->fsb_freq = 6400;
253 break;
254 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300255 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
256 csipll & 0x3ff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200257 dev_priv->fsb_freq = 0;
258 break;
259 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200260}
261
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300262static const struct cxsr_latency cxsr_latency_table[] = {
263 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
264 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
265 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
266 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
267 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
268
269 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
270 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
271 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
272 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
273 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
274
275 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
276 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
277 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
278 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
279 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
280
281 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
282 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
283 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
284 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
285 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
286
287 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
288 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
289 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
290 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
291 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
292
293 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
294 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
295 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
296 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
297 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
298};
299
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100300static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
301 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300302 int fsb,
303 int mem)
304{
305 const struct cxsr_latency *latency;
306 int i;
307
308 if (fsb == 0 || mem == 0)
309 return NULL;
310
311 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
312 latency = &cxsr_latency_table[i];
313 if (is_desktop == latency->is_desktop &&
314 is_ddr3 == latency->is_ddr3 &&
315 fsb == latency->fsb_freq && mem == latency->mem_freq)
316 return latency;
317 }
318
319 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
320
321 return NULL;
322}
323
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200324static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
325{
326 u32 val;
327
Chris Wilson337fa6e2019-04-26 09:17:20 +0100328 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200329
330 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
331 if (enable)
332 val &= ~FORCE_DDR_HIGH_FREQ;
333 else
334 val |= FORCE_DDR_HIGH_FREQ;
335 val &= ~FORCE_DDR_LOW_FREQ;
336 val |= FORCE_DDR_FREQ_REQ_ACK;
337 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
338
339 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
340 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300341 drm_err(&dev_priv->drm,
342 "timed out waiting for Punit DDR DVFS request\n");
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200343
Chris Wilson337fa6e2019-04-26 09:17:20 +0100344 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200345}
346
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200347static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
348{
349 u32 val;
350
Chris Wilson337fa6e2019-04-26 09:17:20 +0100351 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200352
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200353 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200354 if (enable)
355 val |= DSP_MAXFIFO_PM5_ENABLE;
356 else
357 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200358 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200359
Chris Wilson337fa6e2019-04-26 09:17:20 +0100360 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200361}
362
Ville Syrjäläf4998962015-03-10 17:02:21 +0200363#define FW_WM(value, plane) \
364 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
365
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200366static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300367{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200368 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300369 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300370
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100371 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200372 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
373 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
374 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200375 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200376 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
377 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
378 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200379 } else if (IS_PINEVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200380 val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200381 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
382 if (enable)
383 val |= PINEVIEW_SELF_REFRESH_EN;
384 else
385 val &= ~PINEVIEW_SELF_REFRESH_EN;
Jani Nikula5f461662020-11-30 13:15:58 +0200386 intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
387 intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100388 } else if (IS_I945G(dev_priv) || IS_I945GM(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;
Imre Deak5209b1f2014-07-01 12:36:17 +0300390 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
391 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200392 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
393 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100394 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300395 /*
396 * FIXME can't find a bit like this for 915G, and
397 * and yet it does have the related watermark in
398 * FW_BLC_SELF. What's going on?
399 */
Jani Nikula5f461662020-11-30 13:15:58 +0200400 was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300401 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
402 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200403 intel_uncore_write(&dev_priv->uncore, INSTPM, val);
404 intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300405 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200406 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300407 }
408
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200409 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
410
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300411 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
412 enableddisabled(enable),
413 enableddisabled(was_enabled));
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200414
415 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300416}
417
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300418/**
419 * intel_set_memory_cxsr - Configure CxSR state
420 * @dev_priv: i915 device
421 * @enable: Allow vs. disallow CxSR
422 *
423 * Allow or disallow the system to enter a special CxSR
424 * (C-state self refresh) state. What typically happens in CxSR mode
425 * is that several display FIFOs may get combined into a single larger
426 * FIFO for a particular plane (so called max FIFO mode) to allow the
427 * system to defer memory fetches longer, and the memory will enter
428 * self refresh.
429 *
430 * Note that enabling CxSR does not guarantee that the system enter
431 * this special mode, nor does it guarantee that the system stays
432 * in that mode once entered. So this just allows/disallows the system
433 * to autonomously utilize the CxSR mode. Other factors such as core
434 * C-states will affect when/if the system actually enters/exits the
435 * CxSR mode.
436 *
437 * Note that on VLV/CHV this actually only controls the max FIFO mode,
438 * and the system is free to enter/exit memory self refresh at any time
439 * even when the use of CxSR has been disallowed.
440 *
441 * While the system is actually in the CxSR/max FIFO mode, some plane
442 * control registers will not get latched on vblank. Thus in order to
443 * guarantee the system will respond to changes in the plane registers
444 * we must always disallow CxSR prior to making changes to those registers.
445 * Unfortunately the system will re-evaluate the CxSR conditions at
446 * frame start which happens after vblank start (which is when the plane
447 * registers would get latched), so we can't proceed with the plane update
448 * during the same frame where we disallowed CxSR.
449 *
450 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
451 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
452 * the hardware w.r.t. HPLL SR when writing to plane registers.
453 * Disallowing just CxSR is sufficient.
454 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200455bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200456{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200457 bool ret;
458
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200459 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200460 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300461 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
462 dev_priv->wm.vlv.cxsr = enable;
463 else if (IS_G4X(dev_priv))
464 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200465 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200466
467 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200468}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200469
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300470/*
471 * Latency for FIFO fetches is dependent on several factors:
472 * - memory configuration (speed, channels)
473 * - chipset
474 * - current MCH state
475 * It can be fairly high in some situations, so here we assume a fairly
476 * pessimal value. It's a tradeoff between extra memory fetches (if we
477 * set this value too high, the FIFO will fetch frequently to stay full)
478 * and power consumption (set it too low to save power and we might see
479 * FIFO underruns and display "flicker").
480 *
481 * A value of 5us seems to be a good balance; safe for very low end
482 * platforms but not overly aggressive on lower latency configs.
483 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100484static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300485
Ville Syrjäläb5004722015-03-05 21:19:47 +0200486#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
487 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
488
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200489static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200490{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100491 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200493 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200494 enum pipe pipe = crtc->pipe;
495 int sprite0_start, sprite1_start;
Kees Cook2713eb42020-02-20 16:05:17 -0800496 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200497
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200498 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200499 case PIPE_A:
Jani Nikula5f461662020-11-30 13:15:58 +0200500 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
501 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200502 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
503 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
504 break;
505 case PIPE_B:
Jani Nikula5f461662020-11-30 13:15:58 +0200506 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
507 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200508 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
509 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
510 break;
511 case PIPE_C:
Jani Nikula5f461662020-11-30 13:15:58 +0200512 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
513 dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200514 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
515 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
516 break;
517 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200518 MISSING_CASE(pipe);
519 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200520 }
521
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200522 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
523 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
524 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
525 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200526}
527
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200528static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
529 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300530{
Jani Nikula5f461662020-11-30 13:15:58 +0200531 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300532 int size;
533
534 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200535 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300536 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
537
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300538 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
539 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300540
541 return size;
542}
543
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200544static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
545 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300546{
Jani Nikula5f461662020-11-30 13:15:58 +0200547 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300548 int size;
549
550 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200551 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
553 size >>= 1; /* Convert to cachelines */
554
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300555 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
556 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300557
558 return size;
559}
560
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200561static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
562 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300563{
Jani Nikula5f461662020-11-30 13:15:58 +0200564 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300565 int size;
566
567 size = dsparb & 0x7f;
568 size >>= 2; /* Convert to cachelines */
569
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300570 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
571 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300572
573 return size;
574}
575
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300576/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800577static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300578 .fifo_size = PINEVIEW_DISPLAY_FIFO,
579 .max_wm = PINEVIEW_MAX_WM,
580 .default_wm = PINEVIEW_DFT_WM,
581 .guard_size = PINEVIEW_GUARD_WM,
582 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300583};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800584
585static const struct intel_watermark_params pnv_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300586 .fifo_size = PINEVIEW_DISPLAY_FIFO,
587 .max_wm = PINEVIEW_MAX_WM,
588 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
589 .guard_size = PINEVIEW_GUARD_WM,
590 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300591};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800592
593static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300594 .fifo_size = PINEVIEW_CURSOR_FIFO,
595 .max_wm = PINEVIEW_CURSOR_MAX_WM,
596 .default_wm = PINEVIEW_CURSOR_DFT_WM,
597 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
598 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300599};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800600
601static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300602 .fifo_size = PINEVIEW_CURSOR_FIFO,
603 .max_wm = PINEVIEW_CURSOR_MAX_WM,
604 .default_wm = PINEVIEW_CURSOR_DFT_WM,
605 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
606 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300607};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800608
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300609static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300610 .fifo_size = I965_CURSOR_FIFO,
611 .max_wm = I965_CURSOR_MAX_WM,
612 .default_wm = I965_CURSOR_DFT_WM,
613 .guard_size = 2,
614 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300615};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800616
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300617static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300618 .fifo_size = I945_FIFO_SIZE,
619 .max_wm = I915_MAX_WM,
620 .default_wm = 1,
621 .guard_size = 2,
622 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800624
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300625static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300626 .fifo_size = I915_FIFO_SIZE,
627 .max_wm = I915_MAX_WM,
628 .default_wm = 1,
629 .guard_size = 2,
630 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300631};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800632
Ville Syrjälä9d539102014-08-15 01:21:53 +0300633static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300634 .fifo_size = I855GM_FIFO_SIZE,
635 .max_wm = I915_MAX_WM,
636 .default_wm = 1,
637 .guard_size = 2,
638 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300639};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800640
Ville Syrjälä9d539102014-08-15 01:21:53 +0300641static const struct intel_watermark_params i830_bc_wm_info = {
642 .fifo_size = I855GM_FIFO_SIZE,
643 .max_wm = I915_MAX_WM/2,
644 .default_wm = 1,
645 .guard_size = 2,
646 .cacheline_size = I830_FIFO_LINE_SIZE,
647};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800648
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200649static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300650 .fifo_size = I830_FIFO_SIZE,
651 .max_wm = I915_MAX_WM,
652 .default_wm = 1,
653 .guard_size = 2,
654 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300655};
656
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300657/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300658 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
659 * @pixel_rate: Pipe pixel rate in kHz
660 * @cpp: Plane bytes per pixel
661 * @latency: Memory wakeup latency in 0.1us units
662 *
663 * Compute the watermark using the method 1 or "small buffer"
664 * formula. The caller may additonally add extra cachelines
665 * to account for TLB misses and clock crossings.
666 *
667 * This method is concerned with the short term drain rate
668 * of the FIFO, ie. it does not account for blanking periods
669 * which would effectively reduce the average drain rate across
670 * a longer period. The name "small" refers to the fact the
671 * FIFO is relatively small compared to the amount of data
672 * fetched.
673 *
674 * The FIFO level vs. time graph might look something like:
675 *
676 * |\ |\
677 * | \ | \
678 * __---__---__ (- plane active, _ blanking)
679 * -> time
680 *
681 * or perhaps like this:
682 *
683 * |\|\ |\|\
684 * __----__----__ (- plane active, _ blanking)
685 * -> time
686 *
687 * Returns:
688 * The watermark in bytes
689 */
690static unsigned int intel_wm_method1(unsigned int pixel_rate,
691 unsigned int cpp,
692 unsigned int latency)
693{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200694 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300695
Ville Syrjäläd492a292019-04-08 18:27:01 +0300696 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300697 ret = DIV_ROUND_UP_ULL(ret, 10000);
698
699 return ret;
700}
701
702/**
703 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
704 * @pixel_rate: Pipe pixel rate in kHz
705 * @htotal: Pipe horizontal total
706 * @width: Plane width in pixels
707 * @cpp: Plane bytes per pixel
708 * @latency: Memory wakeup latency in 0.1us units
709 *
710 * Compute the watermark using the method 2 or "large buffer"
711 * formula. The caller may additonally add extra cachelines
712 * to account for TLB misses and clock crossings.
713 *
714 * This method is concerned with the long term drain rate
715 * of the FIFO, ie. it does account for blanking periods
716 * which effectively reduce the average drain rate across
717 * a longer period. The name "large" refers to the fact the
718 * FIFO is relatively large compared to the amount of data
719 * fetched.
720 *
721 * The FIFO level vs. time graph might look something like:
722 *
723 * |\___ |\___
724 * | \___ | \___
725 * | \ | \
726 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
727 * -> time
728 *
729 * Returns:
730 * The watermark in bytes
731 */
732static unsigned int intel_wm_method2(unsigned int pixel_rate,
733 unsigned int htotal,
734 unsigned int width,
735 unsigned int cpp,
736 unsigned int latency)
737{
738 unsigned int ret;
739
740 /*
741 * FIXME remove once all users are computing
742 * watermarks in the correct place.
743 */
744 if (WARN_ON_ONCE(htotal == 0))
745 htotal = 1;
746
747 ret = (latency * pixel_rate) / (htotal * 10000);
748 ret = (ret + 1) * width * cpp;
749
750 return ret;
751}
752
753/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300754 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300755 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300756 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000757 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200758 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300759 * @latency_ns: memory latency for the platform
760 *
761 * Calculate the watermark level (the level at which the display plane will
762 * start fetching from memory again). Each chip has a different display
763 * FIFO size and allocation, so the caller needs to figure that out and pass
764 * in the correct intel_watermark_params structure.
765 *
766 * As the pixel clock runs, the FIFO will be drained at a rate that depends
767 * on the pixel size. When it reaches the watermark level, it'll start
768 * fetching FIFO line sized based chunks from memory until the FIFO fills
769 * past the watermark point. If the FIFO drains completely, a FIFO underrun
770 * will occur, and a display engine hang could result.
771 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300772static unsigned int intel_calculate_wm(int pixel_rate,
773 const struct intel_watermark_params *wm,
774 int fifo_size, int cpp,
775 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300776{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300777 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300778
779 /*
780 * Note: we need to make sure we don't overflow for various clock &
781 * latency values.
782 * clocks go from a few thousand to several hundred thousand.
783 * latency is usually a few thousand
784 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300785 entries = intel_wm_method1(pixel_rate, cpp,
786 latency_ns / 100);
787 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
788 wm->guard_size;
789 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300790
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300791 wm_size = fifo_size - entries;
792 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300793
794 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300795 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300796 wm_size = wm->max_wm;
797 if (wm_size <= 0)
798 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300799
800 /*
801 * Bspec seems to indicate that the value shouldn't be lower than
802 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
803 * Lets go for 8 which is the burst size since certain platforms
804 * already use a hardcoded 8 (which is what the spec says should be
805 * done).
806 */
807 if (wm_size <= 8)
808 wm_size = 8;
809
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300810 return wm_size;
811}
812
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300813static bool is_disabling(int old, int new, int threshold)
814{
815 return old >= threshold && new < threshold;
816}
817
818static bool is_enabling(int old, int new, int threshold)
819{
820 return old < threshold && new >= threshold;
821}
822
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300823static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
824{
825 return dev_priv->wm.max_level + 1;
826}
827
Ville Syrjälä24304d812017-03-14 17:10:49 +0200828static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
829 const struct intel_plane_state *plane_state)
830{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100831 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200832
833 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100834 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200835 return false;
836
837 /*
838 * Treat cursor with fb as always visible since cursor updates
839 * can happen faster than the vrefresh rate, and the current
840 * watermark code doesn't handle that correctly. Cursor updates
841 * which set/clear the fb or change the cursor size are going
842 * to get throttled by intel_legacy_cursor_update() to work
843 * around this problem with the watermark code.
844 */
845 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100846 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200847 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100848 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200849}
850
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200851static bool intel_crtc_active(struct intel_crtc *crtc)
852{
853 /* Be paranoid as we can arrive here with only partial
854 * state retrieved from the hardware during setup.
855 *
856 * We can ditch the adjusted_mode.crtc_clock check as soon
857 * as Haswell has gained clock readout/fastboot support.
858 *
859 * We can ditch the crtc->primary->state->fb check as soon as we can
860 * properly reconstruct framebuffers.
861 *
862 * FIXME: The intel_crtc->active here should be switched to
863 * crtc->state->active once we have proper CRTC states wired up
864 * for atomic.
865 */
866 return crtc->active && crtc->base.primary->state->fb &&
867 crtc->config->hw.adjusted_mode.crtc_clock;
868}
869
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200870static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300871{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200872 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300873
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200874 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200875 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300876 if (enabled)
877 return NULL;
878 enabled = crtc;
879 }
880 }
881
882 return enabled;
883}
884
Dave Airlieef9c66a2021-09-29 01:57:47 +0300885static void pnv_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300886{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200887 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300888 const struct cxsr_latency *latency;
889 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300890 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300891
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000892 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100893 dev_priv->is_ddr3,
894 dev_priv->fsb_freq,
895 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300896 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300897 drm_dbg_kms(&dev_priv->drm,
898 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300899 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300900 return;
901 }
902
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200903 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300904 if (crtc) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200905 const struct drm_display_mode *pipe_mode =
906 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200907 const struct drm_framebuffer *fb =
908 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200909 int cpp = fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200910 int clock = pipe_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300911
912 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800913 wm = intel_calculate_wm(clock, &pnv_display_wm,
914 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200915 cpp, latency->display_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200916 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300917 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200918 reg |= FW_WM(wm, SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200919 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300920 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300921
922 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800923 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
924 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300925 4, latency->cursor_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200926 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300927 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200928 reg |= FW_WM(wm, CURSOR_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200929 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300930
931 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800932 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
933 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200934 cpp, latency->display_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200935 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300936 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200937 reg |= FW_WM(wm, HPLL_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200938 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300939
940 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800941 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
942 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300943 4, latency->cursor_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200944 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300945 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200946 reg |= FW_WM(wm, HPLL_CURSOR);
Jani Nikula5f461662020-11-30 13:15:58 +0200947 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300948 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300949
Imre Deak5209b1f2014-07-01 12:36:17 +0300950 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300951 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300952 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300953 }
954}
955
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300956/*
957 * Documentation says:
958 * "If the line size is small, the TLB fetches can get in the way of the
959 * data fetches, causing some lag in the pixel data return which is not
960 * accounted for in the above formulas. The following adjustment only
961 * needs to be applied if eight whole lines fit in the buffer at once.
962 * The WM is adjusted upwards by the difference between the FIFO size
963 * and the size of 8 whole lines. This adjustment is always performed
964 * in the actual pixel depth regardless of whether FBC is enabled or not."
965 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000966static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300967{
968 int tlb_miss = fifo_size * 64 - width * cpp * 8;
969
970 return max(0, tlb_miss);
971}
972
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300973static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
974 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300975{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300976 enum pipe pipe;
977
978 for_each_pipe(dev_priv, pipe)
979 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
980
Jani Nikula5f461662020-11-30 13:15:58 +0200981 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300982 FW_WM(wm->sr.plane, SR) |
983 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
984 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
985 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200986 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300987 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
988 FW_WM(wm->sr.fbc, FBC_SR) |
989 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
990 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
991 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
992 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200993 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300994 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
995 FW_WM(wm->sr.cursor, CURSOR_SR) |
996 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
997 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300998
Jani Nikula5f461662020-11-30 13:15:58 +0200999 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001000}
1001
Ville Syrjälä15665972015-03-10 16:16:28 +02001002#define FW_WM_VLV(value, plane) \
1003 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1004
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001005static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001006 const struct vlv_wm_values *wm)
1007{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001008 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001009
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001010 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001011 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1012
Jani Nikula5f461662020-11-30 13:15:58 +02001013 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001014 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1015 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1016 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1017 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1018 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001019
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001020 /*
1021 * Zero the (unused) WM1 watermarks, and also clear all the
1022 * high order bits so that there are no out of bounds values
1023 * present in the registers during the reprogramming.
1024 */
Jani Nikula5f461662020-11-30 13:15:58 +02001025 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1026 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1027 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1028 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1029 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001030
Jani Nikula5f461662020-11-30 13:15:58 +02001031 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001032 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001033 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1034 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1035 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001036 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001037 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1038 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1039 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001040 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001041 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001042
1043 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02001044 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001045 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1046 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001047 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001048 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1049 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Jani Nikula5f461662020-11-30 13:15:58 +02001050 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001051 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1052 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Jani Nikula5f461662020-11-30 13:15:58 +02001053 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001054 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001055 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1056 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1057 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1058 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1059 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1060 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1061 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1062 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1063 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001064 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02001065 intel_uncore_write(&dev_priv->uncore, DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001066 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1067 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001068 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001069 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001070 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1071 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1072 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1073 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1074 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1075 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001076 }
1077
Jani Nikula5f461662020-11-30 13:15:58 +02001078 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001079}
1080
Ville Syrjälä15665972015-03-10 16:16:28 +02001081#undef FW_WM_VLV
1082
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001083static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1084{
1085 /* all latencies in usec */
1086 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1087 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001088 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001089
Ville Syrjälä79d94302017-04-21 21:14:30 +03001090 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001091}
1092
1093static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1094{
1095 /*
1096 * DSPCNTR[13] supposedly controls whether the
1097 * primary plane can use the FIFO space otherwise
1098 * reserved for the sprite plane. It's not 100% clear
1099 * what the actual FIFO size is, but it looks like we
1100 * can happily set both primary and sprite watermarks
1101 * up to 127 cachelines. So that would seem to mean
1102 * that either DSPCNTR[13] doesn't do anything, or that
1103 * the total FIFO is >= 256 cachelines in size. Either
1104 * way, we don't seem to have to worry about this
1105 * repartitioning as the maximum watermark value the
1106 * register can hold for each plane is lower than the
1107 * minimum FIFO size.
1108 */
1109 switch (plane_id) {
1110 case PLANE_CURSOR:
1111 return 63;
1112 case PLANE_PRIMARY:
1113 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1114 case PLANE_SPRITE0:
1115 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1116 default:
1117 MISSING_CASE(plane_id);
1118 return 0;
1119 }
1120}
1121
1122static int g4x_fbc_fifo_size(int level)
1123{
1124 switch (level) {
1125 case G4X_WM_LEVEL_SR:
1126 return 7;
1127 case G4X_WM_LEVEL_HPLL:
1128 return 15;
1129 default:
1130 MISSING_CASE(level);
1131 return 0;
1132 }
1133}
1134
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001135static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1136 const struct intel_plane_state *plane_state,
1137 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001138{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001139 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001140 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001141 const struct drm_display_mode *pipe_mode =
1142 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001143 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1144 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001145
1146 if (latency == 0)
1147 return USHRT_MAX;
1148
1149 if (!intel_wm_plane_visible(crtc_state, plane_state))
1150 return 0;
1151
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001152 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001153
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001154 /*
Ville Syrjälä52913622021-05-14 15:57:41 +03001155 * WaUse32BppForSRWM:ctg,elk
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001156 *
Ville Syrjälä52913622021-05-14 15:57:41 +03001157 * The spec fails to list this restriction for the
1158 * HPLL watermark, which seems a little strange.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001159 * Let's use 32bpp for the HPLL watermark as well.
1160 */
Ville Syrjälä52913622021-05-14 15:57:41 +03001161 if (plane->id == PLANE_PRIMARY &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001162 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001163 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001164
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001165 clock = pipe_mode->crtc_clock;
1166 htotal = pipe_mode->crtc_htotal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001167
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001168 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001169
1170 if (plane->id == PLANE_CURSOR) {
1171 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1172 } else if (plane->id == PLANE_PRIMARY &&
1173 level == G4X_WM_LEVEL_NORMAL) {
1174 wm = intel_wm_method1(clock, cpp, latency);
1175 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001176 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001177
1178 small = intel_wm_method1(clock, cpp, latency);
1179 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1180
1181 wm = min(small, large);
1182 }
1183
1184 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1185 width, cpp);
1186
1187 wm = DIV_ROUND_UP(wm, 64) + 2;
1188
Chris Wilson1a1f1282017-11-07 14:03:38 +00001189 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001190}
1191
1192static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1193 int level, enum plane_id plane_id, u16 value)
1194{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001195 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001196 bool dirty = false;
1197
1198 for (; level < intel_wm_num_levels(dev_priv); level++) {
1199 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1200
1201 dirty |= raw->plane[plane_id] != value;
1202 raw->plane[plane_id] = value;
1203 }
1204
1205 return dirty;
1206}
1207
1208static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1209 int level, u16 value)
1210{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001211 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001212 bool dirty = false;
1213
1214 /* NORMAL level doesn't have an FBC watermark */
1215 level = max(level, G4X_WM_LEVEL_SR);
1216
1217 for (; level < intel_wm_num_levels(dev_priv); level++) {
1218 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1219
1220 dirty |= raw->fbc != value;
1221 raw->fbc = value;
1222 }
1223
1224 return dirty;
1225}
1226
Maarten Lankhorstec193642019-06-28 10:55:17 +02001227static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1228 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001229 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001230
1231static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1232 const struct intel_plane_state *plane_state)
1233{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001234 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001235 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001236 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1237 enum plane_id plane_id = plane->id;
1238 bool dirty = false;
1239 int level;
1240
1241 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1242 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1243 if (plane_id == PLANE_PRIMARY)
1244 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1245 goto out;
1246 }
1247
1248 for (level = 0; level < num_levels; level++) {
1249 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1250 int wm, max_wm;
1251
1252 wm = g4x_compute_wm(crtc_state, plane_state, level);
1253 max_wm = g4x_plane_fifo_size(plane_id, level);
1254
1255 if (wm > max_wm)
1256 break;
1257
1258 dirty |= raw->plane[plane_id] != wm;
1259 raw->plane[plane_id] = wm;
1260
1261 if (plane_id != PLANE_PRIMARY ||
1262 level == G4X_WM_LEVEL_NORMAL)
1263 continue;
1264
1265 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1266 raw->plane[plane_id]);
1267 max_wm = g4x_fbc_fifo_size(level);
1268
1269 /*
1270 * FBC wm is not mandatory as we
1271 * can always just disable its use.
1272 */
1273 if (wm > max_wm)
1274 wm = USHRT_MAX;
1275
1276 dirty |= raw->fbc != wm;
1277 raw->fbc = wm;
1278 }
1279
1280 /* mark watermarks as invalid */
1281 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1282
1283 if (plane_id == PLANE_PRIMARY)
1284 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1285
1286 out:
1287 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001288 drm_dbg_kms(&dev_priv->drm,
1289 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1290 plane->base.name,
1291 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1292 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1293 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001294
1295 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001296 drm_dbg_kms(&dev_priv->drm,
1297 "FBC watermarks: SR=%d, HPLL=%d\n",
1298 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1299 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001300 }
1301
1302 return dirty;
1303}
1304
1305static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1306 enum plane_id plane_id, int level)
1307{
1308 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1309
1310 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1311}
1312
1313static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1314 int level)
1315{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001316 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001317
1318 if (level > dev_priv->wm.max_level)
1319 return false;
1320
1321 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1322 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1323 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1324}
1325
1326/* mark all levels starting from 'level' as invalid */
1327static void g4x_invalidate_wms(struct intel_crtc *crtc,
1328 struct g4x_wm_state *wm_state, int level)
1329{
1330 if (level <= G4X_WM_LEVEL_NORMAL) {
1331 enum plane_id plane_id;
1332
1333 for_each_plane_id_on_crtc(crtc, plane_id)
1334 wm_state->wm.plane[plane_id] = USHRT_MAX;
1335 }
1336
1337 if (level <= G4X_WM_LEVEL_SR) {
1338 wm_state->cxsr = false;
1339 wm_state->sr.cursor = USHRT_MAX;
1340 wm_state->sr.plane = USHRT_MAX;
1341 wm_state->sr.fbc = USHRT_MAX;
1342 }
1343
1344 if (level <= G4X_WM_LEVEL_HPLL) {
1345 wm_state->hpll_en = false;
1346 wm_state->hpll.cursor = USHRT_MAX;
1347 wm_state->hpll.plane = USHRT_MAX;
1348 wm_state->hpll.fbc = USHRT_MAX;
1349 }
1350}
1351
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001352static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1353 int level)
1354{
1355 if (level < G4X_WM_LEVEL_SR)
1356 return false;
1357
1358 if (level >= G4X_WM_LEVEL_SR &&
1359 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1360 return false;
1361
1362 if (level >= G4X_WM_LEVEL_HPLL &&
1363 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1364 return false;
1365
1366 return true;
1367}
1368
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001369static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1370 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001371{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001372 struct intel_crtc_state *crtc_state =
1373 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001374 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001375 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001376 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001377 const struct intel_plane_state *old_plane_state;
1378 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001379 struct intel_plane *plane;
1380 enum plane_id plane_id;
1381 int i, level;
1382 unsigned int dirty = 0;
1383
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001384 for_each_oldnew_intel_plane_in_state(state, plane,
1385 old_plane_state,
1386 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001387 if (new_plane_state->hw.crtc != &crtc->base &&
1388 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001389 continue;
1390
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001391 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001392 dirty |= BIT(plane->id);
1393 }
1394
1395 if (!dirty)
1396 return 0;
1397
1398 level = G4X_WM_LEVEL_NORMAL;
1399 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1400 goto out;
1401
1402 raw = &crtc_state->wm.g4x.raw[level];
1403 for_each_plane_id_on_crtc(crtc, plane_id)
1404 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1405
1406 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001407 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1408 goto out;
1409
1410 raw = &crtc_state->wm.g4x.raw[level];
1411 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1412 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1413 wm_state->sr.fbc = raw->fbc;
1414
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001415 wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001416
1417 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001418 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1419 goto out;
1420
1421 raw = &crtc_state->wm.g4x.raw[level];
1422 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1423 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1424 wm_state->hpll.fbc = raw->fbc;
1425
1426 wm_state->hpll_en = wm_state->cxsr;
1427
1428 level++;
1429
1430 out:
1431 if (level == G4X_WM_LEVEL_NORMAL)
1432 return -EINVAL;
1433
1434 /* invalidate the higher levels */
1435 g4x_invalidate_wms(crtc, wm_state, level);
1436
1437 /*
1438 * Determine if the FBC watermark(s) can be used. IF
1439 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001440 * watermark(s) rather than disable the SR/HPLL
1441 * level(s) entirely. 'level-1' is the highest valid
1442 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001443 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001444 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001445
1446 return 0;
1447}
1448
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001449static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1450 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001451{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301452 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001453 struct intel_crtc_state *new_crtc_state =
1454 intel_atomic_get_new_crtc_state(state, crtc);
1455 const struct intel_crtc_state *old_crtc_state =
1456 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001457 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1458 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001459 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001460 enum plane_id plane_id;
1461
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001462 if (!new_crtc_state->hw.active ||
1463 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001464 *intermediate = *optimal;
1465
1466 intermediate->cxsr = false;
1467 intermediate->hpll_en = false;
1468 goto out;
1469 }
1470
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001471 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001472 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001473 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001474 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001475 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1476
1477 for_each_plane_id_on_crtc(crtc, plane_id) {
1478 intermediate->wm.plane[plane_id] =
1479 max(optimal->wm.plane[plane_id],
1480 active->wm.plane[plane_id]);
1481
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301482 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1483 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001484 }
1485
1486 intermediate->sr.plane = max(optimal->sr.plane,
1487 active->sr.plane);
1488 intermediate->sr.cursor = max(optimal->sr.cursor,
1489 active->sr.cursor);
1490 intermediate->sr.fbc = max(optimal->sr.fbc,
1491 active->sr.fbc);
1492
1493 intermediate->hpll.plane = max(optimal->hpll.plane,
1494 active->hpll.plane);
1495 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1496 active->hpll.cursor);
1497 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1498 active->hpll.fbc);
1499
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301500 drm_WARN_ON(&dev_priv->drm,
1501 (intermediate->sr.plane >
1502 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1503 intermediate->sr.cursor >
1504 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1505 intermediate->cxsr);
1506 drm_WARN_ON(&dev_priv->drm,
1507 (intermediate->sr.plane >
1508 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1509 intermediate->sr.cursor >
1510 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1511 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001512
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301513 drm_WARN_ON(&dev_priv->drm,
1514 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1515 intermediate->fbc_en && intermediate->cxsr);
1516 drm_WARN_ON(&dev_priv->drm,
1517 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1518 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001519
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001520out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001521 /*
1522 * If our intermediate WM are identical to the final WM, then we can
1523 * omit the post-vblank programming; only update if it's different.
1524 */
1525 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001526 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001527
1528 return 0;
1529}
1530
1531static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1532 struct g4x_wm_values *wm)
1533{
1534 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001535 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001536
1537 wm->cxsr = true;
1538 wm->hpll_en = true;
1539 wm->fbc_en = true;
1540
1541 for_each_intel_crtc(&dev_priv->drm, crtc) {
1542 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1543
1544 if (!crtc->active)
1545 continue;
1546
1547 if (!wm_state->cxsr)
1548 wm->cxsr = false;
1549 if (!wm_state->hpll_en)
1550 wm->hpll_en = false;
1551 if (!wm_state->fbc_en)
1552 wm->fbc_en = false;
1553
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001554 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001555 }
1556
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001557 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001558 wm->cxsr = false;
1559 wm->hpll_en = false;
1560 wm->fbc_en = false;
1561 }
1562
1563 for_each_intel_crtc(&dev_priv->drm, crtc) {
1564 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1565 enum pipe pipe = crtc->pipe;
1566
1567 wm->pipe[pipe] = wm_state->wm;
1568 if (crtc->active && wm->cxsr)
1569 wm->sr = wm_state->sr;
1570 if (crtc->active && wm->hpll_en)
1571 wm->hpll = wm_state->hpll;
1572 }
1573}
1574
1575static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1576{
1577 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1578 struct g4x_wm_values new_wm = {};
1579
1580 g4x_merge_wm(dev_priv, &new_wm);
1581
1582 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1583 return;
1584
1585 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1586 _intel_set_memory_cxsr(dev_priv, false);
1587
1588 g4x_write_wm_values(dev_priv, &new_wm);
1589
1590 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1591 _intel_set_memory_cxsr(dev_priv, true);
1592
1593 *old_wm = new_wm;
1594}
1595
1596static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001597 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001598{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001599 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1600 const struct intel_crtc_state *crtc_state =
1601 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001602
1603 mutex_lock(&dev_priv->wm.wm_mutex);
1604 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1605 g4x_program_watermarks(dev_priv);
1606 mutex_unlock(&dev_priv->wm.wm_mutex);
1607}
1608
1609static void g4x_optimize_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 if (!crtc_state->wm.need_postvbl_update)
1617 return;
1618
1619 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001620 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001621 g4x_program_watermarks(dev_priv);
1622 mutex_unlock(&dev_priv->wm.wm_mutex);
1623}
1624
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001625/* latency must be in 0.1us units. */
1626static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001627 unsigned int htotal,
1628 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001629 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001630 unsigned int latency)
1631{
1632 unsigned int ret;
1633
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001634 ret = intel_wm_method2(pixel_rate, htotal,
1635 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001636 ret = DIV_ROUND_UP(ret, 64);
1637
1638 return ret;
1639}
1640
Ville Syrjäläbb726512016-10-31 22:37:24 +02001641static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001642{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001643 /* all latencies in usec */
1644 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1645
Ville Syrjälä58590c12015-09-08 21:05:12 +03001646 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1647
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001648 if (IS_CHERRYVIEW(dev_priv)) {
1649 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1650 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001651
1652 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001653 }
1654}
1655
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001656static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1657 const struct intel_plane_state *plane_state,
1658 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001659{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001660 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001661 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001662 const struct drm_display_mode *pipe_mode =
1663 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001664 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001665
1666 if (dev_priv->wm.pri_latency[level] == 0)
1667 return USHRT_MAX;
1668
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001669 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001670 return 0;
1671
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001672 cpp = plane_state->hw.fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001673 clock = pipe_mode->crtc_clock;
1674 htotal = pipe_mode->crtc_htotal;
Ville Syrjäläe339d672016-11-28 19:37:17 +02001675 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001676
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001677 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001678 /*
1679 * FIXME the formula gives values that are
1680 * too big for the cursor FIFO, and hence we
1681 * would never be able to use cursors. For
1682 * now just hardcode the watermark.
1683 */
1684 wm = 63;
1685 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001686 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001687 dev_priv->wm.pri_latency[level] * 10);
1688 }
1689
Chris Wilson1a1f1282017-11-07 14:03:38 +00001690 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001691}
1692
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001693static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1694{
1695 return (active_planes & (BIT(PLANE_SPRITE0) |
1696 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1697}
1698
Ville Syrjälä5012e602017-03-02 19:14:56 +02001699static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001700{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001701 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301702 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001703 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001704 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001705 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001706 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001707 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001708 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001709 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001710 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001711 unsigned int total_rate;
1712 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001713
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001714 /*
1715 * When enabling sprite0 after sprite1 has already been enabled
1716 * we tend to get an underrun unless sprite0 already has some
1717 * FIFO space allcoated. Hence we always allocate at least one
1718 * cacheline for sprite0 whenever sprite1 is enabled.
1719 *
1720 * All other plane enable sequences appear immune to this problem.
1721 */
1722 if (vlv_need_sprite0_fifo_workaround(active_planes))
1723 sprite0_fifo_extra = 1;
1724
Ville Syrjälä5012e602017-03-02 19:14:56 +02001725 total_rate = raw->plane[PLANE_PRIMARY] +
1726 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001727 raw->plane[PLANE_SPRITE1] +
1728 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001729
Ville Syrjälä5012e602017-03-02 19:14:56 +02001730 if (total_rate > fifo_size)
1731 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001732
Ville Syrjälä5012e602017-03-02 19:14:56 +02001733 if (total_rate == 0)
1734 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001735
Ville Syrjälä5012e602017-03-02 19:14:56 +02001736 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001737 unsigned int rate;
1738
Ville Syrjälä5012e602017-03-02 19:14:56 +02001739 if ((active_planes & BIT(plane_id)) == 0) {
1740 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001741 continue;
1742 }
1743
Ville Syrjälä5012e602017-03-02 19:14:56 +02001744 rate = raw->plane[plane_id];
1745 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1746 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001747 }
1748
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001749 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1750 fifo_left -= sprite0_fifo_extra;
1751
Ville Syrjälä5012e602017-03-02 19:14:56 +02001752 fifo_state->plane[PLANE_CURSOR] = 63;
1753
1754 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001755
1756 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001757 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001758 int plane_extra;
1759
1760 if (fifo_left == 0)
1761 break;
1762
Ville Syrjälä5012e602017-03-02 19:14:56 +02001763 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001764 continue;
1765
1766 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001767 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001768 fifo_left -= plane_extra;
1769 }
1770
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301771 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001772
1773 /* give it all to the first plane if none are active */
1774 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301775 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001776 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1777 }
1778
1779 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001780}
1781
Ville Syrjäläff32c542017-03-02 19:14:57 +02001782/* mark all levels starting from 'level' as invalid */
1783static void vlv_invalidate_wms(struct intel_crtc *crtc,
1784 struct vlv_wm_state *wm_state, int level)
1785{
1786 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1787
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001788 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001789 enum plane_id plane_id;
1790
1791 for_each_plane_id_on_crtc(crtc, plane_id)
1792 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1793
1794 wm_state->sr[level].cursor = USHRT_MAX;
1795 wm_state->sr[level].plane = USHRT_MAX;
1796 }
1797}
1798
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001799static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1800{
1801 if (wm > fifo_size)
1802 return USHRT_MAX;
1803 else
1804 return fifo_size - wm;
1805}
1806
Ville Syrjäläff32c542017-03-02 19:14:57 +02001807/*
1808 * Starting from 'level' set all higher
1809 * levels to 'value' in the "raw" watermarks.
1810 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001811static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001812 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001813{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001814 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001815 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001816 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001817
Ville Syrjäläff32c542017-03-02 19:14:57 +02001818 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001819 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001820
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001821 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001822 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001823 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001824
1825 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001826}
1827
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001828static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1829 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001830{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001831 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001832 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001833 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001834 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001836 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001837
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001838 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001839 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1840 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001841 }
1842
1843 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001844 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001845 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1846 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1847
Ville Syrjäläff32c542017-03-02 19:14:57 +02001848 if (wm > max_wm)
1849 break;
1850
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001851 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001852 raw->plane[plane_id] = wm;
1853 }
1854
1855 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001856 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001857
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001858out:
1859 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001860 drm_dbg_kms(&dev_priv->drm,
1861 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1862 plane->base.name,
1863 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1864 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1865 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001866
1867 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001868}
1869
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001870static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1871 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001872{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001873 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001874 &crtc_state->wm.vlv.raw[level];
1875 const struct vlv_fifo_state *fifo_state =
1876 &crtc_state->wm.vlv.fifo_state;
1877
1878 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1879}
1880
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001881static 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 +02001882{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001883 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1884 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1885 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1886 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001887}
1888
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001889static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1890 struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001891{
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001892 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001893 struct intel_crtc_state *crtc_state =
1894 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001895 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001896 const struct vlv_fifo_state *fifo_state =
1897 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001898 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1899 int num_active_planes = hweight8(active_planes);
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001900 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001901 const struct intel_plane_state *old_plane_state;
1902 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001903 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001904 enum plane_id plane_id;
1905 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001906 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001907
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001908 for_each_oldnew_intel_plane_in_state(state, plane,
1909 old_plane_state,
1910 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001911 if (new_plane_state->hw.crtc != &crtc->base &&
1912 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001913 continue;
1914
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001915 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001916 dirty |= BIT(plane->id);
1917 }
1918
1919 /*
1920 * DSPARB registers may have been reset due to the
1921 * power well being turned off. Make sure we restore
1922 * them to a consistent state even if no primary/sprite
1923 * planes are initially active.
1924 */
1925 if (needs_modeset)
1926 crtc_state->fifo_changed = true;
1927
1928 if (!dirty)
1929 return 0;
1930
1931 /* cursor changes don't warrant a FIFO recompute */
1932 if (dirty & ~BIT(PLANE_CURSOR)) {
1933 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001934 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001935 const struct vlv_fifo_state *old_fifo_state =
1936 &old_crtc_state->wm.vlv.fifo_state;
1937
1938 ret = vlv_compute_fifo(crtc_state);
1939 if (ret)
1940 return ret;
1941
1942 if (needs_modeset ||
1943 memcmp(old_fifo_state, fifo_state,
1944 sizeof(*fifo_state)) != 0)
1945 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001946 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001947
Ville Syrjäläff32c542017-03-02 19:14:57 +02001948 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001949 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001950 /*
1951 * Note that enabling cxsr with no primary/sprite planes
1952 * enabled can wedge the pipe. Hence we only allow cxsr
1953 * with exactly one enabled primary/sprite plane.
1954 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001955 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001956
Ville Syrjälä5012e602017-03-02 19:14:56 +02001957 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001958 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001959 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001960
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001961 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001962 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001963
Ville Syrjäläff32c542017-03-02 19:14:57 +02001964 for_each_plane_id_on_crtc(crtc, plane_id) {
1965 wm_state->wm[level].plane[plane_id] =
1966 vlv_invert_wm_value(raw->plane[plane_id],
1967 fifo_state->plane[plane_id]);
1968 }
1969
1970 wm_state->sr[level].plane =
1971 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001972 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001973 raw->plane[PLANE_SPRITE1]),
1974 sr_fifo_size);
1975
1976 wm_state->sr[level].cursor =
1977 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1978 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001979 }
1980
Ville Syrjäläff32c542017-03-02 19:14:57 +02001981 if (level == 0)
1982 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001983
Ville Syrjäläff32c542017-03-02 19:14:57 +02001984 /* limit to only levels we can actually handle */
1985 wm_state->num_levels = level;
1986
1987 /* invalidate the higher levels */
1988 vlv_invalidate_wms(crtc, wm_state, level);
1989
1990 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001991}
1992
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001993#define VLV_FIFO(plane, value) \
1994 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1995
Ville Syrjäläff32c542017-03-02 19:14:57 +02001996static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001997 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001998{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001999 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002000 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002001 const struct intel_crtc_state *crtc_state =
2002 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002003 const struct vlv_fifo_state *fifo_state =
2004 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002005 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002006 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002007
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002008 if (!crtc_state->fifo_changed)
2009 return;
2010
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002011 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2012 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2013 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002014
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302015 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2016 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002017
Ville Syrjäläc137d662017-03-02 19:15:06 +02002018 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2019
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002020 /*
2021 * uncore.lock serves a double purpose here. It allows us to
2022 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2023 * it protects the DSPARB registers from getting clobbered by
2024 * parallel updates from multiple pipes.
2025 *
2026 * intel_pipe_update_start() has already disabled interrupts
2027 * for us, so a plain spin_lock() is sufficient here.
2028 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002029 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002030
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002031 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002032 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002033 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2034 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002035
2036 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2037 VLV_FIFO(SPRITEB, 0xff));
2038 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2039 VLV_FIFO(SPRITEB, sprite1_start));
2040
2041 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2042 VLV_FIFO(SPRITEB_HI, 0x1));
2043 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2044 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2045
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002046 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2047 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002048 break;
2049 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002050 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2051 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002052
2053 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2054 VLV_FIFO(SPRITED, 0xff));
2055 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2056 VLV_FIFO(SPRITED, sprite1_start));
2057
2058 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2059 VLV_FIFO(SPRITED_HI, 0xff));
2060 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2061 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2062
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002063 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2064 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002065 break;
2066 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002067 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2068 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002069
2070 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2071 VLV_FIFO(SPRITEF, 0xff));
2072 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2073 VLV_FIFO(SPRITEF, sprite1_start));
2074
2075 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2076 VLV_FIFO(SPRITEF_HI, 0xff));
2077 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2078 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2079
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002080 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2081 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002082 break;
2083 default:
2084 break;
2085 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002086
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002087 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002088
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002089 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002090}
2091
2092#undef VLV_FIFO
2093
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002094static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
2095 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002096{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002097 struct intel_crtc_state *new_crtc_state =
2098 intel_atomic_get_new_crtc_state(state, crtc);
2099 const struct intel_crtc_state *old_crtc_state =
2100 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002101 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2102 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002103 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002104 int level;
2105
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002106 if (!new_crtc_state->hw.active ||
2107 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002108 *intermediate = *optimal;
2109
2110 intermediate->cxsr = false;
2111 goto out;
2112 }
2113
Ville Syrjälä4841da52017-03-02 19:14:59 +02002114 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002115 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002116 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002117
2118 for (level = 0; level < intermediate->num_levels; level++) {
2119 enum plane_id plane_id;
2120
2121 for_each_plane_id_on_crtc(crtc, plane_id) {
2122 intermediate->wm[level].plane[plane_id] =
2123 min(optimal->wm[level].plane[plane_id],
2124 active->wm[level].plane[plane_id]);
2125 }
2126
2127 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2128 active->sr[level].plane);
2129 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2130 active->sr[level].cursor);
2131 }
2132
2133 vlv_invalidate_wms(crtc, intermediate, level);
2134
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002135out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002136 /*
2137 * If our intermediate WM are identical to the final WM, then we can
2138 * omit the post-vblank programming; only update if it's different.
2139 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002140 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002141 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002142
2143 return 0;
2144}
2145
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002146static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002147 struct vlv_wm_values *wm)
2148{
2149 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002150 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002151
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002152 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002153 wm->cxsr = true;
2154
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002155 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002156 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002157
2158 if (!crtc->active)
2159 continue;
2160
2161 if (!wm_state->cxsr)
2162 wm->cxsr = false;
2163
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002164 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002165 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2166 }
2167
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002168 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002169 wm->cxsr = false;
2170
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002171 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002172 wm->level = VLV_WM_LEVEL_PM2;
2173
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002174 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002175 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002176 enum pipe pipe = crtc->pipe;
2177
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002178 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002179 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002180 wm->sr = wm_state->sr[wm->level];
2181
Ville Syrjälä1b313892016-11-28 19:37:08 +02002182 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2183 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2184 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2185 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002186 }
2187}
2188
Ville Syrjäläff32c542017-03-02 19:14:57 +02002189static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002190{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002191 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2192 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002193
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002194 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002195
Ville Syrjäläff32c542017-03-02 19:14:57 +02002196 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002197 return;
2198
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002199 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002200 chv_set_memory_dvfs(dev_priv, false);
2201
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002202 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002203 chv_set_memory_pm5(dev_priv, false);
2204
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002205 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002206 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002207
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002208 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002209
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002210 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002211 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002212
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002213 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002214 chv_set_memory_pm5(dev_priv, true);
2215
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002216 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002217 chv_set_memory_dvfs(dev_priv, true);
2218
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002219 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002220}
2221
Ville Syrjäläff32c542017-03-02 19:14:57 +02002222static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002223 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002224{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002225 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2226 const struct intel_crtc_state *crtc_state =
2227 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002228
2229 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002230 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2231 vlv_program_watermarks(dev_priv);
2232 mutex_unlock(&dev_priv->wm.wm_mutex);
2233}
2234
2235static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002236 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002237{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002238 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2239 const struct intel_crtc_state *crtc_state =
2240 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002241
2242 if (!crtc_state->wm.need_postvbl_update)
2243 return;
2244
2245 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002246 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002247 vlv_program_watermarks(dev_priv);
2248 mutex_unlock(&dev_priv->wm.wm_mutex);
2249}
2250
Dave Airlieef9c66a2021-09-29 01:57:47 +03002251static void i965_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002252{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002253 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002254 int srwm = 1;
2255 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002256 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002257
2258 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002259 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002260 if (crtc) {
2261 /* self-refresh has much higher latency */
2262 static const int sr_latency_ns = 12000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002263 const struct drm_display_mode *pipe_mode =
2264 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002265 const struct drm_framebuffer *fb =
2266 crtc->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002267 int clock = pipe_mode->crtc_clock;
2268 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002269 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002270 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002271 int entries;
2272
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002273 entries = intel_wm_method2(clock, htotal,
2274 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002275 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2276 srwm = I965_FIFO_SIZE - entries;
2277 if (srwm < 0)
2278 srwm = 1;
2279 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002280 drm_dbg_kms(&dev_priv->drm,
2281 "self-refresh entries: %d, wm: %d\n",
2282 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002283
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002284 entries = intel_wm_method2(clock, htotal,
2285 crtc->base.cursor->state->crtc_w, 4,
2286 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002287 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002288 i965_cursor_wm_info.cacheline_size) +
2289 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002290
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002291 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002292 if (cursor_sr > i965_cursor_wm_info.max_wm)
2293 cursor_sr = i965_cursor_wm_info.max_wm;
2294
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002295 drm_dbg_kms(&dev_priv->drm,
2296 "self-refresh watermark: display plane %d "
2297 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002298
Imre Deak98584252014-06-13 14:54:20 +03002299 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002300 } else {
Imre Deak98584252014-06-13 14:54:20 +03002301 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002302 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002303 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002304 }
2305
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002306 drm_dbg_kms(&dev_priv->drm,
2307 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2308 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002309
2310 /* 965 has limitations... */
Jani Nikula5f461662020-11-30 13:15:58 +02002311 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002312 FW_WM(8, CURSORB) |
2313 FW_WM(8, PLANEB) |
2314 FW_WM(8, PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02002315 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002316 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002317 /* update cursor SR watermark */
Jani Nikula5f461662020-11-30 13:15:58 +02002318 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002319
2320 if (cxsr_enabled)
2321 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002322}
2323
Ville Syrjäläf4998962015-03-10 17:02:21 +02002324#undef FW_WM
2325
Dave Airlieef9c66a2021-09-29 01:57:47 +03002326static void i9xx_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002327{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002328 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002329 u32 fwater_lo;
2330 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002331 int cwm, srwm = 1;
2332 int fifo_size;
2333 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002334 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002335
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002336 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002337 wm_info = &i945_wm_info;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002338 else if (DISPLAY_VER(dev_priv) != 2)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002339 wm_info = &i915_wm_info;
2340 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002341 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002342
Dave Airlie758b2fc2021-09-29 01:57:46 +03002343 if (DISPLAY_VER(dev_priv) == 2)
2344 fifo_size = i830_get_fifo_size(dev_priv, PLANE_A);
2345 else
2346 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_A);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002347 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002348 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002349 const struct drm_display_mode *pipe_mode =
2350 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002351 const struct drm_framebuffer *fb =
2352 crtc->base.primary->state->fb;
2353 int cpp;
2354
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002355 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002356 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002357 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002358 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002359
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002360 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002361 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002362 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002363 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002364 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002365 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002366 if (planea_wm > (long)wm_info->max_wm)
2367 planea_wm = wm_info->max_wm;
2368 }
2369
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002370 if (DISPLAY_VER(dev_priv) == 2)
Ville Syrjälä9d539102014-08-15 01:21:53 +03002371 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002372
Dave Airlie758b2fc2021-09-29 01:57:46 +03002373 if (DISPLAY_VER(dev_priv) == 2)
2374 fifo_size = i830_get_fifo_size(dev_priv, PLANE_B);
2375 else
2376 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_B);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002377 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002378 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002379 const struct drm_display_mode *pipe_mode =
2380 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002381 const struct drm_framebuffer *fb =
2382 crtc->base.primary->state->fb;
2383 int cpp;
2384
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002385 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002386 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002387 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002388 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002389
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002390 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002391 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002392 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002393 if (enabled == NULL)
2394 enabled = crtc;
2395 else
2396 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002397 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002398 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002399 if (planeb_wm > (long)wm_info->max_wm)
2400 planeb_wm = wm_info->max_wm;
2401 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002402
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002403 drm_dbg_kms(&dev_priv->drm,
2404 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002405
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002406 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002407 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002408
Ville Syrjäläefc26112016-10-31 22:37:04 +02002409 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002410
2411 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002412 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002413 enabled = NULL;
2414 }
2415
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002416 /*
2417 * Overlay gets an aggressive default since video jitter is bad.
2418 */
2419 cwm = 2;
2420
2421 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002422 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002423
2424 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002425 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002426 /* self-refresh has much higher latency */
2427 static const int sr_latency_ns = 6000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002428 const struct drm_display_mode *pipe_mode =
2429 &enabled->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002430 const struct drm_framebuffer *fb =
2431 enabled->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002432 int clock = pipe_mode->crtc_clock;
2433 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002434 int hdisplay = enabled->config->pipe_src_w;
2435 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002436 int entries;
2437
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002438 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002439 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002440 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002441 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002442
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002443 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2444 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002445 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002446 drm_dbg_kms(&dev_priv->drm,
2447 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002448 srwm = wm_info->fifo_size - entries;
2449 if (srwm < 0)
2450 srwm = 1;
2451
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002452 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02002453 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002454 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002455 else
Jani Nikula5f461662020-11-30 13:15:58 +02002456 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002457 }
2458
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002459 drm_dbg_kms(&dev_priv->drm,
2460 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2461 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002462
2463 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2464 fwater_hi = (cwm & 0x1f);
2465
2466 /* Set request length to 8 cachelines per fetch */
2467 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2468 fwater_hi = fwater_hi | (1 << 8);
2469
Jani Nikula5f461662020-11-30 13:15:58 +02002470 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2471 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002472
Imre Deak5209b1f2014-07-01 12:36:17 +03002473 if (enabled)
2474 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002475}
2476
Dave Airlieef9c66a2021-09-29 01:57:47 +03002477static void i845_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002478{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002479 struct intel_crtc *crtc;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002480 const struct drm_display_mode *pipe_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002481 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002482 int planea_wm;
2483
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002484 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002485 if (crtc == NULL)
2486 return;
2487
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002488 pipe_mode = &crtc->config->hw.pipe_mode;
2489 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002490 &i845_wm_info,
Dave Airlie758b2fc2021-09-29 01:57:46 +03002491 i845_get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002492 4, pessimal_latency_ns);
Jani Nikula5f461662020-11-30 13:15:58 +02002493 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002494 fwater_lo |= (3<<8) | planea_wm;
2495
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002496 drm_dbg_kms(&dev_priv->drm,
2497 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002498
Jani Nikula5f461662020-11-30 13:15:58 +02002499 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002500}
2501
Ville Syrjälä37126462013-08-01 16:18:55 +03002502/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002503static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2504 unsigned int cpp,
2505 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002506{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002507 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002509 ret = intel_wm_method1(pixel_rate, cpp, latency);
2510 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002511
2512 return ret;
2513}
2514
Ville Syrjälä37126462013-08-01 16:18:55 +03002515/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002516static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2517 unsigned int htotal,
2518 unsigned int width,
2519 unsigned int cpp,
2520 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002522 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002523
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002524 ret = intel_wm_method2(pixel_rate, htotal,
2525 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002526 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002527
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002528 return ret;
2529}
2530
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002531static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002532{
Matt Roper15126882015-12-03 11:37:40 -08002533 /*
2534 * Neither of these should be possible since this function shouldn't be
2535 * called if the CRTC is off or the plane is invisible. But let's be
2536 * extra paranoid to avoid a potential divide-by-zero if we screw up
2537 * elsewhere in the driver.
2538 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002539 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002540 return 0;
2541 if (WARN_ON(!horiz_pixels))
2542 return 0;
2543
Ville Syrjäläac484962016-01-20 21:05:26 +02002544 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002545}
2546
Imre Deak820c1982013-12-17 14:46:36 +02002547struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002548 u16 pri;
2549 u16 spr;
2550 u16 cur;
2551 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002552};
2553
Ville Syrjälä37126462013-08-01 16:18:55 +03002554/*
2555 * For both WM_PIPE and WM_LP.
2556 * mem_value must be in 0.1us units.
2557 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002558static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2559 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002560 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002561{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002562 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002563 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002564
Ville Syrjälä03981c62018-11-14 19:34:40 +02002565 if (mem_value == 0)
2566 return U32_MAX;
2567
Maarten Lankhorstec193642019-06-28 10:55:17 +02002568 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002569 return 0;
2570
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002571 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002572
Maarten Lankhorstec193642019-06-28 10:55:17 +02002573 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002574
2575 if (!is_lp)
2576 return method1;
2577
Maarten Lankhorstec193642019-06-28 10:55:17 +02002578 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002579 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002580 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002581 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002582
2583 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002584}
2585
Ville Syrjälä37126462013-08-01 16:18:55 +03002586/*
2587 * For both WM_PIPE and WM_LP.
2588 * mem_value must be in 0.1us units.
2589 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002590static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2591 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002592 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002593{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002594 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002595 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002596
Ville Syrjälä03981c62018-11-14 19:34:40 +02002597 if (mem_value == 0)
2598 return U32_MAX;
2599
Maarten Lankhorstec193642019-06-28 10:55:17 +02002600 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002601 return 0;
2602
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002603 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002604
Maarten Lankhorstec193642019-06-28 10:55:17 +02002605 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2606 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002607 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002608 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002609 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002610 return min(method1, method2);
2611}
2612
Ville Syrjälä37126462013-08-01 16:18:55 +03002613/*
2614 * For both WM_PIPE and WM_LP.
2615 * mem_value must be in 0.1us units.
2616 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002617static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2618 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002619 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002620{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002621 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002622
Ville Syrjälä03981c62018-11-14 19:34:40 +02002623 if (mem_value == 0)
2624 return U32_MAX;
2625
Maarten Lankhorstec193642019-06-28 10:55:17 +02002626 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002627 return 0;
2628
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002629 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002630
Maarten Lankhorstec193642019-06-28 10:55:17 +02002631 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002632 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002633 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002634 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002635}
2636
Paulo Zanonicca32e92013-05-31 11:45:06 -03002637/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002638static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2639 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002640 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002641{
Ville Syrjälä83054942016-11-18 21:53:00 +02002642 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002643
Maarten Lankhorstec193642019-06-28 10:55:17 +02002644 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002645 return 0;
2646
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002647 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002648
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002649 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2650 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002651}
2652
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002653static unsigned int
2654ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002655{
Matt Roper7dadd282021-03-19 21:42:43 -07002656 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002657 return 3072;
Matt Roper7dadd282021-03-19 21:42:43 -07002658 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002659 return 768;
2660 else
2661 return 512;
2662}
2663
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002664static unsigned int
2665ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2666 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002667{
Matt Roper7dadd282021-03-19 21:42:43 -07002668 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002669 /* BDW primary/sprite plane watermarks */
2670 return level == 0 ? 255 : 2047;
Matt Roper7dadd282021-03-19 21:42:43 -07002671 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002672 /* IVB/HSW primary/sprite plane watermarks */
2673 return level == 0 ? 127 : 1023;
2674 else if (!is_sprite)
2675 /* ILK/SNB primary plane watermarks */
2676 return level == 0 ? 127 : 511;
2677 else
2678 /* ILK/SNB sprite plane watermarks */
2679 return level == 0 ? 63 : 255;
2680}
2681
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002682static unsigned int
2683ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002684{
Matt Roper7dadd282021-03-19 21:42:43 -07002685 if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002686 return level == 0 ? 63 : 255;
2687 else
2688 return level == 0 ? 31 : 63;
2689}
2690
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002691static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002692{
Matt Roper7dadd282021-03-19 21:42:43 -07002693 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002694 return 31;
2695 else
2696 return 15;
2697}
2698
Ville Syrjälä158ae642013-08-07 13:28:19 +03002699/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002700static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002701 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002702 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002703 enum intel_ddb_partitioning ddb_partitioning,
2704 bool is_sprite)
2705{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002706 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002707
2708 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002709 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002710 return 0;
2711
2712 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002713 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002714 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002715
2716 /*
2717 * For some reason the non self refresh
2718 * FIFO size is only half of the self
2719 * refresh FIFO size on ILK/SNB.
2720 */
Matt Roper7dadd282021-03-19 21:42:43 -07002721 if (DISPLAY_VER(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002722 fifo_size /= 2;
2723 }
2724
Ville Syrjälä240264f2013-08-07 13:29:12 +03002725 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002726 /* level 0 is always calculated with 1:1 split */
2727 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2728 if (is_sprite)
2729 fifo_size *= 5;
2730 fifo_size /= 6;
2731 } else {
2732 fifo_size /= 2;
2733 }
2734 }
2735
2736 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002737 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002738}
2739
2740/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002741static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002742 int level,
2743 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002744{
2745 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002746 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002747 return 64;
2748
2749 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002750 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002751}
2752
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002753static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002754 int level,
2755 const struct intel_wm_config *config,
2756 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002757 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002758{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002759 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2760 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2761 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2762 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002763}
2764
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002765static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002766 int level,
2767 struct ilk_wm_maximums *max)
2768{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002769 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2770 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2771 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2772 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002773}
2774
Ville Syrjäläd9395652013-10-09 19:18:10 +03002775static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002776 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002777 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002778{
2779 bool ret;
2780
2781 /* already determined to be invalid? */
2782 if (!result->enable)
2783 return false;
2784
2785 result->enable = result->pri_val <= max->pri &&
2786 result->spr_val <= max->spr &&
2787 result->cur_val <= max->cur;
2788
2789 ret = result->enable;
2790
2791 /*
2792 * HACK until we can pre-compute everything,
2793 * and thus fail gracefully if LP0 watermarks
2794 * are exceeded...
2795 */
2796 if (level == 0 && !result->enable) {
2797 if (result->pri_val > max->pri)
2798 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2799 level, result->pri_val, max->pri);
2800 if (result->spr_val > max->spr)
2801 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2802 level, result->spr_val, max->spr);
2803 if (result->cur_val > max->cur)
2804 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2805 level, result->cur_val, max->cur);
2806
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002807 result->pri_val = min_t(u32, result->pri_val, max->pri);
2808 result->spr_val = min_t(u32, result->spr_val, max->spr);
2809 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002810 result->enable = true;
2811 }
2812
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002813 return ret;
2814}
2815
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002816static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002817 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002818 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002819 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002820 const struct intel_plane_state *pristate,
2821 const struct intel_plane_state *sprstate,
2822 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002823 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002824{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002825 u16 pri_latency = dev_priv->wm.pri_latency[level];
2826 u16 spr_latency = dev_priv->wm.spr_latency[level];
2827 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002828
2829 /* WM1+ latency values stored in 0.5us units */
2830 if (level > 0) {
2831 pri_latency *= 5;
2832 spr_latency *= 5;
2833 cur_latency *= 5;
2834 }
2835
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002836 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002837 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002838 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002839 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002840 }
2841
2842 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002843 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002844
2845 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002846 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002847
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002848 result->enable = true;
2849}
2850
Ville Syrjäläbb726512016-10-31 22:37:24 +02002851static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002852 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002853{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002854 struct intel_uncore *uncore = &dev_priv->uncore;
2855
Matt Roper7dadd282021-03-19 21:42:43 -07002856 if (DISPLAY_VER(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002857 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002858 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002859 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roperd3252e12021-08-20 15:57:10 -07002860 int mult = IS_DG2(dev_priv) ? 2 : 1;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002861
2862 /* read the first set of memory latencies[0:3] */
2863 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002864 ret = sandybridge_pcode_read(dev_priv,
2865 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002866 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002867
2868 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002869 drm_err(&dev_priv->drm,
2870 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002871 return;
2872 }
2873
Matt Roperd3252e12021-08-20 15:57:10 -07002874 wm[0] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2875 wm[1] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2876 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2877 wm[2] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2878 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2879 wm[3] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2880 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002881
2882 /* read the second set of memory latencies[4:7] */
2883 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002884 ret = sandybridge_pcode_read(dev_priv,
2885 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002886 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002887 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002888 drm_err(&dev_priv->drm,
2889 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002890 return;
2891 }
2892
Matt Roperd3252e12021-08-20 15:57:10 -07002893 wm[4] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2894 wm[5] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2895 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2896 wm[6] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2897 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2898 wm[7] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2899 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002900
Vandana Kannan367294b2014-11-04 17:06:46 +00002901 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002902 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2903 * need to be disabled. We make sure to sanitize the values out
2904 * of the punit to satisfy this requirement.
2905 */
2906 for (level = 1; level <= max_level; level++) {
2907 if (wm[level] == 0) {
2908 for (i = level + 1; i <= max_level; i++)
2909 wm[i] = 0;
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002910
2911 max_level = level - 1;
2912
Paulo Zanoni0727e402016-09-22 18:00:30 -03002913 break;
2914 }
2915 }
2916
2917 /*
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002918 * WaWmMemoryReadLatency
Damien Lespiau6f972352015-02-09 19:33:07 +00002919 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002920 * punit doesn't take into account the read latency so we need
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002921 * to add proper adjustement to each valid level we retrieve
2922 * from the punit when level 0 response data is 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002923 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002924 if (wm[0] == 0) {
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002925 u8 adjust = DISPLAY_VER(dev_priv) >= 12 ? 3 : 2;
2926
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002927 for (level = 0; level <= max_level; level++)
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002928 wm[level] += adjust;
Paulo Zanoni0727e402016-09-22 18:00:30 -03002929 }
2930
Mahesh Kumar86b59282018-08-31 16:39:42 +05302931 /*
2932 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2933 * If we could not get dimm info enable this WA to prevent from
2934 * any underrun. If not able to get Dimm info assume 16GB dimm
2935 * to avoid any underrun.
2936 */
José Roberto de Souza66a24502021-01-28 08:43:12 -08002937 if (dev_priv->dram_info.wm_lv_0_adjust_needed)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302938 wm[0] += 1;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002939 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002940 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002941
2942 wm[0] = (sskpd >> 56) & 0xFF;
2943 if (wm[0] == 0)
2944 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002945 wm[1] = (sskpd >> 4) & 0xFF;
2946 wm[2] = (sskpd >> 12) & 0xFF;
2947 wm[3] = (sskpd >> 20) & 0x1FF;
2948 wm[4] = (sskpd >> 32) & 0x1FF;
Matt Roper7dadd282021-03-19 21:42:43 -07002949 } else if (DISPLAY_VER(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002950 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002951
2952 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2953 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2954 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2955 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Matt Roper7dadd282021-03-19 21:42:43 -07002956 } else if (DISPLAY_VER(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002957 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002958
2959 /* ILK primary LP0 latency is 700 ns */
2960 wm[0] = 7;
2961 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2962 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002963 } else {
2964 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002965 }
2966}
2967
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002968static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002969 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002970{
2971 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002972 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002973 wm[0] = 13;
2974}
2975
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002976static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002977 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002978{
2979 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002980 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002981 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002982}
2983
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002984int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002985{
2986 /* how many WM levels are we expecting */
Matt Roper7959ffe2021-05-18 17:06:11 -07002987 if (HAS_HW_SAGV_WM(dev_priv))
2988 return 5;
2989 else if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002990 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002991 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002992 return 4;
Matt Roper7dadd282021-03-19 21:42:43 -07002993 else if (DISPLAY_VER(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002994 return 3;
2995 else
2996 return 2;
2997}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002998
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002999static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003000 const char *name,
Linus Torvaldse7c6e402021-04-27 17:05:53 -07003001 const u16 wm[])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003002{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003003 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003004
3005 for (level = 0; level <= max_level; level++) {
3006 unsigned int latency = wm[level];
3007
3008 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003009 drm_dbg_kms(&dev_priv->drm,
3010 "%s WM%d latency not provided\n",
3011 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003012 continue;
3013 }
3014
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003015 /*
3016 * - latencies are in us on gen9.
3017 * - before then, WM1+ latency values are in 0.5us units
3018 */
Matt Roper7dadd282021-03-19 21:42:43 -07003019 if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003020 latency *= 10;
3021 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003022 latency *= 5;
3023
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003024 drm_dbg_kms(&dev_priv->drm,
3025 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3026 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003027 }
3028}
3029
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003030static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003031 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003032{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003033 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003034
3035 if (wm[0] >= min)
3036 return false;
3037
3038 wm[0] = max(wm[0], min);
3039 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003040 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003041
3042 return true;
3043}
3044
Ville Syrjäläbb726512016-10-31 22:37:24 +02003045static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003046{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003047 bool changed;
3048
3049 /*
3050 * The BIOS provided WM memory latency values are often
3051 * inadequate for high resolution displays. Adjust them.
3052 */
3053 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3054 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3055 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3056
3057 if (!changed)
3058 return;
3059
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003060 drm_dbg_kms(&dev_priv->drm,
3061 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003062 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3063 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3064 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003065}
3066
Ville Syrjälä03981c62018-11-14 19:34:40 +02003067static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3068{
3069 /*
3070 * On some SNB machines (Thinkpad X220 Tablet at least)
3071 * LP3 usage can cause vblank interrupts to be lost.
3072 * The DEIIR bit will go high but it looks like the CPU
3073 * never gets interrupted.
3074 *
3075 * It's not clear whether other interrupt source could
3076 * be affected or if this is somehow limited to vblank
3077 * interrupts only. To play it safe we disable LP3
3078 * watermarks entirely.
3079 */
3080 if (dev_priv->wm.pri_latency[3] == 0 &&
3081 dev_priv->wm.spr_latency[3] == 0 &&
3082 dev_priv->wm.cur_latency[3] == 0)
3083 return;
3084
3085 dev_priv->wm.pri_latency[3] = 0;
3086 dev_priv->wm.spr_latency[3] = 0;
3087 dev_priv->wm.cur_latency[3] = 0;
3088
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003089 drm_dbg_kms(&dev_priv->drm,
3090 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003091 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3092 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3093 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3094}
3095
Ville Syrjäläbb726512016-10-31 22:37:24 +02003096static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003097{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003098 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003099
3100 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3101 sizeof(dev_priv->wm.pri_latency));
3102 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3103 sizeof(dev_priv->wm.pri_latency));
3104
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003105 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003106 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003107
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003108 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3109 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3110 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003111
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003112 if (DISPLAY_VER(dev_priv) == 6) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003113 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003114 snb_wm_lp3_irq_quirk(dev_priv);
3115 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003116}
3117
Ville Syrjäläbb726512016-10-31 22:37:24 +02003118static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003119{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003120 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003121 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003122}
3123
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003124static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003125 struct intel_pipe_wm *pipe_wm)
3126{
3127 /* LP0 watermark maximums depend on this pipe alone */
3128 const struct intel_wm_config config = {
3129 .num_pipes_active = 1,
3130 .sprites_enabled = pipe_wm->sprites_enabled,
3131 .sprites_scaled = pipe_wm->sprites_scaled,
3132 };
3133 struct ilk_wm_maximums max;
3134
3135 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003136 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003137
3138 /* At least LP0 must be valid */
3139 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003140 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003141 return false;
3142 }
3143
3144 return true;
3145}
3146
Matt Roper261a27d2015-10-08 15:28:25 -07003147/* Compute new watermarks for the pipe */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003148static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
3149 struct intel_crtc *crtc)
Matt Roper261a27d2015-10-08 15:28:25 -07003150{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003151 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3152 struct intel_crtc_state *crtc_state =
3153 intel_atomic_get_new_crtc_state(state, crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003154 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003155 struct intel_plane *plane;
3156 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003157 const struct intel_plane_state *pristate = NULL;
3158 const struct intel_plane_state *sprstate = NULL;
3159 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003160 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003161 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003162
Maarten Lankhorstec193642019-06-28 10:55:17 +02003163 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003164
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003165 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3166 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3167 pristate = plane_state;
3168 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3169 sprstate = plane_state;
3170 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3171 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003172 }
3173
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003174 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003175 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003176 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3177 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3178 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3179 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003180 }
3181
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003182 usable_level = max_level;
3183
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003184 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Matt Roper7dadd282021-03-19 21:42:43 -07003185 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003186 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003187
3188 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003189 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003190 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003191
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003192 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003193 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003194 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003195
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003196 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003197 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003198
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003199 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003200
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003201 for (level = 1; level <= usable_level; level++) {
3202 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003203
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003204 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003205 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003206
3207 /*
3208 * Disable any watermark level that exceeds the
3209 * register maximums since such watermarks are
3210 * always invalid.
3211 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003212 if (!ilk_validate_wm_level(level, &max, wm)) {
3213 memset(wm, 0, sizeof(*wm));
3214 break;
3215 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003216 }
3217
Matt Roper86c8bbb2015-09-24 15:53:16 -07003218 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003219}
3220
3221/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003222 * Build a set of 'intermediate' watermark values that satisfy both the old
3223 * state and the new state. These can be programmed to the hardware
3224 * immediately.
3225 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003226static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
3227 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08003228{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003229 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3230 struct intel_crtc_state *new_crtc_state =
3231 intel_atomic_get_new_crtc_state(state, crtc);
3232 const struct intel_crtc_state *old_crtc_state =
3233 intel_atomic_get_old_crtc_state(state, crtc);
3234 struct intel_pipe_wm *a = &new_crtc_state->wm.ilk.intermediate;
3235 const struct intel_pipe_wm *b = &old_crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003236 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003237
3238 /*
3239 * Start with the final, target watermarks, then combine with the
3240 * currently active watermarks to get values that are safe both before
3241 * and after the vblank.
3242 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003243 *a = new_crtc_state->wm.ilk.optimal;
3244 if (!new_crtc_state->hw.active ||
3245 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) ||
3246 state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003247 return 0;
3248
Matt Ropered4a6a72016-02-23 17:20:13 -08003249 a->pipe_enabled |= b->pipe_enabled;
3250 a->sprites_enabled |= b->sprites_enabled;
3251 a->sprites_scaled |= b->sprites_scaled;
3252
3253 for (level = 0; level <= max_level; level++) {
3254 struct intel_wm_level *a_wm = &a->wm[level];
3255 const struct intel_wm_level *b_wm = &b->wm[level];
3256
3257 a_wm->enable &= b_wm->enable;
3258 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3259 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3260 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3261 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3262 }
3263
3264 /*
3265 * We need to make sure that these merged watermark values are
3266 * actually a valid configuration themselves. If they're not,
3267 * there's no safe way to transition from the old state to
3268 * the new state, so we need to fail the atomic transaction.
3269 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003270 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003271 return -EINVAL;
3272
3273 /*
3274 * If our intermediate WM are identical to the final WM, then we can
3275 * omit the post-vblank programming; only update if it's different.
3276 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003277 if (memcmp(a, &new_crtc_state->wm.ilk.optimal, sizeof(*a)) != 0)
3278 new_crtc_state->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003279
3280 return 0;
3281}
3282
3283/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003284 * Merge the watermarks from all active pipes for a specific level.
3285 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003286static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003287 int level,
3288 struct intel_wm_level *ret_wm)
3289{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003290 const struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003291
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003292 ret_wm->enable = true;
3293
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003294 for_each_intel_crtc(&dev_priv->drm, crtc) {
3295 const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003296 const struct intel_wm_level *wm = &active->wm[level];
3297
3298 if (!active->pipe_enabled)
3299 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003300
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003301 /*
3302 * The watermark values may have been used in the past,
3303 * so we must maintain them in the registers for some
3304 * time even if the level is now disabled.
3305 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003306 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003307 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003308
3309 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3310 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3311 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3312 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3313 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003314}
3315
3316/*
3317 * Merge all low power watermarks for all active pipes.
3318 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003319static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003320 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003321 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003322 struct intel_pipe_wm *merged)
3323{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003324 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003325 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003326
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003327 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Matt Roper7dadd282021-03-19 21:42:43 -07003328 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003329 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003330 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003331
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003332 /* ILK: FBC WM must be disabled always */
Matt Roper7dadd282021-03-19 21:42:43 -07003333 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003334
3335 /* merge each WM1+ level */
3336 for (level = 1; level <= max_level; level++) {
3337 struct intel_wm_level *wm = &merged->wm[level];
3338
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003339 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003340
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003341 if (level > last_enabled_level)
3342 wm->enable = false;
3343 else if (!ilk_validate_wm_level(level, max, wm))
3344 /* make sure all following levels get disabled */
3345 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003346
3347 /*
3348 * The spec says it is preferred to disable
3349 * FBC WMs instead of disabling a WM level.
3350 */
3351 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003352 if (wm->enable)
3353 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003354 wm->fbc_val = 0;
3355 }
3356 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003357
3358 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3359 /*
3360 * FIXME this is racy. FBC might get enabled later.
3361 * What we should check here is whether FBC can be
3362 * enabled sometime later.
3363 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003364 if (DISPLAY_VER(dev_priv) == 5 && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003365 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003366 for (level = 2; level <= max_level; level++) {
3367 struct intel_wm_level *wm = &merged->wm[level];
3368
3369 wm->enable = false;
3370 }
3371 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003372}
3373
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003374static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3375{
3376 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3377 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3378}
3379
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003380/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003381static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3382 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003383{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003384 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003385 return 2 * level;
3386 else
3387 return dev_priv->wm.pri_latency[level];
3388}
3389
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003390static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003391 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003392 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003393 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003394{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003395 struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003396 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003397
Ville Syrjälä0362c782013-10-09 19:17:57 +03003398 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003399 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003400
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003401 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003402 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003403 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003404
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003405 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003406
Ville Syrjälä0362c782013-10-09 19:17:57 +03003407 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003408
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003409 /*
3410 * Maintain the watermark values even if the level is
3411 * disabled. Doing otherwise could cause underruns.
3412 */
3413 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003414 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003415 (r->pri_val << WM1_LP_SR_SHIFT) |
3416 r->cur_val;
3417
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003418 if (r->enable)
3419 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3420
Matt Roper7dadd282021-03-19 21:42:43 -07003421 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003422 results->wm_lp[wm_lp - 1] |=
3423 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3424 else
3425 results->wm_lp[wm_lp - 1] |=
3426 r->fbc_val << WM1_LP_FBC_SHIFT;
3427
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003428 /*
3429 * Always set WM1S_LP_EN when spr_val != 0, even if the
3430 * level is disabled. Doing otherwise could cause underruns.
3431 */
Matt Roper7dadd282021-03-19 21:42:43 -07003432 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303433 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003434 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3435 } else
3436 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003437 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003438
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003439 /* LP0 register values */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003440 for_each_intel_crtc(&dev_priv->drm, crtc) {
3441 enum pipe pipe = crtc->pipe;
3442 const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003443 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003444
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303445 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003446 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003447
3448 results->wm_pipe[pipe] =
3449 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3450 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3451 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003452 }
3453}
3454
Paulo Zanoni861f3382013-05-31 10:19:21 -03003455/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3456 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003457static struct intel_pipe_wm *
3458ilk_find_best_result(struct drm_i915_private *dev_priv,
3459 struct intel_pipe_wm *r1,
3460 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003461{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003462 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003463 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003464
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003465 for (level = 1; level <= max_level; level++) {
3466 if (r1->wm[level].enable)
3467 level1 = level;
3468 if (r2->wm[level].enable)
3469 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003470 }
3471
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003472 if (level1 == level2) {
3473 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003474 return r2;
3475 else
3476 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003477 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003478 return r1;
3479 } else {
3480 return r2;
3481 }
3482}
3483
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003484/* dirty bits used to track which watermarks need changes */
3485#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003486#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3487#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3488#define WM_DIRTY_FBC (1 << 24)
3489#define WM_DIRTY_DDB (1 << 25)
3490
Damien Lespiau055e3932014-08-18 13:49:10 +01003491static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003492 const struct ilk_wm_values *old,
3493 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003494{
3495 unsigned int dirty = 0;
3496 enum pipe pipe;
3497 int wm_lp;
3498
Damien Lespiau055e3932014-08-18 13:49:10 +01003499 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003500 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3501 dirty |= WM_DIRTY_PIPE(pipe);
3502 /* Must disable LP1+ watermarks too */
3503 dirty |= WM_DIRTY_LP_ALL;
3504 }
3505 }
3506
3507 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3508 dirty |= WM_DIRTY_FBC;
3509 /* Must disable LP1+ watermarks too */
3510 dirty |= WM_DIRTY_LP_ALL;
3511 }
3512
3513 if (old->partitioning != new->partitioning) {
3514 dirty |= WM_DIRTY_DDB;
3515 /* Must disable LP1+ watermarks too */
3516 dirty |= WM_DIRTY_LP_ALL;
3517 }
3518
3519 /* LP1+ watermarks already deemed dirty, no need to continue */
3520 if (dirty & WM_DIRTY_LP_ALL)
3521 return dirty;
3522
3523 /* Find the lowest numbered LP1+ watermark in need of an update... */
3524 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3525 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3526 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3527 break;
3528 }
3529
3530 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3531 for (; wm_lp <= 3; wm_lp++)
3532 dirty |= WM_DIRTY_LP(wm_lp);
3533
3534 return dirty;
3535}
3536
Ville Syrjälä8553c182013-12-05 15:51:39 +02003537static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3538 unsigned int dirty)
3539{
Imre Deak820c1982013-12-17 14:46:36 +02003540 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003541 bool changed = false;
3542
3543 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3544 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003545 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003546 changed = true;
3547 }
3548 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3549 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003550 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003551 changed = true;
3552 }
3553 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3554 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003555 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003556 changed = true;
3557 }
3558
3559 /*
3560 * Don't touch WM1S_LP_EN here.
3561 * Doing so could cause underruns.
3562 */
3563
3564 return changed;
3565}
3566
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003567/*
3568 * The spec says we shouldn't write when we don't need, because every write
3569 * causes WMs to be re-evaluated, expending some power.
3570 */
Imre Deak820c1982013-12-17 14:46:36 +02003571static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3572 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003573{
Imre Deak820c1982013-12-17 14:46:36 +02003574 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003575 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003576 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003577
Damien Lespiau055e3932014-08-18 13:49:10 +01003578 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003579 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003580 return;
3581
Ville Syrjälä8553c182013-12-05 15:51:39 +02003582 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003583
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003584 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Jani Nikula5f461662020-11-30 13:15:58 +02003585 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003586 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Jani Nikula5f461662020-11-30 13:15:58 +02003587 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003588 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Jani Nikula5f461662020-11-30 13:15:58 +02003589 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003590
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003591 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003592 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02003593 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003594 if (results->partitioning == INTEL_DDB_PART_1_2)
3595 val &= ~WM_MISC_DATA_PARTITION_5_6;
3596 else
3597 val |= WM_MISC_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003598 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003599 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02003600 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003601 if (results->partitioning == INTEL_DDB_PART_1_2)
3602 val &= ~DISP_DATA_PARTITION_5_6;
3603 else
3604 val |= DISP_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003605 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003606 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003607 }
3608
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003609 if (dirty & WM_DIRTY_FBC) {
Jani Nikula5f461662020-11-30 13:15:58 +02003610 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003611 if (results->enable_fbc_wm)
3612 val &= ~DISP_FBC_WM_DIS;
3613 else
3614 val |= DISP_FBC_WM_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02003615 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003616 }
3617
Imre Deak954911e2013-12-17 14:46:34 +02003618 if (dirty & WM_DIRTY_LP(1) &&
3619 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003620 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
Imre Deak954911e2013-12-17 14:46:34 +02003621
Matt Roper7dadd282021-03-19 21:42:43 -07003622 if (DISPLAY_VER(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003623 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003624 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003625 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003626 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003627 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003628
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003629 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003630 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003631 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003632 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003633 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003634 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003635
3636 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003637}
3638
Ville Syrjälä60aca572019-11-27 21:05:51 +02003639bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003640{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003641 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3642}
3643
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003644u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303645{
Ville Syrjäläb88da662021-04-16 20:10:09 +03003646 u8 enabled_slices = 0;
3647 enum dbuf_slice slice;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303648
Ville Syrjäläb88da662021-04-16 20:10:09 +03003649 for_each_dbuf_slice(dev_priv, slice) {
3650 if (intel_uncore_read(&dev_priv->uncore,
3651 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3652 enabled_slices |= BIT(slice);
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003653 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303654
Ville Syrjäläb88da662021-04-16 20:10:09 +03003655 return enabled_slices;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303656}
3657
Matt Roper024c9042015-09-24 15:53:11 -07003658/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003659 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3660 * so assume we'll always need it in order to avoid underruns.
3661 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003662static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003663{
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003664 return DISPLAY_VER(dev_priv) == 9;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003665}
3666
Paulo Zanoni56feca92016-09-22 18:00:28 -03003667static bool
3668intel_has_sagv(struct drm_i915_private *dev_priv)
3669{
Matt Roper70bfb302021-04-07 13:39:45 -07003670 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003671 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003672}
3673
James Ausmusb068a862019-10-09 10:23:14 -07003674static void
3675skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3676{
Matt Roper7dadd282021-03-19 21:42:43 -07003677 if (DISPLAY_VER(dev_priv) >= 12) {
James Ausmusda80f042019-10-09 10:23:15 -07003678 u32 val = 0;
3679 int ret;
3680
3681 ret = sandybridge_pcode_read(dev_priv,
3682 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3683 &val, NULL);
3684 if (!ret) {
3685 dev_priv->sagv_block_time_us = val;
3686 return;
3687 }
3688
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003689 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003690 } else if (DISPLAY_VER(dev_priv) == 11) {
James Ausmusb068a862019-10-09 10:23:14 -07003691 dev_priv->sagv_block_time_us = 10;
3692 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003693 } else if (DISPLAY_VER(dev_priv) == 10) {
James Ausmusb068a862019-10-09 10:23:14 -07003694 dev_priv->sagv_block_time_us = 20;
3695 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003696 } else if (DISPLAY_VER(dev_priv) == 9) {
James Ausmusb068a862019-10-09 10:23:14 -07003697 dev_priv->sagv_block_time_us = 30;
3698 return;
3699 } else {
Matt Roper7dadd282021-03-19 21:42:43 -07003700 MISSING_CASE(DISPLAY_VER(dev_priv));
James Ausmusb068a862019-10-09 10:23:14 -07003701 }
3702
3703 /* Default to an unusable block time */
3704 dev_priv->sagv_block_time_us = -1;
3705}
3706
Lyude656d1b82016-08-17 15:55:54 -04003707/*
3708 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3709 * depending on power and performance requirements. The display engine access
3710 * to system memory is blocked during the adjustment time. Because of the
3711 * blocking time, having this enabled can cause full system hangs and/or pipe
3712 * underruns if we don't meet all of the following requirements:
3713 *
3714 * - <= 1 pipe enabled
3715 * - All planes can enable watermarks for latencies >= SAGV engine block time
3716 * - We're not using an interlaced display configuration
3717 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003718static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003719intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003720{
3721 int ret;
3722
Paulo Zanoni56feca92016-09-22 18:00:28 -03003723 if (!intel_has_sagv(dev_priv))
3724 return 0;
3725
3726 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003727 return 0;
3728
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003729 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003730 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3731 GEN9_SAGV_ENABLE);
3732
Ville Syrjäläff61a972018-12-21 19:14:34 +02003733 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003734
3735 /*
3736 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003737 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003738 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003739 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003740 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003741 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003742 return 0;
3743 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003744 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003745 return ret;
3746 }
3747
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003748 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003749 return 0;
3750}
3751
Ville Syrjälä71024042020-09-25 15:17:48 +03003752static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003753intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003754{
Imre Deakb3b8e992016-12-05 18:27:38 +02003755 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003756
Paulo Zanoni56feca92016-09-22 18:00:28 -03003757 if (!intel_has_sagv(dev_priv))
3758 return 0;
3759
3760 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003761 return 0;
3762
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003763 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003764 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003765 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3766 GEN9_SAGV_DISABLE,
3767 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3768 1);
Lyude656d1b82016-08-17 15:55:54 -04003769 /*
3770 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003771 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003772 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003773 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003774 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003775 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003776 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003777 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003778 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003779 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003780 }
3781
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003782 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003783 return 0;
3784}
3785
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003786void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3787{
3788 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003789 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003790 const struct intel_bw_state *old_bw_state;
3791 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003792
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003793 /*
3794 * Just return if we can't control SAGV or don't have it.
3795 * This is different from situation when we have SAGV but just can't
3796 * afford it due to DBuf limitation - in case if SAGV is completely
3797 * disabled in a BIOS, we are not even allowed to send a PCode request,
3798 * as it will throw an error. So have to check it here.
3799 */
3800 if (!intel_has_sagv(dev_priv))
3801 return;
3802
3803 new_bw_state = intel_atomic_get_new_bw_state(state);
3804 if (!new_bw_state)
3805 return;
3806
Matt Roper7dadd282021-03-19 21:42:43 -07003807 if (DISPLAY_VER(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003808 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003809 return;
3810 }
3811
3812 old_bw_state = intel_atomic_get_old_bw_state(state);
3813 /*
3814 * Nothing to mask
3815 */
3816 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3817 return;
3818
3819 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3820
3821 /*
3822 * If new mask is zero - means there is nothing to mask,
3823 * we can only unmask, which should be done in unmask.
3824 */
3825 if (!new_mask)
3826 return;
3827
3828 /*
3829 * Restrict required qgv points before updating the configuration.
3830 * According to BSpec we can't mask and unmask qgv points at the same
3831 * time. Also masking should be done before updating the configuration
3832 * and unmasking afterwards.
3833 */
3834 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003835}
3836
3837void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3838{
3839 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003840 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003841 const struct intel_bw_state *old_bw_state;
3842 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003843
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003844 /*
3845 * Just return if we can't control SAGV or don't have it.
3846 * This is different from situation when we have SAGV but just can't
3847 * afford it due to DBuf limitation - in case if SAGV is completely
3848 * disabled in a BIOS, we are not even allowed to send a PCode request,
3849 * as it will throw an error. So have to check it here.
3850 */
3851 if (!intel_has_sagv(dev_priv))
3852 return;
3853
3854 new_bw_state = intel_atomic_get_new_bw_state(state);
3855 if (!new_bw_state)
3856 return;
3857
Matt Roper7dadd282021-03-19 21:42:43 -07003858 if (DISPLAY_VER(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003859 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003860 return;
3861 }
3862
3863 old_bw_state = intel_atomic_get_old_bw_state(state);
3864 /*
3865 * Nothing to unmask
3866 */
3867 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3868 return;
3869
3870 new_mask = new_bw_state->qgv_points_mask;
3871
3872 /*
3873 * Allow required qgv points after updating the configuration.
3874 * According to BSpec we can't mask and unmask qgv points at the same
3875 * time. Also masking should be done before updating the configuration
3876 * and unmasking afterwards.
3877 */
3878 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003879}
3880
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003881static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003882{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003883 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003884 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä9c312122020-11-06 19:30:40 +02003885 enum plane_id plane_id;
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003886 int max_level = INT_MAX;
Lyude656d1b82016-08-17 15:55:54 -04003887
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003888 if (!intel_has_sagv(dev_priv))
3889 return false;
3890
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003891 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003892 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003893
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02003894 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003895 return false;
3896
Ville Syrjälä9c312122020-11-06 19:30:40 +02003897 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003898 const struct skl_plane_wm *wm =
Ville Syrjälä9c312122020-11-06 19:30:40 +02003899 &crtc_state->wm.skl.optimal.planes[plane_id];
3900 int level;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003901
Lyude656d1b82016-08-17 15:55:54 -04003902 /* Skip this plane if it's not enabled */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003903 if (!wm->wm[0].enable)
Lyude656d1b82016-08-17 15:55:54 -04003904 continue;
3905
3906 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003907 for (level = ilk_wm_max_level(dev_priv);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003908 !wm->wm[level].enable; --level)
Lyude656d1b82016-08-17 15:55:54 -04003909 { }
3910
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003911 /* Highest common enabled wm level for all planes */
3912 max_level = min(level, max_level);
3913 }
3914
3915 /* No enabled planes? */
3916 if (max_level == INT_MAX)
3917 return true;
3918
3919 for_each_plane_id_on_crtc(crtc, plane_id) {
3920 const struct skl_plane_wm *wm =
3921 &crtc_state->wm.skl.optimal.planes[plane_id];
3922
Lyude656d1b82016-08-17 15:55:54 -04003923 /*
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003924 * All enabled planes must have enabled a common wm level that
3925 * can tolerate memory latencies higher than sagv_block_time_us
Lyude656d1b82016-08-17 15:55:54 -04003926 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003927 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
Lyude656d1b82016-08-17 15:55:54 -04003928 return false;
3929 }
3930
3931 return true;
3932}
3933
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003934static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3935{
3936 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3937 enum plane_id plane_id;
3938
3939 if (!crtc_state->hw.active)
3940 return true;
3941
3942 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003943 const struct skl_plane_wm *wm =
3944 &crtc_state->wm.skl.optimal.planes[plane_id];
3945
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003946 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003947 return false;
3948 }
3949
3950 return true;
3951}
3952
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003953static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3954{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003955 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3956 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3957
Matt Roper7dadd282021-03-19 21:42:43 -07003958 if (DISPLAY_VER(dev_priv) >= 12)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003959 return tgl_crtc_can_enable_sagv(crtc_state);
3960 else
3961 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003962}
3963
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003964bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3965 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003966{
Matt Roper7dadd282021-03-19 21:42:43 -07003967 if (DISPLAY_VER(dev_priv) < 11 &&
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003968 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003969 return false;
3970
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003971 return bw_state->pipe_sagv_reject == 0;
3972}
3973
3974static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3975{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003976 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003977 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003978 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003979 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003980 struct intel_bw_state *new_bw_state = NULL;
3981 const struct intel_bw_state *old_bw_state = NULL;
3982 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003983
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003984 for_each_new_intel_crtc_in_state(state, crtc,
3985 new_crtc_state, i) {
3986 new_bw_state = intel_atomic_get_bw_state(state);
3987 if (IS_ERR(new_bw_state))
3988 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003989
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003990 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003991
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003992 if (intel_crtc_can_enable_sagv(new_crtc_state))
3993 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
3994 else
3995 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
3996 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003997
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003998 if (!new_bw_state)
3999 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004000
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004001 new_bw_state->active_pipes =
4002 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03004003
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004004 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4005 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4006 if (ret)
4007 return ret;
4008 }
4009
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004010 for_each_new_intel_crtc_in_state(state, crtc,
4011 new_crtc_state, i) {
4012 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4013
4014 /*
4015 * We store use_sagv_wm in the crtc state rather than relying on
4016 * that bw state since we have no convenient way to get at the
4017 * latter from the plane commit hooks (especially in the legacy
4018 * cursor case)
4019 */
Matt Roper7959ffe2021-05-18 17:06:11 -07004020 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4021 DISPLAY_VER(dev_priv) >= 12 &&
4022 intel_can_enable_sagv(dev_priv, new_bw_state);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004023 }
4024
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004025 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4026 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004027 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4028 if (ret)
4029 return ret;
4030 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4031 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4032 if (ret)
4033 return ret;
4034 }
4035
4036 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004037}
4038
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004039static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4040{
Ville Syrjäläb88da662021-04-16 20:10:09 +03004041 return INTEL_INFO(dev_priv)->dbuf.size /
4042 hweight8(INTEL_INFO(dev_priv)->dbuf.slice_mask);
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004043}
4044
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004045static void
4046skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4047 struct skl_ddb_entry *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304048{
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004049 int slice_size = intel_dbuf_slice_size(dev_priv);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004050
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004051 if (!slice_mask) {
4052 ddb->start = 0;
4053 ddb->end = 0;
4054 return;
4055 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004056
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004057 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4058 ddb->end = fls(slice_mask) * slice_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004059
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004060 WARN_ON(ddb->start >= ddb->end);
Ville Syrjäläb88da662021-04-16 20:10:09 +03004061 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->dbuf.size);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004062}
4063
Ville Syrjälä835c1762021-05-18 17:06:16 -07004064static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
4065{
4066 struct skl_ddb_entry ddb;
4067
4068 if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
4069 slice_mask = BIT(DBUF_S1);
4070 else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
4071 slice_mask = BIT(DBUF_S3);
4072
4073 skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
4074
4075 return ddb.start;
4076}
4077
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004078u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4079 const struct skl_ddb_entry *entry)
4080{
Ville Syrjälä6390e5a2021-04-16 20:10:07 +03004081 int slice_size = intel_dbuf_slice_size(dev_priv);
4082 enum dbuf_slice start_slice, end_slice;
4083 u8 slice_mask = 0;
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004084
4085 if (!skl_ddb_entry_size(entry))
4086 return 0;
4087
4088 start_slice = entry->start / slice_size;
4089 end_slice = (entry->end - 1) / slice_size;
4090
4091 /*
4092 * Per plane DDB entry can in a really worst case be on multiple slices
4093 * but single entry is anyway contigious.
4094 */
4095 while (start_slice <= end_slice) {
4096 slice_mask |= BIT(start_slice);
4097 start_slice++;
4098 }
4099
4100 return slice_mask;
4101}
4102
Ville Syrjälä2791a402021-01-22 22:56:26 +02004103static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4104{
4105 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4106 int hdisplay, vdisplay;
4107
4108 if (!crtc_state->hw.active)
4109 return 0;
4110
4111 /*
4112 * Watermark/ddb requirement highly depends upon width of the
4113 * framebuffer, So instead of allocating DDB equally among pipes
4114 * distribute DDB based on resolution/width of the display.
4115 */
4116 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4117
4118 return hdisplay;
4119}
4120
Ville Syrjäläef79d622021-01-22 22:56:32 +02004121static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4122 enum pipe for_pipe,
4123 unsigned int *weight_start,
4124 unsigned int *weight_end,
4125 unsigned int *weight_total)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004126{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004127 struct drm_i915_private *dev_priv =
4128 to_i915(dbuf_state->base.state->base.dev);
4129 enum pipe pipe;
Ville Syrjälä53630962021-01-22 22:56:31 +02004130
4131 *weight_start = 0;
4132 *weight_end = 0;
4133 *weight_total = 0;
4134
Ville Syrjäläef79d622021-01-22 22:56:32 +02004135 for_each_pipe(dev_priv, pipe) {
4136 int weight = dbuf_state->weight[pipe];
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004137
4138 /*
4139 * Do not account pipes using other slice sets
4140 * luckily as of current BSpec slice sets do not partially
4141 * intersect(pipes share either same one slice or same slice set
4142 * i.e no partial intersection), so it is enough to check for
4143 * equality for now.
4144 */
Ville Syrjäläef79d622021-01-22 22:56:32 +02004145 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304146 continue;
4147
Ville Syrjälä53630962021-01-22 22:56:31 +02004148 *weight_total += weight;
Ville Syrjälä53630962021-01-22 22:56:31 +02004149 if (pipe < for_pipe) {
4150 *weight_start += weight;
4151 *weight_end += weight;
4152 } else if (pipe == for_pipe) {
4153 *weight_end += weight;
4154 }
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304155 }
Ville Syrjälä53630962021-01-22 22:56:31 +02004156}
4157
4158static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02004159skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
Ville Syrjälä53630962021-01-22 22:56:31 +02004160{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004161 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4162 unsigned int weight_total, weight_start, weight_end;
Ville Syrjälä53630962021-01-22 22:56:31 +02004163 const struct intel_dbuf_state *old_dbuf_state =
4164 intel_atomic_get_old_dbuf_state(state);
4165 struct intel_dbuf_state *new_dbuf_state =
4166 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004167 struct intel_crtc_state *crtc_state;
Ville Syrjälä53630962021-01-22 22:56:31 +02004168 struct skl_ddb_entry ddb_slices;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004169 enum pipe pipe = crtc->pipe;
Manasi Navaree2bebb92021-06-03 14:53:38 -07004170 unsigned int mbus_offset = 0;
Ville Syrjälä53630962021-01-22 22:56:31 +02004171 u32 ddb_range_size;
4172 u32 dbuf_slice_mask;
4173 u32 start, end;
4174 int ret;
4175
Ville Syrjäläef79d622021-01-22 22:56:32 +02004176 if (new_dbuf_state->weight[pipe] == 0) {
4177 new_dbuf_state->ddb[pipe].start = 0;
4178 new_dbuf_state->ddb[pipe].end = 0;
4179 goto out;
Ville Syrjälä53630962021-01-22 22:56:31 +02004180 }
4181
Ville Syrjäläef79d622021-01-22 22:56:32 +02004182 dbuf_slice_mask = new_dbuf_state->slices[pipe];
Ville Syrjälä53630962021-01-22 22:56:31 +02004183
4184 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
Ville Syrjälä835c1762021-05-18 17:06:16 -07004185 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_slice_mask);
Ville Syrjälä53630962021-01-22 22:56:31 +02004186 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4187
Ville Syrjäläef79d622021-01-22 22:56:32 +02004188 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4189 &weight_start, &weight_end, &weight_total);
Ville Syrjälä53630962021-01-22 22:56:31 +02004190
4191 start = ddb_range_size * weight_start / weight_total;
4192 end = ddb_range_size * weight_end / weight_total;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004193
Ville Syrjälä835c1762021-05-18 17:06:16 -07004194 new_dbuf_state->ddb[pipe].start = ddb_slices.start - mbus_offset + start;
4195 new_dbuf_state->ddb[pipe].end = ddb_slices.start - mbus_offset + end;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004196out:
Ville Syrjälä835c1762021-05-18 17:06:16 -07004197 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
4198 skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
Ville Syrjäläef79d622021-01-22 22:56:32 +02004199 &new_dbuf_state->ddb[pipe]))
4200 return 0;
4201
4202 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4203 if (ret)
4204 return ret;
4205
4206 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4207 if (IS_ERR(crtc_state))
4208 return PTR_ERR(crtc_state);
4209
Ville Syrjälä835c1762021-05-18 17:06:16 -07004210 /*
4211 * Used for checking overlaps, so we need absolute
4212 * offsets instead of MBUS relative offsets.
4213 */
4214 crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
4215 crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004216
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004217 drm_dbg_kms(&dev_priv->drm,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004218 "[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 +02004219 crtc->base.base.id, crtc->base.name,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004220 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4221 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4222 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4223 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004224
4225 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004226}
4227
Ville Syrjälädf331de2019-03-19 18:03:11 +02004228static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4229 int width, const struct drm_format_info *format,
4230 u64 modifier, unsigned int rotation,
4231 u32 plane_pixel_rate, struct skl_wm_params *wp,
4232 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004233static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004234 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004235 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004236 const struct skl_wm_params *wp,
4237 const struct skl_wm_level *result_prev,
4238 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004239
Ville Syrjälädf331de2019-03-19 18:03:11 +02004240static unsigned int
4241skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4242 int num_active)
4243{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004244 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004245 int level, max_level = ilk_wm_max_level(dev_priv);
4246 struct skl_wm_level wm = {};
4247 int ret, min_ddb_alloc = 0;
4248 struct skl_wm_params wp;
4249
4250 ret = skl_compute_wm_params(crtc_state, 256,
4251 drm_format_info(DRM_FORMAT_ARGB8888),
4252 DRM_FORMAT_MOD_LINEAR,
4253 DRM_MODE_ROTATE_0,
4254 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304255 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004256
4257 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004258 unsigned int latency = dev_priv->wm.skl_latency[level];
4259
4260 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004261 if (wm.min_ddb_alloc == U16_MAX)
4262 break;
4263
4264 min_ddb_alloc = wm.min_ddb_alloc;
4265 }
4266
4267 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004268}
4269
Mahesh Kumar37cde112018-04-26 19:55:17 +05304270static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4271 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004272{
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004273 entry->start = reg & DDB_ENTRY_MASK;
4274 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304275
Damien Lespiau16160e32014-11-04 17:06:53 +00004276 if (entry->end)
4277 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004278}
4279
Mahesh Kumarddf34312018-04-09 09:11:03 +05304280static void
4281skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4282 const enum pipe pipe,
4283 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004284 struct skl_ddb_entry *ddb_y,
4285 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304286{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004287 u32 val, val2;
4288 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304289
4290 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4291 if (plane_id == PLANE_CURSOR) {
Jani Nikula5f461662020-11-30 13:15:58 +02004292 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004293 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304294 return;
4295 }
4296
Jani Nikula5f461662020-11-30 13:15:58 +02004297 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304298
4299 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004300 if (val & PLANE_CTL_ENABLE)
4301 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4302 val & PLANE_CTL_ORDER_RGBX,
4303 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304304
Matt Roper7dadd282021-03-19 21:42:43 -07004305 if (DISPLAY_VER(dev_priv) >= 11) {
Jani Nikula5f461662020-11-30 13:15:58 +02004306 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004307 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4308 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02004309 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4310 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304311
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004312 if (fourcc &&
4313 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004314 swap(val, val2);
4315
4316 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4317 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304318 }
4319}
4320
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004321void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4322 struct skl_ddb_entry *ddb_y,
4323 struct skl_ddb_entry *ddb_uv)
4324{
4325 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4326 enum intel_display_power_domain power_domain;
4327 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004328 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004329 enum plane_id plane_id;
4330
4331 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004332 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4333 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004334 return;
4335
4336 for_each_plane_id_on_crtc(crtc, plane_id)
4337 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4338 plane_id,
4339 &ddb_y[plane_id],
4340 &ddb_uv[plane_id]);
4341
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004342 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004343}
4344
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004345/*
4346 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4347 * The bspec defines downscale amount as:
4348 *
4349 * """
4350 * Horizontal down scale amount = maximum[1, Horizontal source size /
4351 * Horizontal destination size]
4352 * Vertical down scale amount = maximum[1, Vertical source size /
4353 * Vertical destination size]
4354 * Total down scale amount = Horizontal down scale amount *
4355 * Vertical down scale amount
4356 * """
4357 *
4358 * Return value is provided in 16.16 fixed point form to retain fractional part.
4359 * Caller should take care of dividing & rounding off the value.
4360 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304361static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004362skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4363 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004364{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304365 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004366 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304367 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4368 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004369
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304370 if (drm_WARN_ON(&dev_priv->drm,
4371 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304372 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004373
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004374 /*
4375 * Src coordinates are already rotated by 270 degrees for
4376 * the 90/270 degree plane rotation cases (to match the
4377 * GTT mapping), hence no need to account for rotation here.
4378 *
4379 * n.b., src is 16.16 fixed point, dst is whole integer.
4380 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004381 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4382 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4383 dst_w = drm_rect_width(&plane_state->uapi.dst);
4384 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004385
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304386 fp_w_ratio = div_fixed16(src_w, dst_w);
4387 fp_h_ratio = div_fixed16(src_h, dst_h);
4388 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4389 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004390
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304391 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004392}
4393
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004394struct dbuf_slice_conf_entry {
4395 u8 active_pipes;
4396 u8 dbuf_mask[I915_MAX_PIPES];
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004397 bool join_mbus;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004398};
4399
4400/*
4401 * Table taken from Bspec 12716
4402 * Pipes do have some preferred DBuf slice affinity,
4403 * plus there are some hardcoded requirements on how
4404 * those should be distributed for multipipe scenarios.
4405 * For more DBuf slices algorithm can get even more messy
4406 * and less readable, so decided to use a table almost
4407 * as is from BSpec itself - that way it is at least easier
4408 * to compare, change and check.
4409 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004410static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004411/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4412{
4413 {
4414 .active_pipes = BIT(PIPE_A),
4415 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004416 [PIPE_A] = BIT(DBUF_S1),
4417 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004418 },
4419 {
4420 .active_pipes = BIT(PIPE_B),
4421 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004422 [PIPE_B] = BIT(DBUF_S1),
4423 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004424 },
4425 {
4426 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4427 .dbuf_mask = {
4428 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004429 [PIPE_B] = BIT(DBUF_S2),
4430 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004431 },
4432 {
4433 .active_pipes = BIT(PIPE_C),
4434 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004435 [PIPE_C] = BIT(DBUF_S2),
4436 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004437 },
4438 {
4439 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4440 .dbuf_mask = {
4441 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004442 [PIPE_C] = BIT(DBUF_S2),
4443 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004444 },
4445 {
4446 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4447 .dbuf_mask = {
4448 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004449 [PIPE_C] = BIT(DBUF_S2),
4450 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004451 },
4452 {
4453 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4454 .dbuf_mask = {
4455 [PIPE_A] = BIT(DBUF_S1),
4456 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004457 [PIPE_C] = BIT(DBUF_S2),
4458 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004459 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004460 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004461};
4462
4463/*
4464 * Table taken from Bspec 49255
4465 * Pipes do have some preferred DBuf slice affinity,
4466 * plus there are some hardcoded requirements on how
4467 * those should be distributed for multipipe scenarios.
4468 * For more DBuf slices algorithm can get even more messy
4469 * and less readable, so decided to use a table almost
4470 * as is from BSpec itself - that way it is at least easier
4471 * to compare, change and check.
4472 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004473static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004474/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4475{
4476 {
4477 .active_pipes = BIT(PIPE_A),
4478 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004479 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4480 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004481 },
4482 {
4483 .active_pipes = BIT(PIPE_B),
4484 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004485 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4486 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004487 },
4488 {
4489 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4490 .dbuf_mask = {
4491 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004492 [PIPE_B] = BIT(DBUF_S1),
4493 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004494 },
4495 {
4496 .active_pipes = BIT(PIPE_C),
4497 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004498 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4499 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004500 },
4501 {
4502 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4503 .dbuf_mask = {
4504 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004505 [PIPE_C] = BIT(DBUF_S2),
4506 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004507 },
4508 {
4509 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4510 .dbuf_mask = {
4511 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004512 [PIPE_C] = BIT(DBUF_S2),
4513 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004514 },
4515 {
4516 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4517 .dbuf_mask = {
4518 [PIPE_A] = BIT(DBUF_S1),
4519 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004520 [PIPE_C] = BIT(DBUF_S2),
4521 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004522 },
4523 {
4524 .active_pipes = BIT(PIPE_D),
4525 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004526 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4527 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004528 },
4529 {
4530 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4531 .dbuf_mask = {
4532 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004533 [PIPE_D] = BIT(DBUF_S2),
4534 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004535 },
4536 {
4537 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4538 .dbuf_mask = {
4539 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004540 [PIPE_D] = BIT(DBUF_S2),
4541 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004542 },
4543 {
4544 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4545 .dbuf_mask = {
4546 [PIPE_A] = BIT(DBUF_S1),
4547 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004548 [PIPE_D] = BIT(DBUF_S2),
4549 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004550 },
4551 {
4552 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4553 .dbuf_mask = {
4554 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004555 [PIPE_D] = BIT(DBUF_S2),
4556 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004557 },
4558 {
4559 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4560 .dbuf_mask = {
4561 [PIPE_A] = BIT(DBUF_S1),
4562 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004563 [PIPE_D] = BIT(DBUF_S2),
4564 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004565 },
4566 {
4567 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4568 .dbuf_mask = {
4569 [PIPE_B] = BIT(DBUF_S1),
4570 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004571 [PIPE_D] = BIT(DBUF_S2),
4572 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004573 },
4574 {
4575 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4576 .dbuf_mask = {
4577 [PIPE_A] = BIT(DBUF_S1),
4578 [PIPE_B] = BIT(DBUF_S1),
4579 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004580 [PIPE_D] = BIT(DBUF_S2),
4581 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004582 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004583 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004584};
4585
Matt Roper49f75632021-07-21 15:30:40 -07004586static const struct dbuf_slice_conf_entry dg2_allowed_dbufs[] = {
4587 {
4588 .active_pipes = BIT(PIPE_A),
4589 .dbuf_mask = {
4590 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4591 },
4592 },
4593 {
4594 .active_pipes = BIT(PIPE_B),
4595 .dbuf_mask = {
4596 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4597 },
4598 },
4599 {
4600 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4601 .dbuf_mask = {
4602 [PIPE_A] = BIT(DBUF_S1),
4603 [PIPE_B] = BIT(DBUF_S2),
4604 },
4605 },
4606 {
4607 .active_pipes = BIT(PIPE_C),
4608 .dbuf_mask = {
4609 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4610 },
4611 },
4612 {
4613 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4614 .dbuf_mask = {
4615 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4616 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4617 },
4618 },
4619 {
4620 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4621 .dbuf_mask = {
4622 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4623 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4624 },
4625 },
4626 {
4627 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4628 .dbuf_mask = {
4629 [PIPE_A] = BIT(DBUF_S1),
4630 [PIPE_B] = BIT(DBUF_S2),
4631 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4632 },
4633 },
4634 {
4635 .active_pipes = BIT(PIPE_D),
4636 .dbuf_mask = {
4637 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4638 },
4639 },
4640 {
4641 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4642 .dbuf_mask = {
4643 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4644 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4645 },
4646 },
4647 {
4648 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4649 .dbuf_mask = {
4650 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4651 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4652 },
4653 },
4654 {
4655 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4656 .dbuf_mask = {
4657 [PIPE_A] = BIT(DBUF_S1),
4658 [PIPE_B] = BIT(DBUF_S2),
4659 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4660 },
4661 },
4662 {
4663 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4664 .dbuf_mask = {
4665 [PIPE_C] = BIT(DBUF_S3),
4666 [PIPE_D] = BIT(DBUF_S4),
4667 },
4668 },
4669 {
4670 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4671 .dbuf_mask = {
4672 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4673 [PIPE_C] = BIT(DBUF_S3),
4674 [PIPE_D] = BIT(DBUF_S4),
4675 },
4676 },
4677 {
4678 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4679 .dbuf_mask = {
4680 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4681 [PIPE_C] = BIT(DBUF_S3),
4682 [PIPE_D] = BIT(DBUF_S4),
4683 },
4684 },
4685 {
4686 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4687 .dbuf_mask = {
4688 [PIPE_A] = BIT(DBUF_S1),
4689 [PIPE_B] = BIT(DBUF_S2),
4690 [PIPE_C] = BIT(DBUF_S3),
4691 [PIPE_D] = BIT(DBUF_S4),
4692 },
4693 },
4694 {}
4695};
4696
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004697static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
4698 {
4699 .active_pipes = BIT(PIPE_A),
4700 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004701 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004702 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004703 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004704 },
4705 {
4706 .active_pipes = BIT(PIPE_B),
4707 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004708 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004709 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004710 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004711 },
4712 {
4713 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4714 .dbuf_mask = {
4715 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4716 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4717 },
4718 },
4719 {
4720 .active_pipes = BIT(PIPE_C),
4721 .dbuf_mask = {
4722 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4723 },
4724 },
4725 {
4726 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4727 .dbuf_mask = {
4728 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4729 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4730 },
4731 },
4732 {
4733 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4734 .dbuf_mask = {
4735 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4736 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4737 },
4738 },
4739 {
4740 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4741 .dbuf_mask = {
4742 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4743 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4744 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4745 },
4746 },
4747 {
4748 .active_pipes = BIT(PIPE_D),
4749 .dbuf_mask = {
4750 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4751 },
4752 },
4753 {
4754 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4755 .dbuf_mask = {
4756 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4757 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4758 },
4759 },
4760 {
4761 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4762 .dbuf_mask = {
4763 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4764 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4765 },
4766 },
4767 {
4768 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4769 .dbuf_mask = {
4770 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4771 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4772 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4773 },
4774 },
4775 {
4776 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4777 .dbuf_mask = {
4778 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4779 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4780 },
4781 },
4782 {
4783 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4784 .dbuf_mask = {
4785 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4786 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4787 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4788 },
4789 },
4790 {
4791 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4792 .dbuf_mask = {
4793 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4794 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4795 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4796 },
4797 },
4798 {
4799 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4800 .dbuf_mask = {
4801 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4802 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4803 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4804 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4805 },
4806 },
4807 {}
4808
4809};
4810
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004811static bool check_mbus_joined(u8 active_pipes,
4812 const struct dbuf_slice_conf_entry *dbuf_slices)
4813{
4814 int i;
4815
4816 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
4817 if (dbuf_slices[i].active_pipes == active_pipes)
4818 return dbuf_slices[i].join_mbus;
4819 }
4820 return false;
4821}
4822
4823static bool adlp_check_mbus_joined(u8 active_pipes)
4824{
4825 return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
4826}
4827
Ville Syrjälä05e81552020-02-25 19:11:09 +02004828static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4829 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004830{
4831 int i;
4832
Ville Syrjälä05e81552020-02-25 19:11:09 +02004833 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004834 if (dbuf_slices[i].active_pipes == active_pipes)
4835 return dbuf_slices[i].dbuf_mask[pipe];
4836 }
4837 return 0;
4838}
4839
4840/*
4841 * This function finds an entry with same enabled pipe configuration and
4842 * returns correspondent DBuf slice mask as stated in BSpec for particular
4843 * platform.
4844 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004845static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004846{
4847 /*
4848 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4849 * required calculating "pipe ratio" in order to determine
4850 * if one or two slices can be used for single pipe configurations
4851 * as additional constraint to the existing table.
4852 * However based on recent info, it should be not "pipe ratio"
4853 * but rather ratio between pixel_rate and cdclk with additional
4854 * constants, so for now we are using only table until this is
4855 * clarified. Also this is the reason why crtc_state param is
4856 * still here - we will need it once those additional constraints
4857 * pop up.
4858 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004859 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004860}
4861
Ville Syrjälä05e81552020-02-25 19:11:09 +02004862static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004863{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004864 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004865}
4866
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004867static u32 adlp_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4868{
4869 return compute_dbuf_slices(pipe, active_pipes, adlp_allowed_dbufs);
4870}
4871
Matt Roper49f75632021-07-21 15:30:40 -07004872static u32 dg2_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4873{
4874 return compute_dbuf_slices(pipe, active_pipes, dg2_allowed_dbufs);
4875}
4876
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004877static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004878{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004879 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4880 enum pipe pipe = crtc->pipe;
4881
Matt Roper49f75632021-07-21 15:30:40 -07004882 if (IS_DG2(dev_priv))
4883 return dg2_compute_dbuf_slices(pipe, active_pipes);
4884 else if (IS_ALDERLAKE_P(dev_priv))
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004885 return adlp_compute_dbuf_slices(pipe, active_pipes);
4886 else if (DISPLAY_VER(dev_priv) == 12)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004887 return tgl_compute_dbuf_slices(pipe, active_pipes);
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004888 else if (DISPLAY_VER(dev_priv) == 11)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004889 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004890 /*
4891 * For anything else just return one slice yet.
4892 * Should be extended for other platforms.
4893 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004894 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004895}
4896
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004897static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004898skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4899 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004900 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004901{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004902 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004903 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004904 u32 data_rate;
4905 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304906 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004907 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004908
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004909 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004910 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004911
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004912 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004913 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004914
4915 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004916 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004917 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004918
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004919 /*
4920 * Src coordinates are already rotated by 270 degrees for
4921 * the 90/270 degree plane rotation cases (to match the
4922 * GTT mapping), hence no need to account for rotation here.
4923 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004924 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4925 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004926
Mahesh Kumarb879d582018-04-09 09:11:01 +05304927 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004928 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304929 width /= 2;
4930 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004931 }
4932
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004933 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304934
Maarten Lankhorstec193642019-06-28 10:55:17 +02004935 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004936
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004937 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4938
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004939 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004940 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004941}
4942
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004943static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004944skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4945 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004946{
Ville Syrjäläab016302020-11-06 19:30:41 +02004947 struct intel_crtc_state *crtc_state =
4948 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004949 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004950 struct intel_plane *plane;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004951 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004952 enum plane_id plane_id;
4953 int i;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004954
Matt Ropera1de91e2016-05-12 07:05:57 -07004955 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004956 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4957 if (plane->pipe != crtc->pipe)
4958 continue;
4959
4960 plane_id = plane->id;
Matt Roper024c9042015-09-24 15:53:11 -07004961
Mahesh Kumarb879d582018-04-09 09:11:01 +05304962 /* packed/y */
Ville Syrjäläab016302020-11-06 19:30:41 +02004963 crtc_state->plane_data_rate[plane_id] =
4964 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Matt Roper9c74d822016-05-12 07:05:58 -07004965
Mahesh Kumarb879d582018-04-09 09:11:01 +05304966 /* uv-plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004967 crtc_state->uv_plane_data_rate[plane_id] =
4968 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4969 }
4970
4971 for_each_plane_id_on_crtc(crtc, plane_id) {
4972 total_data_rate += crtc_state->plane_data_rate[plane_id];
4973 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004974 }
4975
4976 return total_data_rate;
4977}
4978
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004979static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004980icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4981 struct intel_crtc *crtc)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004982{
Ville Syrjäläab016302020-11-06 19:30:41 +02004983 struct intel_crtc_state *crtc_state =
4984 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004985 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004986 struct intel_plane *plane;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004987 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004988 enum plane_id plane_id;
4989 int i;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004990
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004991 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004992 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4993 if (plane->pipe != crtc->pipe)
4994 continue;
4995
4996 plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004997
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004998 if (!plane_state->planar_linked_plane) {
Ville Syrjäläab016302020-11-06 19:30:41 +02004999 crtc_state->plane_data_rate[plane_id] =
5000 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005001 } else {
5002 enum plane_id y_plane_id;
5003
5004 /*
5005 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005006 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005007 * and needs the master plane state which may be
5008 * NULL if we try get_new_plane_state(), so we
5009 * always calculate from the master.
5010 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005011 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005012 continue;
5013
5014 /* Y plane rate is calculated on the slave */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005015 y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjäläab016302020-11-06 19:30:41 +02005016 crtc_state->plane_data_rate[y_plane_id] =
5017 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005018
Ville Syrjäläab016302020-11-06 19:30:41 +02005019 crtc_state->plane_data_rate[plane_id] =
5020 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005021 }
5022 }
5023
Ville Syrjäläab016302020-11-06 19:30:41 +02005024 for_each_plane_id_on_crtc(crtc, plane_id)
5025 total_data_rate += crtc_state->plane_data_rate[plane_id];
5026
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005027 return total_data_rate;
5028}
5029
Ville Syrjälä5516e892021-02-26 17:32:03 +02005030const struct skl_wm_level *
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005031skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005032 enum plane_id plane_id,
5033 int level)
5034{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005035 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5036
5037 if (level == 0 && pipe_wm->use_sagv_wm)
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005038 return &wm->sagv.wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005039
5040 return &wm->wm[level];
5041}
5042
Ville Syrjälä5516e892021-02-26 17:32:03 +02005043const struct skl_wm_level *
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005044skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
5045 enum plane_id plane_id)
5046{
5047 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5048
5049 if (pipe_wm->use_sagv_wm)
5050 return &wm->sagv.trans_wm;
5051
5052 return &wm->trans_wm;
5053}
5054
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005055/*
5056 * We only disable the watermarks for each plane if
5057 * they exceed the ddb allocation of said plane. This
5058 * is done so that we don't end up touching cursor
5059 * watermarks needlessly when some other plane reduces
5060 * our max possible watermark level.
5061 *
5062 * Bspec has this to say about the PLANE_WM enable bit:
5063 * "All the watermarks at this level for all enabled
5064 * planes must be enabled before the level will be used."
5065 * So this is actually safe to do.
5066 */
5067static void
5068skl_check_wm_level(struct skl_wm_level *wm, u64 total)
5069{
5070 if (wm->min_ddb_alloc > total)
5071 memset(wm, 0, sizeof(*wm));
5072}
5073
5074static void
5075skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
5076 u64 total, u64 uv_total)
5077{
5078 if (wm->min_ddb_alloc > total ||
5079 uv_wm->min_ddb_alloc > uv_total) {
5080 memset(wm, 0, sizeof(*wm));
5081 memset(uv_wm, 0, sizeof(*uv_wm));
5082 }
5083}
5084
Matt Roperc107acf2016-05-12 07:06:01 -07005085static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02005086skl_allocate_plane_ddb(struct intel_atomic_state *state,
5087 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00005088{
Ville Syrjäläef79d622021-01-22 22:56:32 +02005089 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläffc90032020-11-06 19:30:37 +02005090 struct intel_crtc_state *crtc_state =
5091 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005092 const struct intel_dbuf_state *dbuf_state =
Ville Syrjälä47a14952021-01-22 22:56:30 +02005093 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005094 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
5095 int num_active = hweight8(dbuf_state->active_pipes);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005096 u16 alloc_size, start = 0;
5097 u16 total[I915_MAX_PLANES] = {};
5098 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02005099 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005100 enum plane_id plane_id;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005101 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08005102 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005103
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005104 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005105 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
5106 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005107
Ville Syrjäläef79d622021-01-22 22:56:32 +02005108 if (!crtc_state->hw.active)
Matt Roperc107acf2016-05-12 07:06:01 -07005109 return 0;
Matt Roperc107acf2016-05-12 07:06:01 -07005110
Matt Roper7dadd282021-03-19 21:42:43 -07005111 if (DISPLAY_VER(dev_priv) >= 11)
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005112 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005113 icl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005114 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005115 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005116 skl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005117
Damien Lespiau34bb56a2014-11-04 17:07:01 +00005118 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05305119 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005120 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005121
Matt Roperd8e87492018-12-11 09:31:07 -08005122 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005123 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08005124 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005125 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08005126 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005127 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005128
Matt Ropera1de91e2016-05-12 07:05:57 -07005129 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005130 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005131
Matt Roperd8e87492018-12-11 09:31:07 -08005132 /*
5133 * Find the highest watermark level for which we can satisfy the block
5134 * requirement of active planes.
5135 */
5136 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08005137 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005138 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005139 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005140 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005141
5142 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05305143 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305144 drm_WARN_ON(&dev_priv->drm,
5145 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005146 blocks = U32_MAX;
5147 break;
5148 }
5149 continue;
5150 }
5151
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005152 blocks += wm->wm[level].min_ddb_alloc;
5153 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08005154 }
5155
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02005156 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08005157 alloc_size -= blocks;
5158 break;
5159 }
5160 }
5161
5162 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005163 drm_dbg_kms(&dev_priv->drm,
5164 "Requested display configuration exceeds system DDB limitations");
5165 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
5166 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08005167 return -EINVAL;
5168 }
5169
5170 /*
5171 * Grant each plane the blocks it requires at the highest achievable
5172 * watermark level, plus an extra share of the leftover blocks
5173 * proportional to its relative data rate.
5174 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005175 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005176 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005177 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005178 u64 rate;
5179 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005180
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005181 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005182 continue;
5183
Damien Lespiaub9cec072014-11-04 17:06:43 +00005184 /*
Matt Roperd8e87492018-12-11 09:31:07 -08005185 * We've accounted for all active planes; remaining planes are
5186 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00005187 */
Matt Roperd8e87492018-12-11 09:31:07 -08005188 if (total_data_rate == 0)
5189 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005190
Ville Syrjäläab016302020-11-06 19:30:41 +02005191 rate = crtc_state->plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005192 extra = min_t(u16, alloc_size,
5193 DIV64_U64_ROUND_UP(alloc_size * rate,
5194 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005195 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005196 alloc_size -= extra;
5197 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005198
Matt Roperd8e87492018-12-11 09:31:07 -08005199 if (total_data_rate == 0)
5200 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07005201
Ville Syrjäläab016302020-11-06 19:30:41 +02005202 rate = crtc_state->uv_plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005203 extra = min_t(u16, alloc_size,
5204 DIV64_U64_ROUND_UP(alloc_size * rate,
5205 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005206 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005207 alloc_size -= extra;
5208 total_data_rate -= rate;
5209 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305210 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08005211
5212 /* Set the actual DDB start/end points for each plane */
5213 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005214 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005215 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005216 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005217 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005218 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005219
5220 if (plane_id == PLANE_CURSOR)
5221 continue;
5222
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005223 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305224 drm_WARN_ON(&dev_priv->drm,
Matt Roper7dadd282021-03-19 21:42:43 -07005225 DISPLAY_VER(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005226
Matt Roperd8e87492018-12-11 09:31:07 -08005227 /* Leave disabled planes at (0,0) */
5228 if (total[plane_id]) {
5229 plane_alloc->start = start;
5230 start += total[plane_id];
5231 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07005232 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005233
Matt Roperd8e87492018-12-11 09:31:07 -08005234 if (uv_total[plane_id]) {
5235 uv_plane_alloc->start = start;
5236 start += uv_total[plane_id];
5237 uv_plane_alloc->end = start;
5238 }
5239 }
5240
5241 /*
5242 * When we calculated watermark values we didn't know how high
5243 * of a level we'd actually be able to hit, so we just marked
5244 * all levels as "enabled." Go back now and disable the ones
5245 * that aren't actually possible.
5246 */
5247 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005248 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005249 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005250 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02005251
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005252 skl_check_nv12_wm_level(&wm->wm[level], &wm->uv_wm[level],
5253 total[plane_id], uv_total[plane_id]);
Ville Syrjälä290248c2019-02-13 18:54:24 +02005254
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005255 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005256 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005257 * Underruns with WM1+ disabled
5258 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005259 if (DISPLAY_VER(dev_priv) == 11 &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005260 level == 1 && wm->wm[0].enable) {
5261 wm->wm[level].blocks = wm->wm[0].blocks;
5262 wm->wm[level].lines = wm->wm[0].lines;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005263 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005264 }
Matt Roperd8e87492018-12-11 09:31:07 -08005265 }
5266 }
5267
5268 /*
Ville Syrjälädf4a50a2021-02-26 17:31:59 +02005269 * Go back and disable the transition and SAGV watermarks
5270 * if it turns out we don't have enough DDB blocks for them.
Matt Roperd8e87492018-12-11 09:31:07 -08005271 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005272 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005273 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005274 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005275
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005276 skl_check_wm_level(&wm->trans_wm, total[plane_id]);
5277 skl_check_wm_level(&wm->sagv.wm0, total[plane_id]);
5278 skl_check_wm_level(&wm->sagv.trans_wm, total[plane_id]);
Damien Lespiaub9cec072014-11-04 17:06:43 +00005279 }
5280
Matt Roperc107acf2016-05-12 07:06:01 -07005281 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005282}
5283
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005284/*
5285 * 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 +02005286 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005287 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5288 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5289*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005290static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005291skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5292 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005293{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005294 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305295 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005296
5297 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305298 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005299
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305300 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005301 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005302
Matt Roper2b5a4562021-03-22 16:38:40 -07005303 if (DISPLAY_VER(dev_priv) >= 10)
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005304 ret = add_fixed16_u32(ret, 1);
5305
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005306 return ret;
5307}
5308
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005309static uint_fixed_16_16_t
5310skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5311 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005312{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005313 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305314 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005315
5316 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305317 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005318
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005319 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305320 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5321 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305322 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005323 return ret;
5324}
5325
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305326static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005327intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305328{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305329 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005330 u32 pixel_rate;
5331 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305332 uint_fixed_16_16_t linetime_us;
5333
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005334 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305335 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305336
Maarten Lankhorstec193642019-06-28 10:55:17 +02005337 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305338
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305339 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305340 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305341
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005342 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305343 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305344
5345 return linetime_us;
5346}
5347
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305348static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005349skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5350 int width, const struct drm_format_info *format,
5351 u64 modifier, unsigned int rotation,
5352 u32 plane_pixel_rate, struct skl_wm_params *wp,
5353 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305354{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005355 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005356 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005357 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305358
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305359 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005360 if (color_plane == 1 &&
5361 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005362 drm_dbg_kms(&dev_priv->drm,
5363 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305364 return -EINVAL;
5365 }
5366
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005367 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5368 modifier == I915_FORMAT_MOD_Yf_TILED ||
5369 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5370 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5371 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5372 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5373 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005374 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305375
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005376 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005377 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305378 wp->width /= 2;
5379
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005380 wp->cpp = format->cpp[color_plane];
5381 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305382
Matt Roper7dadd282021-03-19 21:42:43 -07005383 if (DISPLAY_VER(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005384 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005385 wp->dbuf_block_size = 256;
5386 else
5387 wp->dbuf_block_size = 512;
5388
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005389 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305390 switch (wp->cpp) {
5391 case 1:
5392 wp->y_min_scanlines = 16;
5393 break;
5394 case 2:
5395 wp->y_min_scanlines = 8;
5396 break;
5397 case 4:
5398 wp->y_min_scanlines = 4;
5399 break;
5400 default:
5401 MISSING_CASE(wp->cpp);
5402 return -EINVAL;
5403 }
5404 } else {
5405 wp->y_min_scanlines = 4;
5406 }
5407
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005408 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305409 wp->y_min_scanlines *= 2;
5410
5411 wp->plane_bytes_per_line = wp->width * wp->cpp;
5412 if (wp->y_tiled) {
5413 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005414 wp->y_min_scanlines,
5415 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305416
Matt Roper2b5a4562021-03-22 16:38:40 -07005417 if (DISPLAY_VER(dev_priv) >= 10)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305418 interm_pbpl++;
5419
5420 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5421 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305422 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005423 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005424 wp->dbuf_block_size);
5425
Matt Roper2b5a4562021-03-22 16:38:40 -07005426 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005427 interm_pbpl++;
5428
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305429 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5430 }
5431
5432 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5433 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005434
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305435 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005436 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305437
5438 return 0;
5439}
5440
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005441static int
5442skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5443 const struct intel_plane_state *plane_state,
5444 struct skl_wm_params *wp, int color_plane)
5445{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005446 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005447 int width;
5448
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005449 /*
5450 * Src coordinates are already rotated by 270 degrees for
5451 * the 90/270 degree plane rotation cases (to match the
5452 * GTT mapping), hence no need to account for rotation here.
5453 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005454 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005455
5456 return skl_compute_wm_params(crtc_state, width,
5457 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005458 plane_state->hw.rotation,
Ville Syrjälä3df3fe22020-11-06 19:30:42 +02005459 intel_plane_pixel_rate(crtc_state, plane_state),
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005460 wp, color_plane);
5461}
5462
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005463static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5464{
Matt Roper2b5a4562021-03-22 16:38:40 -07005465 if (DISPLAY_VER(dev_priv) >= 10)
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005466 return true;
5467
5468 /* The number of lines are ignored for the level 0 watermark. */
5469 return level > 0;
5470}
5471
Matt Roper1003cee2021-05-14 08:36:54 -07005472static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5473{
5474 if (DISPLAY_VER(dev_priv) >= 13)
5475 return 255;
5476 else
5477 return 31;
5478}
5479
Maarten Lankhorstec193642019-06-28 10:55:17 +02005480static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005481 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005482 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005483 const struct skl_wm_params *wp,
5484 const struct skl_wm_level *result_prev,
5485 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005486{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005487 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305488 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305489 uint_fixed_16_16_t selected_result;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005490 u32 blocks, lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005491
Ville Syrjälä0aded172019-02-05 17:50:53 +02005492 if (latency == 0) {
5493 /* reject it */
5494 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005495 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005496 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005497
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005498 /*
5499 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5500 * Display WA #1141: kbl,cfl
5501 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005502 if ((IS_KABYLAKE(dev_priv) ||
5503 IS_COFFEELAKE(dev_priv) ||
5504 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005505 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305506 latency += 4;
5507
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005508 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005509 latency += 15;
5510
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305511 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005512 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305513 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005514 crtc_state->hw.pipe_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005515 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305516 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005517
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305518 if (wp->y_tiled) {
5519 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005520 } else {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005521 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005522 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005523 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005524 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005525 } else if (latency >= wp->linetime_us) {
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005526 if (DISPLAY_VER(dev_priv) == 9)
Paulo Zanoni077b5822018-10-04 16:15:57 -07005527 selected_result = min_fixed16(method1, method2);
5528 else
5529 selected_result = method2;
5530 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005531 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005532 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005533 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005534
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005535 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5536 lines = div_round_up_fixed16(selected_result,
5537 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005538
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005539 if (DISPLAY_VER(dev_priv) == 9) {
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005540 /* Display WA #1125: skl,bxt,kbl */
5541 if (level == 0 && wp->rc_surface)
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005542 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005543
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005544 /* Display WA #1126: skl,bxt,kbl */
5545 if (level >= 1 && level <= 7) {
5546 if (wp->y_tiled) {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005547 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5548 lines += wp->y_min_scanlines;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005549 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005550 blocks++;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005551 }
5552
5553 /*
5554 * Make sure result blocks for higher latency levels are
5555 * atleast as high as level below the current level.
5556 * Assumption in DDB algorithm optimization for special
5557 * cases. Also covers Display WA #1125 for RC.
5558 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005559 if (result_prev->blocks > blocks)
5560 blocks = result_prev->blocks;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005561 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005562 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005563
Matt Roper7dadd282021-03-19 21:42:43 -07005564 if (DISPLAY_VER(dev_priv) >= 11) {
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005565 if (wp->y_tiled) {
5566 int extra_lines;
5567
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005568 if (lines % wp->y_min_scanlines == 0)
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005569 extra_lines = wp->y_min_scanlines;
5570 else
5571 extra_lines = wp->y_min_scanlines * 2 -
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005572 lines % wp->y_min_scanlines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005573
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005574 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005575 wp->plane_blocks_per_line);
5576 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005577 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005578 }
5579 }
5580
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005581 if (!skl_wm_has_lines(dev_priv, level))
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005582 lines = 0;
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005583
Matt Roper1003cee2021-05-14 08:36:54 -07005584 if (lines > skl_wm_max_lines(dev_priv)) {
Ville Syrjälä0aded172019-02-05 17:50:53 +02005585 /* reject it */
5586 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005587 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005588 }
Matt Roperd8e87492018-12-11 09:31:07 -08005589
5590 /*
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005591 * If lines is valid, assume we can use this watermark level
Matt Roperd8e87492018-12-11 09:31:07 -08005592 * for now. We'll come back and disable it after we calculate the
5593 * DDB allocation if it turns out we don't actually have enough
5594 * blocks to satisfy it.
5595 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005596 result->blocks = blocks;
5597 result->lines = lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005598 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005599 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5600 result->enable = true;
Ville Syrjälä9c312122020-11-06 19:30:40 +02005601
Matt Roper7dadd282021-03-19 21:42:43 -07005602 if (DISPLAY_VER(dev_priv) < 12)
Ville Syrjälä9c312122020-11-06 19:30:40 +02005603 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005604}
5605
Matt Roperd8e87492018-12-11 09:31:07 -08005606static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005607skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305608 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005609 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005610{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005611 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305612 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005613 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005614
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305615 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005616 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005617 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305618
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005619 skl_compute_plane_wm(crtc_state, level, latency,
5620 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005621
5622 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305623 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005624}
5625
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005626static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5627 const struct skl_wm_params *wm_params,
5628 struct skl_plane_wm *plane_wm)
5629{
5630 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005631 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005632 struct skl_wm_level *levels = plane_wm->wm;
5633 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5634
5635 skl_compute_plane_wm(crtc_state, 0, latency,
5636 wm_params, &levels[0],
5637 sagv_wm);
5638}
5639
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005640static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5641 struct skl_wm_level *trans_wm,
5642 const struct skl_wm_level *wm0,
5643 const struct skl_wm_params *wp)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005644{
Ville Syrjäläc834d032020-02-28 22:35:52 +02005645 u16 trans_min, trans_amount, trans_y_tile_min;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005646 u16 wm0_blocks, trans_offset, blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005647
Kumar, Maheshca476672017-08-17 19:15:24 +05305648 /* Transition WM don't make any sense if ipc is disabled */
5649 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005650 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305651
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005652 /*
5653 * WaDisableTWM:skl,kbl,cfl,bxt
5654 * Transition WM are not recommended by HW team for GEN9
5655 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005656 if (DISPLAY_VER(dev_priv) == 9)
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005657 return;
5658
Matt Roper7dadd282021-03-19 21:42:43 -07005659 if (DISPLAY_VER(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305660 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005661 else
5662 trans_min = 14;
5663
5664 /* Display WA #1140: glk,cnl */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005665 if (DISPLAY_VER(dev_priv) == 10)
Ville Syrjäläc834d032020-02-28 22:35:52 +02005666 trans_amount = 0;
5667 else
5668 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305669
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005670 trans_offset = trans_min + trans_amount;
Kumar, Maheshca476672017-08-17 19:15:24 +05305671
Paulo Zanonicbacc792018-10-04 16:15:58 -07005672 /*
5673 * The spec asks for Selected Result Blocks for wm0 (the real value),
5674 * not Result Blocks (the integer value). Pay attention to the capital
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005675 * letters. The value wm_l0->blocks is actually Result Blocks, but
Paulo Zanonicbacc792018-10-04 16:15:58 -07005676 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5677 * and since we later will have to get the ceiling of the sum in the
5678 * transition watermarks calculation, we can just pretend Selected
5679 * Result Blocks is Result Blocks minus 1 and it should work for the
5680 * current platforms.
5681 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005682 wm0_blocks = wm0->blocks - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005683
Kumar, Maheshca476672017-08-17 19:15:24 +05305684 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005685 trans_y_tile_min =
5686 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005687 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305688 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005689 blocks = wm0_blocks + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305690 }
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005691 blocks++;
Kumar, Maheshca476672017-08-17 19:15:24 +05305692
Matt Roperd8e87492018-12-11 09:31:07 -08005693 /*
5694 * Just assume we can enable the transition watermark. After
5695 * computing the DDB we'll come back and disable it if that
5696 * assumption turns out to be false.
5697 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005698 trans_wm->blocks = blocks;
5699 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5700 trans_wm->enable = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005701}
5702
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005703static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005704 const struct intel_plane_state *plane_state,
5705 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005706{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005707 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5708 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälädbf71382020-11-06 19:30:38 +02005709 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005710 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005711 int ret;
5712
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005713 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005714 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005715 if (ret)
5716 return ret;
5717
Ville Syrjälä67155a62019-03-12 22:58:37 +02005718 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005719
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005720 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5721 &wm->wm[0], &wm_params);
5722
Matt Roper7dadd282021-03-19 21:42:43 -07005723 if (DISPLAY_VER(dev_priv) >= 12) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005724 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5725
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005726 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5727 &wm->sagv.wm0, &wm_params);
5728 }
Ville Syrjälä83158472018-11-27 18:57:26 +02005729
5730 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005731}
5732
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005733static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005734 const struct intel_plane_state *plane_state,
5735 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005736{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005737 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005738 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005739 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005740
Ville Syrjälä83158472018-11-27 18:57:26 +02005741 wm->is_planar = true;
5742
5743 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005744 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005745 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005746 if (ret)
5747 return ret;
5748
Ville Syrjälä67155a62019-03-12 22:58:37 +02005749 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005750
5751 return 0;
5752}
5753
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005754static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005755 const struct intel_plane_state *plane_state)
5756{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005757 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä83158472018-11-27 18:57:26 +02005758 enum plane_id plane_id = plane->id;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005759 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5760 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005761 int ret;
5762
Ville Syrjälädbf71382020-11-06 19:30:38 +02005763 memset(wm, 0, sizeof(*wm));
5764
Ville Syrjälä83158472018-11-27 18:57:26 +02005765 if (!intel_wm_plane_visible(crtc_state, plane_state))
5766 return 0;
5767
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005768 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005769 plane_id, 0);
5770 if (ret)
5771 return ret;
5772
5773 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005774 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005775 plane_id);
5776 if (ret)
5777 return ret;
5778 }
5779
5780 return 0;
5781}
5782
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005783static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005784 const struct intel_plane_state *plane_state)
5785{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005786 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5787 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5788 enum plane_id plane_id = plane->id;
5789 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005790 int ret;
5791
5792 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005793 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005794 return 0;
5795
Ville Syrjäläf99b8052021-03-27 02:59:45 +02005796 memset(wm, 0, sizeof(*wm));
5797
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005798 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005799 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005800 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005801
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305802 drm_WARN_ON(&dev_priv->drm,
5803 !intel_wm_plane_visible(crtc_state, plane_state));
5804 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5805 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005806
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005807 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005808 y_plane_id, 0);
5809 if (ret)
5810 return ret;
5811
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005812 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005813 plane_id, 1);
5814 if (ret)
5815 return ret;
5816 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005817 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005818 plane_id, 0);
5819 if (ret)
5820 return ret;
5821 }
5822
5823 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005824}
5825
Ville Syrjäläffc90032020-11-06 19:30:37 +02005826static int skl_build_pipe_wm(struct intel_atomic_state *state,
5827 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005828{
Ville Syrjäläffc90032020-11-06 19:30:37 +02005829 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5830 struct intel_crtc_state *crtc_state =
5831 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005832 const struct intel_plane_state *plane_state;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005833 struct intel_plane *plane;
5834 int ret, i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005835
Ville Syrjälädbf71382020-11-06 19:30:38 +02005836 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5837 /*
5838 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5839 * instead but we don't populate that correctly for NV12 Y
5840 * planes so for now hack this.
5841 */
5842 if (plane->pipe != crtc->pipe)
5843 continue;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305844
Matt Roper7dadd282021-03-19 21:42:43 -07005845 if (DISPLAY_VER(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005846 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005847 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005848 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305849 if (ret)
5850 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005851 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305852
Ville Syrjälädbf71382020-11-06 19:30:38 +02005853 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5854
Matt Roper55994c22016-05-12 07:06:08 -07005855 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005856}
5857
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005858static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5859 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005860 const struct skl_ddb_entry *entry)
5861{
5862 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005863 intel_de_write_fw(dev_priv, reg,
5864 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005865 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005866 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005867}
5868
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005869static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5870 i915_reg_t reg,
5871 const struct skl_wm_level *level)
5872{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005873 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005874
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005875 if (level->enable)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005876 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005877 if (level->ignore_lines)
5878 val |= PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005879 val |= level->blocks;
Matt Roper1003cee2021-05-14 08:36:54 -07005880 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005881
Jani Nikula9b6320a2020-01-23 16:00:04 +02005882 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005883}
5884
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005885void skl_write_plane_wm(struct intel_plane *plane,
5886 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005887{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005888 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005889 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005890 enum plane_id plane_id = plane->id;
5891 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005892 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5893 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005894 const struct skl_ddb_entry *ddb_y =
5895 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5896 const struct skl_ddb_entry *ddb_uv =
5897 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005898
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005899 for (level = 0; level <= max_level; level++)
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005900 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005901 skl_plane_wm_level(pipe_wm, plane_id, level));
5902
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005903 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005904 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005905
Matt Roper7959ffe2021-05-18 17:06:11 -07005906 if (HAS_HW_SAGV_WM(dev_priv)) {
5907 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5908 &wm->sagv.wm0);
5909 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5910 &wm->sagv.trans_wm);
5911 }
5912
Matt Roper7dadd282021-03-19 21:42:43 -07005913 if (DISPLAY_VER(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005914 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005915 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5916 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305917 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005918
5919 if (wm->is_planar)
5920 swap(ddb_y, ddb_uv);
5921
5922 skl_ddb_entry_write(dev_priv,
5923 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5924 skl_ddb_entry_write(dev_priv,
5925 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005926}
5927
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005928void skl_write_cursor_wm(struct intel_plane *plane,
5929 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005930{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005931 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005932 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005933 enum plane_id plane_id = plane->id;
5934 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005935 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005936 const struct skl_ddb_entry *ddb =
5937 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005938
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005939 for (level = 0; level <= max_level; level++)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005940 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005941 skl_plane_wm_level(pipe_wm, plane_id, level));
5942
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005943 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5944 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005945
Matt Roper7959ffe2021-05-18 17:06:11 -07005946 if (HAS_HW_SAGV_WM(dev_priv)) {
5947 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5948
5949 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5950 &wm->sagv.wm0);
5951 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5952 &wm->sagv.trans_wm);
5953 }
5954
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005955 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005956}
5957
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005958bool skl_wm_level_equals(const struct skl_wm_level *l1,
5959 const struct skl_wm_level *l2)
5960{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005961 return l1->enable == l2->enable &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005962 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005963 l1->lines == l2->lines &&
5964 l1->blocks == l2->blocks;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005965}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005966
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005967static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5968 const struct skl_plane_wm *wm1,
5969 const struct skl_plane_wm *wm2)
5970{
5971 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005972
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005973 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005974 /*
5975 * We don't check uv_wm as the hardware doesn't actually
5976 * use it. It only gets used for calculating the required
5977 * ddb allocation.
5978 */
5979 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005980 return false;
5981 }
5982
Ville Syrjäläf11449d2021-02-26 17:32:00 +02005983 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005984 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5985 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005986}
5987
Jani Nikula81b55ef2020-04-20 17:04:38 +03005988static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5989 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005990{
Lyude27082492016-08-24 07:48:10 +02005991 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005992}
5993
Ville Syrjälä33c9c502021-01-22 22:56:33 +02005994static void skl_ddb_entry_union(struct skl_ddb_entry *a,
5995 const struct skl_ddb_entry *b)
5996{
5997 if (a->end && b->end) {
5998 a->start = min(a->start, b->start);
5999 a->end = max(a->end, b->end);
6000 } else if (b->end) {
6001 a->start = b->start;
6002 a->end = b->end;
6003 }
6004}
6005
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006006bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03006007 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006008 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006009{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006010 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006011
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006012 for (i = 0; i < num_entries; i++) {
6013 if (i != ignore_idx &&
6014 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02006015 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03006016 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006017
Lyude27082492016-08-24 07:48:10 +02006018 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006019}
6020
Jani Nikulabb7791b2016-10-04 12:29:17 +03006021static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006022skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
6023 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006024{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006025 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
6026 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006027 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6028 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006029
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006030 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6031 struct intel_plane_state *plane_state;
6032 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006033
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006034 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
6035 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
6036 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
6037 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006038 continue;
6039
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006040 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006041 if (IS_ERR(plane_state))
6042 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02006043
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006044 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006045 }
6046
6047 return 0;
6048}
6049
Ville Syrjäläef79d622021-01-22 22:56:32 +02006050static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
6051{
6052 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
6053 u8 enabled_slices;
6054 enum pipe pipe;
6055
6056 /*
6057 * FIXME: For now we always enable slice S1 as per
6058 * the Bspec display initialization sequence.
6059 */
6060 enabled_slices = BIT(DBUF_S1);
6061
6062 for_each_pipe(dev_priv, pipe)
6063 enabled_slices |= dbuf_state->slices[pipe];
6064
6065 return enabled_slices;
6066}
6067
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006068static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006069skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07006070{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006071 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6072 const struct intel_dbuf_state *old_dbuf_state;
Ville Syrjäläef79d622021-01-22 22:56:32 +02006073 struct intel_dbuf_state *new_dbuf_state = NULL;
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006074 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006075 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306076 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306077 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07006078
Ville Syrjäläef79d622021-01-22 22:56:32 +02006079 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6080 new_dbuf_state = intel_atomic_get_dbuf_state(state);
6081 if (IS_ERR(new_dbuf_state))
6082 return PTR_ERR(new_dbuf_state);
6083
6084 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6085 break;
6086 }
6087
6088 if (!new_dbuf_state)
6089 return 0;
6090
6091 new_dbuf_state->active_pipes =
6092 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6093
6094 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
6095 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6096 if (ret)
6097 return ret;
6098 }
6099
6100 for_each_intel_crtc(&dev_priv->drm, crtc) {
6101 enum pipe pipe = crtc->pipe;
6102
6103 new_dbuf_state->slices[pipe] =
6104 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes);
6105
6106 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
6107 continue;
6108
6109 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6110 if (ret)
6111 return ret;
6112 }
6113
6114 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
6115
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006116 if (IS_ALDERLAKE_P(dev_priv))
6117 new_dbuf_state->joined_mbus = adlp_check_mbus_joined(new_dbuf_state->active_pipes);
6118
6119 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
6120 old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006121 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
6122 if (ret)
6123 return ret;
6124
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006125 if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6126 /* TODO: Implement vblank synchronized MBUS joining changes */
6127 ret = intel_modeset_all_pipes(state);
6128 if (ret)
6129 return ret;
6130 }
6131
Ville Syrjäläef79d622021-01-22 22:56:32 +02006132 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006133 "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 +02006134 old_dbuf_state->enabled_slices,
6135 new_dbuf_state->enabled_slices,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006136 INTEL_INFO(dev_priv)->dbuf.slice_mask,
6137 yesno(old_dbuf_state->joined_mbus),
6138 yesno(new_dbuf_state->joined_mbus));
Ville Syrjäläef79d622021-01-22 22:56:32 +02006139 }
6140
6141 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6142 enum pipe pipe = crtc->pipe;
6143
6144 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
6145
6146 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
6147 continue;
6148
6149 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6150 if (ret)
6151 return ret;
6152 }
6153
6154 for_each_intel_crtc(&dev_priv->drm, crtc) {
6155 ret = skl_crtc_allocate_ddb(state, crtc);
6156 if (ret)
6157 return ret;
6158 }
6159
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006160 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006161 new_crtc_state, i) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006162 ret = skl_allocate_plane_ddb(state, crtc);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006163 if (ret)
6164 return ret;
6165
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006166 ret = skl_ddb_add_affected_planes(old_crtc_state,
6167 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006168 if (ret)
6169 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07006170 }
6171
6172 return 0;
6173}
6174
Ville Syrjäläab98e942019-02-08 22:05:27 +02006175static char enast(bool enable)
6176{
6177 return enable ? '*' : ' ';
6178}
6179
Matt Roper2722efb2016-08-17 15:55:55 -04006180static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006181skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006182{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006183 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6184 const struct intel_crtc_state *old_crtc_state;
6185 const struct intel_crtc_state *new_crtc_state;
6186 struct intel_plane *plane;
6187 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01006188 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006189
Jani Nikulabdbf43d2019-10-28 12:38:15 +02006190 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02006191 return;
6192
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006193 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6194 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02006195 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
6196
6197 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
6198 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
6199
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006200 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6201 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006202 const struct skl_ddb_entry *old, *new;
6203
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006204 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
6205 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006206
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006207 if (skl_ddb_entry_equal(old, new))
6208 continue;
6209
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006210 drm_dbg_kms(&dev_priv->drm,
6211 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
6212 plane->base.base.id, plane->base.name,
6213 old->start, old->end, new->start, new->end,
6214 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006215 }
6216
6217 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6218 enum plane_id plane_id = plane->id;
6219 const struct skl_plane_wm *old_wm, *new_wm;
6220
6221 old_wm = &old_pipe_wm->planes[plane_id];
6222 new_wm = &new_pipe_wm->planes[plane_id];
6223
6224 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
6225 continue;
6226
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006227 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006228 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
6229 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006230 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006231 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
6232 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
6233 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
6234 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
6235 enast(old_wm->trans_wm.enable),
6236 enast(old_wm->sagv.wm0.enable),
6237 enast(old_wm->sagv.trans_wm.enable),
6238 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
6239 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
6240 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
6241 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
6242 enast(new_wm->trans_wm.enable),
6243 enast(new_wm->sagv.wm0.enable),
6244 enast(new_wm->sagv.trans_wm.enable));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006245
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006246 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006247 "[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"
6248 " -> %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 +03006249 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006250 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
6251 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
6252 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
6253 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
6254 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
6255 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
6256 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
6257 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
6258 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
6259 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
6260 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
6261 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
6262 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
6263 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
6264 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
6265 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
6266 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
6267 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
6268 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
6269 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
6270 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
6271 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006272
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006273 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006274 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6275 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006276 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006277 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
6278 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
6279 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
6280 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
6281 old_wm->trans_wm.blocks,
6282 old_wm->sagv.wm0.blocks,
6283 old_wm->sagv.trans_wm.blocks,
6284 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
6285 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
6286 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
6287 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
6288 new_wm->trans_wm.blocks,
6289 new_wm->sagv.wm0.blocks,
6290 new_wm->sagv.trans_wm.blocks);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006291
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006292 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006293 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6294 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006295 plane->base.base.id, plane->base.name,
6296 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6297 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6298 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6299 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6300 old_wm->trans_wm.min_ddb_alloc,
Ville Syrjäläa68aa482021-02-26 17:32:01 +02006301 old_wm->sagv.wm0.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006302 old_wm->sagv.trans_wm.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006303 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6304 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6305 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6306 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006307 new_wm->trans_wm.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006308 new_wm->sagv.wm0.min_ddb_alloc,
6309 new_wm->sagv.trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006310 }
6311 }
6312}
6313
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006314static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6315 const struct skl_pipe_wm *old_pipe_wm,
6316 const struct skl_pipe_wm *new_pipe_wm)
6317{
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006318 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6319 int level, max_level = ilk_wm_max_level(i915);
6320
6321 for (level = 0; level <= max_level; level++) {
6322 /*
6323 * We don't check uv_wm as the hardware doesn't actually
6324 * use it. It only gets used for calculating the required
6325 * ddb allocation.
6326 */
Ville Syrjälä93fe8622021-03-25 02:44:14 +02006327 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6328 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006329 return false;
6330 }
6331
Matt Roper7959ffe2021-05-18 17:06:11 -07006332 if (HAS_HW_SAGV_WM(i915)) {
6333 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6334 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6335
6336 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6337 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6338 return false;
6339 }
6340
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006341 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6342 skl_plane_trans_wm(new_pipe_wm, plane->id));
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006343}
6344
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006345/*
6346 * To make sure the cursor watermark registers are always consistent
6347 * with our computed state the following scenario needs special
6348 * treatment:
6349 *
6350 * 1. enable cursor
6351 * 2. move cursor entirely offscreen
6352 * 3. disable cursor
6353 *
6354 * Step 2. does call .disable_plane() but does not zero the watermarks
6355 * (since we consider an offscreen cursor still active for the purposes
6356 * of watermarks). Step 3. would not normally call .disable_plane()
6357 * because the actual plane visibility isn't changing, and we don't
6358 * deallocate the cursor ddb until the pipe gets disabled. So we must
6359 * force step 3. to call .disable_plane() to update the watermark
6360 * registers properly.
6361 *
6362 * Other planes do not suffer from this issues as their watermarks are
6363 * calculated based on the actual plane visibility. The only time this
6364 * can trigger for the other planes is during the initial readout as the
6365 * default value of the watermarks registers is not zero.
6366 */
6367static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6368 struct intel_crtc *crtc)
6369{
6370 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6371 const struct intel_crtc_state *old_crtc_state =
6372 intel_atomic_get_old_crtc_state(state, crtc);
6373 struct intel_crtc_state *new_crtc_state =
6374 intel_atomic_get_new_crtc_state(state, crtc);
6375 struct intel_plane *plane;
6376
6377 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6378 struct intel_plane_state *plane_state;
6379 enum plane_id plane_id = plane->id;
6380
6381 /*
6382 * Force a full wm update for every plane on modeset.
6383 * Required because the reset value of the wm registers
6384 * is non-zero, whereas we want all disabled planes to
6385 * have zero watermarks. So if we turn off the relevant
6386 * power well the hardware state will go out of sync
6387 * with the software state.
6388 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006389 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006390 skl_plane_selected_wm_equals(plane,
6391 &old_crtc_state->wm.skl.optimal,
6392 &new_crtc_state->wm.skl.optimal))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006393 continue;
6394
6395 plane_state = intel_atomic_get_plane_state(state, plane);
6396 if (IS_ERR(plane_state))
6397 return PTR_ERR(plane_state);
6398
6399 new_crtc_state->update_planes |= BIT(plane_id);
6400 }
6401
6402 return 0;
6403}
6404
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306405static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006406skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306407{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006408 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006409 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306410 int ret, i;
6411
Ville Syrjäläffc90032020-11-06 19:30:37 +02006412 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6413 ret = skl_build_pipe_wm(state, crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006414 if (ret)
6415 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006416 }
6417
Matt Roperd8e87492018-12-11 09:31:07 -08006418 ret = skl_compute_ddb(state);
6419 if (ret)
6420 return ret;
6421
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006422 ret = intel_compute_sagv_mask(state);
6423 if (ret)
6424 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006425
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006426 /*
6427 * skl_compute_ddb() will have adjusted the final watermarks
6428 * based on how much ddb is available. Now we can actually
6429 * check if the final watermarks changed.
6430 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006431 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006432 ret = skl_wm_add_affected_planes(state, crtc);
6433 if (ret)
6434 return ret;
6435 }
6436
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006437 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006438
Matt Roper98d39492016-05-12 07:06:03 -07006439 return 0;
6440}
6441
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006442static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006443 struct intel_wm_config *config)
6444{
6445 struct intel_crtc *crtc;
6446
6447 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006448 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006449 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6450
6451 if (!wm->pipe_enabled)
6452 continue;
6453
6454 config->sprites_enabled |= wm->sprites_enabled;
6455 config->sprites_scaled |= wm->sprites_scaled;
6456 config->num_pipes_active++;
6457 }
6458}
6459
Matt Ropered4a6a72016-02-23 17:20:13 -08006460static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006461{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006462 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006463 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006464 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006465 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006466 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006467
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006468 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006469
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006470 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6471 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006472
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006473 /* 5/6 split only in single pipe config on IVB+ */
Matt Roper7dadd282021-03-19 21:42:43 -07006474 if (DISPLAY_VER(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006475 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006476 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6477 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006478
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006479 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006480 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006481 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006482 }
6483
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006484 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006485 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006486
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006487 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006488
Imre Deak820c1982013-12-17 14:46:36 +02006489 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006490}
6491
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006492static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006493 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006494{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6496 const struct intel_crtc_state *crtc_state =
6497 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006498
Matt Ropered4a6a72016-02-23 17:20:13 -08006499 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006500 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006501 ilk_program_watermarks(dev_priv);
6502 mutex_unlock(&dev_priv->wm.wm_mutex);
6503}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006504
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006505static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006506 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006507{
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ä88016a92019-07-01 19:05:45 +03006511
6512 if (!crtc_state->wm.need_postvbl_update)
6513 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006514
6515 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006516 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6517 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006518 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006519}
6520
Jani Nikula81b55ef2020-04-20 17:04:38 +03006521static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006522{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006523 level->enable = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006524 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006525 level->blocks = val & PLANE_WM_BLOCKS_MASK;
Matt Roper1003cee2021-05-14 08:36:54 -07006526 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
Pradeep Bhat30789992014-11-04 17:06:45 +00006527}
6528
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006529void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006530 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006531{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6533 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006534 int level, max_level;
6535 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006536 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006537
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006538 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006539
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006540 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006541 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006542
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006543 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006544 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006545 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006546 else
Jani Nikula5f461662020-11-30 13:15:58 +02006547 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006548
6549 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6550 }
6551
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006552 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006553 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006554 else
Jani Nikula5f461662020-11-30 13:15:58 +02006555 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006556
6557 skl_wm_level_from_reg_val(val, &wm->trans_wm);
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006558
Matt Roper7959ffe2021-05-18 17:06:11 -07006559 if (HAS_HW_SAGV_WM(dev_priv)) {
6560 if (plane_id != PLANE_CURSOR)
6561 val = intel_uncore_read(&dev_priv->uncore,
6562 PLANE_WM_SAGV(pipe, plane_id));
6563 else
6564 val = intel_uncore_read(&dev_priv->uncore,
6565 CUR_WM_SAGV(pipe));
6566
6567 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6568
6569 if (plane_id != PLANE_CURSOR)
6570 val = intel_uncore_read(&dev_priv->uncore,
6571 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6572 else
6573 val = intel_uncore_read(&dev_priv->uncore,
6574 CUR_WM_SAGV_TRANS(pipe));
6575
6576 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6577 } else if (DISPLAY_VER(dev_priv) >= 12) {
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006578 wm->sagv.wm0 = wm->wm[0];
6579 wm->sagv.trans_wm = wm->trans_wm;
6580 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006581 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006582}
6583
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006584void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006585{
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006586 struct intel_dbuf_state *dbuf_state =
6587 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006588 struct intel_crtc *crtc;
Pradeep Bhat30789992014-11-04 17:06:45 +00006589
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006590 if (IS_ALDERLAKE_P(dev_priv))
6591 dbuf_state->joined_mbus = intel_de_read(dev_priv, MBUS_CTL) & MBUS_JOIN;
6592
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006593 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006594 struct intel_crtc_state *crtc_state =
6595 to_intel_crtc_state(crtc->base.state);
6596 enum pipe pipe = crtc->pipe;
Ville Syrjälä835c1762021-05-18 17:06:16 -07006597 unsigned int mbus_offset;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006598 enum plane_id plane_id;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006599
Maarten Lankhorstec193642019-06-28 10:55:17 +02006600 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
Ville Syrjälädbf71382020-11-06 19:30:38 +02006601 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006602
6603 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6604
6605 for_each_plane_id_on_crtc(crtc, plane_id) {
6606 struct skl_ddb_entry *ddb_y =
6607 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6608 struct skl_ddb_entry *ddb_uv =
6609 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
6610
6611 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6612 plane_id, ddb_y, ddb_uv);
6613
6614 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6615 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_uv);
6616 }
6617
6618 dbuf_state->slices[pipe] =
6619 skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes);
6620
6621 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6622
Ville Syrjälä835c1762021-05-18 17:06:16 -07006623 /*
6624 * Used for checking overlaps, so we need absolute
6625 * offsets instead of MBUS relative offsets.
6626 */
6627 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_state->slices[pipe]);
6628 crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
6629 crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006630
6631 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006632 "[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 +02006633 crtc->base.base.id, crtc->base.name,
6634 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006635 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
6636 yesno(dbuf_state->joined_mbus));
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006637 }
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006638
6639 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
Pradeep Bhat30789992014-11-04 17:06:45 +00006640}
6641
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006642static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006643{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006644 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006645 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006646 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006647 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6648 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006649 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006650
Jani Nikula5f461662020-11-30 13:15:58 +02006651 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006652
Ville Syrjälä15606532016-05-13 17:55:17 +03006653 memset(active, 0, sizeof(*active));
6654
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006655 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006656
6657 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006658 u32 tmp = hw->wm_pipe[pipe];
6659
6660 /*
6661 * For active pipes LP0 watermark is marked as
6662 * enabled, and LP1+ watermaks as disabled since
6663 * we can't really reverse compute them in case
6664 * multiple pipes are active.
6665 */
6666 active->wm[0].enable = true;
6667 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6668 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6669 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006670 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006671 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006672
6673 /*
6674 * For inactive pipes, all watermark levels
6675 * should be marked as enabled but zeroed,
6676 * which is what we'd compute them to.
6677 */
6678 for (level = 0; level <= max_level; level++)
6679 active->wm[level].enable = true;
6680 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006681
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006682 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006683}
6684
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006685#define _FW_WM(value, plane) \
6686 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6687#define _FW_WM_VLV(value, plane) \
6688 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6689
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006690static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6691 struct g4x_wm_values *wm)
6692{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006693 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006694
Jani Nikula5f461662020-11-30 13:15:58 +02006695 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006696 wm->sr.plane = _FW_WM(tmp, SR);
6697 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6698 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6699 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6700
Jani Nikula5f461662020-11-30 13:15:58 +02006701 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006702 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6703 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6704 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6705 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6706 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6707 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6708
Jani Nikula5f461662020-11-30 13:15:58 +02006709 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006710 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6711 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6712 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6713 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6714}
6715
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006716static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6717 struct vlv_wm_values *wm)
6718{
6719 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006720 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006721
6722 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006723 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006724
Ville Syrjälä1b313892016-11-28 19:37:08 +02006725 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006726 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006727 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006728 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006729 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006730 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006731 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006732 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6733 }
6734
Jani Nikula5f461662020-11-30 13:15:58 +02006735 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006736 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006737 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6738 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6739 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006740
Jani Nikula5f461662020-11-30 13:15:58 +02006741 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006742 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6743 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6744 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006745
Jani Nikula5f461662020-11-30 13:15:58 +02006746 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006747 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6748
6749 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02006750 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006751 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6752 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
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, DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006755 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6756 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006757
Jani Nikula5f461662020-11-30 13:15:58 +02006758 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006759 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6760 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006761
Jani Nikula5f461662020-11-30 13:15:58 +02006762 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006763 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006764 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6765 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6766 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6767 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6768 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6769 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6770 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6771 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6772 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006773 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02006774 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006775 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6776 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006777
Jani Nikula5f461662020-11-30 13:15:58 +02006778 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006779 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006780 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 }
6787}
6788
6789#undef _FW_WM
6790#undef _FW_WM_VLV
6791
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006792void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006793{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006794 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6795 struct intel_crtc *crtc;
6796
6797 g4x_read_wm_values(dev_priv, wm);
6798
Jani Nikula5f461662020-11-30 13:15:58 +02006799 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006800
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006801 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006802 struct intel_crtc_state *crtc_state =
6803 to_intel_crtc_state(crtc->base.state);
6804 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6805 struct g4x_pipe_wm *raw;
6806 enum pipe pipe = crtc->pipe;
6807 enum plane_id plane_id;
6808 int level, max_level;
6809
6810 active->cxsr = wm->cxsr;
6811 active->hpll_en = wm->hpll_en;
6812 active->fbc_en = wm->fbc_en;
6813
6814 active->sr = wm->sr;
6815 active->hpll = wm->hpll;
6816
6817 for_each_plane_id_on_crtc(crtc, plane_id) {
6818 active->wm.plane[plane_id] =
6819 wm->pipe[pipe].plane[plane_id];
6820 }
6821
6822 if (wm->cxsr && wm->hpll_en)
6823 max_level = G4X_WM_LEVEL_HPLL;
6824 else if (wm->cxsr)
6825 max_level = G4X_WM_LEVEL_SR;
6826 else
6827 max_level = G4X_WM_LEVEL_NORMAL;
6828
6829 level = G4X_WM_LEVEL_NORMAL;
6830 raw = &crtc_state->wm.g4x.raw[level];
6831 for_each_plane_id_on_crtc(crtc, plane_id)
6832 raw->plane[plane_id] = active->wm.plane[plane_id];
6833
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006834 level = G4X_WM_LEVEL_SR;
6835 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006836 goto out;
6837
6838 raw = &crtc_state->wm.g4x.raw[level];
6839 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6840 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6841 raw->plane[PLANE_SPRITE0] = 0;
6842 raw->fbc = active->sr.fbc;
6843
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006844 level = G4X_WM_LEVEL_HPLL;
6845 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006846 goto out;
6847
6848 raw = &crtc_state->wm.g4x.raw[level];
6849 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6850 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6851 raw->plane[PLANE_SPRITE0] = 0;
6852 raw->fbc = active->hpll.fbc;
6853
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006854 level++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006855 out:
6856 for_each_plane_id_on_crtc(crtc, plane_id)
6857 g4x_raw_plane_wm_set(crtc_state, level,
6858 plane_id, USHRT_MAX);
6859 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6860
6861 crtc_state->wm.g4x.optimal = *active;
6862 crtc_state->wm.g4x.intermediate = *active;
6863
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006864 drm_dbg_kms(&dev_priv->drm,
6865 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6866 pipe_name(pipe),
6867 wm->pipe[pipe].plane[PLANE_PRIMARY],
6868 wm->pipe[pipe].plane[PLANE_CURSOR],
6869 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006870 }
6871
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006872 drm_dbg_kms(&dev_priv->drm,
6873 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6874 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6875 drm_dbg_kms(&dev_priv->drm,
6876 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6877 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6878 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6879 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006880}
6881
6882void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6883{
6884 struct intel_plane *plane;
6885 struct intel_crtc *crtc;
6886
6887 mutex_lock(&dev_priv->wm.wm_mutex);
6888
6889 for_each_intel_plane(&dev_priv->drm, plane) {
6890 struct intel_crtc *crtc =
6891 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6892 struct intel_crtc_state *crtc_state =
6893 to_intel_crtc_state(crtc->base.state);
6894 struct intel_plane_state *plane_state =
6895 to_intel_plane_state(plane->base.state);
6896 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6897 enum plane_id plane_id = plane->id;
6898 int level;
6899
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006900 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006901 continue;
6902
6903 for (level = 0; level < 3; level++) {
6904 struct g4x_pipe_wm *raw =
6905 &crtc_state->wm.g4x.raw[level];
6906
6907 raw->plane[plane_id] = 0;
6908 wm_state->wm.plane[plane_id] = 0;
6909 }
6910
6911 if (plane_id == PLANE_PRIMARY) {
6912 for (level = 0; level < 3; level++) {
6913 struct g4x_pipe_wm *raw =
6914 &crtc_state->wm.g4x.raw[level];
6915 raw->fbc = 0;
6916 }
6917
6918 wm_state->sr.fbc = 0;
6919 wm_state->hpll.fbc = 0;
6920 wm_state->fbc_en = false;
6921 }
6922 }
6923
6924 for_each_intel_crtc(&dev_priv->drm, crtc) {
6925 struct intel_crtc_state *crtc_state =
6926 to_intel_crtc_state(crtc->base.state);
6927
6928 crtc_state->wm.g4x.intermediate =
6929 crtc_state->wm.g4x.optimal;
6930 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6931 }
6932
6933 g4x_program_watermarks(dev_priv);
6934
6935 mutex_unlock(&dev_priv->wm.wm_mutex);
6936}
6937
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006938void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006939{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006940 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006941 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006942 u32 val;
6943
6944 vlv_read_wm_values(dev_priv, wm);
6945
Jani Nikula5f461662020-11-30 13:15:58 +02006946 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006947 wm->level = VLV_WM_LEVEL_PM2;
6948
6949 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006950 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006951
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006952 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006953 if (val & DSP_MAXFIFO_PM5_ENABLE)
6954 wm->level = VLV_WM_LEVEL_PM5;
6955
Ville Syrjälä58590c12015-09-08 21:05:12 +03006956 /*
6957 * If DDR DVFS is disabled in the BIOS, Punit
6958 * will never ack the request. So if that happens
6959 * assume we don't have to enable/disable DDR DVFS
6960 * dynamically. To test that just set the REQ_ACK
6961 * bit to poke the Punit, but don't change the
6962 * HIGH/LOW bits so that we don't actually change
6963 * the current state.
6964 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006965 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006966 val |= FORCE_DDR_FREQ_REQ_ACK;
6967 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6968
6969 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6970 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006971 drm_dbg_kms(&dev_priv->drm,
6972 "Punit not acking DDR DVFS request, "
6973 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006974 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6975 } else {
6976 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6977 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6978 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6979 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006980
Chris Wilson337fa6e2019-04-26 09:17:20 +01006981 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006982 }
6983
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006984 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006985 struct intel_crtc_state *crtc_state =
6986 to_intel_crtc_state(crtc->base.state);
6987 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6988 const struct vlv_fifo_state *fifo_state =
6989 &crtc_state->wm.vlv.fifo_state;
6990 enum pipe pipe = crtc->pipe;
6991 enum plane_id plane_id;
6992 int level;
6993
6994 vlv_get_fifo_size(crtc_state);
6995
6996 active->num_levels = wm->level + 1;
6997 active->cxsr = wm->cxsr;
6998
Ville Syrjäläff32c542017-03-02 19:14:57 +02006999 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03007000 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02007001 &crtc_state->wm.vlv.raw[level];
7002
7003 active->sr[level].plane = wm->sr.plane;
7004 active->sr[level].cursor = wm->sr.cursor;
7005
7006 for_each_plane_id_on_crtc(crtc, plane_id) {
7007 active->wm[level].plane[plane_id] =
7008 wm->pipe[pipe].plane[plane_id];
7009
7010 raw->plane[plane_id] =
7011 vlv_invert_wm_value(active->wm[level].plane[plane_id],
7012 fifo_state->plane[plane_id]);
7013 }
7014 }
7015
7016 for_each_plane_id_on_crtc(crtc, plane_id)
7017 vlv_raw_plane_wm_set(crtc_state, level,
7018 plane_id, USHRT_MAX);
7019 vlv_invalidate_wms(crtc, active, level);
7020
7021 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007022 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007023
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007024 drm_dbg_kms(&dev_priv->drm,
7025 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
7026 pipe_name(pipe),
7027 wm->pipe[pipe].plane[PLANE_PRIMARY],
7028 wm->pipe[pipe].plane[PLANE_CURSOR],
7029 wm->pipe[pipe].plane[PLANE_SPRITE0],
7030 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007031 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007032
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007033 drm_dbg_kms(&dev_priv->drm,
7034 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
7035 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007036}
7037
Ville Syrjälä602ae832017-03-02 19:15:02 +02007038void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
7039{
7040 struct intel_plane *plane;
7041 struct intel_crtc *crtc;
7042
7043 mutex_lock(&dev_priv->wm.wm_mutex);
7044
7045 for_each_intel_plane(&dev_priv->drm, plane) {
7046 struct intel_crtc *crtc =
7047 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
7048 struct intel_crtc_state *crtc_state =
7049 to_intel_crtc_state(crtc->base.state);
7050 struct intel_plane_state *plane_state =
7051 to_intel_plane_state(plane->base.state);
7052 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
7053 const struct vlv_fifo_state *fifo_state =
7054 &crtc_state->wm.vlv.fifo_state;
7055 enum plane_id plane_id = plane->id;
7056 int level;
7057
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01007058 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02007059 continue;
7060
7061 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03007062 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02007063 &crtc_state->wm.vlv.raw[level];
7064
7065 raw->plane[plane_id] = 0;
7066
7067 wm_state->wm[level].plane[plane_id] =
7068 vlv_invert_wm_value(raw->plane[plane_id],
7069 fifo_state->plane[plane_id]);
7070 }
7071 }
7072
7073 for_each_intel_crtc(&dev_priv->drm, crtc) {
7074 struct intel_crtc_state *crtc_state =
7075 to_intel_crtc_state(crtc->base.state);
7076
7077 crtc_state->wm.vlv.intermediate =
7078 crtc_state->wm.vlv.optimal;
7079 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
7080 }
7081
7082 vlv_program_watermarks(dev_priv);
7083
7084 mutex_unlock(&dev_priv->wm.wm_mutex);
7085}
7086
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007087/*
7088 * FIXME should probably kill this and improve
7089 * the real watermark readout/sanitation instead
7090 */
7091static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
7092{
Jani Nikula5f461662020-11-30 13:15:58 +02007093 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM1_LP_SR_EN);
7094 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM1_LP_SR_EN);
7095 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 +02007096
7097 /*
7098 * Don't touch WM1S_LP_EN here.
7099 * Doing so could cause underruns.
7100 */
7101}
7102
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007103void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007104{
Imre Deak820c1982013-12-17 14:46:36 +02007105 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007106 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007107
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007108 ilk_init_lp_watermarks(dev_priv);
7109
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007110 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007111 ilk_pipe_wm_get_hw_state(crtc);
7112
Jani Nikula5f461662020-11-30 13:15:58 +02007113 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
7114 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
7115 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007116
Jani Nikula5f461662020-11-30 13:15:58 +02007117 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
Matt Roper7dadd282021-03-19 21:42:43 -07007118 if (DISPLAY_VER(dev_priv) >= 7) {
Jani Nikula5f461662020-11-30 13:15:58 +02007119 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
7120 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02007121 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007122
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007123 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007124 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 +02007125 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01007126 else if (IS_IVYBRIDGE(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007127 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 +02007128 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007129
7130 hw->enable_fbc_wm =
Jani Nikula5f461662020-11-30 13:15:58 +02007131 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007132}
7133
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307134void intel_enable_ipc(struct drm_i915_private *dev_priv)
7135{
7136 u32 val;
7137
José Roberto de Souzafd847b82018-09-18 13:47:11 -07007138 if (!HAS_IPC(dev_priv))
7139 return;
7140
Jani Nikula5f461662020-11-30 13:15:58 +02007141 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307142
7143 if (dev_priv->ipc_enabled)
7144 val |= DISP_IPC_ENABLE;
7145 else
7146 val &= ~DISP_IPC_ENABLE;
7147
Jani Nikula5f461662020-11-30 13:15:58 +02007148 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307149}
7150
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007151static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
7152{
7153 /* Display WA #0477 WaDisableIPC: skl */
7154 if (IS_SKYLAKE(dev_priv))
7155 return false;
7156
7157 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01007158 if (IS_KABYLAKE(dev_priv) ||
7159 IS_COFFEELAKE(dev_priv) ||
7160 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007161 return dev_priv->dram_info.symmetric_memory;
7162
7163 return true;
7164}
7165
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307166void intel_init_ipc(struct drm_i915_private *dev_priv)
7167{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307168 if (!HAS_IPC(dev_priv))
7169 return;
7170
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007171 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07007172
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307173 intel_enable_ipc(dev_priv);
7174}
7175
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007176static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007177{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007178 /*
7179 * On Ibex Peak and Cougar Point, we need to disable clock
7180 * gating for the panel power sequencer or it will fail to
7181 * start up when no ports are active.
7182 */
Jani Nikula5f461662020-11-30 13:15:58 +02007183 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007184}
7185
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007186static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007187{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007188 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007189
Damien Lespiau055e3932014-08-18 13:49:10 +01007190 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007191 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
7192 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007193 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007194
Jani Nikula5f461662020-11-30 13:15:58 +02007195 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
7196 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007197 }
7198}
7199
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007200static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007201{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007202 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007203
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007204 /*
7205 * Required for FBC
7206 * WaFbcDisableDpfcClockGating:ilk
7207 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007208 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7209 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7210 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007211
Jani Nikula5f461662020-11-30 13:15:58 +02007212 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007213 MARIUNIT_CLOCK_GATE_DISABLE |
7214 SVSMUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007215 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007216 VFMUNIT_CLOCK_GATE_DISABLE);
7217
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007218 /*
7219 * According to the spec the following bits should be set in
7220 * order to enable memory self-refresh
7221 * The bit 22/21 of 0x42004
7222 * The bit 5 of 0x42020
7223 * The bit 15 of 0x45000
7224 */
Jani Nikula5f461662020-11-30 13:15:58 +02007225 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7226 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007227 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007228 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007229 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
7230 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007231 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007232
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007233 /*
7234 * Based on the document from hardware guys the following bits
7235 * should be set unconditionally in order to enable FBC.
7236 * The bit 22 of 0x42000
7237 * The bit 22 of 0x42004
7238 * The bit 7,8,9 of 0x42020.
7239 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007240 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007241 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Jani Nikula5f461662020-11-30 13:15:58 +02007242 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7243 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007244 ILK_FBCQ_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007245 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7246 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007247 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007248 }
7249
Jani Nikula5f461662020-11-30 13:15:58 +02007250 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007251
Jani Nikula5f461662020-11-30 13:15:58 +02007252 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7253 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007254 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05307255
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007256 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007257
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007258 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007259}
7260
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007261static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007262{
Ville Syrjäläd048a262019-08-21 20:30:31 +03007263 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007264 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007265
7266 /*
7267 * On Ibex Peak and Cougar Point, we need to disable clock
7268 * gating for the panel power sequencer or it will fail to
7269 * start up when no ports are active.
7270 */
Jani Nikula5f461662020-11-30 13:15:58 +02007271 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnescd664072013-10-02 10:34:19 -07007272 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7273 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007274 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
Daniel Vetter3107bd42012-10-31 22:52:31 +01007275 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007276 /* The below fixes the weird display corruption, a few pixels shifted
7277 * downward, on (only) LVDS of some HP laptops with IVY.
7278 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007279 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007280 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007281 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7282 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007283 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007284 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007285 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7286 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Jani Nikula5f461662020-11-30 13:15:58 +02007287 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007288 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007289 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007290 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007291 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
Daniel Vetter3107bd42012-10-31 22:52:31 +01007292 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7293 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007294}
7295
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007296static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007297{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007298 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007299
Jani Nikula5f461662020-11-30 13:15:58 +02007300 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007301 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007302 drm_dbg_kms(&dev_priv->drm,
7303 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7304 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007305}
7306
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007307static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007308{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007309 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007310
Jani Nikula5f461662020-11-30 13:15:58 +02007311 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007312
Jani Nikula5f461662020-11-30 13:15:58 +02007313 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7314 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007315 ILK_ELPIN_409_SELECT);
7316
Jani Nikula5f461662020-11-30 13:15:58 +02007317 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7318 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007319 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7320 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7321
7322 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7323 * gating disable must be set. Failure to set it results in
7324 * flickering pixels due to Z write ordering failures after
7325 * some amount of runtime in the Mesa "fire" demo, and Unigine
7326 * Sanctuary and Tropics, and apparently anything else with
7327 * alpha test or pixel discard.
7328 *
7329 * According to the spec, bit 11 (RCCUNIT) must also be set,
7330 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007331 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007332 * WaDisableRCCUnitClockGating:snb
7333 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007334 */
Jani Nikula5f461662020-11-30 13:15:58 +02007335 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007336 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7337 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7338
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007339 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007340 * According to the spec the following bits should be
7341 * set in order to enable memory self-refresh and fbc:
7342 * The bit21 and bit22 of 0x42000
7343 * The bit21 and bit22 of 0x42004
7344 * The bit5 and bit7 of 0x42020
7345 * The bit14 of 0x70180
7346 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007347 *
7348 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007349 */
Jani Nikula5f461662020-11-30 13:15:58 +02007350 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7351 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007352 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007353 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7354 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007355 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Jani Nikula5f461662020-11-30 13:15:58 +02007356 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7357 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
Damien Lespiau231e54f2012-10-19 17:55:41 +01007358 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7359 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007360
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007361 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007362
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007363 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007364
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007365 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007366}
7367
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007368static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007369{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007370 /*
7371 * TODO: this bit should only be enabled when really needed, then
7372 * disabled when not needed anymore in order to save power.
7373 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007374 if (HAS_PCH_LPT_LP(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007375 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7376 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007377 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007378
7379 /* WADPOClockGatingDisable:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007380 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7381 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007382 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007383}
7384
Ville Syrjälä712bf362016-10-31 22:37:23 +02007385static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007386{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007387 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02007388 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007389
7390 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007391 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
Imre Deak7d708ee2013-04-17 14:04:50 +03007392 }
7393}
7394
Imre Deak450174f2016-05-03 15:54:21 +03007395static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7396 int general_prio_credits,
7397 int high_prio_credits)
7398{
7399 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007400 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007401
7402 /* WaTempDisableDOPClkGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007403 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7404 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
Imre Deak450174f2016-05-03 15:54:21 +03007405
Jani Nikula5f461662020-11-30 13:15:58 +02007406 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Oscar Mateo930a7842017-10-17 13:25:45 -07007407 val &= ~L3_PRIO_CREDITS_MASK;
7408 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7409 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
Jani Nikula5f461662020-11-30 13:15:58 +02007410 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007411
7412 /*
7413 * Wait at least 100 clocks before re-enabling clock gating.
7414 * See the definition of L3SQCREG1 in BSpec.
7415 */
Jani Nikula5f461662020-11-30 13:15:58 +02007416 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Imre Deak450174f2016-05-03 15:54:21 +03007417 udelay(1);
Jani Nikula5f461662020-11-30 13:15:58 +02007418 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
Imre Deak450174f2016-05-03 15:54:21 +03007419}
7420
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007421static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7422{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007423 /* Wa_1409120013:icl,ehl */
Jani Nikula5f461662020-11-30 13:15:58 +02007424 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä885f1822020-07-08 16:12:20 +03007425 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7426
Matt Atwood6f4194c2020-01-13 23:11:28 -05007427 /*Wa_14010594013:icl, ehl */
7428 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
Lucas De Marchidbac4f32021-07-28 14:59:38 -07007429 0, ICL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007430}
7431
José Roberto de Souza35f08372021-01-13 05:37:59 -08007432static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
Michel Thierry5d869232019-08-23 01:20:34 -07007433{
José Roberto de Souzac4924052021-07-12 17:38:50 -07007434 /* Wa_1409120013:tgl,rkl,adl-s,dg1 */
Clint Taylor8c209f42021-06-08 10:47:21 -07007435 if (IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv) ||
7436 IS_ALDERLAKE_S(dev_priv) || IS_DG1(dev_priv))
7437 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
7438 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007439
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007440 /* Wa_1409825376:tgl (pre-prod)*/
Matt Roper46b0d702021-07-16 22:14:26 -07007441 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007442 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 -08007443 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007444
José Roberto de Souza41c70d22021-04-08 13:49:16 -07007445 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7446 if (DISPLAY_VER(dev_priv) == 12)
7447 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7448 CLKREQ_POLICY_MEM_UP_OVRD, 0);
Michel Thierry5d869232019-08-23 01:20:34 -07007449}
7450
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007451static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7452{
7453 gen12lp_init_clock_gating(dev_priv);
7454
7455 /* Wa_22011091694:adlp */
7456 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7457}
7458
Stuart Summersda9427502020-10-14 12:19:34 -07007459static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7460{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007461 gen12lp_init_clock_gating(dev_priv);
7462
Stuart Summersda9427502020-10-14 12:19:34 -07007463 /* Wa_1409836686:dg1[a0] */
José Roberto de Souzac1f110e2021-10-19 17:23:53 -07007464 if (IS_DG1_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007465 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 -07007466 DPT_GATING_DIS);
7467}
7468
Stuart Summersd73dd1f2021-11-02 15:25:09 -07007469static void xehpsdv_init_clock_gating(struct drm_i915_private *dev_priv)
7470{
7471 /* Wa_22010146351:xehpsdv */
7472 if (IS_XEHPSDV_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
7473 intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGR_DIS);
7474}
7475
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007476static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7477{
7478 if (!HAS_PCH_CNP(dev_priv))
7479 return;
7480
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007481 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Jani Nikula5f461662020-11-30 13:15:58 +02007482 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 -07007483 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007484}
7485
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007486static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7487{
7488 cnp_init_clock_gating(dev_priv);
7489 gen9_init_clock_gating(dev_priv);
7490
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007491 /* WAC6entrylatency:cfl */
Jani Nikula5f461662020-11-30 13:15:58 +02007492 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 +03007493 FBC_LLC_FULLY_OPEN);
7494
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007495 /*
7496 * WaFbcTurnOffFbcWatermark:cfl
7497 * Display WA #0562: cfl
7498 */
Jani Nikula5f461662020-11-30 13:15:58 +02007499 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 +03007500 DISP_FBC_WM_DIS);
7501
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007502 /*
7503 * WaFbcNukeOnHostModify:cfl
7504 * Display WA #0873: cfl
7505 */
Jani Nikula5f461662020-11-30 13:15:58 +02007506 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007507 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7508}
7509
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007510static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007511{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007512 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007513
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007514 /* WAC6entrylatency:kbl */
Jani Nikula5f461662020-11-30 13:15:58 +02007515 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 +03007516 FBC_LLC_FULLY_OPEN);
7517
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007518 /* WaDisableSDEUnitClockGating:kbl */
José Roberto de Souzac1f110e2021-10-19 17:23:53 -07007519 if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007520 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007521 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007522
7523 /* WaDisableGamClockGating:kbl */
José Roberto de Souzac1f110e2021-10-19 17:23:53 -07007524 if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007525 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007526 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007527
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007528 /*
7529 * WaFbcTurnOffFbcWatermark:kbl
7530 * Display WA #0562: kbl
7531 */
Jani Nikula5f461662020-11-30 13:15:58 +02007532 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007533 DISP_FBC_WM_DIS);
7534
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007535 /*
7536 * WaFbcNukeOnHostModify:kbl
7537 * Display WA #0873: kbl
7538 */
Jani Nikula5f461662020-11-30 13:15:58 +02007539 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007540 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007541}
7542
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007543static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007544{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007545 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007546
Ville Syrjäläf1421192020-07-16 22:04:25 +03007547 /* WaDisableDopClockGating:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007548 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 +03007549 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7550
Mika Kuoppala44fff992016-06-07 17:19:09 +03007551 /* WAC6entrylatency:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007552 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 +03007553 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007554
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007555 /*
7556 * WaFbcTurnOffFbcWatermark:skl
7557 * Display WA #0562: skl
7558 */
Jani Nikula5f461662020-11-30 13:15:58 +02007559 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 +03007560 DISP_FBC_WM_DIS);
7561
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007562 /*
7563 * WaFbcNukeOnHostModify:skl
7564 * Display WA #0873: skl
7565 */
Jani Nikula5f461662020-11-30 13:15:58 +02007566 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007567 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007568
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007569 /*
7570 * WaFbcHighMemBwCorruptionAvoidance:skl
7571 * Display WA #0883: skl
7572 */
Jani Nikula5f461662020-11-30 13:15:58 +02007573 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007574 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007575}
7576
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007577static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007578{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007579 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007580
Ville Syrjälä885f1822020-07-08 16:12:20 +03007581 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007582 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7583 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007584 HSW_FBCQ_DIS);
7585
Ben Widawskyab57fff2013-12-12 15:28:04 -08007586 /* WaSwitchSolVfFArbitrationPriority:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007587 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 -07007588
Ben Widawskyab57fff2013-12-12 15:28:04 -08007589 /* WaPsrDPAMaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007590 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7591 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007592
Damien Lespiau055e3932014-08-18 13:49:10 +01007593 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007594 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007595 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7596 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007597 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007598 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007599
Ben Widawskyab57fff2013-12-12 15:28:04 -08007600 /* WaVSRefCountFullforceMissDisable:bdw */
7601 /* WaDSRefCountFullforceMissDisable:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007602 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7603 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ben Widawskyab57fff2013-12-12 15:28:04 -08007604 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007605
Jani Nikula5f461662020-11-30 13:15:58 +02007606 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007607 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007608
7609 /* WaDisableSDEUnitClockGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007610 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 +02007611 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007612
Imre Deak450174f2016-05-03 15:54:21 +03007613 /* WaProgramL3SqcReg1Default:bdw */
7614 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007615
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007616 /* WaKVMNotificationOnConfigChange:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007617 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 +03007618 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7619
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007620 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007621
7622 /* WaDisableDopClockGating:bdw
7623 *
7624 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7625 * clock gating.
7626 */
Jani Nikula5f461662020-11-30 13:15:58 +02007627 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7628 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007629}
7630
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007631static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007632{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007633 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007634 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7635 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007636 HSW_FBCQ_DIS);
7637
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007638 /* This is required by WaCatErrorRejectionIssue:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007639 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7640 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007641 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007642
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007643 /* WaSwitchSolVfFArbitrationPriority:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007644 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 -07007645
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007646 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007647}
7648
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007649static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007650{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007651 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007652
Jani Nikula5f461662020-11-30 13:15:58 +02007653 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007654
Ville Syrjälä885f1822020-07-08 16:12:20 +03007655 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007656 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7657 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007658 ILK_FBCQ_DIS);
7659
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007660 /* WaDisableBackToBackFlipFix:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007661 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007662 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7663 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7664
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007665 if (IS_IVB_GT1(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007666 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007667 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007668 else {
7669 /* must write both registers */
Jani Nikula5f461662020-11-30 13:15:58 +02007670 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Ville Syrjälä412236c2014-01-22 21:32:44 +02007671 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jani Nikula5f461662020-11-30 13:15:58 +02007672 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
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 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007675
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007676 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007677 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007678 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007679 */
Jani Nikula5f461662020-11-30 13:15:58 +02007680 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007681 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007682
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007683 /* This is required by WaCatErrorRejectionIssue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007684 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7685 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007686 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7687
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007688 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007689
Jani Nikula5f461662020-11-30 13:15:58 +02007690 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
Ben Widawsky20848222012-05-04 18:58:59 -07007691 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7692 snpcr |= GEN6_MBC_SNPCR_MED;
Jani Nikula5f461662020-11-30 13:15:58 +02007693 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007694
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007695 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007696 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007697
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007698 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007699}
7700
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007701static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007702{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007703 /* WaDisableBackToBackFlipFix:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007704 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007705 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7706 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7707
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007708 /* WaDisableDopClockGating:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007709 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007710 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7711
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007712 /* This is required by WaCatErrorRejectionIssue:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007713 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7714 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007715 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7716
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007717 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007718 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007719 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007720 */
Jani Nikula5f461662020-11-30 13:15:58 +02007721 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007722 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007723
Akash Goelc98f5062014-03-24 23:00:07 +05307724 /* WaDisableL3Bank2xClockGate:vlv
7725 * Disabling L3 clock gating- MMIO 940c[25] = 1
7726 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
Jani Nikula5f461662020-11-30 13:15:58 +02007727 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7728 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007729
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007730 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007731 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007732 * Disable clock gating on th GCFG unit to prevent a delay
7733 * in the reporting of vblank events.
7734 */
Jani Nikula5f461662020-11-30 13:15:58 +02007735 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007736}
7737
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007738static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007739{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007740 /* WaVSRefCountFullforceMissDisable:chv */
7741 /* WaDSRefCountFullforceMissDisable:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007742 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7743 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ville Syrjälä232ce332014-04-09 13:28:35 +03007744 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007745
7746 /* WaDisableSemaphoreAndSyncFlipWait:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007747 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007748 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007749
7750 /* WaDisableCSUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007751 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 +03007752 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007753
7754 /* WaDisableSDEUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007755 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 +03007756 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007757
7758 /*
Imre Deak450174f2016-05-03 15:54:21 +03007759 * WaProgramL3SqcReg1Default:chv
7760 * See gfxspecs/Related Documents/Performance Guide/
7761 * LSQC Setting Recommendations.
7762 */
7763 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007764}
7765
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007766static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007767{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007768 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007769
Jani Nikula5f461662020-11-30 13:15:58 +02007770 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7771 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007772 GS_UNIT_CLOCK_GATE_DISABLE |
7773 CL_UNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007774 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007775 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7776 OVRUNIT_CLOCK_GATE_DISABLE |
7777 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007778 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007779 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007780 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007781
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007782 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007783}
7784
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007785static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007786{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007787 struct intel_uncore *uncore = &dev_priv->uncore;
7788
7789 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7790 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7791 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7792 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7793 intel_uncore_write16(uncore, DEUC, 0);
7794 intel_uncore_write(uncore,
7795 MI_ARB_STATE,
7796 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007797}
7798
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007799static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007800{
Jani Nikula5f461662020-11-30 13:15:58 +02007801 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007802 I965_RCC_CLOCK_GATE_DISABLE |
7803 I965_RCPB_CLOCK_GATE_DISABLE |
7804 I965_ISC_CLOCK_GATE_DISABLE |
7805 I965_FBC_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007806 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7807 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä20f94962013-06-07 10:47:02 +03007808 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007809}
7810
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007811static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007812{
Jani Nikula5f461662020-11-30 13:15:58 +02007813 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007814
7815 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7816 DSTATE_DOT_CLOCK_GATING;
Jani Nikula5f461662020-11-30 13:15:58 +02007817 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007818
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007819 if (IS_PINEVIEW(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007820 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007821
7822 /* IIR "flip pending" means done if this bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007823 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007824
7825 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007826 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007827
7828 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007829 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 +03007830
Jani Nikula5f461662020-11-30 13:15:58 +02007831 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007832 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007833}
7834
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007835static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007836{
Jani Nikula5f461662020-11-30 13:15:58 +02007837 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007838
7839 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007840 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007841 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007842
Jani Nikula5f461662020-11-30 13:15:58 +02007843 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007844 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007845
7846 /*
7847 * Have FBC ignore 3D activity since we use software
7848 * render tracking, and otherwise a pure 3D workload
7849 * (even if it just renders a single frame and then does
7850 * abosultely nothing) would not allow FBC to recompress
7851 * until a 2D blit occurs.
7852 */
Jani Nikula5f461662020-11-30 13:15:58 +02007853 intel_uncore_write(&dev_priv->uncore, SCPD0,
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007854 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007855}
7856
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007857static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007858{
Jani Nikula5f461662020-11-30 13:15:58 +02007859 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007860 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7861 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007862}
7863
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007864void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007865{
Dave Airlieeba4b792021-09-29 01:58:07 +03007866 dev_priv->clock_gating_funcs->init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007867}
7868
Ville Syrjälä712bf362016-10-31 22:37:23 +02007869void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007870{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007871 if (HAS_PCH_LPT(dev_priv))
7872 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007873}
7874
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007875static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007876{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007877 drm_dbg_kms(&dev_priv->drm,
7878 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007879}
7880
Dave Airlieeba4b792021-09-29 01:58:07 +03007881#define CG_FUNCS(platform) \
7882static const struct drm_i915_clock_gating_funcs platform##_clock_gating_funcs = { \
7883 .init_clock_gating = platform##_init_clock_gating, \
7884}
7885
Stuart Summersd73dd1f2021-11-02 15:25:09 -07007886CG_FUNCS(xehpsdv);
Dave Airlieeba4b792021-09-29 01:58:07 +03007887CG_FUNCS(adlp);
7888CG_FUNCS(dg1);
7889CG_FUNCS(gen12lp);
7890CG_FUNCS(icl);
7891CG_FUNCS(cfl);
7892CG_FUNCS(skl);
7893CG_FUNCS(kbl);
7894CG_FUNCS(bxt);
7895CG_FUNCS(glk);
7896CG_FUNCS(bdw);
7897CG_FUNCS(chv);
7898CG_FUNCS(hsw);
7899CG_FUNCS(ivb);
7900CG_FUNCS(vlv);
7901CG_FUNCS(gen6);
7902CG_FUNCS(ilk);
7903CG_FUNCS(g4x);
7904CG_FUNCS(i965gm);
7905CG_FUNCS(i965g);
7906CG_FUNCS(gen3);
7907CG_FUNCS(i85x);
7908CG_FUNCS(i830);
7909CG_FUNCS(nop);
7910#undef CG_FUNCS
7911
Imre Deakbb400da2016-03-16 13:38:54 +02007912/**
7913 * intel_init_clock_gating_hooks - setup the clock gating hooks
7914 * @dev_priv: device private
7915 *
7916 * Setup the hooks that configure which clocks of a given platform can be
7917 * gated and also apply various GT and display specific workarounds for these
7918 * platforms. Note that some GT specific workarounds are applied separately
7919 * when GPU contexts or batchbuffers start their execution.
7920 */
7921void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7922{
Stuart Summersd73dd1f2021-11-02 15:25:09 -07007923 if (IS_XEHPSDV(dev_priv))
7924 dev_priv->clock_gating_funcs = &xehpsdv_clock_gating_funcs;
7925 else if (IS_ALDERLAKE_P(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007926 dev_priv->clock_gating_funcs = &adlp_clock_gating_funcs;
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007927 else if (IS_DG1(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007928 dev_priv->clock_gating_funcs = &dg1_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007929 else if (GRAPHICS_VER(dev_priv) == 12)
Dave Airlieeba4b792021-09-29 01:58:07 +03007930 dev_priv->clock_gating_funcs = &gen12lp_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007931 else if (GRAPHICS_VER(dev_priv) == 11)
Dave Airlieeba4b792021-09-29 01:58:07 +03007932 dev_priv->clock_gating_funcs = &icl_clock_gating_funcs;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007933 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007934 dev_priv->clock_gating_funcs = &cfl_clock_gating_funcs;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007935 else if (IS_SKYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007936 dev_priv->clock_gating_funcs = &skl_clock_gating_funcs;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007937 else if (IS_KABYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007938 dev_priv->clock_gating_funcs = &kbl_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007939 else if (IS_BROXTON(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007940 dev_priv->clock_gating_funcs = &bxt_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007941 else if (IS_GEMINILAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007942 dev_priv->clock_gating_funcs = &glk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007943 else if (IS_BROADWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007944 dev_priv->clock_gating_funcs = &bdw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007945 else if (IS_CHERRYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007946 dev_priv->clock_gating_funcs = &chv_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007947 else if (IS_HASWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007948 dev_priv->clock_gating_funcs = &hsw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007949 else if (IS_IVYBRIDGE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007950 dev_priv->clock_gating_funcs = &ivb_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007951 else if (IS_VALLEYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007952 dev_priv->clock_gating_funcs = &vlv_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007953 else if (GRAPHICS_VER(dev_priv) == 6)
Dave Airlieeba4b792021-09-29 01:58:07 +03007954 dev_priv->clock_gating_funcs = &gen6_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007955 else if (GRAPHICS_VER(dev_priv) == 5)
Dave Airlieeba4b792021-09-29 01:58:07 +03007956 dev_priv->clock_gating_funcs = &ilk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007957 else if (IS_G4X(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007958 dev_priv->clock_gating_funcs = &g4x_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007959 else if (IS_I965GM(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007960 dev_priv->clock_gating_funcs = &i965gm_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007961 else if (IS_I965G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007962 dev_priv->clock_gating_funcs = &i965g_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007963 else if (GRAPHICS_VER(dev_priv) == 3)
Dave Airlieeba4b792021-09-29 01:58:07 +03007964 dev_priv->clock_gating_funcs = &gen3_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007965 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007966 dev_priv->clock_gating_funcs = &i85x_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007967 else if (GRAPHICS_VER(dev_priv) == 2)
Dave Airlieeba4b792021-09-29 01:58:07 +03007968 dev_priv->clock_gating_funcs = &i830_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007969 else {
7970 MISSING_CASE(INTEL_DEVID(dev_priv));
Dave Airlieeba4b792021-09-29 01:58:07 +03007971 dev_priv->clock_gating_funcs = &nop_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007972 }
7973}
7974
Dave Airliedde98a52021-09-29 01:58:08 +03007975static const struct drm_i915_wm_disp_funcs skl_wm_funcs = {
7976 .compute_global_watermarks = skl_compute_wm,
7977};
7978
7979static const struct drm_i915_wm_disp_funcs ilk_wm_funcs = {
7980 .compute_pipe_wm = ilk_compute_pipe_wm,
7981 .compute_intermediate_wm = ilk_compute_intermediate_wm,
7982 .initial_watermarks = ilk_initial_watermarks,
7983 .optimize_watermarks = ilk_optimize_watermarks,
7984};
7985
7986static const struct drm_i915_wm_disp_funcs vlv_wm_funcs = {
7987 .compute_pipe_wm = vlv_compute_pipe_wm,
7988 .compute_intermediate_wm = vlv_compute_intermediate_wm,
7989 .initial_watermarks = vlv_initial_watermarks,
7990 .optimize_watermarks = vlv_optimize_watermarks,
7991 .atomic_update_watermarks = vlv_atomic_update_fifo,
7992};
7993
7994static const struct drm_i915_wm_disp_funcs g4x_wm_funcs = {
7995 .compute_pipe_wm = g4x_compute_pipe_wm,
7996 .compute_intermediate_wm = g4x_compute_intermediate_wm,
7997 .initial_watermarks = g4x_initial_watermarks,
7998 .optimize_watermarks = g4x_optimize_watermarks,
7999};
8000
8001static const struct drm_i915_wm_disp_funcs pnv_wm_funcs = {
8002 .update_wm = pnv_update_wm,
8003};
8004
8005static const struct drm_i915_wm_disp_funcs i965_wm_funcs = {
8006 .update_wm = i965_update_wm,
8007};
8008
8009static const struct drm_i915_wm_disp_funcs i9xx_wm_funcs = {
8010 .update_wm = i9xx_update_wm,
8011};
8012
8013static const struct drm_i915_wm_disp_funcs i845_wm_funcs = {
8014 .update_wm = i845_update_wm,
8015};
8016
8017static const struct drm_i915_wm_disp_funcs nop_funcs = {
8018};
8019
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008020/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008021void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008022{
Daniel Vetterc921aba2012-04-26 23:28:17 +02008023 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008024 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08008025 pnv_get_mem_freq(dev_priv);
Lucas De Marchi651e7d42021-06-05 21:50:49 -07008026 else if (GRAPHICS_VER(dev_priv) == 5)
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08008027 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008028
James Ausmusb068a862019-10-09 10:23:14 -07008029 if (intel_has_sagv(dev_priv))
8030 skl_setup_sagv_block_time(dev_priv);
8031
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008032 /* For FIFO watermark updates */
Matt Roper7dadd282021-03-19 21:42:43 -07008033 if (DISPLAY_VER(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008034 skl_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008035 dev_priv->wm_disp = &skl_wm_funcs;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008036 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008037 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008038
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008039 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008040 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008041 (DISPLAY_VER(dev_priv) != 5 && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008042 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Dave Airliedde98a52021-09-29 01:58:08 +03008043 dev_priv->wm_disp = &ilk_wm_funcs;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008044 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008045 drm_dbg_kms(&dev_priv->drm,
8046 "Failed to read display plane latency. "
8047 "Disable CxSR\n");
Dave Airliedde98a52021-09-29 01:58:08 +03008048 dev_priv->wm_disp = &nop_funcs;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008049 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008050 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008051 vlv_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008052 dev_priv->wm_disp = &vlv_wm_funcs;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008053 } else if (IS_G4X(dev_priv)) {
8054 g4x_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008055 dev_priv->wm_disp = &g4x_wm_funcs;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008056 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00008057 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008058 dev_priv->is_ddr3,
8059 dev_priv->fsb_freq,
8060 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008061 drm_info(&dev_priv->drm,
8062 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008063 "(found ddr%s fsb freq %d, mem freq %d), "
8064 "disabling CxSR\n",
8065 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8066 dev_priv->fsb_freq, dev_priv->mem_freq);
8067 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008068 intel_set_memory_cxsr(dev_priv, false);
Dave Airliedde98a52021-09-29 01:58:08 +03008069 dev_priv->wm_disp = &nop_funcs;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008070 } else
Dave Airliedde98a52021-09-29 01:58:08 +03008071 dev_priv->wm_disp = &pnv_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008072 } else if (DISPLAY_VER(dev_priv) == 4) {
Dave Airliedde98a52021-09-29 01:58:08 +03008073 dev_priv->wm_disp = &i965_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008074 } else if (DISPLAY_VER(dev_priv) == 3) {
Dave Airliedde98a52021-09-29 01:58:08 +03008075 dev_priv->wm_disp = &i9xx_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008076 } else if (DISPLAY_VER(dev_priv) == 2) {
Dave Airlie758b2fc2021-09-29 01:57:46 +03008077 if (INTEL_NUM_PIPES(dev_priv) == 1)
Dave Airliedde98a52021-09-29 01:58:08 +03008078 dev_priv->wm_disp = &i845_wm_funcs;
Dave Airlie758b2fc2021-09-29 01:57:46 +03008079 else
Dave Airliedde98a52021-09-29 01:58:08 +03008080 dev_priv->wm_disp = &i9xx_wm_funcs;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008081 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008082 drm_err(&dev_priv->drm,
8083 "unexpected fall-through in %s\n", __func__);
Dave Airliedde98a52021-09-29 01:58:08 +03008084 dev_priv->wm_disp = &nop_funcs;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008085 }
8086}
8087
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008088void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008089{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01008090 dev_priv->runtime_pm.suspended = false;
8091 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008092}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02008093
8094static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
8095{
8096 struct intel_dbuf_state *dbuf_state;
8097
8098 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
8099 if (!dbuf_state)
8100 return NULL;
8101
8102 return &dbuf_state->base;
8103}
8104
8105static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
8106 struct intel_global_state *state)
8107{
8108 kfree(state);
8109}
8110
8111static const struct intel_global_state_funcs intel_dbuf_funcs = {
8112 .atomic_duplicate_state = intel_dbuf_duplicate_state,
8113 .atomic_destroy_state = intel_dbuf_destroy_state,
8114};
8115
8116struct intel_dbuf_state *
8117intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
8118{
8119 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8120 struct intel_global_state *dbuf_state;
8121
8122 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
8123 if (IS_ERR(dbuf_state))
8124 return ERR_CAST(dbuf_state);
8125
8126 return to_intel_dbuf_state(dbuf_state);
8127}
8128
8129int intel_dbuf_init(struct drm_i915_private *dev_priv)
8130{
8131 struct intel_dbuf_state *dbuf_state;
8132
8133 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
8134 if (!dbuf_state)
8135 return -ENOMEM;
8136
8137 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
8138 &dbuf_state->base, &intel_dbuf_funcs);
8139
8140 return 0;
8141}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008142
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008143/*
8144 * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
8145 * update the request state of all DBUS slices.
8146 */
8147static void update_mbus_pre_enable(struct intel_atomic_state *state)
8148{
8149 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8150 u32 mbus_ctl, dbuf_min_tracker_val;
8151 enum dbuf_slice slice;
8152 const struct intel_dbuf_state *dbuf_state =
8153 intel_atomic_get_new_dbuf_state(state);
8154
8155 if (!IS_ALDERLAKE_P(dev_priv))
8156 return;
8157
8158 /*
8159 * TODO: Implement vblank synchronized MBUS joining changes.
8160 * Must be properly coordinated with dbuf reprogramming.
8161 */
8162 if (dbuf_state->joined_mbus) {
8163 mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
8164 MBUS_JOIN_PIPE_SELECT_NONE;
8165 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
8166 } else {
8167 mbus_ctl = MBUS_HASHING_MODE_2x2 |
8168 MBUS_JOIN_PIPE_SELECT_NONE;
8169 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
8170 }
8171
8172 intel_de_rmw(dev_priv, MBUS_CTL,
8173 MBUS_HASHING_MODE_MASK | MBUS_JOIN |
8174 MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
8175
8176 for_each_dbuf_slice(dev_priv, slice)
8177 intel_de_rmw(dev_priv, DBUF_CTL_S(slice),
8178 DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
8179 dbuf_min_tracker_val);
8180}
8181
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008182void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
8183{
8184 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8185 const struct intel_dbuf_state *new_dbuf_state =
8186 intel_atomic_get_new_dbuf_state(state);
8187 const struct intel_dbuf_state *old_dbuf_state =
8188 intel_atomic_get_old_dbuf_state(state);
8189
8190 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008191 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8192 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008193 return;
8194
8195 WARN_ON(!new_dbuf_state->base.changed);
8196
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008197 update_mbus_pre_enable(state);
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008198 gen9_dbuf_slices_update(dev_priv,
8199 old_dbuf_state->enabled_slices |
8200 new_dbuf_state->enabled_slices);
8201}
8202
8203void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
8204{
8205 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8206 const struct intel_dbuf_state *new_dbuf_state =
8207 intel_atomic_get_new_dbuf_state(state);
8208 const struct intel_dbuf_state *old_dbuf_state =
8209 intel_atomic_get_old_dbuf_state(state);
8210
8211 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008212 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8213 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008214 return;
8215
8216 WARN_ON(!new_dbuf_state->base.changed);
8217
8218 gen9_dbuf_slices_update(dev_priv,
8219 new_dbuf_state->enabled_slices);
8220}