blob: e69258bcef226d9a5043e5956889b500e6beff18 [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"
Jani Nikula1d455f82019-08-06 14:39:33 +030038#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030039#include "display/intel_fbc.h"
40#include "display/intel_sprite.h"
41
Andi Shyti0dc3c562019-10-20 19:41:39 +010042#include "gt/intel_llc.h"
43
Eugeni Dodonov85208be2012-04-16 22:20:34 -030044#include "i915_drv.h"
Jani Nikulaa10510a2020-02-27 19:00:47 +020045#include "i915_fixed.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030046#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030047#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030048#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010049#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020050#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030051
Jani Nikulaa10510a2020-02-27 19:00:47 +020052/* Stores plane specific WM parameters */
53struct skl_wm_params {
54 bool x_tiled, y_tiled;
55 bool rc_surface;
56 bool is_planar;
57 u32 width;
58 u8 cpp;
59 u32 plane_pixel_rate;
60 u32 y_min_scanlines;
61 u32 plane_bytes_per_line;
62 uint_fixed_16_16_t plane_blocks_per_line;
63 uint_fixed_16_16_t y_tile_minimum;
64 u32 linetime_us;
65 u32 dbuf_block_size;
66};
67
68/* used in computing the new watermarks state */
69struct intel_wm_config {
70 unsigned int num_pipes_active;
71 bool sprites_enabled;
72 bool sprites_scaled;
73};
74
Ville Syrjälä46f16e62016-10-31 22:37:22 +020075static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030076{
Ville Syrjälä93564042017-08-24 22:10:51 +030077 if (HAS_LLC(dev_priv)) {
78 /*
79 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080080 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030081 *
82 * Must match Sampler, Pixel Back End, and Media. See
83 * WaCompressedResourceSamplerPbeMediaNewHashMode.
84 */
Jani Nikula5f461662020-11-30 13:15:58 +020085 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
86 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
Ville Syrjälä93564042017-08-24 22:10:51 +030087 SKL_DE_COMPRESSED_HASH_MODE);
88 }
89
Rodrigo Vivi82525c12017-06-08 08:50:00 -070090 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +020091 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
92 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030093
Rodrigo Vivi82525c12017-06-08 08:50:00 -070094 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +020095 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
96 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030097
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +030098 /*
99 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
100 * Display WA #0859: skl,bxt,kbl,glk,cfl
101 */
Jani Nikula5f461662020-11-30 13:15:58 +0200102 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 +0300103 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300104}
105
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200106static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200107{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200108 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200109
Nick Hoatha7546152015-06-29 14:07:32 +0100110 /* WaDisableSDEUnitClockGating:bxt */
Jani Nikula5f461662020-11-30 13:15:58 +0200111 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Nick Hoatha7546152015-06-29 14:07:32 +0100112 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
113
Imre Deak32608ca2015-03-11 11:10:27 +0200114 /*
115 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200116 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200117 */
Jani Nikula5f461662020-11-30 13:15:58 +0200118 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200119 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200120
121 /*
122 * Wa: Backlight PWM may stop in the asserted state, causing backlight
123 * to stay fully on.
124 */
Jani Nikula5f461662020-11-30 13:15:58 +0200125 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 +0200126 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530127
128 /*
129 * Lower the display internal timeout.
130 * This is needed to avoid any hard hangs when DSI port PLL
131 * is off and a MMIO access is attempted by any privilege
132 * application, using batch buffers or any other means.
133 */
Jani Nikula5f461662020-11-30 13:15:58 +0200134 intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300135
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300136 /*
137 * WaFbcTurnOffFbcWatermark:bxt
138 * Display WA #0562: bxt
139 */
Jani Nikula5f461662020-11-30 13:15:58 +0200140 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 +0300141 DISP_FBC_WM_DIS);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300142
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300143 /*
144 * WaFbcHighMemBwCorruptionAvoidance:bxt
145 * Display WA #0883: bxt
146 */
Jani Nikula5f461662020-11-30 13:15:58 +0200147 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 +0300148 ILK_DPFC_DISABLE_DUMMY0);
Imre Deaka82abe42015-03-27 14:00:04 +0200149}
150
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200151static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
152{
153 gen9_init_clock_gating(dev_priv);
154
155 /*
156 * WaDisablePWMClockGating:glk
157 * Backlight PWM may stop in the asserted state, causing backlight
158 * to stay fully on.
159 */
Jani Nikula5f461662020-11-30 13:15:58 +0200160 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 +0200161 PWM1_GATING_DIS | PWM2_GATING_DIS);
162}
163
Lucas De Marchi1d218222019-12-24 00:40:04 -0800164static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200165{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200166 u32 tmp;
167
Jani Nikula5f461662020-11-30 13:15:58 +0200168 tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200169
170 switch (tmp & CLKCFG_FSB_MASK) {
171 case CLKCFG_FSB_533:
172 dev_priv->fsb_freq = 533; /* 133*4 */
173 break;
174 case CLKCFG_FSB_800:
175 dev_priv->fsb_freq = 800; /* 200*4 */
176 break;
177 case CLKCFG_FSB_667:
178 dev_priv->fsb_freq = 667; /* 167*4 */
179 break;
180 case CLKCFG_FSB_400:
181 dev_priv->fsb_freq = 400; /* 100*4 */
182 break;
183 }
184
185 switch (tmp & CLKCFG_MEM_MASK) {
186 case CLKCFG_MEM_533:
187 dev_priv->mem_freq = 533;
188 break;
189 case CLKCFG_MEM_667:
190 dev_priv->mem_freq = 667;
191 break;
192 case CLKCFG_MEM_800:
193 dev_priv->mem_freq = 800;
194 break;
195 }
196
197 /* detect pineview DDR3 setting */
Jani Nikula5f461662020-11-30 13:15:58 +0200198 tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200199 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
200}
201
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800202static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200203{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200204 u16 ddrpll, csipll;
205
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100206 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
207 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200208
209 switch (ddrpll & 0xff) {
210 case 0xc:
211 dev_priv->mem_freq = 800;
212 break;
213 case 0x10:
214 dev_priv->mem_freq = 1066;
215 break;
216 case 0x14:
217 dev_priv->mem_freq = 1333;
218 break;
219 case 0x18:
220 dev_priv->mem_freq = 1600;
221 break;
222 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300223 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
224 ddrpll & 0xff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200225 dev_priv->mem_freq = 0;
226 break;
227 }
228
Daniel Vetterc921aba2012-04-26 23:28:17 +0200229 switch (csipll & 0x3ff) {
230 case 0x00c:
231 dev_priv->fsb_freq = 3200;
232 break;
233 case 0x00e:
234 dev_priv->fsb_freq = 3733;
235 break;
236 case 0x010:
237 dev_priv->fsb_freq = 4266;
238 break;
239 case 0x012:
240 dev_priv->fsb_freq = 4800;
241 break;
242 case 0x014:
243 dev_priv->fsb_freq = 5333;
244 break;
245 case 0x016:
246 dev_priv->fsb_freq = 5866;
247 break;
248 case 0x018:
249 dev_priv->fsb_freq = 6400;
250 break;
251 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300252 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
253 csipll & 0x3ff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200254 dev_priv->fsb_freq = 0;
255 break;
256 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200257}
258
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300259static const struct cxsr_latency cxsr_latency_table[] = {
260 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
261 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
262 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
263 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
264 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
265
266 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
267 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
268 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
269 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
270 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
271
272 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
273 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
274 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
275 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
276 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
277
278 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
279 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
280 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
281 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
282 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
283
284 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
285 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
286 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
287 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
288 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
289
290 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
291 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
292 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
293 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
294 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
295};
296
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100297static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
298 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300299 int fsb,
300 int mem)
301{
302 const struct cxsr_latency *latency;
303 int i;
304
305 if (fsb == 0 || mem == 0)
306 return NULL;
307
308 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
309 latency = &cxsr_latency_table[i];
310 if (is_desktop == latency->is_desktop &&
311 is_ddr3 == latency->is_ddr3 &&
312 fsb == latency->fsb_freq && mem == latency->mem_freq)
313 return latency;
314 }
315
316 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
317
318 return NULL;
319}
320
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200321static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
322{
323 u32 val;
324
Chris Wilson337fa6e2019-04-26 09:17:20 +0100325 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200326
327 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
328 if (enable)
329 val &= ~FORCE_DDR_HIGH_FREQ;
330 else
331 val |= FORCE_DDR_HIGH_FREQ;
332 val &= ~FORCE_DDR_LOW_FREQ;
333 val |= FORCE_DDR_FREQ_REQ_ACK;
334 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
335
336 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
337 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300338 drm_err(&dev_priv->drm,
339 "timed out waiting for Punit DDR DVFS request\n");
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200340
Chris Wilson337fa6e2019-04-26 09:17:20 +0100341 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200342}
343
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200344static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
345{
346 u32 val;
347
Chris Wilson337fa6e2019-04-26 09:17:20 +0100348 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200349
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200350 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200351 if (enable)
352 val |= DSP_MAXFIFO_PM5_ENABLE;
353 else
354 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200355 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200356
Chris Wilson337fa6e2019-04-26 09:17:20 +0100357 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200358}
359
Ville Syrjäläf4998962015-03-10 17:02:21 +0200360#define FW_WM(value, plane) \
361 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
362
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200363static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300364{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200365 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300366 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300367
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100368 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200369 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
370 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
371 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200372 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200373 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
374 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
375 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200376 } else if (IS_PINEVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200377 val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200378 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
379 if (enable)
380 val |= PINEVIEW_SELF_REFRESH_EN;
381 else
382 val &= ~PINEVIEW_SELF_REFRESH_EN;
Jani Nikula5f461662020-11-30 13:15:58 +0200383 intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
384 intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100385 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200386 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300387 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
388 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200389 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
390 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100391 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300392 /*
393 * FIXME can't find a bit like this for 915G, and
394 * and yet it does have the related watermark in
395 * FW_BLC_SELF. What's going on?
396 */
Jani Nikula5f461662020-11-30 13:15:58 +0200397 was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300398 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
399 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200400 intel_uncore_write(&dev_priv->uncore, INSTPM, val);
401 intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300402 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200403 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300404 }
405
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200406 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
407
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300408 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
409 enableddisabled(enable),
410 enableddisabled(was_enabled));
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200411
412 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300413}
414
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300415/**
416 * intel_set_memory_cxsr - Configure CxSR state
417 * @dev_priv: i915 device
418 * @enable: Allow vs. disallow CxSR
419 *
420 * Allow or disallow the system to enter a special CxSR
421 * (C-state self refresh) state. What typically happens in CxSR mode
422 * is that several display FIFOs may get combined into a single larger
423 * FIFO for a particular plane (so called max FIFO mode) to allow the
424 * system to defer memory fetches longer, and the memory will enter
425 * self refresh.
426 *
427 * Note that enabling CxSR does not guarantee that the system enter
428 * this special mode, nor does it guarantee that the system stays
429 * in that mode once entered. So this just allows/disallows the system
430 * to autonomously utilize the CxSR mode. Other factors such as core
431 * C-states will affect when/if the system actually enters/exits the
432 * CxSR mode.
433 *
434 * Note that on VLV/CHV this actually only controls the max FIFO mode,
435 * and the system is free to enter/exit memory self refresh at any time
436 * even when the use of CxSR has been disallowed.
437 *
438 * While the system is actually in the CxSR/max FIFO mode, some plane
439 * control registers will not get latched on vblank. Thus in order to
440 * guarantee the system will respond to changes in the plane registers
441 * we must always disallow CxSR prior to making changes to those registers.
442 * Unfortunately the system will re-evaluate the CxSR conditions at
443 * frame start which happens after vblank start (which is when the plane
444 * registers would get latched), so we can't proceed with the plane update
445 * during the same frame where we disallowed CxSR.
446 *
447 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
448 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
449 * the hardware w.r.t. HPLL SR when writing to plane registers.
450 * Disallowing just CxSR is sufficient.
451 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200452bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200453{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200454 bool ret;
455
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200456 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200457 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300458 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
459 dev_priv->wm.vlv.cxsr = enable;
460 else if (IS_G4X(dev_priv))
461 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200462 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200463
464 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200465}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200466
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300467/*
468 * Latency for FIFO fetches is dependent on several factors:
469 * - memory configuration (speed, channels)
470 * - chipset
471 * - current MCH state
472 * It can be fairly high in some situations, so here we assume a fairly
473 * pessimal value. It's a tradeoff between extra memory fetches (if we
474 * set this value too high, the FIFO will fetch frequently to stay full)
475 * and power consumption (set it too low to save power and we might see
476 * FIFO underruns and display "flicker").
477 *
478 * A value of 5us seems to be a good balance; safe for very low end
479 * platforms but not overly aggressive on lower latency configs.
480 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100481static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482
Ville Syrjäläb5004722015-03-05 21:19:47 +0200483#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
484 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
485
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200486static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200487{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100488 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200489 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200490 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200491 enum pipe pipe = crtc->pipe;
492 int sprite0_start, sprite1_start;
Kees Cook2713eb42020-02-20 16:05:17 -0800493 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200494
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200495 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200496 case PIPE_A:
Jani Nikula5f461662020-11-30 13:15:58 +0200497 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
498 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200499 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
500 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
501 break;
502 case PIPE_B:
Jani Nikula5f461662020-11-30 13:15:58 +0200503 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
504 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200505 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
506 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
507 break;
508 case PIPE_C:
Jani Nikula5f461662020-11-30 13:15:58 +0200509 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
510 dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200511 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
512 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
513 break;
514 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200515 MISSING_CASE(pipe);
516 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200517 }
518
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200519 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
520 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
521 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
522 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200523}
524
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200525static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
526 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300527{
Jani Nikula5f461662020-11-30 13:15:58 +0200528 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300529 int size;
530
531 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200532 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
534
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300535 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
536 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300537
538 return size;
539}
540
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200541static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
542 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300543{
Jani Nikula5f461662020-11-30 13:15:58 +0200544 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545 int size;
546
547 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200548 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300549 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
550 size >>= 1; /* Convert to cachelines */
551
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300552 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
553 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300554
555 return size;
556}
557
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200558static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
559 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300560{
Jani Nikula5f461662020-11-30 13:15:58 +0200561 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300562 int size;
563
564 size = dsparb & 0x7f;
565 size >>= 2; /* Convert to cachelines */
566
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300567 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
568 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300569
570 return size;
571}
572
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800574static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300575 .fifo_size = PINEVIEW_DISPLAY_FIFO,
576 .max_wm = PINEVIEW_MAX_WM,
577 .default_wm = PINEVIEW_DFT_WM,
578 .guard_size = PINEVIEW_GUARD_WM,
579 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300580};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800581
582static const struct intel_watermark_params pnv_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300583 .fifo_size = PINEVIEW_DISPLAY_FIFO,
584 .max_wm = PINEVIEW_MAX_WM,
585 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
586 .guard_size = PINEVIEW_GUARD_WM,
587 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300588};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800589
590static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300591 .fifo_size = PINEVIEW_CURSOR_FIFO,
592 .max_wm = PINEVIEW_CURSOR_MAX_WM,
593 .default_wm = PINEVIEW_CURSOR_DFT_WM,
594 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
595 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300596};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800597
598static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300599 .fifo_size = PINEVIEW_CURSOR_FIFO,
600 .max_wm = PINEVIEW_CURSOR_MAX_WM,
601 .default_wm = PINEVIEW_CURSOR_DFT_WM,
602 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
603 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300604};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800605
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300606static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300607 .fifo_size = I965_CURSOR_FIFO,
608 .max_wm = I965_CURSOR_MAX_WM,
609 .default_wm = I965_CURSOR_DFT_WM,
610 .guard_size = 2,
611 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300612};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800613
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300614static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300615 .fifo_size = I945_FIFO_SIZE,
616 .max_wm = I915_MAX_WM,
617 .default_wm = 1,
618 .guard_size = 2,
619 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300620};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800621
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300622static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300623 .fifo_size = I915_FIFO_SIZE,
624 .max_wm = I915_MAX_WM,
625 .default_wm = 1,
626 .guard_size = 2,
627 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300628};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800629
Ville Syrjälä9d539102014-08-15 01:21:53 +0300630static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300631 .fifo_size = I855GM_FIFO_SIZE,
632 .max_wm = I915_MAX_WM,
633 .default_wm = 1,
634 .guard_size = 2,
635 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300636};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800637
Ville Syrjälä9d539102014-08-15 01:21:53 +0300638static const struct intel_watermark_params i830_bc_wm_info = {
639 .fifo_size = I855GM_FIFO_SIZE,
640 .max_wm = I915_MAX_WM/2,
641 .default_wm = 1,
642 .guard_size = 2,
643 .cacheline_size = I830_FIFO_LINE_SIZE,
644};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800645
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200646static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300647 .fifo_size = I830_FIFO_SIZE,
648 .max_wm = I915_MAX_WM,
649 .default_wm = 1,
650 .guard_size = 2,
651 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300652};
653
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300654/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300655 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
656 * @pixel_rate: Pipe pixel rate in kHz
657 * @cpp: Plane bytes per pixel
658 * @latency: Memory wakeup latency in 0.1us units
659 *
660 * Compute the watermark using the method 1 or "small buffer"
661 * formula. The caller may additonally add extra cachelines
662 * to account for TLB misses and clock crossings.
663 *
664 * This method is concerned with the short term drain rate
665 * of the FIFO, ie. it does not account for blanking periods
666 * which would effectively reduce the average drain rate across
667 * a longer period. The name "small" refers to the fact the
668 * FIFO is relatively small compared to the amount of data
669 * fetched.
670 *
671 * The FIFO level vs. time graph might look something like:
672 *
673 * |\ |\
674 * | \ | \
675 * __---__---__ (- plane active, _ blanking)
676 * -> time
677 *
678 * or perhaps like this:
679 *
680 * |\|\ |\|\
681 * __----__----__ (- plane active, _ blanking)
682 * -> time
683 *
684 * Returns:
685 * The watermark in bytes
686 */
687static unsigned int intel_wm_method1(unsigned int pixel_rate,
688 unsigned int cpp,
689 unsigned int latency)
690{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200691 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300692
Ville Syrjäläd492a292019-04-08 18:27:01 +0300693 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300694 ret = DIV_ROUND_UP_ULL(ret, 10000);
695
696 return ret;
697}
698
699/**
700 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
701 * @pixel_rate: Pipe pixel rate in kHz
702 * @htotal: Pipe horizontal total
703 * @width: Plane width in pixels
704 * @cpp: Plane bytes per pixel
705 * @latency: Memory wakeup latency in 0.1us units
706 *
707 * Compute the watermark using the method 2 or "large buffer"
708 * formula. The caller may additonally add extra cachelines
709 * to account for TLB misses and clock crossings.
710 *
711 * This method is concerned with the long term drain rate
712 * of the FIFO, ie. it does account for blanking periods
713 * which effectively reduce the average drain rate across
714 * a longer period. The name "large" refers to the fact the
715 * FIFO is relatively large compared to the amount of data
716 * fetched.
717 *
718 * The FIFO level vs. time graph might look something like:
719 *
720 * |\___ |\___
721 * | \___ | \___
722 * | \ | \
723 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
724 * -> time
725 *
726 * Returns:
727 * The watermark in bytes
728 */
729static unsigned int intel_wm_method2(unsigned int pixel_rate,
730 unsigned int htotal,
731 unsigned int width,
732 unsigned int cpp,
733 unsigned int latency)
734{
735 unsigned int ret;
736
737 /*
738 * FIXME remove once all users are computing
739 * watermarks in the correct place.
740 */
741 if (WARN_ON_ONCE(htotal == 0))
742 htotal = 1;
743
744 ret = (latency * pixel_rate) / (htotal * 10000);
745 ret = (ret + 1) * width * cpp;
746
747 return ret;
748}
749
750/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300751 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300752 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300753 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000754 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200755 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300756 * @latency_ns: memory latency for the platform
757 *
758 * Calculate the watermark level (the level at which the display plane will
759 * start fetching from memory again). Each chip has a different display
760 * FIFO size and allocation, so the caller needs to figure that out and pass
761 * in the correct intel_watermark_params structure.
762 *
763 * As the pixel clock runs, the FIFO will be drained at a rate that depends
764 * on the pixel size. When it reaches the watermark level, it'll start
765 * fetching FIFO line sized based chunks from memory until the FIFO fills
766 * past the watermark point. If the FIFO drains completely, a FIFO underrun
767 * will occur, and a display engine hang could result.
768 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300769static unsigned int intel_calculate_wm(int pixel_rate,
770 const struct intel_watermark_params *wm,
771 int fifo_size, int cpp,
772 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300773{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300774 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775
776 /*
777 * Note: we need to make sure we don't overflow for various clock &
778 * latency values.
779 * clocks go from a few thousand to several hundred thousand.
780 * latency is usually a few thousand
781 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300782 entries = intel_wm_method1(pixel_rate, cpp,
783 latency_ns / 100);
784 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
785 wm->guard_size;
786 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300787
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300788 wm_size = fifo_size - entries;
789 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300790
791 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300792 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300793 wm_size = wm->max_wm;
794 if (wm_size <= 0)
795 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300796
797 /*
798 * Bspec seems to indicate that the value shouldn't be lower than
799 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
800 * Lets go for 8 which is the burst size since certain platforms
801 * already use a hardcoded 8 (which is what the spec says should be
802 * done).
803 */
804 if (wm_size <= 8)
805 wm_size = 8;
806
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300807 return wm_size;
808}
809
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300810static bool is_disabling(int old, int new, int threshold)
811{
812 return old >= threshold && new < threshold;
813}
814
815static bool is_enabling(int old, int new, int threshold)
816{
817 return old < threshold && new >= threshold;
818}
819
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300820static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
821{
822 return dev_priv->wm.max_level + 1;
823}
824
Ville Syrjälä24304d812017-03-14 17:10:49 +0200825static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
826 const struct intel_plane_state *plane_state)
827{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100828 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200829
830 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100831 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200832 return false;
833
834 /*
835 * Treat cursor with fb as always visible since cursor updates
836 * can happen faster than the vrefresh rate, and the current
837 * watermark code doesn't handle that correctly. Cursor updates
838 * which set/clear the fb or change the cursor size are going
839 * to get throttled by intel_legacy_cursor_update() to work
840 * around this problem with the watermark code.
841 */
842 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100843 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200844 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100845 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200846}
847
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200848static bool intel_crtc_active(struct intel_crtc *crtc)
849{
850 /* Be paranoid as we can arrive here with only partial
851 * state retrieved from the hardware during setup.
852 *
853 * We can ditch the adjusted_mode.crtc_clock check as soon
854 * as Haswell has gained clock readout/fastboot support.
855 *
856 * We can ditch the crtc->primary->state->fb check as soon as we can
857 * properly reconstruct framebuffers.
858 *
859 * FIXME: The intel_crtc->active here should be switched to
860 * crtc->state->active once we have proper CRTC states wired up
861 * for atomic.
862 */
863 return crtc->active && crtc->base.primary->state->fb &&
864 crtc->config->hw.adjusted_mode.crtc_clock;
865}
866
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200867static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200869 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300870
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200871 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200872 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300873 if (enabled)
874 return NULL;
875 enabled = crtc;
876 }
877 }
878
879 return enabled;
880}
881
Lucas De Marchi1d218222019-12-24 00:40:04 -0800882static void pnv_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300883{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200884 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200885 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300886 const struct cxsr_latency *latency;
887 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300888 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300889
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000890 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100891 dev_priv->is_ddr3,
892 dev_priv->fsb_freq,
893 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300894 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300895 drm_dbg_kms(&dev_priv->drm,
896 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300897 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300898 return;
899 }
900
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200901 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300902 if (crtc) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200903 const struct drm_display_mode *pipe_mode =
904 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200905 const struct drm_framebuffer *fb =
906 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200907 int cpp = fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200908 int clock = pipe_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300909
910 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800911 wm = intel_calculate_wm(clock, &pnv_display_wm,
912 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200913 cpp, latency->display_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200914 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300915 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200916 reg |= FW_WM(wm, SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200917 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300918 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300919
920 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800921 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
922 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300923 4, latency->cursor_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200924 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300925 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200926 reg |= FW_WM(wm, CURSOR_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200927 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300928
929 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800930 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
931 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200932 cpp, latency->display_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200933 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300934 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200935 reg |= FW_WM(wm, HPLL_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200936 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300937
938 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800939 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
940 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300941 4, latency->cursor_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200942 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300943 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200944 reg |= FW_WM(wm, HPLL_CURSOR);
Jani Nikula5f461662020-11-30 13:15:58 +0200945 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300946 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300947
Imre Deak5209b1f2014-07-01 12:36:17 +0300948 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300949 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300950 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300951 }
952}
953
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300954/*
955 * Documentation says:
956 * "If the line size is small, the TLB fetches can get in the way of the
957 * data fetches, causing some lag in the pixel data return which is not
958 * accounted for in the above formulas. The following adjustment only
959 * needs to be applied if eight whole lines fit in the buffer at once.
960 * The WM is adjusted upwards by the difference between the FIFO size
961 * and the size of 8 whole lines. This adjustment is always performed
962 * in the actual pixel depth regardless of whether FBC is enabled or not."
963 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000964static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300965{
966 int tlb_miss = fifo_size * 64 - width * cpp * 8;
967
968 return max(0, tlb_miss);
969}
970
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300971static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
972 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300973{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300974 enum pipe pipe;
975
976 for_each_pipe(dev_priv, pipe)
977 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
978
Jani Nikula5f461662020-11-30 13:15:58 +0200979 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300980 FW_WM(wm->sr.plane, SR) |
981 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
982 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
983 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200984 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300985 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
986 FW_WM(wm->sr.fbc, FBC_SR) |
987 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
988 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
989 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
990 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200991 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300992 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
993 FW_WM(wm->sr.cursor, CURSOR_SR) |
994 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
995 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300996
Jani Nikula5f461662020-11-30 13:15:58 +0200997 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300998}
999
Ville Syrjälä15665972015-03-10 16:16:28 +02001000#define FW_WM_VLV(value, plane) \
1001 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1002
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001003static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001004 const struct vlv_wm_values *wm)
1005{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001006 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001007
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001008 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001009 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1010
Jani Nikula5f461662020-11-30 13:15:58 +02001011 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001012 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1013 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1014 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1015 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1016 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001017
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001018 /*
1019 * Zero the (unused) WM1 watermarks, and also clear all the
1020 * high order bits so that there are no out of bounds values
1021 * present in the registers during the reprogramming.
1022 */
Jani Nikula5f461662020-11-30 13:15:58 +02001023 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1024 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1025 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1026 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1027 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001028
Jani Nikula5f461662020-11-30 13:15:58 +02001029 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001030 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001031 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1032 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1033 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001034 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001035 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1036 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1037 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001038 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001039 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001040
1041 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02001042 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001043 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1044 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001045 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001046 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1047 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Jani Nikula5f461662020-11-30 13:15:58 +02001048 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001049 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1050 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Jani Nikula5f461662020-11-30 13:15:58 +02001051 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001052 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001053 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1054 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1055 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1056 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1057 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1058 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1059 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1060 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1061 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001062 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02001063 intel_uncore_write(&dev_priv->uncore, DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001064 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1065 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001066 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001067 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001068 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1069 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1070 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1071 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1072 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1073 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001074 }
1075
Jani Nikula5f461662020-11-30 13:15:58 +02001076 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001077}
1078
Ville Syrjälä15665972015-03-10 16:16:28 +02001079#undef FW_WM_VLV
1080
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001081static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1082{
1083 /* all latencies in usec */
1084 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1085 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001086 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001087
Ville Syrjälä79d94302017-04-21 21:14:30 +03001088 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001089}
1090
1091static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1092{
1093 /*
1094 * DSPCNTR[13] supposedly controls whether the
1095 * primary plane can use the FIFO space otherwise
1096 * reserved for the sprite plane. It's not 100% clear
1097 * what the actual FIFO size is, but it looks like we
1098 * can happily set both primary and sprite watermarks
1099 * up to 127 cachelines. So that would seem to mean
1100 * that either DSPCNTR[13] doesn't do anything, or that
1101 * the total FIFO is >= 256 cachelines in size. Either
1102 * way, we don't seem to have to worry about this
1103 * repartitioning as the maximum watermark value the
1104 * register can hold for each plane is lower than the
1105 * minimum FIFO size.
1106 */
1107 switch (plane_id) {
1108 case PLANE_CURSOR:
1109 return 63;
1110 case PLANE_PRIMARY:
1111 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1112 case PLANE_SPRITE0:
1113 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1114 default:
1115 MISSING_CASE(plane_id);
1116 return 0;
1117 }
1118}
1119
1120static int g4x_fbc_fifo_size(int level)
1121{
1122 switch (level) {
1123 case G4X_WM_LEVEL_SR:
1124 return 7;
1125 case G4X_WM_LEVEL_HPLL:
1126 return 15;
1127 default:
1128 MISSING_CASE(level);
1129 return 0;
1130 }
1131}
1132
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001133static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1134 const struct intel_plane_state *plane_state,
1135 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001136{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001137 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001138 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001139 const struct drm_display_mode *pipe_mode =
1140 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001141 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1142 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001143
1144 if (latency == 0)
1145 return USHRT_MAX;
1146
1147 if (!intel_wm_plane_visible(crtc_state, plane_state))
1148 return 0;
1149
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001150 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001151
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001152 /*
1153 * Not 100% sure which way ELK should go here as the
1154 * spec only says CL/CTG should assume 32bpp and BW
1155 * doesn't need to. But as these things followed the
1156 * mobile vs. desktop lines on gen3 as well, let's
1157 * assume ELK doesn't need this.
1158 *
1159 * The spec also fails to list such a restriction for
1160 * the HPLL watermark, which seems a little strange.
1161 * Let's use 32bpp for the HPLL watermark as well.
1162 */
1163 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1164 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001165 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001166
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001167 clock = pipe_mode->crtc_clock;
1168 htotal = pipe_mode->crtc_htotal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001169
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001170 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001171
1172 if (plane->id == PLANE_CURSOR) {
1173 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1174 } else if (plane->id == PLANE_PRIMARY &&
1175 level == G4X_WM_LEVEL_NORMAL) {
1176 wm = intel_wm_method1(clock, cpp, latency);
1177 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001178 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001179
1180 small = intel_wm_method1(clock, cpp, latency);
1181 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1182
1183 wm = min(small, large);
1184 }
1185
1186 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1187 width, cpp);
1188
1189 wm = DIV_ROUND_UP(wm, 64) + 2;
1190
Chris Wilson1a1f1282017-11-07 14:03:38 +00001191 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001192}
1193
1194static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1195 int level, enum plane_id plane_id, u16 value)
1196{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001197 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001198 bool dirty = false;
1199
1200 for (; level < intel_wm_num_levels(dev_priv); level++) {
1201 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1202
1203 dirty |= raw->plane[plane_id] != value;
1204 raw->plane[plane_id] = value;
1205 }
1206
1207 return dirty;
1208}
1209
1210static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1211 int level, u16 value)
1212{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001213 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001214 bool dirty = false;
1215
1216 /* NORMAL level doesn't have an FBC watermark */
1217 level = max(level, G4X_WM_LEVEL_SR);
1218
1219 for (; level < intel_wm_num_levels(dev_priv); level++) {
1220 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1221
1222 dirty |= raw->fbc != value;
1223 raw->fbc = value;
1224 }
1225
1226 return dirty;
1227}
1228
Maarten Lankhorstec193642019-06-28 10:55:17 +02001229static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1230 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001231 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001232
1233static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1234 const struct intel_plane_state *plane_state)
1235{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001236 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001237 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001238 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1239 enum plane_id plane_id = plane->id;
1240 bool dirty = false;
1241 int level;
1242
1243 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1244 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1245 if (plane_id == PLANE_PRIMARY)
1246 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1247 goto out;
1248 }
1249
1250 for (level = 0; level < num_levels; level++) {
1251 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1252 int wm, max_wm;
1253
1254 wm = g4x_compute_wm(crtc_state, plane_state, level);
1255 max_wm = g4x_plane_fifo_size(plane_id, level);
1256
1257 if (wm > max_wm)
1258 break;
1259
1260 dirty |= raw->plane[plane_id] != wm;
1261 raw->plane[plane_id] = wm;
1262
1263 if (plane_id != PLANE_PRIMARY ||
1264 level == G4X_WM_LEVEL_NORMAL)
1265 continue;
1266
1267 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1268 raw->plane[plane_id]);
1269 max_wm = g4x_fbc_fifo_size(level);
1270
1271 /*
1272 * FBC wm is not mandatory as we
1273 * can always just disable its use.
1274 */
1275 if (wm > max_wm)
1276 wm = USHRT_MAX;
1277
1278 dirty |= raw->fbc != wm;
1279 raw->fbc = wm;
1280 }
1281
1282 /* mark watermarks as invalid */
1283 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1284
1285 if (plane_id == PLANE_PRIMARY)
1286 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1287
1288 out:
1289 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001290 drm_dbg_kms(&dev_priv->drm,
1291 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1292 plane->base.name,
1293 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1294 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1295 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001296
1297 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001298 drm_dbg_kms(&dev_priv->drm,
1299 "FBC watermarks: SR=%d, HPLL=%d\n",
1300 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1301 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001302 }
1303
1304 return dirty;
1305}
1306
1307static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1308 enum plane_id plane_id, int level)
1309{
1310 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1311
1312 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1313}
1314
1315static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1316 int level)
1317{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001318 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001319
1320 if (level > dev_priv->wm.max_level)
1321 return false;
1322
1323 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1324 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1325 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1326}
1327
1328/* mark all levels starting from 'level' as invalid */
1329static void g4x_invalidate_wms(struct intel_crtc *crtc,
1330 struct g4x_wm_state *wm_state, int level)
1331{
1332 if (level <= G4X_WM_LEVEL_NORMAL) {
1333 enum plane_id plane_id;
1334
1335 for_each_plane_id_on_crtc(crtc, plane_id)
1336 wm_state->wm.plane[plane_id] = USHRT_MAX;
1337 }
1338
1339 if (level <= G4X_WM_LEVEL_SR) {
1340 wm_state->cxsr = false;
1341 wm_state->sr.cursor = USHRT_MAX;
1342 wm_state->sr.plane = USHRT_MAX;
1343 wm_state->sr.fbc = USHRT_MAX;
1344 }
1345
1346 if (level <= G4X_WM_LEVEL_HPLL) {
1347 wm_state->hpll_en = false;
1348 wm_state->hpll.cursor = USHRT_MAX;
1349 wm_state->hpll.plane = USHRT_MAX;
1350 wm_state->hpll.fbc = USHRT_MAX;
1351 }
1352}
1353
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001354static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1355 int level)
1356{
1357 if (level < G4X_WM_LEVEL_SR)
1358 return false;
1359
1360 if (level >= G4X_WM_LEVEL_SR &&
1361 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1362 return false;
1363
1364 if (level >= G4X_WM_LEVEL_HPLL &&
1365 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1366 return false;
1367
1368 return true;
1369}
1370
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001371static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1372{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001373 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001374 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001375 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001376 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001377 int num_active_planes = hweight8(crtc_state->active_planes &
1378 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001379 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001380 const struct intel_plane_state *old_plane_state;
1381 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001382 struct intel_plane *plane;
1383 enum plane_id plane_id;
1384 int i, level;
1385 unsigned int dirty = 0;
1386
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001387 for_each_oldnew_intel_plane_in_state(state, plane,
1388 old_plane_state,
1389 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001390 if (new_plane_state->hw.crtc != &crtc->base &&
1391 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001392 continue;
1393
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001394 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001395 dirty |= BIT(plane->id);
1396 }
1397
1398 if (!dirty)
1399 return 0;
1400
1401 level = G4X_WM_LEVEL_NORMAL;
1402 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1403 goto out;
1404
1405 raw = &crtc_state->wm.g4x.raw[level];
1406 for_each_plane_id_on_crtc(crtc, plane_id)
1407 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1408
1409 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001410 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1411 goto out;
1412
1413 raw = &crtc_state->wm.g4x.raw[level];
1414 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1415 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1416 wm_state->sr.fbc = raw->fbc;
1417
1418 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1419
1420 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001421 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1422 goto out;
1423
1424 raw = &crtc_state->wm.g4x.raw[level];
1425 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1426 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1427 wm_state->hpll.fbc = raw->fbc;
1428
1429 wm_state->hpll_en = wm_state->cxsr;
1430
1431 level++;
1432
1433 out:
1434 if (level == G4X_WM_LEVEL_NORMAL)
1435 return -EINVAL;
1436
1437 /* invalidate the higher levels */
1438 g4x_invalidate_wms(crtc, wm_state, level);
1439
1440 /*
1441 * Determine if the FBC watermark(s) can be used. IF
1442 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001443 * watermark(s) rather than disable the SR/HPLL
1444 * level(s) entirely. 'level-1' is the highest valid
1445 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001446 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001447 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001448
1449 return 0;
1450}
1451
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001452static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001453{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001454 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301455 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001456 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1457 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1458 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001459 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001460 const struct intel_crtc_state *old_crtc_state =
1461 intel_atomic_get_old_crtc_state(intel_state, crtc);
1462 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001463 enum plane_id plane_id;
1464
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001465 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001466 *intermediate = *optimal;
1467
1468 intermediate->cxsr = false;
1469 intermediate->hpll_en = false;
1470 goto out;
1471 }
1472
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001473 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001474 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001475 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001476 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001477 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1478
1479 for_each_plane_id_on_crtc(crtc, plane_id) {
1480 intermediate->wm.plane[plane_id] =
1481 max(optimal->wm.plane[plane_id],
1482 active->wm.plane[plane_id]);
1483
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301484 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1485 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001486 }
1487
1488 intermediate->sr.plane = max(optimal->sr.plane,
1489 active->sr.plane);
1490 intermediate->sr.cursor = max(optimal->sr.cursor,
1491 active->sr.cursor);
1492 intermediate->sr.fbc = max(optimal->sr.fbc,
1493 active->sr.fbc);
1494
1495 intermediate->hpll.plane = max(optimal->hpll.plane,
1496 active->hpll.plane);
1497 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1498 active->hpll.cursor);
1499 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1500 active->hpll.fbc);
1501
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301502 drm_WARN_ON(&dev_priv->drm,
1503 (intermediate->sr.plane >
1504 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1505 intermediate->sr.cursor >
1506 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1507 intermediate->cxsr);
1508 drm_WARN_ON(&dev_priv->drm,
1509 (intermediate->sr.plane >
1510 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1511 intermediate->sr.cursor >
1512 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1513 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001514
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301515 drm_WARN_ON(&dev_priv->drm,
1516 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1517 intermediate->fbc_en && intermediate->cxsr);
1518 drm_WARN_ON(&dev_priv->drm,
1519 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1520 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001521
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001522out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001523 /*
1524 * If our intermediate WM are identical to the final WM, then we can
1525 * omit the post-vblank programming; only update if it's different.
1526 */
1527 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001528 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001529
1530 return 0;
1531}
1532
1533static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1534 struct g4x_wm_values *wm)
1535{
1536 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001537 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001538
1539 wm->cxsr = true;
1540 wm->hpll_en = true;
1541 wm->fbc_en = true;
1542
1543 for_each_intel_crtc(&dev_priv->drm, crtc) {
1544 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1545
1546 if (!crtc->active)
1547 continue;
1548
1549 if (!wm_state->cxsr)
1550 wm->cxsr = false;
1551 if (!wm_state->hpll_en)
1552 wm->hpll_en = false;
1553 if (!wm_state->fbc_en)
1554 wm->fbc_en = false;
1555
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001556 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001557 }
1558
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001559 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001560 wm->cxsr = false;
1561 wm->hpll_en = false;
1562 wm->fbc_en = false;
1563 }
1564
1565 for_each_intel_crtc(&dev_priv->drm, crtc) {
1566 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1567 enum pipe pipe = crtc->pipe;
1568
1569 wm->pipe[pipe] = wm_state->wm;
1570 if (crtc->active && wm->cxsr)
1571 wm->sr = wm_state->sr;
1572 if (crtc->active && wm->hpll_en)
1573 wm->hpll = wm_state->hpll;
1574 }
1575}
1576
1577static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1578{
1579 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1580 struct g4x_wm_values new_wm = {};
1581
1582 g4x_merge_wm(dev_priv, &new_wm);
1583
1584 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1585 return;
1586
1587 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1588 _intel_set_memory_cxsr(dev_priv, false);
1589
1590 g4x_write_wm_values(dev_priv, &new_wm);
1591
1592 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1593 _intel_set_memory_cxsr(dev_priv, true);
1594
1595 *old_wm = new_wm;
1596}
1597
1598static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001599 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001600{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001601 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1602 const struct intel_crtc_state *crtc_state =
1603 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001604
1605 mutex_lock(&dev_priv->wm.wm_mutex);
1606 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1607 g4x_program_watermarks(dev_priv);
1608 mutex_unlock(&dev_priv->wm.wm_mutex);
1609}
1610
1611static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001612 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001613{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001614 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1615 const struct intel_crtc_state *crtc_state =
1616 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001617
1618 if (!crtc_state->wm.need_postvbl_update)
1619 return;
1620
1621 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001622 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001623 g4x_program_watermarks(dev_priv);
1624 mutex_unlock(&dev_priv->wm.wm_mutex);
1625}
1626
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001627/* latency must be in 0.1us units. */
1628static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001629 unsigned int htotal,
1630 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001631 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001632 unsigned int latency)
1633{
1634 unsigned int ret;
1635
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001636 ret = intel_wm_method2(pixel_rate, htotal,
1637 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001638 ret = DIV_ROUND_UP(ret, 64);
1639
1640 return ret;
1641}
1642
Ville Syrjäläbb726512016-10-31 22:37:24 +02001643static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001644{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001645 /* all latencies in usec */
1646 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1647
Ville Syrjälä58590c12015-09-08 21:05:12 +03001648 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1649
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001650 if (IS_CHERRYVIEW(dev_priv)) {
1651 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1652 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001653
1654 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001655 }
1656}
1657
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001658static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1659 const struct intel_plane_state *plane_state,
1660 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001661{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001662 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001663 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001664 const struct drm_display_mode *pipe_mode =
1665 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001666 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001667
1668 if (dev_priv->wm.pri_latency[level] == 0)
1669 return USHRT_MAX;
1670
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001671 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001672 return 0;
1673
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001674 cpp = plane_state->hw.fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001675 clock = pipe_mode->crtc_clock;
1676 htotal = pipe_mode->crtc_htotal;
Ville Syrjäläe339d672016-11-28 19:37:17 +02001677 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001678
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001679 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001680 /*
1681 * FIXME the formula gives values that are
1682 * too big for the cursor FIFO, and hence we
1683 * would never be able to use cursors. For
1684 * now just hardcode the watermark.
1685 */
1686 wm = 63;
1687 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001688 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001689 dev_priv->wm.pri_latency[level] * 10);
1690 }
1691
Chris Wilson1a1f1282017-11-07 14:03:38 +00001692 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001693}
1694
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001695static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1696{
1697 return (active_planes & (BIT(PLANE_SPRITE0) |
1698 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1699}
1700
Ville Syrjälä5012e602017-03-02 19:14:56 +02001701static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001702{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001703 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301704 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001705 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001706 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001707 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001708 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001709 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001710 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001711 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001712 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001713 unsigned int total_rate;
1714 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001715
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001716 /*
1717 * When enabling sprite0 after sprite1 has already been enabled
1718 * we tend to get an underrun unless sprite0 already has some
1719 * FIFO space allcoated. Hence we always allocate at least one
1720 * cacheline for sprite0 whenever sprite1 is enabled.
1721 *
1722 * All other plane enable sequences appear immune to this problem.
1723 */
1724 if (vlv_need_sprite0_fifo_workaround(active_planes))
1725 sprite0_fifo_extra = 1;
1726
Ville Syrjälä5012e602017-03-02 19:14:56 +02001727 total_rate = raw->plane[PLANE_PRIMARY] +
1728 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001729 raw->plane[PLANE_SPRITE1] +
1730 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001731
Ville Syrjälä5012e602017-03-02 19:14:56 +02001732 if (total_rate > fifo_size)
1733 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001734
Ville Syrjälä5012e602017-03-02 19:14:56 +02001735 if (total_rate == 0)
1736 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001737
Ville Syrjälä5012e602017-03-02 19:14:56 +02001738 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001739 unsigned int rate;
1740
Ville Syrjälä5012e602017-03-02 19:14:56 +02001741 if ((active_planes & BIT(plane_id)) == 0) {
1742 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001743 continue;
1744 }
1745
Ville Syrjälä5012e602017-03-02 19:14:56 +02001746 rate = raw->plane[plane_id];
1747 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1748 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001749 }
1750
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001751 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1752 fifo_left -= sprite0_fifo_extra;
1753
Ville Syrjälä5012e602017-03-02 19:14:56 +02001754 fifo_state->plane[PLANE_CURSOR] = 63;
1755
1756 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001757
1758 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001759 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001760 int plane_extra;
1761
1762 if (fifo_left == 0)
1763 break;
1764
Ville Syrjälä5012e602017-03-02 19:14:56 +02001765 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001766 continue;
1767
1768 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001769 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001770 fifo_left -= plane_extra;
1771 }
1772
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301773 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001774
1775 /* give it all to the first plane if none are active */
1776 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301777 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001778 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1779 }
1780
1781 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001782}
1783
Ville Syrjäläff32c542017-03-02 19:14:57 +02001784/* mark all levels starting from 'level' as invalid */
1785static void vlv_invalidate_wms(struct intel_crtc *crtc,
1786 struct vlv_wm_state *wm_state, int level)
1787{
1788 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1789
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001790 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001791 enum plane_id plane_id;
1792
1793 for_each_plane_id_on_crtc(crtc, plane_id)
1794 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1795
1796 wm_state->sr[level].cursor = USHRT_MAX;
1797 wm_state->sr[level].plane = USHRT_MAX;
1798 }
1799}
1800
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001801static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1802{
1803 if (wm > fifo_size)
1804 return USHRT_MAX;
1805 else
1806 return fifo_size - wm;
1807}
1808
Ville Syrjäläff32c542017-03-02 19:14:57 +02001809/*
1810 * Starting from 'level' set all higher
1811 * levels to 'value' in the "raw" watermarks.
1812 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001813static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001814 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001815{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001816 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001817 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001818 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001819
Ville Syrjäläff32c542017-03-02 19:14:57 +02001820 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001821 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001822
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001823 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001824 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001825 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001826
1827 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001828}
1829
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001830static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1831 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001832{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001833 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001834 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001836 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001837 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001838 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001839
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001840 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001841 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1842 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001843 }
1844
1845 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001846 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001847 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1848 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1849
Ville Syrjäläff32c542017-03-02 19:14:57 +02001850 if (wm > max_wm)
1851 break;
1852
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001853 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001854 raw->plane[plane_id] = wm;
1855 }
1856
1857 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001858 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001859
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001860out:
1861 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001862 drm_dbg_kms(&dev_priv->drm,
1863 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1864 plane->base.name,
1865 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1866 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1867 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001868
1869 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001870}
1871
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001872static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1873 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001874{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001875 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001876 &crtc_state->wm.vlv.raw[level];
1877 const struct vlv_fifo_state *fifo_state =
1878 &crtc_state->wm.vlv.fifo_state;
1879
1880 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1881}
1882
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001883static 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 +02001884{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001885 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1886 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1887 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1888 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001889}
1890
1891static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001892{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001893 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001894 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001895 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001896 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001897 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001898 const struct vlv_fifo_state *fifo_state =
1899 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001900 int num_active_planes = hweight8(crtc_state->active_planes &
1901 ~BIT(PLANE_CURSOR));
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001902 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001903 const struct intel_plane_state *old_plane_state;
1904 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001905 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001906 enum plane_id plane_id;
1907 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001908 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001909
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001910 for_each_oldnew_intel_plane_in_state(state, plane,
1911 old_plane_state,
1912 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001913 if (new_plane_state->hw.crtc != &crtc->base &&
1914 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001915 continue;
1916
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001917 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001918 dirty |= BIT(plane->id);
1919 }
1920
1921 /*
1922 * DSPARB registers may have been reset due to the
1923 * power well being turned off. Make sure we restore
1924 * them to a consistent state even if no primary/sprite
1925 * planes are initially active.
1926 */
1927 if (needs_modeset)
1928 crtc_state->fifo_changed = true;
1929
1930 if (!dirty)
1931 return 0;
1932
1933 /* cursor changes don't warrant a FIFO recompute */
1934 if (dirty & ~BIT(PLANE_CURSOR)) {
1935 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001936 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001937 const struct vlv_fifo_state *old_fifo_state =
1938 &old_crtc_state->wm.vlv.fifo_state;
1939
1940 ret = vlv_compute_fifo(crtc_state);
1941 if (ret)
1942 return ret;
1943
1944 if (needs_modeset ||
1945 memcmp(old_fifo_state, fifo_state,
1946 sizeof(*fifo_state)) != 0)
1947 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001948 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001949
Ville Syrjäläff32c542017-03-02 19:14:57 +02001950 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001951 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001952 /*
1953 * Note that enabling cxsr with no primary/sprite planes
1954 * enabled can wedge the pipe. Hence we only allow cxsr
1955 * with exactly one enabled primary/sprite plane.
1956 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001957 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001958
Ville Syrjälä5012e602017-03-02 19:14:56 +02001959 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001960 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001961 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001962
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001963 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001964 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001965
Ville Syrjäläff32c542017-03-02 19:14:57 +02001966 for_each_plane_id_on_crtc(crtc, plane_id) {
1967 wm_state->wm[level].plane[plane_id] =
1968 vlv_invert_wm_value(raw->plane[plane_id],
1969 fifo_state->plane[plane_id]);
1970 }
1971
1972 wm_state->sr[level].plane =
1973 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001974 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001975 raw->plane[PLANE_SPRITE1]),
1976 sr_fifo_size);
1977
1978 wm_state->sr[level].cursor =
1979 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1980 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001981 }
1982
Ville Syrjäläff32c542017-03-02 19:14:57 +02001983 if (level == 0)
1984 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001985
Ville Syrjäläff32c542017-03-02 19:14:57 +02001986 /* limit to only levels we can actually handle */
1987 wm_state->num_levels = level;
1988
1989 /* invalidate the higher levels */
1990 vlv_invalidate_wms(crtc, wm_state, level);
1991
1992 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001993}
1994
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001995#define VLV_FIFO(plane, value) \
1996 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1997
Ville Syrjäläff32c542017-03-02 19:14:57 +02001998static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001999 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002000{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002001 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002002 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002003 const struct intel_crtc_state *crtc_state =
2004 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002005 const struct vlv_fifo_state *fifo_state =
2006 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002007 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002008 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002009
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002010 if (!crtc_state->fifo_changed)
2011 return;
2012
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002013 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2014 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2015 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002016
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302017 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2018 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002019
Ville Syrjäläc137d662017-03-02 19:15:06 +02002020 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2021
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002022 /*
2023 * uncore.lock serves a double purpose here. It allows us to
2024 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2025 * it protects the DSPARB registers from getting clobbered by
2026 * parallel updates from multiple pipes.
2027 *
2028 * intel_pipe_update_start() has already disabled interrupts
2029 * for us, so a plain spin_lock() is sufficient here.
2030 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002031 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002032
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002033 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002034 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002035 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2036 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002037
2038 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2039 VLV_FIFO(SPRITEB, 0xff));
2040 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2041 VLV_FIFO(SPRITEB, sprite1_start));
2042
2043 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2044 VLV_FIFO(SPRITEB_HI, 0x1));
2045 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2046 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2047
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002048 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2049 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002050 break;
2051 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002052 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2053 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002054
2055 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2056 VLV_FIFO(SPRITED, 0xff));
2057 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2058 VLV_FIFO(SPRITED, sprite1_start));
2059
2060 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2061 VLV_FIFO(SPRITED_HI, 0xff));
2062 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2063 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2064
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002065 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2066 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002067 break;
2068 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002069 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2070 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002071
2072 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2073 VLV_FIFO(SPRITEF, 0xff));
2074 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2075 VLV_FIFO(SPRITEF, sprite1_start));
2076
2077 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2078 VLV_FIFO(SPRITEF_HI, 0xff));
2079 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2080 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2081
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002082 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2083 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002084 break;
2085 default:
2086 break;
2087 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002088
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002089 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002090
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002091 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002092}
2093
2094#undef VLV_FIFO
2095
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002096static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002097{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002098 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002099 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2100 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2101 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002102 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002103 const struct intel_crtc_state *old_crtc_state =
2104 intel_atomic_get_old_crtc_state(intel_state, crtc);
2105 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002106 int level;
2107
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002108 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002109 *intermediate = *optimal;
2110
2111 intermediate->cxsr = false;
2112 goto out;
2113 }
2114
Ville Syrjälä4841da52017-03-02 19:14:59 +02002115 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002116 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002117 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002118
2119 for (level = 0; level < intermediate->num_levels; level++) {
2120 enum plane_id plane_id;
2121
2122 for_each_plane_id_on_crtc(crtc, plane_id) {
2123 intermediate->wm[level].plane[plane_id] =
2124 min(optimal->wm[level].plane[plane_id],
2125 active->wm[level].plane[plane_id]);
2126 }
2127
2128 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2129 active->sr[level].plane);
2130 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2131 active->sr[level].cursor);
2132 }
2133
2134 vlv_invalidate_wms(crtc, intermediate, level);
2135
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002136out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002137 /*
2138 * If our intermediate WM are identical to the final WM, then we can
2139 * omit the post-vblank programming; only update if it's different.
2140 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002141 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002142 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002143
2144 return 0;
2145}
2146
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002147static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002148 struct vlv_wm_values *wm)
2149{
2150 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002151 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002152
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002153 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002154 wm->cxsr = true;
2155
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002156 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002157 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002158
2159 if (!crtc->active)
2160 continue;
2161
2162 if (!wm_state->cxsr)
2163 wm->cxsr = false;
2164
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002165 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002166 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2167 }
2168
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002169 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002170 wm->cxsr = false;
2171
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002172 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002173 wm->level = VLV_WM_LEVEL_PM2;
2174
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002175 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002176 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002177 enum pipe pipe = crtc->pipe;
2178
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002179 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002180 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002181 wm->sr = wm_state->sr[wm->level];
2182
Ville Syrjälä1b313892016-11-28 19:37:08 +02002183 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2184 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2185 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2186 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002187 }
2188}
2189
Ville Syrjäläff32c542017-03-02 19:14:57 +02002190static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002191{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002192 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2193 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002194
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002195 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002196
Ville Syrjäläff32c542017-03-02 19:14:57 +02002197 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002198 return;
2199
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002200 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002201 chv_set_memory_dvfs(dev_priv, false);
2202
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002203 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002204 chv_set_memory_pm5(dev_priv, false);
2205
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002206 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002207 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002208
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002209 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002210
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002211 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002212 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002213
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002214 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002215 chv_set_memory_pm5(dev_priv, true);
2216
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002217 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002218 chv_set_memory_dvfs(dev_priv, true);
2219
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002220 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002221}
2222
Ville Syrjäläff32c542017-03-02 19:14:57 +02002223static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002224 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002225{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002226 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2227 const struct intel_crtc_state *crtc_state =
2228 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002229
2230 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002231 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2232 vlv_program_watermarks(dev_priv);
2233 mutex_unlock(&dev_priv->wm.wm_mutex);
2234}
2235
2236static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002237 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002238{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002239 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2240 const struct intel_crtc_state *crtc_state =
2241 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002242
2243 if (!crtc_state->wm.need_postvbl_update)
2244 return;
2245
2246 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002247 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002248 vlv_program_watermarks(dev_priv);
2249 mutex_unlock(&dev_priv->wm.wm_mutex);
2250}
2251
Ville Syrjälä432081b2016-10-31 22:37:03 +02002252static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002253{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002254 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002255 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002256 int srwm = 1;
2257 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002258 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002259
2260 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002261 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002262 if (crtc) {
2263 /* self-refresh has much higher latency */
2264 static const int sr_latency_ns = 12000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002265 const struct drm_display_mode *pipe_mode =
2266 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002267 const struct drm_framebuffer *fb =
2268 crtc->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002269 int clock = pipe_mode->crtc_clock;
2270 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002271 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002272 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002273 int entries;
2274
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002275 entries = intel_wm_method2(clock, htotal,
2276 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002277 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2278 srwm = I965_FIFO_SIZE - entries;
2279 if (srwm < 0)
2280 srwm = 1;
2281 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002282 drm_dbg_kms(&dev_priv->drm,
2283 "self-refresh entries: %d, wm: %d\n",
2284 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002285
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002286 entries = intel_wm_method2(clock, htotal,
2287 crtc->base.cursor->state->crtc_w, 4,
2288 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002289 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002290 i965_cursor_wm_info.cacheline_size) +
2291 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002292
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002293 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002294 if (cursor_sr > i965_cursor_wm_info.max_wm)
2295 cursor_sr = i965_cursor_wm_info.max_wm;
2296
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002297 drm_dbg_kms(&dev_priv->drm,
2298 "self-refresh watermark: display plane %d "
2299 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002300
Imre Deak98584252014-06-13 14:54:20 +03002301 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002302 } else {
Imre Deak98584252014-06-13 14:54:20 +03002303 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002304 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002305 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002306 }
2307
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002308 drm_dbg_kms(&dev_priv->drm,
2309 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2310 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002311
2312 /* 965 has limitations... */
Jani Nikula5f461662020-11-30 13:15:58 +02002313 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002314 FW_WM(8, CURSORB) |
2315 FW_WM(8, PLANEB) |
2316 FW_WM(8, PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02002317 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002318 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002319 /* update cursor SR watermark */
Jani Nikula5f461662020-11-30 13:15:58 +02002320 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002321
2322 if (cxsr_enabled)
2323 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002324}
2325
Ville Syrjäläf4998962015-03-10 17:02:21 +02002326#undef FW_WM
2327
Ville Syrjälä432081b2016-10-31 22:37:03 +02002328static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002329{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002330 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002331 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002332 u32 fwater_lo;
2333 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002334 int cwm, srwm = 1;
2335 int fifo_size;
2336 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002337 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002338
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002339 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002340 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002341 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002342 wm_info = &i915_wm_info;
2343 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002344 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002345
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002346 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2347 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 Marchicf819ef2018-12-12 10:10:43 -08002355 if (IS_GEN(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 Marchicf819ef2018-12-12 10:10:43 -08002370 if (IS_GEN(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
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002373 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2374 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002375 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002376 const struct drm_display_mode *pipe_mode =
2377 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002378 const struct drm_framebuffer *fb =
2379 crtc->base.primary->state->fb;
2380 int cpp;
2381
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002382 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002383 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002384 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002385 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002386
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002387 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002388 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002389 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002390 if (enabled == NULL)
2391 enabled = crtc;
2392 else
2393 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002394 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002395 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002396 if (planeb_wm > (long)wm_info->max_wm)
2397 planeb_wm = wm_info->max_wm;
2398 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002399
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002400 drm_dbg_kms(&dev_priv->drm,
2401 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002402
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002403 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002404 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002405
Ville Syrjäläefc26112016-10-31 22:37:04 +02002406 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002407
2408 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002409 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002410 enabled = NULL;
2411 }
2412
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002413 /*
2414 * Overlay gets an aggressive default since video jitter is bad.
2415 */
2416 cwm = 2;
2417
2418 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002419 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002420
2421 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002422 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002423 /* self-refresh has much higher latency */
2424 static const int sr_latency_ns = 6000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002425 const struct drm_display_mode *pipe_mode =
2426 &enabled->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002427 const struct drm_framebuffer *fb =
2428 enabled->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002429 int clock = pipe_mode->crtc_clock;
2430 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002431 int hdisplay = enabled->config->pipe_src_w;
2432 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002433 int entries;
2434
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002435 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002436 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002437 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002438 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002439
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002440 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2441 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002442 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002443 drm_dbg_kms(&dev_priv->drm,
2444 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002445 srwm = wm_info->fifo_size - entries;
2446 if (srwm < 0)
2447 srwm = 1;
2448
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002449 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02002450 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002451 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002452 else
Jani Nikula5f461662020-11-30 13:15:58 +02002453 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002454 }
2455
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002456 drm_dbg_kms(&dev_priv->drm,
2457 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2458 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002459
2460 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2461 fwater_hi = (cwm & 0x1f);
2462
2463 /* Set request length to 8 cachelines per fetch */
2464 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2465 fwater_hi = fwater_hi | (1 << 8);
2466
Jani Nikula5f461662020-11-30 13:15:58 +02002467 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2468 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002469
Imre Deak5209b1f2014-07-01 12:36:17 +03002470 if (enabled)
2471 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002472}
2473
Ville Syrjälä432081b2016-10-31 22:37:03 +02002474static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002475{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002476 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002477 struct intel_crtc *crtc;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002478 const struct drm_display_mode *pipe_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002479 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002480 int planea_wm;
2481
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002482 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002483 if (crtc == NULL)
2484 return;
2485
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002486 pipe_mode = &crtc->config->hw.pipe_mode;
2487 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002488 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002489 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002490 4, pessimal_latency_ns);
Jani Nikula5f461662020-11-30 13:15:58 +02002491 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002492 fwater_lo |= (3<<8) | planea_wm;
2493
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002494 drm_dbg_kms(&dev_priv->drm,
2495 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002496
Jani Nikula5f461662020-11-30 13:15:58 +02002497 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002498}
2499
Ville Syrjälä37126462013-08-01 16:18:55 +03002500/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002501static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2502 unsigned int cpp,
2503 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002504{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002505 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002506
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002507 ret = intel_wm_method1(pixel_rate, cpp, latency);
2508 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002509
2510 return ret;
2511}
2512
Ville Syrjälä37126462013-08-01 16:18:55 +03002513/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002514static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2515 unsigned int htotal,
2516 unsigned int width,
2517 unsigned int cpp,
2518 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002520 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002522 ret = intel_wm_method2(pixel_rate, htotal,
2523 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002524 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002525
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002526 return ret;
2527}
2528
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002529static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002530{
Matt Roper15126882015-12-03 11:37:40 -08002531 /*
2532 * Neither of these should be possible since this function shouldn't be
2533 * called if the CRTC is off or the plane is invisible. But let's be
2534 * extra paranoid to avoid a potential divide-by-zero if we screw up
2535 * elsewhere in the driver.
2536 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002537 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002538 return 0;
2539 if (WARN_ON(!horiz_pixels))
2540 return 0;
2541
Ville Syrjäläac484962016-01-20 21:05:26 +02002542 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002543}
2544
Imre Deak820c1982013-12-17 14:46:36 +02002545struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002546 u16 pri;
2547 u16 spr;
2548 u16 cur;
2549 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002550};
2551
Ville Syrjälä37126462013-08-01 16:18:55 +03002552/*
2553 * For both WM_PIPE and WM_LP.
2554 * mem_value must be in 0.1us units.
2555 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002556static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2557 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002558 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002559{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002560 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002561 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002562
Ville Syrjälä03981c62018-11-14 19:34:40 +02002563 if (mem_value == 0)
2564 return U32_MAX;
2565
Maarten Lankhorstec193642019-06-28 10:55:17 +02002566 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002567 return 0;
2568
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002569 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002570
Maarten Lankhorstec193642019-06-28 10:55:17 +02002571 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002572
2573 if (!is_lp)
2574 return method1;
2575
Maarten Lankhorstec193642019-06-28 10:55:17 +02002576 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002577 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002578 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002579 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002580
2581 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002582}
2583
Ville Syrjälä37126462013-08-01 16:18:55 +03002584/*
2585 * For both WM_PIPE and WM_LP.
2586 * mem_value must be in 0.1us units.
2587 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002588static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2589 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002590 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002591{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002592 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002593 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002594
Ville Syrjälä03981c62018-11-14 19:34:40 +02002595 if (mem_value == 0)
2596 return U32_MAX;
2597
Maarten Lankhorstec193642019-06-28 10:55:17 +02002598 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002599 return 0;
2600
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002601 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002602
Maarten Lankhorstec193642019-06-28 10:55:17 +02002603 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2604 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002605 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002606 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002607 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002608 return min(method1, method2);
2609}
2610
Ville Syrjälä37126462013-08-01 16:18:55 +03002611/*
2612 * For both WM_PIPE and WM_LP.
2613 * mem_value must be in 0.1us units.
2614 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002615static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2616 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002617 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002618{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002619 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002620
Ville Syrjälä03981c62018-11-14 19:34:40 +02002621 if (mem_value == 0)
2622 return U32_MAX;
2623
Maarten Lankhorstec193642019-06-28 10:55:17 +02002624 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002625 return 0;
2626
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002627 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002628
Maarten Lankhorstec193642019-06-28 10:55:17 +02002629 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002630 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002631 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002632 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002633}
2634
Paulo Zanonicca32e92013-05-31 11:45:06 -03002635/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002636static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2637 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002638 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002639{
Ville Syrjälä83054942016-11-18 21:53:00 +02002640 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002641
Maarten Lankhorstec193642019-06-28 10:55:17 +02002642 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002643 return 0;
2644
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002645 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002646
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002647 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2648 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002649}
2650
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002651static unsigned int
2652ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002653{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002654 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002655 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002656 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002657 return 768;
2658 else
2659 return 512;
2660}
2661
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002662static unsigned int
2663ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2664 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002665{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002666 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002667 /* BDW primary/sprite plane watermarks */
2668 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002669 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002670 /* IVB/HSW primary/sprite plane watermarks */
2671 return level == 0 ? 127 : 1023;
2672 else if (!is_sprite)
2673 /* ILK/SNB primary plane watermarks */
2674 return level == 0 ? 127 : 511;
2675 else
2676 /* ILK/SNB sprite plane watermarks */
2677 return level == 0 ? 63 : 255;
2678}
2679
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002680static unsigned int
2681ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002682{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002683 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002684 return level == 0 ? 63 : 255;
2685 else
2686 return level == 0 ? 31 : 63;
2687}
2688
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002689static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002690{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002691 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002692 return 31;
2693 else
2694 return 15;
2695}
2696
Ville Syrjälä158ae642013-08-07 13:28:19 +03002697/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002698static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002699 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002700 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002701 enum intel_ddb_partitioning ddb_partitioning,
2702 bool is_sprite)
2703{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002704 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002705
2706 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002707 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002708 return 0;
2709
2710 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002711 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002712 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002713
2714 /*
2715 * For some reason the non self refresh
2716 * FIFO size is only half of the self
2717 * refresh FIFO size on ILK/SNB.
2718 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002719 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002720 fifo_size /= 2;
2721 }
2722
Ville Syrjälä240264f2013-08-07 13:29:12 +03002723 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002724 /* level 0 is always calculated with 1:1 split */
2725 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2726 if (is_sprite)
2727 fifo_size *= 5;
2728 fifo_size /= 6;
2729 } else {
2730 fifo_size /= 2;
2731 }
2732 }
2733
2734 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002735 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002736}
2737
2738/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002739static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002740 int level,
2741 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002742{
2743 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002744 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002745 return 64;
2746
2747 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002748 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002749}
2750
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002751static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002752 int level,
2753 const struct intel_wm_config *config,
2754 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002755 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002756{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002757 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2758 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2759 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2760 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002761}
2762
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002763static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002764 int level,
2765 struct ilk_wm_maximums *max)
2766{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002767 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2768 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2769 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2770 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002771}
2772
Ville Syrjäläd9395652013-10-09 19:18:10 +03002773static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002774 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002775 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002776{
2777 bool ret;
2778
2779 /* already determined to be invalid? */
2780 if (!result->enable)
2781 return false;
2782
2783 result->enable = result->pri_val <= max->pri &&
2784 result->spr_val <= max->spr &&
2785 result->cur_val <= max->cur;
2786
2787 ret = result->enable;
2788
2789 /*
2790 * HACK until we can pre-compute everything,
2791 * and thus fail gracefully if LP0 watermarks
2792 * are exceeded...
2793 */
2794 if (level == 0 && !result->enable) {
2795 if (result->pri_val > max->pri)
2796 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2797 level, result->pri_val, max->pri);
2798 if (result->spr_val > max->spr)
2799 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2800 level, result->spr_val, max->spr);
2801 if (result->cur_val > max->cur)
2802 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2803 level, result->cur_val, max->cur);
2804
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002805 result->pri_val = min_t(u32, result->pri_val, max->pri);
2806 result->spr_val = min_t(u32, result->spr_val, max->spr);
2807 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002808 result->enable = true;
2809 }
2810
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002811 return ret;
2812}
2813
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002814static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002815 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002816 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002817 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002818 const struct intel_plane_state *pristate,
2819 const struct intel_plane_state *sprstate,
2820 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002821 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002822{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002823 u16 pri_latency = dev_priv->wm.pri_latency[level];
2824 u16 spr_latency = dev_priv->wm.spr_latency[level];
2825 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002826
2827 /* WM1+ latency values stored in 0.5us units */
2828 if (level > 0) {
2829 pri_latency *= 5;
2830 spr_latency *= 5;
2831 cur_latency *= 5;
2832 }
2833
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002834 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002835 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002836 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002837 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002838 }
2839
2840 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002841 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002842
2843 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002844 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002845
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002846 result->enable = true;
2847}
2848
Ville Syrjäläbb726512016-10-31 22:37:24 +02002849static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002850 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002851{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002852 struct intel_uncore *uncore = &dev_priv->uncore;
2853
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002854 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002855 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002856 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002857 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002858
2859 /* read the first set of memory latencies[0:3] */
2860 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002861 ret = sandybridge_pcode_read(dev_priv,
2862 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002863 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002864
2865 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002866 drm_err(&dev_priv->drm,
2867 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002868 return;
2869 }
2870
2871 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2872 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2873 GEN9_MEM_LATENCY_LEVEL_MASK;
2874 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2875 GEN9_MEM_LATENCY_LEVEL_MASK;
2876 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2877 GEN9_MEM_LATENCY_LEVEL_MASK;
2878
2879 /* read the second set of memory latencies[4:7] */
2880 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002881 ret = sandybridge_pcode_read(dev_priv,
2882 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002883 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002884 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002885 drm_err(&dev_priv->drm,
2886 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002887 return;
2888 }
2889
2890 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2891 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2892 GEN9_MEM_LATENCY_LEVEL_MASK;
2893 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2894 GEN9_MEM_LATENCY_LEVEL_MASK;
2895 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2896 GEN9_MEM_LATENCY_LEVEL_MASK;
2897
Vandana Kannan367294b2014-11-04 17:06:46 +00002898 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002899 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2900 * need to be disabled. We make sure to sanitize the values out
2901 * of the punit to satisfy this requirement.
2902 */
2903 for (level = 1; level <= max_level; level++) {
2904 if (wm[level] == 0) {
2905 for (i = level + 1; i <= max_level; i++)
2906 wm[i] = 0;
2907 break;
2908 }
2909 }
2910
2911 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002912 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002913 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002914 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002915 * to add 2us to the various latency levels we retrieve from the
2916 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002917 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002918 if (wm[0] == 0) {
2919 wm[0] += 2;
2920 for (level = 1; level <= max_level; level++) {
2921 if (wm[level] == 0)
2922 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002923 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002924 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002925 }
2926
Mahesh Kumar86b59282018-08-31 16:39:42 +05302927 /*
2928 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2929 * If we could not get dimm info enable this WA to prevent from
2930 * any underrun. If not able to get Dimm info assume 16GB dimm
2931 * to avoid any underrun.
2932 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002933 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302934 wm[0] += 1;
2935
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002936 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002937 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002938
2939 wm[0] = (sskpd >> 56) & 0xFF;
2940 if (wm[0] == 0)
2941 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002942 wm[1] = (sskpd >> 4) & 0xFF;
2943 wm[2] = (sskpd >> 12) & 0xFF;
2944 wm[3] = (sskpd >> 20) & 0x1FF;
2945 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002946 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002947 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002948
2949 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2950 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2951 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2952 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002953 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002954 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002955
2956 /* ILK primary LP0 latency is 700 ns */
2957 wm[0] = 7;
2958 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2959 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002960 } else {
2961 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002962 }
2963}
2964
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002965static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002966 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002967{
2968 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002969 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002970 wm[0] = 13;
2971}
2972
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002973static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002974 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002975{
2976 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002977 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002978 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002979}
2980
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002981int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002982{
2983 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002984 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002985 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002986 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002987 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002988 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002989 return 3;
2990 else
2991 return 2;
2992}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002993
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002994static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002995 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002996 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002997{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002998 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002999
3000 for (level = 0; level <= max_level; level++) {
3001 unsigned int latency = wm[level];
3002
3003 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003004 drm_dbg_kms(&dev_priv->drm,
3005 "%s WM%d latency not provided\n",
3006 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003007 continue;
3008 }
3009
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003010 /*
3011 * - latencies are in us on gen9.
3012 * - before then, WM1+ latency values are in 0.5us units
3013 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07003014 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003015 latency *= 10;
3016 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003017 latency *= 5;
3018
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003019 drm_dbg_kms(&dev_priv->drm,
3020 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3021 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003022 }
3023}
3024
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003025static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003026 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003027{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003028 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003029
3030 if (wm[0] >= min)
3031 return false;
3032
3033 wm[0] = max(wm[0], min);
3034 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003035 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003036
3037 return true;
3038}
3039
Ville Syrjäläbb726512016-10-31 22:37:24 +02003040static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003041{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003042 bool changed;
3043
3044 /*
3045 * The BIOS provided WM memory latency values are often
3046 * inadequate for high resolution displays. Adjust them.
3047 */
3048 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3049 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3050 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3051
3052 if (!changed)
3053 return;
3054
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003055 drm_dbg_kms(&dev_priv->drm,
3056 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003057 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3058 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3059 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003060}
3061
Ville Syrjälä03981c62018-11-14 19:34:40 +02003062static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3063{
3064 /*
3065 * On some SNB machines (Thinkpad X220 Tablet at least)
3066 * LP3 usage can cause vblank interrupts to be lost.
3067 * The DEIIR bit will go high but it looks like the CPU
3068 * never gets interrupted.
3069 *
3070 * It's not clear whether other interrupt source could
3071 * be affected or if this is somehow limited to vblank
3072 * interrupts only. To play it safe we disable LP3
3073 * watermarks entirely.
3074 */
3075 if (dev_priv->wm.pri_latency[3] == 0 &&
3076 dev_priv->wm.spr_latency[3] == 0 &&
3077 dev_priv->wm.cur_latency[3] == 0)
3078 return;
3079
3080 dev_priv->wm.pri_latency[3] = 0;
3081 dev_priv->wm.spr_latency[3] = 0;
3082 dev_priv->wm.cur_latency[3] = 0;
3083
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003084 drm_dbg_kms(&dev_priv->drm,
3085 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003086 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3087 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3088 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3089}
3090
Ville Syrjäläbb726512016-10-31 22:37:24 +02003091static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003092{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003093 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003094
3095 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3096 sizeof(dev_priv->wm.pri_latency));
3097 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3098 sizeof(dev_priv->wm.pri_latency));
3099
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003100 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003101 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003102
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003103 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3104 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3105 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003106
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003107 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003108 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003109 snb_wm_lp3_irq_quirk(dev_priv);
3110 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003111}
3112
Ville Syrjäläbb726512016-10-31 22:37:24 +02003113static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003114{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003115 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003116 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003117}
3118
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003119static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003120 struct intel_pipe_wm *pipe_wm)
3121{
3122 /* LP0 watermark maximums depend on this pipe alone */
3123 const struct intel_wm_config config = {
3124 .num_pipes_active = 1,
3125 .sprites_enabled = pipe_wm->sprites_enabled,
3126 .sprites_scaled = pipe_wm->sprites_scaled,
3127 };
3128 struct ilk_wm_maximums max;
3129
3130 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003131 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003132
3133 /* At least LP0 must be valid */
3134 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003135 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003136 return false;
3137 }
3138
3139 return true;
3140}
3141
Matt Roper261a27d2015-10-08 15:28:25 -07003142/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003143static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003144{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003145 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003146 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003147 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003148 struct intel_plane *plane;
3149 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003150 const struct intel_plane_state *pristate = NULL;
3151 const struct intel_plane_state *sprstate = NULL;
3152 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003153 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003154 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003155
Maarten Lankhorstec193642019-06-28 10:55:17 +02003156 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003157
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003158 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3159 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3160 pristate = plane_state;
3161 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3162 sprstate = plane_state;
3163 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3164 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003165 }
3166
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003167 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003168 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003169 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3170 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3171 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3172 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003173 }
3174
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003175 usable_level = max_level;
3176
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003177 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003178 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003179 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003180
3181 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003182 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003183 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003184
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003185 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003186 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003187 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003188
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003189 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003190 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003191
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003192 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003193
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003194 for (level = 1; level <= usable_level; level++) {
3195 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003196
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003197 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003198 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003199
3200 /*
3201 * Disable any watermark level that exceeds the
3202 * register maximums since such watermarks are
3203 * always invalid.
3204 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003205 if (!ilk_validate_wm_level(level, &max, wm)) {
3206 memset(wm, 0, sizeof(*wm));
3207 break;
3208 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003209 }
3210
Matt Roper86c8bbb2015-09-24 15:53:16 -07003211 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003212}
3213
3214/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003215 * Build a set of 'intermediate' watermark values that satisfy both the old
3216 * state and the new state. These can be programmed to the hardware
3217 * immediately.
3218 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003219static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003220{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003221 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->uapi.crtc);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003222 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003223 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003224 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003225 to_intel_atomic_state(newstate->uapi.state);
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003226 const struct intel_crtc_state *oldstate =
3227 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3228 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003229 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003230
3231 /*
3232 * Start with the final, target watermarks, then combine with the
3233 * currently active watermarks to get values that are safe both before
3234 * and after the vblank.
3235 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003236 *a = newstate->wm.ilk.optimal;
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003237 if (!newstate->hw.active || drm_atomic_crtc_needs_modeset(&newstate->uapi) ||
Ville Syrjäläf255c622018-11-08 17:10:13 +02003238 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003239 return 0;
3240
Matt Ropered4a6a72016-02-23 17:20:13 -08003241 a->pipe_enabled |= b->pipe_enabled;
3242 a->sprites_enabled |= b->sprites_enabled;
3243 a->sprites_scaled |= b->sprites_scaled;
3244
3245 for (level = 0; level <= max_level; level++) {
3246 struct intel_wm_level *a_wm = &a->wm[level];
3247 const struct intel_wm_level *b_wm = &b->wm[level];
3248
3249 a_wm->enable &= b_wm->enable;
3250 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3251 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3252 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3253 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3254 }
3255
3256 /*
3257 * We need to make sure that these merged watermark values are
3258 * actually a valid configuration themselves. If they're not,
3259 * there's no safe way to transition from the old state to
3260 * the new state, so we need to fail the atomic transaction.
3261 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003262 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003263 return -EINVAL;
3264
3265 /*
3266 * If our intermediate WM are identical to the final WM, then we can
3267 * omit the post-vblank programming; only update if it's different.
3268 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003269 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3270 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003271
3272 return 0;
3273}
3274
3275/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003276 * Merge the watermarks from all active pipes for a specific level.
3277 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003278static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003279 int level,
3280 struct intel_wm_level *ret_wm)
3281{
3282 const struct intel_crtc *intel_crtc;
3283
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003284 ret_wm->enable = true;
3285
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003286 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003287 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003288 const struct intel_wm_level *wm = &active->wm[level];
3289
3290 if (!active->pipe_enabled)
3291 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003292
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003293 /*
3294 * The watermark values may have been used in the past,
3295 * so we must maintain them in the registers for some
3296 * time even if the level is now disabled.
3297 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003298 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003299 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003300
3301 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3302 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3303 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3304 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3305 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003306}
3307
3308/*
3309 * Merge all low power watermarks for all active pipes.
3310 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003311static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003312 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003313 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003314 struct intel_pipe_wm *merged)
3315{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003316 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003317 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003318
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003319 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003320 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003321 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003322 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003323
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003324 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003325 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003326
3327 /* merge each WM1+ level */
3328 for (level = 1; level <= max_level; level++) {
3329 struct intel_wm_level *wm = &merged->wm[level];
3330
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003331 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003332
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003333 if (level > last_enabled_level)
3334 wm->enable = false;
3335 else if (!ilk_validate_wm_level(level, max, wm))
3336 /* make sure all following levels get disabled */
3337 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003338
3339 /*
3340 * The spec says it is preferred to disable
3341 * FBC WMs instead of disabling a WM level.
3342 */
3343 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003344 if (wm->enable)
3345 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003346 wm->fbc_val = 0;
3347 }
3348 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003349
3350 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3351 /*
3352 * FIXME this is racy. FBC might get enabled later.
3353 * What we should check here is whether FBC can be
3354 * enabled sometime later.
3355 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003356 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003357 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003358 for (level = 2; level <= max_level; level++) {
3359 struct intel_wm_level *wm = &merged->wm[level];
3360
3361 wm->enable = false;
3362 }
3363 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003364}
3365
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003366static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3367{
3368 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3369 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3370}
3371
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003372/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003373static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3374 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003375{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003376 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003377 return 2 * level;
3378 else
3379 return dev_priv->wm.pri_latency[level];
3380}
3381
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003382static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003383 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003384 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003385 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003386{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003387 struct intel_crtc *intel_crtc;
3388 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003389
Ville Syrjälä0362c782013-10-09 19:17:57 +03003390 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003391 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003392
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003393 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003394 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003395 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003396
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003397 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003398
Ville Syrjälä0362c782013-10-09 19:17:57 +03003399 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003400
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003401 /*
3402 * Maintain the watermark values even if the level is
3403 * disabled. Doing otherwise could cause underruns.
3404 */
3405 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003406 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003407 (r->pri_val << WM1_LP_SR_SHIFT) |
3408 r->cur_val;
3409
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003410 if (r->enable)
3411 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3412
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003413 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003414 results->wm_lp[wm_lp - 1] |=
3415 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3416 else
3417 results->wm_lp[wm_lp - 1] |=
3418 r->fbc_val << WM1_LP_FBC_SHIFT;
3419
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003420 /*
3421 * Always set WM1S_LP_EN when spr_val != 0, even if the
3422 * level is disabled. Doing otherwise could cause underruns.
3423 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003424 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303425 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003426 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3427 } else
3428 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003429 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003430
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003431 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003432 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003433 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003434 const struct intel_pipe_wm *pipe_wm = &intel_crtc->wm.active.ilk;
3435 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003436
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303437 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003438 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003439
3440 results->wm_pipe[pipe] =
3441 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3442 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3443 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003444 }
3445}
3446
Paulo Zanoni861f3382013-05-31 10:19:21 -03003447/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3448 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003449static struct intel_pipe_wm *
3450ilk_find_best_result(struct drm_i915_private *dev_priv,
3451 struct intel_pipe_wm *r1,
3452 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003453{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003454 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003455 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003456
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003457 for (level = 1; level <= max_level; level++) {
3458 if (r1->wm[level].enable)
3459 level1 = level;
3460 if (r2->wm[level].enable)
3461 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003462 }
3463
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003464 if (level1 == level2) {
3465 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003466 return r2;
3467 else
3468 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003469 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003470 return r1;
3471 } else {
3472 return r2;
3473 }
3474}
3475
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003476/* dirty bits used to track which watermarks need changes */
3477#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003478#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3479#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3480#define WM_DIRTY_FBC (1 << 24)
3481#define WM_DIRTY_DDB (1 << 25)
3482
Damien Lespiau055e3932014-08-18 13:49:10 +01003483static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003484 const struct ilk_wm_values *old,
3485 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003486{
3487 unsigned int dirty = 0;
3488 enum pipe pipe;
3489 int wm_lp;
3490
Damien Lespiau055e3932014-08-18 13:49:10 +01003491 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003492 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3493 dirty |= WM_DIRTY_PIPE(pipe);
3494 /* Must disable LP1+ watermarks too */
3495 dirty |= WM_DIRTY_LP_ALL;
3496 }
3497 }
3498
3499 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3500 dirty |= WM_DIRTY_FBC;
3501 /* Must disable LP1+ watermarks too */
3502 dirty |= WM_DIRTY_LP_ALL;
3503 }
3504
3505 if (old->partitioning != new->partitioning) {
3506 dirty |= WM_DIRTY_DDB;
3507 /* Must disable LP1+ watermarks too */
3508 dirty |= WM_DIRTY_LP_ALL;
3509 }
3510
3511 /* LP1+ watermarks already deemed dirty, no need to continue */
3512 if (dirty & WM_DIRTY_LP_ALL)
3513 return dirty;
3514
3515 /* Find the lowest numbered LP1+ watermark in need of an update... */
3516 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3517 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3518 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3519 break;
3520 }
3521
3522 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3523 for (; wm_lp <= 3; wm_lp++)
3524 dirty |= WM_DIRTY_LP(wm_lp);
3525
3526 return dirty;
3527}
3528
Ville Syrjälä8553c182013-12-05 15:51:39 +02003529static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3530 unsigned int dirty)
3531{
Imre Deak820c1982013-12-17 14:46:36 +02003532 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003533 bool changed = false;
3534
3535 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3536 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003537 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003538 changed = true;
3539 }
3540 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3541 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003542 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003543 changed = true;
3544 }
3545 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3546 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003547 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003548 changed = true;
3549 }
3550
3551 /*
3552 * Don't touch WM1S_LP_EN here.
3553 * Doing so could cause underruns.
3554 */
3555
3556 return changed;
3557}
3558
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003559/*
3560 * The spec says we shouldn't write when we don't need, because every write
3561 * causes WMs to be re-evaluated, expending some power.
3562 */
Imre Deak820c1982013-12-17 14:46:36 +02003563static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3564 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003565{
Imre Deak820c1982013-12-17 14:46:36 +02003566 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003567 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003568 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003569
Damien Lespiau055e3932014-08-18 13:49:10 +01003570 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003571 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003572 return;
3573
Ville Syrjälä8553c182013-12-05 15:51:39 +02003574 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003575
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003576 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Jani Nikula5f461662020-11-30 13:15:58 +02003577 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003578 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Jani Nikula5f461662020-11-30 13:15:58 +02003579 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003580 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Jani Nikula5f461662020-11-30 13:15:58 +02003581 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003582
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003583 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003584 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02003585 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003586 if (results->partitioning == INTEL_DDB_PART_1_2)
3587 val &= ~WM_MISC_DATA_PARTITION_5_6;
3588 else
3589 val |= WM_MISC_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003590 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003591 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02003592 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003593 if (results->partitioning == INTEL_DDB_PART_1_2)
3594 val &= ~DISP_DATA_PARTITION_5_6;
3595 else
3596 val |= DISP_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003597 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003598 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003599 }
3600
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003601 if (dirty & WM_DIRTY_FBC) {
Jani Nikula5f461662020-11-30 13:15:58 +02003602 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003603 if (results->enable_fbc_wm)
3604 val &= ~DISP_FBC_WM_DIS;
3605 else
3606 val |= DISP_FBC_WM_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02003607 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003608 }
3609
Imre Deak954911e2013-12-17 14:46:34 +02003610 if (dirty & WM_DIRTY_LP(1) &&
3611 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003612 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
Imre Deak954911e2013-12-17 14:46:34 +02003613
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003614 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003615 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003616 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003617 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003618 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003619 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003620
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003621 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003622 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003623 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003624 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003625 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003626 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003627
3628 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003629}
3630
Ville Syrjälä60aca572019-11-27 21:05:51 +02003631bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003632{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003633 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3634}
3635
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003636u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303637{
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003638 int i;
3639 int max_slices = INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
3640 u8 enabled_slices_mask = 0;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303641
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003642 for (i = 0; i < max_slices; i++) {
Jani Nikula5f461662020-11-30 13:15:58 +02003643 if (intel_uncore_read(&dev_priv->uncore, DBUF_CTL_S(i)) & DBUF_POWER_STATE)
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003644 enabled_slices_mask |= BIT(i);
3645 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303646
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003647 return enabled_slices_mask;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303648}
3649
Matt Roper024c9042015-09-24 15:53:11 -07003650/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003651 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3652 * so assume we'll always need it in order to avoid underruns.
3653 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003654static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003655{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003656 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003657}
3658
Paulo Zanoni56feca92016-09-22 18:00:28 -03003659static bool
3660intel_has_sagv(struct drm_i915_private *dev_priv)
3661{
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003662 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3663 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003664}
3665
James Ausmusb068a862019-10-09 10:23:14 -07003666static void
3667skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3668{
James Ausmusda80f042019-10-09 10:23:15 -07003669 if (INTEL_GEN(dev_priv) >= 12) {
3670 u32 val = 0;
3671 int ret;
3672
3673 ret = sandybridge_pcode_read(dev_priv,
3674 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3675 &val, NULL);
3676 if (!ret) {
3677 dev_priv->sagv_block_time_us = val;
3678 return;
3679 }
3680
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003681 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
James Ausmusda80f042019-10-09 10:23:15 -07003682 } else if (IS_GEN(dev_priv, 11)) {
James Ausmusb068a862019-10-09 10:23:14 -07003683 dev_priv->sagv_block_time_us = 10;
3684 return;
3685 } else if (IS_GEN(dev_priv, 10)) {
3686 dev_priv->sagv_block_time_us = 20;
3687 return;
3688 } else if (IS_GEN(dev_priv, 9)) {
3689 dev_priv->sagv_block_time_us = 30;
3690 return;
3691 } else {
3692 MISSING_CASE(INTEL_GEN(dev_priv));
3693 }
3694
3695 /* Default to an unusable block time */
3696 dev_priv->sagv_block_time_us = -1;
3697}
3698
Lyude656d1b82016-08-17 15:55:54 -04003699/*
3700 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3701 * depending on power and performance requirements. The display engine access
3702 * to system memory is blocked during the adjustment time. Because of the
3703 * blocking time, having this enabled can cause full system hangs and/or pipe
3704 * underruns if we don't meet all of the following requirements:
3705 *
3706 * - <= 1 pipe enabled
3707 * - All planes can enable watermarks for latencies >= SAGV engine block time
3708 * - We're not using an interlaced display configuration
3709 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003710static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003711intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003712{
3713 int ret;
3714
Paulo Zanoni56feca92016-09-22 18:00:28 -03003715 if (!intel_has_sagv(dev_priv))
3716 return 0;
3717
3718 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003719 return 0;
3720
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003721 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003722 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3723 GEN9_SAGV_ENABLE);
3724
Ville Syrjäläff61a972018-12-21 19:14:34 +02003725 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003726
3727 /*
3728 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003729 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003730 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003731 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003732 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003733 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003734 return 0;
3735 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003736 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003737 return ret;
3738 }
3739
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003740 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003741 return 0;
3742}
3743
Ville Syrjälä71024042020-09-25 15:17:48 +03003744static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003745intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003746{
Imre Deakb3b8e992016-12-05 18:27:38 +02003747 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003748
Paulo Zanoni56feca92016-09-22 18:00:28 -03003749 if (!intel_has_sagv(dev_priv))
3750 return 0;
3751
3752 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003753 return 0;
3754
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003755 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003756 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003757 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3758 GEN9_SAGV_DISABLE,
3759 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3760 1);
Lyude656d1b82016-08-17 15:55:54 -04003761 /*
3762 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003763 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003764 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003765 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003766 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003767 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003768 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003769 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003770 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003771 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003772 }
3773
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003774 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003775 return 0;
3776}
3777
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003778void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3779{
3780 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003781 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003782 const struct intel_bw_state *old_bw_state;
3783 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003784
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003785 /*
3786 * Just return if we can't control SAGV or don't have it.
3787 * This is different from situation when we have SAGV but just can't
3788 * afford it due to DBuf limitation - in case if SAGV is completely
3789 * disabled in a BIOS, we are not even allowed to send a PCode request,
3790 * as it will throw an error. So have to check it here.
3791 */
3792 if (!intel_has_sagv(dev_priv))
3793 return;
3794
3795 new_bw_state = intel_atomic_get_new_bw_state(state);
3796 if (!new_bw_state)
3797 return;
3798
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003799 if (INTEL_GEN(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003800 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003801 return;
3802 }
3803
3804 old_bw_state = intel_atomic_get_old_bw_state(state);
3805 /*
3806 * Nothing to mask
3807 */
3808 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3809 return;
3810
3811 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3812
3813 /*
3814 * If new mask is zero - means there is nothing to mask,
3815 * we can only unmask, which should be done in unmask.
3816 */
3817 if (!new_mask)
3818 return;
3819
3820 /*
3821 * Restrict required qgv points before updating the configuration.
3822 * According to BSpec we can't mask and unmask qgv points at the same
3823 * time. Also masking should be done before updating the configuration
3824 * and unmasking afterwards.
3825 */
3826 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003827}
3828
3829void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3830{
3831 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003832 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003833 const struct intel_bw_state *old_bw_state;
3834 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003835
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003836 /*
3837 * Just return if we can't control SAGV or don't have it.
3838 * This is different from situation when we have SAGV but just can't
3839 * afford it due to DBuf limitation - in case if SAGV is completely
3840 * disabled in a BIOS, we are not even allowed to send a PCode request,
3841 * as it will throw an error. So have to check it here.
3842 */
3843 if (!intel_has_sagv(dev_priv))
3844 return;
3845
3846 new_bw_state = intel_atomic_get_new_bw_state(state);
3847 if (!new_bw_state)
3848 return;
3849
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003850 if (INTEL_GEN(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003851 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003852 return;
3853 }
3854
3855 old_bw_state = intel_atomic_get_old_bw_state(state);
3856 /*
3857 * Nothing to unmask
3858 */
3859 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3860 return;
3861
3862 new_mask = new_bw_state->qgv_points_mask;
3863
3864 /*
3865 * Allow required qgv points after updating the configuration.
3866 * According to BSpec we can't mask and unmask qgv points at the same
3867 * time. Also masking should be done before updating the configuration
3868 * and unmasking afterwards.
3869 */
3870 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003871}
3872
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003873static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003874{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003875 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003876 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä9c312122020-11-06 19:30:40 +02003877 enum plane_id plane_id;
Lyude656d1b82016-08-17 15:55:54 -04003878
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003879 if (!intel_has_sagv(dev_priv))
3880 return false;
3881
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003882 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003883 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003884
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02003885 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003886 return false;
3887
Ville Syrjälä9c312122020-11-06 19:30:40 +02003888 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003889 const struct skl_plane_wm *wm =
Ville Syrjälä9c312122020-11-06 19:30:40 +02003890 &crtc_state->wm.skl.optimal.planes[plane_id];
3891 int level;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003892
Lyude656d1b82016-08-17 15:55:54 -04003893 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003894 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003895 continue;
3896
3897 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003898 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003899 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003900 { }
3901
3902 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003903 * If any of the planes on this pipe don't enable wm levels that
3904 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003905 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003906 */
Ville Syrjälä9c312122020-11-06 19:30:40 +02003907 if (!wm->wm[level].can_sagv)
Lyude656d1b82016-08-17 15:55:54 -04003908 return false;
3909 }
3910
3911 return true;
3912}
3913
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003914static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3915{
3916 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3917 enum plane_id plane_id;
3918
3919 if (!crtc_state->hw.active)
3920 return true;
3921
3922 for_each_plane_id_on_crtc(crtc, plane_id) {
3923 const struct skl_ddb_entry *plane_alloc =
3924 &crtc_state->wm.skl.plane_ddb_y[plane_id];
3925 const struct skl_plane_wm *wm =
3926 &crtc_state->wm.skl.optimal.planes[plane_id];
3927
3928 if (skl_ddb_entry_size(plane_alloc) < wm->sagv_wm0.min_ddb_alloc)
3929 return false;
3930 }
3931
3932 return true;
3933}
3934
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003935static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3936{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003937 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3938 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3939
3940 if (INTEL_GEN(dev_priv) >= 12)
3941 return tgl_crtc_can_enable_sagv(crtc_state);
3942 else
3943 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003944}
3945
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003946bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3947 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003948{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003949 if (INTEL_GEN(dev_priv) < 11 &&
3950 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003951 return false;
3952
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003953 return bw_state->pipe_sagv_reject == 0;
3954}
3955
3956static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3957{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003958 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003959 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003960 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003961 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003962 struct intel_bw_state *new_bw_state = NULL;
3963 const struct intel_bw_state *old_bw_state = NULL;
3964 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003965
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003966 for_each_new_intel_crtc_in_state(state, crtc,
3967 new_crtc_state, i) {
3968 new_bw_state = intel_atomic_get_bw_state(state);
3969 if (IS_ERR(new_bw_state))
3970 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003971
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003972 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003973
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003974 if (intel_crtc_can_enable_sagv(new_crtc_state))
3975 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
3976 else
3977 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
3978 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003979
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003980 if (!new_bw_state)
3981 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003982
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003983 new_bw_state->active_pipes =
3984 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003985
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003986 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
3987 ret = intel_atomic_lock_global_state(&new_bw_state->base);
3988 if (ret)
3989 return ret;
3990 }
3991
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003992 for_each_new_intel_crtc_in_state(state, crtc,
3993 new_crtc_state, i) {
3994 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
3995
3996 /*
3997 * We store use_sagv_wm in the crtc state rather than relying on
3998 * that bw state since we have no convenient way to get at the
3999 * latter from the plane commit hooks (especially in the legacy
4000 * cursor case)
4001 */
4002 pipe_wm->use_sagv_wm = INTEL_GEN(dev_priv) >= 12 &&
4003 intel_can_enable_sagv(dev_priv, new_bw_state);
4004 }
4005
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004006 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4007 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004008 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4009 if (ret)
4010 return ret;
4011 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4012 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4013 if (ret)
4014 return ret;
4015 }
4016
4017 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004018}
4019
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004020static int intel_dbuf_size(struct drm_i915_private *dev_priv)
4021{
4022 int ddb_size = INTEL_INFO(dev_priv)->ddb_size;
4023
4024 drm_WARN_ON(&dev_priv->drm, ddb_size == 0);
4025
4026 if (INTEL_GEN(dev_priv) < 11)
4027 return ddb_size - 4; /* 4 blocks for bypass path allocation */
4028
4029 return ddb_size;
4030}
4031
4032static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4033{
4034 return intel_dbuf_size(dev_priv) /
4035 INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
4036}
4037
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004038static void
4039skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4040 struct skl_ddb_entry *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304041{
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004042 int slice_size = intel_dbuf_slice_size(dev_priv);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004043
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004044 if (!slice_mask) {
4045 ddb->start = 0;
4046 ddb->end = 0;
4047 return;
4048 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004049
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004050 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4051 ddb->end = fls(slice_mask) * slice_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004052
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004053 WARN_ON(ddb->start >= ddb->end);
4054 WARN_ON(ddb->end > intel_dbuf_size(dev_priv));
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004055}
4056
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004057u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4058 const struct skl_ddb_entry *entry)
4059{
4060 u32 slice_mask = 0;
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004061 u16 ddb_size = intel_dbuf_size(dev_priv);
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004062 u16 num_supported_slices = INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
4063 u16 slice_size = ddb_size / num_supported_slices;
4064 u16 start_slice;
4065 u16 end_slice;
4066
4067 if (!skl_ddb_entry_size(entry))
4068 return 0;
4069
4070 start_slice = entry->start / slice_size;
4071 end_slice = (entry->end - 1) / slice_size;
4072
4073 /*
4074 * Per plane DDB entry can in a really worst case be on multiple slices
4075 * but single entry is anyway contigious.
4076 */
4077 while (start_slice <= end_slice) {
4078 slice_mask |= BIT(start_slice);
4079 start_slice++;
4080 }
4081
4082 return slice_mask;
4083}
4084
Ville Syrjälä2791a402021-01-22 22:56:26 +02004085static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4086{
4087 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4088 int hdisplay, vdisplay;
4089
4090 if (!crtc_state->hw.active)
4091 return 0;
4092
4093 /*
4094 * Watermark/ddb requirement highly depends upon width of the
4095 * framebuffer, So instead of allocating DDB equally among pipes
4096 * distribute DDB based on resolution/width of the display.
4097 */
4098 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4099
4100 return hdisplay;
4101}
4102
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004103static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc,
Ville Syrjälä05e81552020-02-25 19:11:09 +02004104 u8 active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004105
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004106static int
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004107skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Ville Syrjälä47a14952021-01-22 22:56:30 +02004108 struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004109 const u64 total_data_rate,
Matt Roperc107acf2016-05-12 07:06:01 -07004110 struct skl_ddb_entry *alloc, /* out */
4111 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004112{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004113 struct drm_atomic_state *state = crtc_state->uapi.state;
Matt Roperc107acf2016-05-12 07:06:01 -07004114 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004115 struct intel_crtc *for_crtc = to_intel_crtc(crtc_state->uapi.crtc);
4116 struct intel_crtc *crtc;
Ville Syrjälä2791a402021-01-22 22:56:26 +02004117 unsigned int pipe_weight = 0, total_weight = 0, weight_before_pipe = 0;
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004118 enum pipe for_pipe = for_crtc->pipe;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004119 struct intel_dbuf_state *new_dbuf_state =
4120 intel_atomic_get_new_dbuf_state(intel_state);
4121 const struct intel_dbuf_state *old_dbuf_state =
4122 intel_atomic_get_old_dbuf_state(intel_state);
4123 u8 active_pipes = new_dbuf_state->active_pipes;
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004124 struct skl_ddb_entry ddb_slices;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004125 u32 ddb_range_size;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304126 u32 i;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004127 u32 dbuf_slice_mask;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004128 u32 total_slice_mask;
4129 u32 start, end;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004130 int ret;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004131
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004132 *num_active = hweight8(active_pipes);
4133
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004134 if (!crtc_state->hw.active) {
4135 alloc->start = 0;
4136 alloc->end = 0;
Ville Syrjälä47a14952021-01-22 22:56:30 +02004137 crtc_state->wm.skl.ddb = *alloc;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004138 return 0;
4139 }
4140
Matt Roperc107acf2016-05-12 07:06:01 -07004141 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304142 * If the state doesn't change the active CRTC's or there is no
4143 * modeset request, then there's no need to recalculate;
4144 * the existing pipe allocation limits should remain unchanged.
4145 * Note that we're safe from racing commits since any racing commit
4146 * that changes the active CRTC list or do modeset would need to
4147 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07004148 */
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004149 if (old_dbuf_state->active_pipes == new_dbuf_state->active_pipes &&
Ville Syrjälä47a14952021-01-22 22:56:30 +02004150 !dev_priv->wm.distrust_bios_wm)
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004151 return 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004152
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304153 /*
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004154 * Get allowed DBuf slices for correspondent pipe and platform.
4155 */
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004156 dbuf_slice_mask = skl_compute_dbuf_slices(for_crtc, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004157
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004158 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
4159 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004160
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004161 total_slice_mask = dbuf_slice_mask;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004162 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004163 enum pipe pipe = crtc->pipe;
Ville Syrjälä2791a402021-01-22 22:56:26 +02004164 unsigned int weight;
4165 u8 pipe_dbuf_slice_mask;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304166
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004167 if (!crtc_state->hw.active)
4168 continue;
4169
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004170 pipe_dbuf_slice_mask =
4171 skl_compute_dbuf_slices(crtc, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004172
4173 /*
4174 * According to BSpec pipe can share one dbuf slice with another
4175 * pipes or pipe can use multiple dbufs, in both cases we
4176 * account for other pipes only if they have exactly same mask.
4177 * However we need to account how many slices we should enable
4178 * in total.
4179 */
4180 total_slice_mask |= pipe_dbuf_slice_mask;
4181
4182 /*
4183 * Do not account pipes using other slice sets
4184 * luckily as of current BSpec slice sets do not partially
4185 * intersect(pipes share either same one slice or same slice set
4186 * i.e no partial intersection), so it is enough to check for
4187 * equality for now.
4188 */
4189 if (dbuf_slice_mask != pipe_dbuf_slice_mask)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304190 continue;
4191
Ville Syrjälä2791a402021-01-22 22:56:26 +02004192 weight = intel_crtc_ddb_weight(crtc_state);
4193 total_weight += weight;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304194
4195 if (pipe < for_pipe)
Ville Syrjälä2791a402021-01-22 22:56:26 +02004196 weight_before_pipe += weight;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304197 else if (pipe == for_pipe)
Ville Syrjälä2791a402021-01-22 22:56:26 +02004198 pipe_weight = weight;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304199 }
4200
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004201 /*
4202 * FIXME: For now we always enable slice S1 as per
4203 * the Bspec display initialization sequence.
4204 */
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004205 new_dbuf_state->enabled_slices = total_slice_mask | BIT(DBUF_S1);
4206
4207 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices) {
4208 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
4209 if (ret)
4210 return ret;
4211 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004212
Ville Syrjälä2791a402021-01-22 22:56:26 +02004213 start = ddb_range_size * weight_before_pipe / total_weight;
4214 end = ddb_range_size * (weight_before_pipe + pipe_weight) / total_weight;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004215
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004216 alloc->start = ddb_slices.start + start;
4217 alloc->end = ddb_slices.start + end;
Ville Syrjälä47a14952021-01-22 22:56:30 +02004218 crtc_state->wm.skl.ddb = *alloc;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004219
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004220 drm_dbg_kms(&dev_priv->drm,
4221 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x\n",
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004222 for_crtc->base.base.id, for_crtc->base.name,
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004223 dbuf_slice_mask, alloc->start, alloc->end, 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{
Mahesh Kumar37cde112018-04-26 19:55:17 +05304273
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004274 entry->start = reg & DDB_ENTRY_MASK;
4275 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304276
Damien Lespiau16160e32014-11-04 17:06:53 +00004277 if (entry->end)
4278 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004279}
4280
Mahesh Kumarddf34312018-04-09 09:11:03 +05304281static void
4282skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4283 const enum pipe pipe,
4284 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004285 struct skl_ddb_entry *ddb_y,
4286 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304287{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004288 u32 val, val2;
4289 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304290
4291 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4292 if (plane_id == PLANE_CURSOR) {
Jani Nikula5f461662020-11-30 13:15:58 +02004293 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004294 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304295 return;
4296 }
4297
Jani Nikula5f461662020-11-30 13:15:58 +02004298 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304299
4300 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004301 if (val & PLANE_CTL_ENABLE)
4302 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4303 val & PLANE_CTL_ORDER_RGBX,
4304 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304305
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004306 if (INTEL_GEN(dev_priv) >= 11) {
Jani Nikula5f461662020-11-30 13:15:58 +02004307 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004308 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4309 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02004310 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4311 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304312
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004313 if (fourcc &&
4314 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004315 swap(val, val2);
4316
4317 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4318 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304319 }
4320}
4321
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004322void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4323 struct skl_ddb_entry *ddb_y,
4324 struct skl_ddb_entry *ddb_uv)
4325{
4326 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4327 enum intel_display_power_domain power_domain;
4328 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004329 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004330 enum plane_id plane_id;
4331
4332 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004333 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4334 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004335 return;
4336
4337 for_each_plane_id_on_crtc(crtc, plane_id)
4338 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4339 plane_id,
4340 &ddb_y[plane_id],
4341 &ddb_uv[plane_id]);
4342
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004343 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004344}
4345
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004346/*
4347 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4348 * The bspec defines downscale amount as:
4349 *
4350 * """
4351 * Horizontal down scale amount = maximum[1, Horizontal source size /
4352 * Horizontal destination size]
4353 * Vertical down scale amount = maximum[1, Vertical source size /
4354 * Vertical destination size]
4355 * Total down scale amount = Horizontal down scale amount *
4356 * Vertical down scale amount
4357 * """
4358 *
4359 * Return value is provided in 16.16 fixed point form to retain fractional part.
4360 * Caller should take care of dividing & rounding off the value.
4361 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304362static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004363skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4364 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004365{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304366 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004367 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304368 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4369 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004370
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304371 if (drm_WARN_ON(&dev_priv->drm,
4372 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304373 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004374
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004375 /*
4376 * Src coordinates are already rotated by 270 degrees for
4377 * the 90/270 degree plane rotation cases (to match the
4378 * GTT mapping), hence no need to account for rotation here.
4379 *
4380 * n.b., src is 16.16 fixed point, dst is whole integer.
4381 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004382 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4383 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4384 dst_w = drm_rect_width(&plane_state->uapi.dst);
4385 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004386
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304387 fp_w_ratio = div_fixed16(src_w, dst_w);
4388 fp_h_ratio = div_fixed16(src_h, dst_h);
4389 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4390 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004391
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304392 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004393}
4394
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004395struct dbuf_slice_conf_entry {
4396 u8 active_pipes;
4397 u8 dbuf_mask[I915_MAX_PIPES];
4398};
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
Ville Syrjälä05e81552020-02-25 19:11:09 +02004586static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4587 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004588{
4589 int i;
4590
Ville Syrjälä05e81552020-02-25 19:11:09 +02004591 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004592 if (dbuf_slices[i].active_pipes == active_pipes)
4593 return dbuf_slices[i].dbuf_mask[pipe];
4594 }
4595 return 0;
4596}
4597
4598/*
4599 * This function finds an entry with same enabled pipe configuration and
4600 * returns correspondent DBuf slice mask as stated in BSpec for particular
4601 * platform.
4602 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004603static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004604{
4605 /*
4606 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4607 * required calculating "pipe ratio" in order to determine
4608 * if one or two slices can be used for single pipe configurations
4609 * as additional constraint to the existing table.
4610 * However based on recent info, it should be not "pipe ratio"
4611 * but rather ratio between pixel_rate and cdclk with additional
4612 * constants, so for now we are using only table until this is
4613 * clarified. Also this is the reason why crtc_state param is
4614 * still here - we will need it once those additional constraints
4615 * pop up.
4616 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004617 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004618}
4619
Ville Syrjälä05e81552020-02-25 19:11:09 +02004620static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004621{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004622 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004623}
4624
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004625static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004626{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004627 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4628 enum pipe pipe = crtc->pipe;
4629
4630 if (IS_GEN(dev_priv, 12))
Ville Syrjälä05e81552020-02-25 19:11:09 +02004631 return tgl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004632 else if (IS_GEN(dev_priv, 11))
Ville Syrjälä05e81552020-02-25 19:11:09 +02004633 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004634 /*
4635 * For anything else just return one slice yet.
4636 * Should be extended for other platforms.
4637 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004638 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004639}
4640
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004641static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004642skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4643 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004644 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004645{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004646 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004647 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004648 u32 data_rate;
4649 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304650 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004651 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004652
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004653 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004654 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004655
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004656 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004657 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004658
4659 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004660 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004661 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004662
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004663 /*
4664 * Src coordinates are already rotated by 270 degrees for
4665 * the 90/270 degree plane rotation cases (to match the
4666 * GTT mapping), hence no need to account for rotation here.
4667 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004668 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4669 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004670
Mahesh Kumarb879d582018-04-09 09:11:01 +05304671 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004672 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304673 width /= 2;
4674 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004675 }
4676
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004677 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304678
Maarten Lankhorstec193642019-06-28 10:55:17 +02004679 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004680
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004681 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4682
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004683 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004684 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004685}
4686
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004687static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004688skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4689 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004690{
Ville Syrjäläab016302020-11-06 19:30:41 +02004691 struct intel_crtc_state *crtc_state =
4692 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004693 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004694 struct intel_plane *plane;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004695 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004696 enum plane_id plane_id;
4697 int i;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004698
Matt Ropera1de91e2016-05-12 07:05:57 -07004699 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004700 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4701 if (plane->pipe != crtc->pipe)
4702 continue;
4703
4704 plane_id = plane->id;
Matt Roper024c9042015-09-24 15:53:11 -07004705
Mahesh Kumarb879d582018-04-09 09:11:01 +05304706 /* packed/y */
Ville Syrjäläab016302020-11-06 19:30:41 +02004707 crtc_state->plane_data_rate[plane_id] =
4708 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Matt Roper9c74d822016-05-12 07:05:58 -07004709
Mahesh Kumarb879d582018-04-09 09:11:01 +05304710 /* uv-plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004711 crtc_state->uv_plane_data_rate[plane_id] =
4712 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4713 }
4714
4715 for_each_plane_id_on_crtc(crtc, plane_id) {
4716 total_data_rate += crtc_state->plane_data_rate[plane_id];
4717 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004718 }
4719
4720 return total_data_rate;
4721}
4722
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004723static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004724icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4725 struct intel_crtc *crtc)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004726{
Ville Syrjäläab016302020-11-06 19:30:41 +02004727 struct intel_crtc_state *crtc_state =
4728 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004729 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004730 struct intel_plane *plane;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004731 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004732 enum plane_id plane_id;
4733 int i;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004734
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004735 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004736 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4737 if (plane->pipe != crtc->pipe)
4738 continue;
4739
4740 plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004741
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004742 if (!plane_state->planar_linked_plane) {
Ville Syrjäläab016302020-11-06 19:30:41 +02004743 crtc_state->plane_data_rate[plane_id] =
4744 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004745 } else {
4746 enum plane_id y_plane_id;
4747
4748 /*
4749 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004750 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004751 * and needs the master plane state which may be
4752 * NULL if we try get_new_plane_state(), so we
4753 * always calculate from the master.
4754 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004755 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004756 continue;
4757
4758 /* Y plane rate is calculated on the slave */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004759 y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjäläab016302020-11-06 19:30:41 +02004760 crtc_state->plane_data_rate[y_plane_id] =
4761 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004762
Ville Syrjäläab016302020-11-06 19:30:41 +02004763 crtc_state->plane_data_rate[plane_id] =
4764 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004765 }
4766 }
4767
Ville Syrjäläab016302020-11-06 19:30:41 +02004768 for_each_plane_id_on_crtc(crtc, plane_id)
4769 total_data_rate += crtc_state->plane_data_rate[plane_id];
4770
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004771 return total_data_rate;
4772}
4773
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004774static const struct skl_wm_level *
4775skl_plane_wm_level(const struct intel_crtc_state *crtc_state,
4776 enum plane_id plane_id,
4777 int level)
4778{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004779 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
4780 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4781
4782 if (level == 0 && pipe_wm->use_sagv_wm)
4783 return &wm->sagv_wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004784
4785 return &wm->wm[level];
4786}
4787
Matt Roperc107acf2016-05-12 07:06:01 -07004788static int
Ville Syrjäläffc90032020-11-06 19:30:37 +02004789skl_allocate_pipe_ddb(struct intel_atomic_state *state,
4790 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004791{
Ville Syrjäläffc90032020-11-06 19:30:37 +02004792 struct intel_crtc_state *crtc_state =
4793 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004794 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä47a14952021-01-22 22:56:30 +02004795 struct intel_dbuf_state *dbuf_state =
4796 intel_atomic_get_new_dbuf_state(state);
4797 struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004798 u16 alloc_size, start = 0;
4799 u16 total[I915_MAX_PLANES] = {};
4800 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004801 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004802 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004803 int num_active;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004804 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004805 int level;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004806 int ret;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004807
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004808 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004809 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4810 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004811
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004812 if (!crtc_state->hw.active) {
Ville Syrjäläb6a13a32020-05-18 15:13:54 +03004813 struct intel_atomic_state *state =
4814 to_intel_atomic_state(crtc_state->uapi.state);
4815 struct intel_dbuf_state *new_dbuf_state =
4816 intel_atomic_get_new_dbuf_state(state);
4817 const struct intel_dbuf_state *old_dbuf_state =
4818 intel_atomic_get_old_dbuf_state(state);
4819
4820 /*
4821 * FIXME hack to make sure we compute this sensibly when
4822 * turning off all the pipes. Otherwise we leave it at
4823 * whatever we had previously, and then runtime PM will
4824 * mess it up by turning off all but S1. Remove this
4825 * once the dbuf state computation flow becomes sane.
4826 */
4827 if (new_dbuf_state->active_pipes == 0) {
4828 new_dbuf_state->enabled_slices = BIT(DBUF_S1);
4829
4830 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices) {
4831 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
4832 if (ret)
4833 return ret;
4834 }
4835 }
4836
Lyudece0ba282016-09-15 10:46:35 -04004837 alloc->start = alloc->end = 0;
Ville Syrjälä47a14952021-01-22 22:56:30 +02004838 crtc_state->wm.skl.ddb = *alloc;
Matt Roperc107acf2016-05-12 07:06:01 -07004839 return 0;
4840 }
4841
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004842 if (INTEL_GEN(dev_priv) >= 11)
4843 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02004844 icl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004845 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004846 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02004847 skl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004848
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004849 ret = skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state,
4850 total_data_rate,
4851 alloc, &num_active);
4852 if (ret)
4853 return ret;
4854
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004855 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304856 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004857 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004858
Matt Roperd8e87492018-12-11 09:31:07 -08004859 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004860 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004861 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004862 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004863 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004864 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004865
Matt Ropera1de91e2016-05-12 07:05:57 -07004866 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004867 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004868
Matt Roperd8e87492018-12-11 09:31:07 -08004869 /*
4870 * Find the highest watermark level for which we can satisfy the block
4871 * requirement of active planes.
4872 */
4873 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004874 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004875 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004876 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004877 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004878
4879 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05304880 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304881 drm_WARN_ON(&dev_priv->drm,
4882 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004883 blocks = U32_MAX;
4884 break;
4885 }
4886 continue;
4887 }
4888
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004889 blocks += wm->wm[level].min_ddb_alloc;
4890 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004891 }
4892
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004893 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004894 alloc_size -= blocks;
4895 break;
4896 }
4897 }
4898
4899 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03004900 drm_dbg_kms(&dev_priv->drm,
4901 "Requested display configuration exceeds system DDB limitations");
4902 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
4903 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08004904 return -EINVAL;
4905 }
4906
4907 /*
4908 * Grant each plane the blocks it requires at the highest achievable
4909 * watermark level, plus an extra share of the leftover blocks
4910 * proportional to its relative data rate.
4911 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004912 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004913 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004914 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004915 u64 rate;
4916 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004917
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004918 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004919 continue;
4920
Damien Lespiaub9cec072014-11-04 17:06:43 +00004921 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004922 * We've accounted for all active planes; remaining planes are
4923 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004924 */
Matt Roperd8e87492018-12-11 09:31:07 -08004925 if (total_data_rate == 0)
4926 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004927
Ville Syrjäläab016302020-11-06 19:30:41 +02004928 rate = crtc_state->plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004929 extra = min_t(u16, alloc_size,
4930 DIV64_U64_ROUND_UP(alloc_size * rate,
4931 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004932 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004933 alloc_size -= extra;
4934 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004935
Matt Roperd8e87492018-12-11 09:31:07 -08004936 if (total_data_rate == 0)
4937 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004938
Ville Syrjäläab016302020-11-06 19:30:41 +02004939 rate = crtc_state->uv_plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004940 extra = min_t(u16, alloc_size,
4941 DIV64_U64_ROUND_UP(alloc_size * rate,
4942 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004943 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004944 alloc_size -= extra;
4945 total_data_rate -= rate;
4946 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304947 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08004948
4949 /* Set the actual DDB start/end points for each plane */
4950 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004951 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004952 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004953 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004954 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004955 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004956
4957 if (plane_id == PLANE_CURSOR)
4958 continue;
4959
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004960 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304961 drm_WARN_ON(&dev_priv->drm,
4962 INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004963
Matt Roperd8e87492018-12-11 09:31:07 -08004964 /* Leave disabled planes at (0,0) */
4965 if (total[plane_id]) {
4966 plane_alloc->start = start;
4967 start += total[plane_id];
4968 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004969 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004970
Matt Roperd8e87492018-12-11 09:31:07 -08004971 if (uv_total[plane_id]) {
4972 uv_plane_alloc->start = start;
4973 start += uv_total[plane_id];
4974 uv_plane_alloc->end = start;
4975 }
4976 }
4977
4978 /*
4979 * When we calculated watermark values we didn't know how high
4980 * of a level we'd actually be able to hit, so we just marked
4981 * all levels as "enabled." Go back now and disable the ones
4982 * that aren't actually possible.
4983 */
4984 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004985 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004986 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004987 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004988
4989 /*
4990 * We only disable the watermarks for each plane if
4991 * they exceed the ddb allocation of said plane. This
4992 * is done so that we don't end up touching cursor
4993 * watermarks needlessly when some other plane reduces
4994 * our max possible watermark level.
4995 *
4996 * Bspec has this to say about the PLANE_WM enable bit:
4997 * "All the watermarks at this level for all enabled
4998 * planes must be enabled before the level will be used."
4999 * So this is actually safe to do.
5000 */
5001 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
5002 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
5003 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02005004
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005005 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005006 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005007 * Underruns with WM1+ disabled
5008 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07005009 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02005010 level == 1 && wm->wm[0].plane_en) {
5011 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005012 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
5013 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005014 }
Matt Roperd8e87492018-12-11 09:31:07 -08005015 }
5016 }
5017
5018 /*
5019 * Go back and disable the transition watermark if it turns out we
5020 * don't have enough DDB blocks for it.
5021 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005022 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005023 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005024 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005025
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02005026 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08005027 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00005028 }
5029
Matt Roperc107acf2016-05-12 07:06:01 -07005030 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005031}
5032
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005033/*
5034 * 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 +02005035 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005036 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5037 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5038*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005039static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005040skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5041 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005042{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005043 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305044 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005045
5046 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305047 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005048
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305049 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005050 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005051
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005052 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005053 ret = add_fixed16_u32(ret, 1);
5054
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005055 return ret;
5056}
5057
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005058static uint_fixed_16_16_t
5059skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5060 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005061{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005062 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305063 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005064
5065 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305066 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005067
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005068 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305069 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5070 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305071 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005072 return ret;
5073}
5074
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305075static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005076intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305077{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305078 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005079 u32 pixel_rate;
5080 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305081 uint_fixed_16_16_t linetime_us;
5082
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005083 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305084 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305085
Maarten Lankhorstec193642019-06-28 10:55:17 +02005086 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305087
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305088 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305089 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305090
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005091 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305092 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305093
5094 return linetime_us;
5095}
5096
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305097static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005098skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5099 int width, const struct drm_format_info *format,
5100 u64 modifier, unsigned int rotation,
5101 u32 plane_pixel_rate, struct skl_wm_params *wp,
5102 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305103{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005104 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005105 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005106 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305107
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305108 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005109 if (color_plane == 1 &&
5110 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005111 drm_dbg_kms(&dev_priv->drm,
5112 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305113 return -EINVAL;
5114 }
5115
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005116 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5117 modifier == I915_FORMAT_MOD_Yf_TILED ||
5118 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5119 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5120 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5121 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5122 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005123 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305124
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005125 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005126 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305127 wp->width /= 2;
5128
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005129 wp->cpp = format->cpp[color_plane];
5130 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305131
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005132 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005133 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005134 wp->dbuf_block_size = 256;
5135 else
5136 wp->dbuf_block_size = 512;
5137
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005138 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305139 switch (wp->cpp) {
5140 case 1:
5141 wp->y_min_scanlines = 16;
5142 break;
5143 case 2:
5144 wp->y_min_scanlines = 8;
5145 break;
5146 case 4:
5147 wp->y_min_scanlines = 4;
5148 break;
5149 default:
5150 MISSING_CASE(wp->cpp);
5151 return -EINVAL;
5152 }
5153 } else {
5154 wp->y_min_scanlines = 4;
5155 }
5156
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005157 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305158 wp->y_min_scanlines *= 2;
5159
5160 wp->plane_bytes_per_line = wp->width * wp->cpp;
5161 if (wp->y_tiled) {
5162 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005163 wp->y_min_scanlines,
5164 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305165
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005166 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305167 interm_pbpl++;
5168
5169 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5170 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305171 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005172 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005173 wp->dbuf_block_size);
5174
5175 if (!wp->x_tiled ||
5176 INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
5177 interm_pbpl++;
5178
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305179 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5180 }
5181
5182 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5183 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005184
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305185 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005186 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305187
5188 return 0;
5189}
5190
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005191static int
5192skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5193 const struct intel_plane_state *plane_state,
5194 struct skl_wm_params *wp, int color_plane)
5195{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005196 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005197 int width;
5198
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005199 /*
5200 * Src coordinates are already rotated by 270 degrees for
5201 * the 90/270 degree plane rotation cases (to match the
5202 * GTT mapping), hence no need to account for rotation here.
5203 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005204 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005205
5206 return skl_compute_wm_params(crtc_state, width,
5207 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005208 plane_state->hw.rotation,
Ville Syrjälä3df3fe22020-11-06 19:30:42 +02005209 intel_plane_pixel_rate(crtc_state, plane_state),
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005210 wp, color_plane);
5211}
5212
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005213static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5214{
5215 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
5216 return true;
5217
5218 /* The number of lines are ignored for the level 0 watermark. */
5219 return level > 0;
5220}
5221
Maarten Lankhorstec193642019-06-28 10:55:17 +02005222static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005223 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005224 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005225 const struct skl_wm_params *wp,
5226 const struct skl_wm_level *result_prev,
5227 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005228{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005229 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305230 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305231 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005232 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005233
Ville Syrjälä0aded172019-02-05 17:50:53 +02005234 if (latency == 0) {
5235 /* reject it */
5236 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005237 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005238 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005239
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005240 /*
5241 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5242 * Display WA #1141: kbl,cfl
5243 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005244 if ((IS_KABYLAKE(dev_priv) ||
5245 IS_COFFEELAKE(dev_priv) ||
5246 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005247 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305248 latency += 4;
5249
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005250 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005251 latency += 15;
5252
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305253 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005254 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305255 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005256 crtc_state->hw.pipe_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005257 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305258 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005259
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305260 if (wp->y_tiled) {
5261 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005262 } else {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005263 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005264 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005265 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005266 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005267 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005268 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005269 !IS_GEMINILAKE(dev_priv))
5270 selected_result = min_fixed16(method1, method2);
5271 else
5272 selected_result = method2;
5273 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005274 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005275 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005276 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005277
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305278 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05305279 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305280 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005281
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005282 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
5283 /* Display WA #1125: skl,bxt,kbl */
5284 if (level == 0 && wp->rc_surface)
5285 res_blocks +=
5286 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005287
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005288 /* Display WA #1126: skl,bxt,kbl */
5289 if (level >= 1 && level <= 7) {
5290 if (wp->y_tiled) {
5291 res_blocks +=
5292 fixed16_to_u32_round_up(wp->y_tile_minimum);
5293 res_lines += wp->y_min_scanlines;
5294 } else {
5295 res_blocks++;
5296 }
5297
5298 /*
5299 * Make sure result blocks for higher latency levels are
5300 * atleast as high as level below the current level.
5301 * Assumption in DDB algorithm optimization for special
5302 * cases. Also covers Display WA #1125 for RC.
5303 */
5304 if (result_prev->plane_res_b > res_blocks)
5305 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005306 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005307 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005308
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005309 if (INTEL_GEN(dev_priv) >= 11) {
5310 if (wp->y_tiled) {
5311 int extra_lines;
5312
5313 if (res_lines % wp->y_min_scanlines == 0)
5314 extra_lines = wp->y_min_scanlines;
5315 else
5316 extra_lines = wp->y_min_scanlines * 2 -
5317 res_lines % wp->y_min_scanlines;
5318
5319 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
5320 wp->plane_blocks_per_line);
5321 } else {
5322 min_ddb_alloc = res_blocks +
5323 DIV_ROUND_UP(res_blocks, 10);
5324 }
5325 }
5326
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005327 if (!skl_wm_has_lines(dev_priv, level))
5328 res_lines = 0;
5329
Ville Syrjälä0aded172019-02-05 17:50:53 +02005330 if (res_lines > 31) {
5331 /* reject it */
5332 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005333 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005334 }
Matt Roperd8e87492018-12-11 09:31:07 -08005335
5336 /*
5337 * If res_lines is valid, assume we can use this watermark level
5338 * for now. We'll come back and disable it after we calculate the
5339 * DDB allocation if it turns out we don't actually have enough
5340 * blocks to satisfy it.
5341 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05305342 result->plane_res_b = res_blocks;
5343 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005344 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
5345 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05305346 result->plane_en = true;
Ville Syrjälä9c312122020-11-06 19:30:40 +02005347
5348 if (INTEL_GEN(dev_priv) < 12)
5349 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005350}
5351
Matt Roperd8e87492018-12-11 09:31:07 -08005352static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005353skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305354 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005355 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005356{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005357 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305358 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005359 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005360
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305361 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005362 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005363 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305364
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005365 skl_compute_plane_wm(crtc_state, level, latency,
5366 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005367
5368 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305369 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005370}
5371
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005372static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5373 const struct skl_wm_params *wm_params,
5374 struct skl_plane_wm *plane_wm)
5375{
5376 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5377 struct skl_wm_level *sagv_wm = &plane_wm->sagv_wm0;
5378 struct skl_wm_level *levels = plane_wm->wm;
5379 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5380
5381 skl_compute_plane_wm(crtc_state, 0, latency,
5382 wm_params, &levels[0],
5383 sagv_wm);
5384}
5385
Maarten Lankhorstec193642019-06-28 10:55:17 +02005386static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02005387 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08005388 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005389{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005390 struct drm_device *dev = crtc_state->uapi.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05305391 const struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc834d032020-02-28 22:35:52 +02005392 u16 trans_min, trans_amount, trans_y_tile_min;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005393 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005394
Kumar, Maheshca476672017-08-17 19:15:24 +05305395 /* Transition WM don't make any sense if ipc is disabled */
5396 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005397 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305398
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005399 /*
5400 * WaDisableTWM:skl,kbl,cfl,bxt
5401 * Transition WM are not recommended by HW team for GEN9
5402 */
5403 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
5404 return;
5405
Paulo Zanoni91961a82018-10-04 16:15:56 -07005406 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305407 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005408 else
5409 trans_min = 14;
5410
5411 /* Display WA #1140: glk,cnl */
5412 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
5413 trans_amount = 0;
5414 else
5415 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305416
5417 trans_offset_b = trans_min + trans_amount;
5418
Paulo Zanonicbacc792018-10-04 16:15:58 -07005419 /*
5420 * The spec asks for Selected Result Blocks for wm0 (the real value),
5421 * not Result Blocks (the integer value). Pay attention to the capital
5422 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
5423 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5424 * and since we later will have to get the ceiling of the sum in the
5425 * transition watermarks calculation, we can just pretend Selected
5426 * Result Blocks is Result Blocks minus 1 and it should work for the
5427 * current platforms.
5428 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02005429 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005430
Kumar, Maheshca476672017-08-17 19:15:24 +05305431 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005432 trans_y_tile_min =
5433 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07005434 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05305435 trans_offset_b;
5436 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07005437 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05305438 }
5439
Matt Roperd8e87492018-12-11 09:31:07 -08005440 /*
5441 * Just assume we can enable the transition watermark. After
5442 * computing the DDB we'll come back and disable it if that
5443 * assumption turns out to be false.
5444 */
5445 wm->trans_wm.plane_res_b = res_blocks + 1;
5446 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005447}
5448
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005449static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005450 const struct intel_plane_state *plane_state,
5451 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005452{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005453 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5454 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälädbf71382020-11-06 19:30:38 +02005455 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005456 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005457 int ret;
5458
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005459 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005460 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005461 if (ret)
5462 return ret;
5463
Ville Syrjälä67155a62019-03-12 22:58:37 +02005464 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005465
5466 if (INTEL_GEN(dev_priv) >= 12)
5467 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5468
Matt Roperd8e87492018-12-11 09:31:07 -08005469 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005470
5471 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005472}
5473
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005474static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005475 const struct intel_plane_state *plane_state,
5476 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005477{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005478 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005479 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005480 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005481
Ville Syrjälä83158472018-11-27 18:57:26 +02005482 wm->is_planar = true;
5483
5484 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005485 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005486 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005487 if (ret)
5488 return ret;
5489
Ville Syrjälä67155a62019-03-12 22:58:37 +02005490 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005491
5492 return 0;
5493}
5494
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005495static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005496 const struct intel_plane_state *plane_state)
5497{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005498 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä83158472018-11-27 18:57:26 +02005499 enum plane_id plane_id = plane->id;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005500 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5501 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005502 int ret;
5503
Ville Syrjälädbf71382020-11-06 19:30:38 +02005504 memset(wm, 0, sizeof(*wm));
5505
Ville Syrjälä83158472018-11-27 18:57:26 +02005506 if (!intel_wm_plane_visible(crtc_state, plane_state))
5507 return 0;
5508
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005509 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005510 plane_id, 0);
5511 if (ret)
5512 return ret;
5513
5514 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005515 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005516 plane_id);
5517 if (ret)
5518 return ret;
5519 }
5520
5521 return 0;
5522}
5523
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005524static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005525 const struct intel_plane_state *plane_state)
5526{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005527 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5528 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5529 enum plane_id plane_id = plane->id;
5530 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005531 int ret;
5532
Ville Syrjälädbf71382020-11-06 19:30:38 +02005533 memset(wm, 0, sizeof(*wm));
5534
Ville Syrjälä83158472018-11-27 18:57:26 +02005535 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005536 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005537 return 0;
5538
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005539 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005540 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005541 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005542
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305543 drm_WARN_ON(&dev_priv->drm,
5544 !intel_wm_plane_visible(crtc_state, plane_state));
5545 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5546 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005547
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005548 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005549 y_plane_id, 0);
5550 if (ret)
5551 return ret;
5552
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005553 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005554 plane_id, 1);
5555 if (ret)
5556 return ret;
5557 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005558 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005559 plane_id, 0);
5560 if (ret)
5561 return ret;
5562 }
5563
5564 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005565}
5566
Ville Syrjäläffc90032020-11-06 19:30:37 +02005567static int skl_build_pipe_wm(struct intel_atomic_state *state,
5568 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005569{
Ville Syrjäläffc90032020-11-06 19:30:37 +02005570 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5571 struct intel_crtc_state *crtc_state =
5572 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005573 const struct intel_plane_state *plane_state;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005574 struct intel_plane *plane;
5575 int ret, i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005576
Ville Syrjälädbf71382020-11-06 19:30:38 +02005577 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5578 /*
5579 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5580 * instead but we don't populate that correctly for NV12 Y
5581 * planes so for now hack this.
5582 */
5583 if (plane->pipe != crtc->pipe)
5584 continue;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305585
Ville Syrjälä83158472018-11-27 18:57:26 +02005586 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005587 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005588 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005589 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305590 if (ret)
5591 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005592 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305593
Ville Syrjälädbf71382020-11-06 19:30:38 +02005594 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5595
Matt Roper55994c22016-05-12 07:06:08 -07005596 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005597}
5598
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005599static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5600 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005601 const struct skl_ddb_entry *entry)
5602{
5603 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005604 intel_de_write_fw(dev_priv, reg,
5605 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005606 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005607 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005608}
5609
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005610static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5611 i915_reg_t reg,
5612 const struct skl_wm_level *level)
5613{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005614 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005615
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005616 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005617 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005618 if (level->ignore_lines)
5619 val |= PLANE_WM_IGNORE_LINES;
5620 val |= level->plane_res_b;
5621 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005622
Jani Nikula9b6320a2020-01-23 16:00:04 +02005623 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005624}
5625
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005626void skl_write_plane_wm(struct intel_plane *plane,
5627 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005628{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005629 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005630 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005631 enum plane_id plane_id = plane->id;
5632 enum pipe pipe = plane->pipe;
5633 const struct skl_plane_wm *wm =
5634 &crtc_state->wm.skl.optimal.planes[plane_id];
5635 const struct skl_ddb_entry *ddb_y =
5636 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5637 const struct skl_ddb_entry *ddb_uv =
5638 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005639
5640 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005641 const struct skl_wm_level *wm_level;
5642
5643 wm_level = skl_plane_wm_level(crtc_state, plane_id, level);
5644
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005645 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005646 wm_level);
Lyude62e0fb82016-08-22 12:50:08 -04005647 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005648 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005649 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005650
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005651 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005652 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005653 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5654 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305655 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005656
5657 if (wm->is_planar)
5658 swap(ddb_y, ddb_uv);
5659
5660 skl_ddb_entry_write(dev_priv,
5661 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5662 skl_ddb_entry_write(dev_priv,
5663 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005664}
5665
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005666void skl_write_cursor_wm(struct intel_plane *plane,
5667 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005668{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005669 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005670 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005671 enum plane_id plane_id = plane->id;
5672 enum pipe pipe = plane->pipe;
5673 const struct skl_plane_wm *wm =
5674 &crtc_state->wm.skl.optimal.planes[plane_id];
5675 const struct skl_ddb_entry *ddb =
5676 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005677
5678 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005679 const struct skl_wm_level *wm_level;
5680
5681 wm_level = skl_plane_wm_level(crtc_state, plane_id, level);
5682
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005683 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005684 wm_level);
Lyude62e0fb82016-08-22 12:50:08 -04005685 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005686 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005687
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005688 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005689}
5690
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005691bool skl_wm_level_equals(const struct skl_wm_level *l1,
5692 const struct skl_wm_level *l2)
5693{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005694 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005695 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005696 l1->plane_res_l == l2->plane_res_l &&
5697 l1->plane_res_b == l2->plane_res_b;
5698}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005699
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005700static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5701 const struct skl_plane_wm *wm1,
5702 const struct skl_plane_wm *wm2)
5703{
5704 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005705
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005706 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005707 /*
5708 * We don't check uv_wm as the hardware doesn't actually
5709 * use it. It only gets used for calculating the required
5710 * ddb allocation.
5711 */
5712 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005713 return false;
5714 }
5715
5716 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005717}
5718
Jani Nikula81b55ef2020-04-20 17:04:38 +03005719static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5720 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005721{
Lyude27082492016-08-24 07:48:10 +02005722 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005723}
5724
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005725bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005726 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005727 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005728{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005729 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005730
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005731 for (i = 0; i < num_entries; i++) {
5732 if (i != ignore_idx &&
5733 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005734 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005735 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005736
Lyude27082492016-08-24 07:48:10 +02005737 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005738}
5739
Jani Nikulabb7791b2016-10-04 12:29:17 +03005740static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005741skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5742 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005743{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005744 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
5745 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005746 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5747 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005748
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005749 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5750 struct intel_plane_state *plane_state;
5751 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005752
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005753 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5754 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5755 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5756 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005757 continue;
5758
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005759 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005760 if (IS_ERR(plane_state))
5761 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005762
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005763 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005764 }
5765
5766 return 0;
5767}
5768
5769static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005770skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005771{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005772 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5773 const struct intel_dbuf_state *old_dbuf_state;
5774 const struct intel_dbuf_state *new_dbuf_state;
5775 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005776 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305777 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305778 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005779
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005780 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005781 new_crtc_state, i) {
Ville Syrjäläffc90032020-11-06 19:30:37 +02005782 ret = skl_allocate_pipe_ddb(state, crtc);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005783 if (ret)
5784 return ret;
5785
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005786 ret = skl_ddb_add_affected_planes(old_crtc_state,
5787 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005788 if (ret)
5789 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005790 }
5791
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005792 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
5793 new_dbuf_state = intel_atomic_get_new_dbuf_state(state);
5794
5795 if (new_dbuf_state &&
5796 new_dbuf_state->enabled_slices != old_dbuf_state->enabled_slices)
5797 drm_dbg_kms(&dev_priv->drm,
5798 "Enabled dbuf slices 0x%x -> 0x%x (out of %d dbuf slices)\n",
5799 old_dbuf_state->enabled_slices,
5800 new_dbuf_state->enabled_slices,
5801 INTEL_INFO(dev_priv)->num_supported_dbuf_slices);
5802
Matt Roper98d39492016-05-12 07:06:03 -07005803 return 0;
5804}
5805
Ville Syrjäläab98e942019-02-08 22:05:27 +02005806static char enast(bool enable)
5807{
5808 return enable ? '*' : ' ';
5809}
5810
Matt Roper2722efb2016-08-17 15:55:55 -04005811static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005812skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005813{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005814 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5815 const struct intel_crtc_state *old_crtc_state;
5816 const struct intel_crtc_state *new_crtc_state;
5817 struct intel_plane *plane;
5818 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005819 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005820
Jani Nikulabdbf43d2019-10-28 12:38:15 +02005821 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02005822 return;
5823
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005824 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5825 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005826 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5827
5828 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5829 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5830
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005831 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5832 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005833 const struct skl_ddb_entry *old, *new;
5834
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005835 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5836 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005837
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005838 if (skl_ddb_entry_equal(old, new))
5839 continue;
5840
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005841 drm_dbg_kms(&dev_priv->drm,
5842 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
5843 plane->base.base.id, plane->base.name,
5844 old->start, old->end, new->start, new->end,
5845 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005846 }
5847
5848 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5849 enum plane_id plane_id = plane->id;
5850 const struct skl_plane_wm *old_wm, *new_wm;
5851
5852 old_wm = &old_pipe_wm->planes[plane_id];
5853 new_wm = &new_pipe_wm->planes[plane_id];
5854
5855 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5856 continue;
5857
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005858 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005859 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm"
5860 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005861 plane->base.base.id, plane->base.name,
5862 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5863 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5864 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5865 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5866 enast(old_wm->trans_wm.plane_en),
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005867 enast(old_wm->sagv_wm0.plane_en),
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005868 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5869 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5870 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5871 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005872 enast(new_wm->trans_wm.plane_en),
5873 enast(new_wm->sagv_wm0.plane_en));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005874
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005875 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005876 "[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d"
5877 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005878 plane->base.base.id, plane->base.name,
5879 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5880 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5881 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5882 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5883 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5884 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5885 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5886 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5887 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005888 enast(old_wm->sagv_wm0.ignore_lines), old_wm->sagv_wm0.plane_res_l,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005889
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005890 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5891 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5892 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5893 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5894 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5895 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5896 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5897 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005898 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l,
5899 enast(new_wm->sagv_wm0.ignore_lines), new_wm->sagv_wm0.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005900
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005901 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005902 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5903 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005904 plane->base.base.id, plane->base.name,
5905 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5906 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5907 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5908 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5909 old_wm->trans_wm.plane_res_b,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005910 old_wm->sagv_wm0.plane_res_b,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005911 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5912 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5913 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5914 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005915 new_wm->trans_wm.plane_res_b,
5916 new_wm->sagv_wm0.plane_res_b);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005917
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005918 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005919 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5920 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005921 plane->base.base.id, plane->base.name,
5922 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5923 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5924 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5925 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5926 old_wm->trans_wm.min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005927 old_wm->sagv_wm0.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005928 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5929 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5930 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5931 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005932 new_wm->trans_wm.min_ddb_alloc,
5933 new_wm->sagv_wm0.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005934 }
5935 }
5936}
5937
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005938static int intel_add_affected_pipes(struct intel_atomic_state *state,
5939 u8 pipe_mask)
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005940{
5941 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5942 struct intel_crtc *crtc;
5943
5944 for_each_intel_crtc(&dev_priv->drm, crtc) {
5945 struct intel_crtc_state *crtc_state;
5946
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005947 if ((pipe_mask & BIT(crtc->pipe)) == 0)
5948 continue;
5949
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005950 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5951 if (IS_ERR(crtc_state))
5952 return PTR_ERR(crtc_state);
5953 }
5954
5955 return 0;
5956}
5957
Matt Roper98d39492016-05-12 07:06:03 -07005958static int
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005959skl_ddb_add_affected_pipes(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005960{
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005961 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005962 struct intel_crtc_state *crtc_state;
5963 struct intel_crtc *crtc;
5964 int i, ret;
Matt Roper98d39492016-05-12 07:06:03 -07005965
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305966 if (dev_priv->wm.distrust_bios_wm) {
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005967 /*
5968 * skl_ddb_get_pipe_allocation_limits() currently requires
5969 * all active pipes to be included in the state so that
5970 * it can redistribute the dbuf among them, and it really
5971 * wants to recompute things when distrust_bios_wm is set
5972 * so we add all the pipes to the state.
5973 */
5974 ret = intel_add_affected_pipes(state, ~0);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305975 if (ret)
5976 return ret;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305977 }
5978
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005979 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5980 struct intel_dbuf_state *new_dbuf_state;
5981 const struct intel_dbuf_state *old_dbuf_state;
5982
5983 new_dbuf_state = intel_atomic_get_dbuf_state(state);
5984 if (IS_ERR(new_dbuf_state))
Chris Wilsoncba597a2020-05-16 20:09:40 +01005985 return PTR_ERR(new_dbuf_state);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005986
5987 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
5988
5989 new_dbuf_state->active_pipes =
5990 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
5991
5992 if (old_dbuf_state->active_pipes == new_dbuf_state->active_pipes)
5993 break;
5994
5995 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005996 if (ret)
5997 return ret;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005998
5999 /*
6000 * skl_ddb_get_pipe_allocation_limits() currently requires
6001 * all active pipes to be included in the state so that
6002 * it can redistribute the dbuf among them.
6003 */
6004 ret = intel_add_affected_pipes(state,
6005 new_dbuf_state->active_pipes);
6006 if (ret)
6007 return ret;
6008
6009 break;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306010 }
6011
6012 return 0;
6013}
6014
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006015/*
6016 * To make sure the cursor watermark registers are always consistent
6017 * with our computed state the following scenario needs special
6018 * treatment:
6019 *
6020 * 1. enable cursor
6021 * 2. move cursor entirely offscreen
6022 * 3. disable cursor
6023 *
6024 * Step 2. does call .disable_plane() but does not zero the watermarks
6025 * (since we consider an offscreen cursor still active for the purposes
6026 * of watermarks). Step 3. would not normally call .disable_plane()
6027 * because the actual plane visibility isn't changing, and we don't
6028 * deallocate the cursor ddb until the pipe gets disabled. So we must
6029 * force step 3. to call .disable_plane() to update the watermark
6030 * registers properly.
6031 *
6032 * Other planes do not suffer from this issues as their watermarks are
6033 * calculated based on the actual plane visibility. The only time this
6034 * can trigger for the other planes is during the initial readout as the
6035 * default value of the watermarks registers is not zero.
6036 */
6037static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6038 struct intel_crtc *crtc)
6039{
6040 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6041 const struct intel_crtc_state *old_crtc_state =
6042 intel_atomic_get_old_crtc_state(state, crtc);
6043 struct intel_crtc_state *new_crtc_state =
6044 intel_atomic_get_new_crtc_state(state, crtc);
6045 struct intel_plane *plane;
6046
6047 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6048 struct intel_plane_state *plane_state;
6049 enum plane_id plane_id = plane->id;
6050
6051 /*
6052 * Force a full wm update for every plane on modeset.
6053 * Required because the reset value of the wm registers
6054 * is non-zero, whereas we want all disabled planes to
6055 * have zero watermarks. So if we turn off the relevant
6056 * power well the hardware state will go out of sync
6057 * with the software state.
6058 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006059 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006060 skl_plane_wm_equals(dev_priv,
6061 &old_crtc_state->wm.skl.optimal.planes[plane_id],
6062 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
6063 continue;
6064
6065 plane_state = intel_atomic_get_plane_state(state, plane);
6066 if (IS_ERR(plane_state))
6067 return PTR_ERR(plane_state);
6068
6069 new_crtc_state->update_planes |= BIT(plane_id);
6070 }
6071
6072 return 0;
6073}
6074
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306075static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006076skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306077{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006078 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006079 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306080 int ret, i;
6081
Ville Syrjäläd7a14582019-10-11 23:09:42 +03006082 ret = skl_ddb_add_affected_pipes(state);
6083 if (ret)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306084 return ret;
6085
Matt Roper734fa012016-05-12 15:11:40 -07006086 /*
6087 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08006088 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Ville Syrjäläf119a5e2020-01-20 19:47:13 +02006089 * weren't otherwise being modified if pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07006090 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006091 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6092 ret = skl_build_pipe_wm(state, crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006093 if (ret)
6094 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006095 }
6096
Matt Roperd8e87492018-12-11 09:31:07 -08006097 ret = skl_compute_ddb(state);
6098 if (ret)
6099 return ret;
6100
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006101 ret = intel_compute_sagv_mask(state);
6102 if (ret)
6103 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006104
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006105 /*
6106 * skl_compute_ddb() will have adjusted the final watermarks
6107 * based on how much ddb is available. Now we can actually
6108 * check if the final watermarks changed.
6109 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006110 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006111 ret = skl_wm_add_affected_planes(state, crtc);
6112 if (ret)
6113 return ret;
6114 }
6115
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006116 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006117
Matt Roper98d39492016-05-12 07:06:03 -07006118 return 0;
6119}
6120
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006121static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006122 struct intel_wm_config *config)
6123{
6124 struct intel_crtc *crtc;
6125
6126 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006127 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006128 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6129
6130 if (!wm->pipe_enabled)
6131 continue;
6132
6133 config->sprites_enabled |= wm->sprites_enabled;
6134 config->sprites_scaled |= wm->sprites_scaled;
6135 config->num_pipes_active++;
6136 }
6137}
6138
Matt Ropered4a6a72016-02-23 17:20:13 -08006139static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006140{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006141 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006142 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006143 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006144 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006145 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006146
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006147 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006148
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006149 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6150 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006151
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006152 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006153 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006154 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006155 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6156 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006157
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006158 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006159 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006160 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006161 }
6162
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006163 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006164 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006165
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006166 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006167
Imre Deak820c1982013-12-17 14:46:36 +02006168 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006169}
6170
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006171static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006172 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006173{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006174 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6175 const struct intel_crtc_state *crtc_state =
6176 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006177
Matt Ropered4a6a72016-02-23 17:20:13 -08006178 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006179 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006180 ilk_program_watermarks(dev_priv);
6181 mutex_unlock(&dev_priv->wm.wm_mutex);
6182}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006183
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006184static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006185 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006186{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006187 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6188 const struct intel_crtc_state *crtc_state =
6189 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006190
6191 if (!crtc_state->wm.need_postvbl_update)
6192 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006193
6194 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006195 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6196 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006197 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006198}
6199
Jani Nikula81b55ef2020-04-20 17:04:38 +03006200static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006201{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006202 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006203 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006204 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
6205 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
6206 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00006207}
6208
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006209void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006210 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006211{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006212 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6213 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006214 int level, max_level;
6215 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006216 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006217
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006218 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006219
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006220 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006221 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006222
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006223 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006224 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006225 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006226 else
Jani Nikula5f461662020-11-30 13:15:58 +02006227 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006228
6229 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6230 }
6231
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006232 if (INTEL_GEN(dev_priv) >= 12)
6233 wm->sagv_wm0 = wm->wm[0];
6234
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006235 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006236 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006237 else
Jani Nikula5f461662020-11-30 13:15:58 +02006238 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006239
6240 skl_wm_level_from_reg_val(val, &wm->trans_wm);
6241 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006242
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006243 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00006244 return;
Pradeep Bhat30789992014-11-04 17:06:45 +00006245}
6246
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006247void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006248{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006249 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006250 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00006251
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006252 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02006253 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006254
Maarten Lankhorstec193642019-06-28 10:55:17 +02006255 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
Ville Syrjälädbf71382020-11-06 19:30:38 +02006256 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006257 }
Matt Ropera1de91e2016-05-12 07:05:57 -07006258
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03006259 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07006260 /* Fully recompute DDB on first atomic commit */
6261 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07006262 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006263}
6264
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006265static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006266{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006267 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006268 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006269 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006270 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6271 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006272 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006273
Jani Nikula5f461662020-11-30 13:15:58 +02006274 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006275
Ville Syrjälä15606532016-05-13 17:55:17 +03006276 memset(active, 0, sizeof(*active));
6277
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006278 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006279
6280 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006281 u32 tmp = hw->wm_pipe[pipe];
6282
6283 /*
6284 * For active pipes LP0 watermark is marked as
6285 * enabled, and LP1+ watermaks as disabled since
6286 * we can't really reverse compute them in case
6287 * multiple pipes are active.
6288 */
6289 active->wm[0].enable = true;
6290 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6291 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6292 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006293 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006294 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006295
6296 /*
6297 * For inactive pipes, all watermark levels
6298 * should be marked as enabled but zeroed,
6299 * which is what we'd compute them to.
6300 */
6301 for (level = 0; level <= max_level; level++)
6302 active->wm[level].enable = true;
6303 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006304
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006305 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006306}
6307
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006308#define _FW_WM(value, plane) \
6309 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6310#define _FW_WM_VLV(value, plane) \
6311 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6312
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006313static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6314 struct g4x_wm_values *wm)
6315{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006316 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006317
Jani Nikula5f461662020-11-30 13:15:58 +02006318 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006319 wm->sr.plane = _FW_WM(tmp, SR);
6320 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6321 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6322 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6323
Jani Nikula5f461662020-11-30 13:15:58 +02006324 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006325 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6326 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6327 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6328 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6329 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6330 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6331
Jani Nikula5f461662020-11-30 13:15:58 +02006332 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006333 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6334 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6335 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6336 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6337}
6338
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006339static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6340 struct vlv_wm_values *wm)
6341{
6342 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006343 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006344
6345 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006346 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006347
Ville Syrjälä1b313892016-11-28 19:37:08 +02006348 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006349 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006350 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006351 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006352 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006353 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006354 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006355 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6356 }
6357
Jani Nikula5f461662020-11-30 13:15:58 +02006358 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006359 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006360 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6361 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6362 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006363
Jani Nikula5f461662020-11-30 13:15:58 +02006364 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006365 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6366 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6367 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006368
Jani Nikula5f461662020-11-30 13:15:58 +02006369 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006370 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6371
6372 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02006373 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006374 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6375 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006376
Jani Nikula5f461662020-11-30 13:15:58 +02006377 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006378 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6379 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006380
Jani Nikula5f461662020-11-30 13:15:58 +02006381 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006382 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6383 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006384
Jani Nikula5f461662020-11-30 13:15:58 +02006385 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006386 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006387 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6388 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6389 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6390 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6391 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6392 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6393 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6394 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6395 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006396 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02006397 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006398 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6399 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006400
Jani Nikula5f461662020-11-30 13:15:58 +02006401 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006402 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006403 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6404 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6405 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6406 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6407 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6408 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006409 }
6410}
6411
6412#undef _FW_WM
6413#undef _FW_WM_VLV
6414
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006415void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006416{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006417 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6418 struct intel_crtc *crtc;
6419
6420 g4x_read_wm_values(dev_priv, wm);
6421
Jani Nikula5f461662020-11-30 13:15:58 +02006422 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006423
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006424 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006425 struct intel_crtc_state *crtc_state =
6426 to_intel_crtc_state(crtc->base.state);
6427 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6428 struct g4x_pipe_wm *raw;
6429 enum pipe pipe = crtc->pipe;
6430 enum plane_id plane_id;
6431 int level, max_level;
6432
6433 active->cxsr = wm->cxsr;
6434 active->hpll_en = wm->hpll_en;
6435 active->fbc_en = wm->fbc_en;
6436
6437 active->sr = wm->sr;
6438 active->hpll = wm->hpll;
6439
6440 for_each_plane_id_on_crtc(crtc, plane_id) {
6441 active->wm.plane[plane_id] =
6442 wm->pipe[pipe].plane[plane_id];
6443 }
6444
6445 if (wm->cxsr && wm->hpll_en)
6446 max_level = G4X_WM_LEVEL_HPLL;
6447 else if (wm->cxsr)
6448 max_level = G4X_WM_LEVEL_SR;
6449 else
6450 max_level = G4X_WM_LEVEL_NORMAL;
6451
6452 level = G4X_WM_LEVEL_NORMAL;
6453 raw = &crtc_state->wm.g4x.raw[level];
6454 for_each_plane_id_on_crtc(crtc, plane_id)
6455 raw->plane[plane_id] = active->wm.plane[plane_id];
6456
6457 if (++level > max_level)
6458 goto out;
6459
6460 raw = &crtc_state->wm.g4x.raw[level];
6461 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6462 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6463 raw->plane[PLANE_SPRITE0] = 0;
6464 raw->fbc = active->sr.fbc;
6465
6466 if (++level > max_level)
6467 goto out;
6468
6469 raw = &crtc_state->wm.g4x.raw[level];
6470 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6471 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6472 raw->plane[PLANE_SPRITE0] = 0;
6473 raw->fbc = active->hpll.fbc;
6474
6475 out:
6476 for_each_plane_id_on_crtc(crtc, plane_id)
6477 g4x_raw_plane_wm_set(crtc_state, level,
6478 plane_id, USHRT_MAX);
6479 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6480
6481 crtc_state->wm.g4x.optimal = *active;
6482 crtc_state->wm.g4x.intermediate = *active;
6483
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006484 drm_dbg_kms(&dev_priv->drm,
6485 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6486 pipe_name(pipe),
6487 wm->pipe[pipe].plane[PLANE_PRIMARY],
6488 wm->pipe[pipe].plane[PLANE_CURSOR],
6489 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006490 }
6491
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006492 drm_dbg_kms(&dev_priv->drm,
6493 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6494 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6495 drm_dbg_kms(&dev_priv->drm,
6496 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6497 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6498 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6499 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006500}
6501
6502void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6503{
6504 struct intel_plane *plane;
6505 struct intel_crtc *crtc;
6506
6507 mutex_lock(&dev_priv->wm.wm_mutex);
6508
6509 for_each_intel_plane(&dev_priv->drm, plane) {
6510 struct intel_crtc *crtc =
6511 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6512 struct intel_crtc_state *crtc_state =
6513 to_intel_crtc_state(crtc->base.state);
6514 struct intel_plane_state *plane_state =
6515 to_intel_plane_state(plane->base.state);
6516 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6517 enum plane_id plane_id = plane->id;
6518 int level;
6519
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006520 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006521 continue;
6522
6523 for (level = 0; level < 3; level++) {
6524 struct g4x_pipe_wm *raw =
6525 &crtc_state->wm.g4x.raw[level];
6526
6527 raw->plane[plane_id] = 0;
6528 wm_state->wm.plane[plane_id] = 0;
6529 }
6530
6531 if (plane_id == PLANE_PRIMARY) {
6532 for (level = 0; level < 3; level++) {
6533 struct g4x_pipe_wm *raw =
6534 &crtc_state->wm.g4x.raw[level];
6535 raw->fbc = 0;
6536 }
6537
6538 wm_state->sr.fbc = 0;
6539 wm_state->hpll.fbc = 0;
6540 wm_state->fbc_en = false;
6541 }
6542 }
6543
6544 for_each_intel_crtc(&dev_priv->drm, crtc) {
6545 struct intel_crtc_state *crtc_state =
6546 to_intel_crtc_state(crtc->base.state);
6547
6548 crtc_state->wm.g4x.intermediate =
6549 crtc_state->wm.g4x.optimal;
6550 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6551 }
6552
6553 g4x_program_watermarks(dev_priv);
6554
6555 mutex_unlock(&dev_priv->wm.wm_mutex);
6556}
6557
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006558void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006559{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006560 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006561 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006562 u32 val;
6563
6564 vlv_read_wm_values(dev_priv, wm);
6565
Jani Nikula5f461662020-11-30 13:15:58 +02006566 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006567 wm->level = VLV_WM_LEVEL_PM2;
6568
6569 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006570 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006571
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006572 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006573 if (val & DSP_MAXFIFO_PM5_ENABLE)
6574 wm->level = VLV_WM_LEVEL_PM5;
6575
Ville Syrjälä58590c12015-09-08 21:05:12 +03006576 /*
6577 * If DDR DVFS is disabled in the BIOS, Punit
6578 * will never ack the request. So if that happens
6579 * assume we don't have to enable/disable DDR DVFS
6580 * dynamically. To test that just set the REQ_ACK
6581 * bit to poke the Punit, but don't change the
6582 * HIGH/LOW bits so that we don't actually change
6583 * the current state.
6584 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006585 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006586 val |= FORCE_DDR_FREQ_REQ_ACK;
6587 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6588
6589 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6590 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006591 drm_dbg_kms(&dev_priv->drm,
6592 "Punit not acking DDR DVFS request, "
6593 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006594 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6595 } else {
6596 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6597 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6598 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6599 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006600
Chris Wilson337fa6e2019-04-26 09:17:20 +01006601 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006602 }
6603
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006604 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006605 struct intel_crtc_state *crtc_state =
6606 to_intel_crtc_state(crtc->base.state);
6607 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6608 const struct vlv_fifo_state *fifo_state =
6609 &crtc_state->wm.vlv.fifo_state;
6610 enum pipe pipe = crtc->pipe;
6611 enum plane_id plane_id;
6612 int level;
6613
6614 vlv_get_fifo_size(crtc_state);
6615
6616 active->num_levels = wm->level + 1;
6617 active->cxsr = wm->cxsr;
6618
Ville Syrjäläff32c542017-03-02 19:14:57 +02006619 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006620 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006621 &crtc_state->wm.vlv.raw[level];
6622
6623 active->sr[level].plane = wm->sr.plane;
6624 active->sr[level].cursor = wm->sr.cursor;
6625
6626 for_each_plane_id_on_crtc(crtc, plane_id) {
6627 active->wm[level].plane[plane_id] =
6628 wm->pipe[pipe].plane[plane_id];
6629
6630 raw->plane[plane_id] =
6631 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6632 fifo_state->plane[plane_id]);
6633 }
6634 }
6635
6636 for_each_plane_id_on_crtc(crtc, plane_id)
6637 vlv_raw_plane_wm_set(crtc_state, level,
6638 plane_id, USHRT_MAX);
6639 vlv_invalidate_wms(crtc, active, level);
6640
6641 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006642 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006643
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006644 drm_dbg_kms(&dev_priv->drm,
6645 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
6646 pipe_name(pipe),
6647 wm->pipe[pipe].plane[PLANE_PRIMARY],
6648 wm->pipe[pipe].plane[PLANE_CURSOR],
6649 wm->pipe[pipe].plane[PLANE_SPRITE0],
6650 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006651 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006652
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006653 drm_dbg_kms(&dev_priv->drm,
6654 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6655 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006656}
6657
Ville Syrjälä602ae832017-03-02 19:15:02 +02006658void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6659{
6660 struct intel_plane *plane;
6661 struct intel_crtc *crtc;
6662
6663 mutex_lock(&dev_priv->wm.wm_mutex);
6664
6665 for_each_intel_plane(&dev_priv->drm, plane) {
6666 struct intel_crtc *crtc =
6667 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6668 struct intel_crtc_state *crtc_state =
6669 to_intel_crtc_state(crtc->base.state);
6670 struct intel_plane_state *plane_state =
6671 to_intel_plane_state(plane->base.state);
6672 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6673 const struct vlv_fifo_state *fifo_state =
6674 &crtc_state->wm.vlv.fifo_state;
6675 enum plane_id plane_id = plane->id;
6676 int level;
6677
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006678 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02006679 continue;
6680
6681 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006682 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006683 &crtc_state->wm.vlv.raw[level];
6684
6685 raw->plane[plane_id] = 0;
6686
6687 wm_state->wm[level].plane[plane_id] =
6688 vlv_invert_wm_value(raw->plane[plane_id],
6689 fifo_state->plane[plane_id]);
6690 }
6691 }
6692
6693 for_each_intel_crtc(&dev_priv->drm, crtc) {
6694 struct intel_crtc_state *crtc_state =
6695 to_intel_crtc_state(crtc->base.state);
6696
6697 crtc_state->wm.vlv.intermediate =
6698 crtc_state->wm.vlv.optimal;
6699 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6700 }
6701
6702 vlv_program_watermarks(dev_priv);
6703
6704 mutex_unlock(&dev_priv->wm.wm_mutex);
6705}
6706
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006707/*
6708 * FIXME should probably kill this and improve
6709 * the real watermark readout/sanitation instead
6710 */
6711static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6712{
Jani Nikula5f461662020-11-30 13:15:58 +02006713 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM1_LP_SR_EN);
6714 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM1_LP_SR_EN);
6715 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 +02006716
6717 /*
6718 * Don't touch WM1S_LP_EN here.
6719 * Doing so could cause underruns.
6720 */
6721}
6722
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006723void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006724{
Imre Deak820c1982013-12-17 14:46:36 +02006725 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006726 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006727
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006728 ilk_init_lp_watermarks(dev_priv);
6729
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006730 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006731 ilk_pipe_wm_get_hw_state(crtc);
6732
Jani Nikula5f461662020-11-30 13:15:58 +02006733 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
6734 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
6735 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006736
Jani Nikula5f461662020-11-30 13:15:58 +02006737 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006738 if (INTEL_GEN(dev_priv) >= 7) {
Jani Nikula5f461662020-11-30 13:15:58 +02006739 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
6740 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006741 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006742
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006743 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02006744 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 +02006745 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006746 else if (IS_IVYBRIDGE(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02006747 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 +02006748 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006749
6750 hw->enable_fbc_wm =
Jani Nikula5f461662020-11-30 13:15:58 +02006751 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006752}
6753
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006754/**
6755 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006756 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006757 *
6758 * Calculate watermark values for the various WM regs based on current mode
6759 * and plane configuration.
6760 *
6761 * There are several cases to deal with here:
6762 * - normal (i.e. non-self-refresh)
6763 * - self-refresh (SR) mode
6764 * - lines are large relative to FIFO size (buffer can hold up to 2)
6765 * - lines are small relative to FIFO size (buffer can hold more than 2
6766 * lines), so need to account for TLB latency
6767 *
6768 * The normal calculation is:
6769 * watermark = dotclock * bytes per pixel * latency
6770 * where latency is platform & configuration dependent (we assume pessimal
6771 * values here).
6772 *
6773 * The SR calculation is:
6774 * watermark = (trunc(latency/line time)+1) * surface width *
6775 * bytes per pixel
6776 * where
6777 * line time = htotal / dotclock
6778 * surface width = hdisplay for normal plane and 64 for cursor
6779 * and latency is assumed to be high, as above.
6780 *
6781 * The final value programmed to the register should always be rounded up,
6782 * and include an extra 2 entries to account for clock crossings.
6783 *
6784 * We don't use the sprite, so we can ignore that. And on Crestline we have
6785 * to set the non-SR watermarks to 8.
6786 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006787void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006788{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006789 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006790
6791 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006792 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006793}
6794
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306795void intel_enable_ipc(struct drm_i915_private *dev_priv)
6796{
6797 u32 val;
6798
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006799 if (!HAS_IPC(dev_priv))
6800 return;
6801
Jani Nikula5f461662020-11-30 13:15:58 +02006802 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306803
6804 if (dev_priv->ipc_enabled)
6805 val |= DISP_IPC_ENABLE;
6806 else
6807 val &= ~DISP_IPC_ENABLE;
6808
Jani Nikula5f461662020-11-30 13:15:58 +02006809 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306810}
6811
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006812static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6813{
6814 /* Display WA #0477 WaDisableIPC: skl */
6815 if (IS_SKYLAKE(dev_priv))
6816 return false;
6817
6818 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01006819 if (IS_KABYLAKE(dev_priv) ||
6820 IS_COFFEELAKE(dev_priv) ||
6821 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006822 return dev_priv->dram_info.symmetric_memory;
6823
6824 return true;
6825}
6826
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306827void intel_init_ipc(struct drm_i915_private *dev_priv)
6828{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306829 if (!HAS_IPC(dev_priv))
6830 return;
6831
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006832 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006833
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306834 intel_enable_ipc(dev_priv);
6835}
6836
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006837static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006838{
Daniel Vetter3107bd42012-10-31 22:52:31 +01006839 /*
6840 * On Ibex Peak and Cougar Point, we need to disable clock
6841 * gating for the panel power sequencer or it will fail to
6842 * start up when no ports are active.
6843 */
Jani Nikula5f461662020-11-30 13:15:58 +02006844 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006845}
6846
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006847static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006848{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006849 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006850
Damien Lespiau055e3932014-08-18 13:49:10 +01006851 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006852 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
6853 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006854 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006855
Jani Nikula5f461662020-11-30 13:15:58 +02006856 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
6857 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006858 }
6859}
6860
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006861static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006862{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006863 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006864
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006865 /*
6866 * Required for FBC
6867 * WaFbcDisableDpfcClockGating:ilk
6868 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006869 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6870 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6871 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006872
Jani Nikula5f461662020-11-30 13:15:58 +02006873 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006874 MARIUNIT_CLOCK_GATE_DISABLE |
6875 SVSMUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02006876 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006877 VFMUNIT_CLOCK_GATE_DISABLE);
6878
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006879 /*
6880 * According to the spec the following bits should be set in
6881 * order to enable memory self-refresh
6882 * The bit 22/21 of 0x42004
6883 * The bit 5 of 0x42020
6884 * The bit 15 of 0x45000
6885 */
Jani Nikula5f461662020-11-30 13:15:58 +02006886 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
6887 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006888 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006889 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02006890 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
6891 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006892 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006893
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006894 /*
6895 * Based on the document from hardware guys the following bits
6896 * should be set unconditionally in order to enable FBC.
6897 * The bit 22 of 0x42000
6898 * The bit 22 of 0x42004
6899 * The bit 7,8,9 of 0x42020.
6900 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006901 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006902 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Jani Nikula5f461662020-11-30 13:15:58 +02006903 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
6904 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006905 ILK_FBCQ_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02006906 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
6907 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006908 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006909 }
6910
Jani Nikula5f461662020-11-30 13:15:58 +02006911 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006912
Jani Nikula5f461662020-11-30 13:15:58 +02006913 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
6914 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006915 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05306916
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006917 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006918
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006919 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006920}
6921
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006922static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006923{
Ville Syrjäläd048a262019-08-21 20:30:31 +03006924 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006925 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006926
6927 /*
6928 * On Ibex Peak and Cougar Point, we need to disable clock
6929 * gating for the panel power sequencer or it will fail to
6930 * start up when no ports are active.
6931 */
Jani Nikula5f461662020-11-30 13:15:58 +02006932 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnescd664072013-10-02 10:34:19 -07006933 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6934 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02006935 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
Daniel Vetter3107bd42012-10-31 22:52:31 +01006936 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006937 /* The below fixes the weird display corruption, a few pixels shifted
6938 * downward, on (only) LVDS of some HP laptops with IVY.
6939 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006940 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006941 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006942 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6943 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006944 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006945 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006946 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6947 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Jani Nikula5f461662020-11-30 13:15:58 +02006948 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006949 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006950 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006951 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006952 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
Daniel Vetter3107bd42012-10-31 22:52:31 +01006953 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6954 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006955}
6956
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006957static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006958{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006959 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006960
Jani Nikula5f461662020-11-30 13:15:58 +02006961 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006962 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006963 drm_dbg_kms(&dev_priv->drm,
6964 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6965 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006966}
6967
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006968static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006969{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006970 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006971
Jani Nikula5f461662020-11-30 13:15:58 +02006972 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006973
Jani Nikula5f461662020-11-30 13:15:58 +02006974 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
6975 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006976 ILK_ELPIN_409_SELECT);
6977
Jani Nikula5f461662020-11-30 13:15:58 +02006978 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
6979 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006980 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6981 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6982
6983 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6984 * gating disable must be set. Failure to set it results in
6985 * flickering pixels due to Z write ordering failures after
6986 * some amount of runtime in the Mesa "fire" demo, and Unigine
6987 * Sanctuary and Tropics, and apparently anything else with
6988 * alpha test or pixel discard.
6989 *
6990 * According to the spec, bit 11 (RCCUNIT) must also be set,
6991 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006992 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006993 * WaDisableRCCUnitClockGating:snb
6994 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006995 */
Jani Nikula5f461662020-11-30 13:15:58 +02006996 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006997 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6998 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6999
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007000 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007001 * According to the spec the following bits should be
7002 * set in order to enable memory self-refresh and fbc:
7003 * The bit21 and bit22 of 0x42000
7004 * The bit21 and bit22 of 0x42004
7005 * The bit5 and bit7 of 0x42020
7006 * The bit14 of 0x70180
7007 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007008 *
7009 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007010 */
Jani Nikula5f461662020-11-30 13:15:58 +02007011 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7012 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007013 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007014 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7015 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007016 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Jani Nikula5f461662020-11-30 13:15:58 +02007017 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7018 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
Damien Lespiau231e54f2012-10-19 17:55:41 +01007019 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7020 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007021
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007022 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007023
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007024 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007025
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007026 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007027}
7028
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007029static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007030{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007031 /*
7032 * TODO: this bit should only be enabled when really needed, then
7033 * disabled when not needed anymore in order to save power.
7034 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007035 if (HAS_PCH_LPT_LP(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007036 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7037 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007038 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007039
7040 /* WADPOClockGatingDisable:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007041 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7042 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007043 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007044}
7045
Ville Syrjälä712bf362016-10-31 22:37:23 +02007046static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007047{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007048 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02007049 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007050
7051 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007052 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
Imre Deak7d708ee2013-04-17 14:04:50 +03007053 }
7054}
7055
Imre Deak450174f2016-05-03 15:54:21 +03007056static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7057 int general_prio_credits,
7058 int high_prio_credits)
7059{
7060 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007061 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007062
7063 /* WaTempDisableDOPClkGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007064 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7065 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
Imre Deak450174f2016-05-03 15:54:21 +03007066
Jani Nikula5f461662020-11-30 13:15:58 +02007067 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Oscar Mateo930a7842017-10-17 13:25:45 -07007068 val &= ~L3_PRIO_CREDITS_MASK;
7069 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7070 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
Jani Nikula5f461662020-11-30 13:15:58 +02007071 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007072
7073 /*
7074 * Wait at least 100 clocks before re-enabling clock gating.
7075 * See the definition of L3SQCREG1 in BSpec.
7076 */
Jani Nikula5f461662020-11-30 13:15:58 +02007077 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Imre Deak450174f2016-05-03 15:54:21 +03007078 udelay(1);
Jani Nikula5f461662020-11-30 13:15:58 +02007079 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
Imre Deak450174f2016-05-03 15:54:21 +03007080}
7081
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007082static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7083{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007084 /* Wa_1409120013:icl,ehl */
Jani Nikula5f461662020-11-30 13:15:58 +02007085 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä885f1822020-07-08 16:12:20 +03007086 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7087
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007088 /* This is not an Wa. Enable to reduce Sampler power */
Jani Nikula5f461662020-11-30 13:15:58 +02007089 intel_uncore_write(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7090 intel_uncore_read(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07007091
Matt Atwood6f4194c2020-01-13 23:11:28 -05007092 /*Wa_14010594013:icl, ehl */
7093 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7094 0, CNL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007095}
7096
José Roberto de Souza35f08372021-01-13 05:37:59 -08007097static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
Michel Thierry5d869232019-08-23 01:20:34 -07007098{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007099 /* Wa_1409120013:tgl,rkl,adl_s,dg1 */
Jani Nikula5f461662020-11-30 13:15:58 +02007100 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
José Roberto de Souza35f08372021-01-13 05:37:59 -08007101 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007102
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007103 /* Wa_1409825376:tgl (pre-prod)*/
José Roberto de Souzac33298c2020-08-27 16:39:43 -07007104 if (IS_TGL_DISP_REVID(dev_priv, TGL_REVID_A0, TGL_REVID_B1))
Jani Nikula5f461662020-11-30 13:15:58 +02007105 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 -08007106 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007107
José Roberto de Souza35f08372021-01-13 05:37:59 -08007108 /* Wa_14011059788:tgl,rkl,adl_s,dg1 */
Matt Atwoodf9d77422020-04-15 15:35:35 -04007109 intel_uncore_rmw(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7110 0, DFR_DISABLE);
Michel Thierry5d869232019-08-23 01:20:34 -07007111}
7112
Stuart Summersda9427502020-10-14 12:19:34 -07007113static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7114{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007115 gen12lp_init_clock_gating(dev_priv);
7116
Stuart Summersda9427502020-10-14 12:19:34 -07007117 /* Wa_1409836686:dg1[a0] */
7118 if (IS_DG1_REVID(dev_priv, DG1_REVID_A0, DG1_REVID_A0))
Jani Nikula5f461662020-11-30 13:15:58 +02007119 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 -07007120 DPT_GATING_DIS);
7121}
7122
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007123static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7124{
7125 if (!HAS_PCH_CNP(dev_priv))
7126 return;
7127
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007128 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Jani Nikula5f461662020-11-30 13:15:58 +02007129 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 -07007130 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007131}
7132
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007133static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007134{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07007135 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007136 cnp_init_clock_gating(dev_priv);
7137
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07007138 /* This is not an Wa. Enable for better image quality */
Jani Nikula5f461662020-11-30 13:15:58 +02007139 intel_uncore_write(&dev_priv->uncore, _3D_CHICKEN3,
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07007140 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
7141
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007142 /* WaEnableChickenDCPR:cnl */
Jani Nikula5f461662020-11-30 13:15:58 +02007143 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7144 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007145
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007146 /*
7147 * WaFbcWakeMemOn:cnl
7148 * Display WA #0859: cnl
7149 */
Jani Nikula5f461662020-11-30 13:15:58 +02007150 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007151 DISP_FBC_MEMORY_WAKE);
7152
Jani Nikula5f461662020-11-30 13:15:58 +02007153 val = intel_uncore_read(&dev_priv->uncore, SLICE_UNIT_LEVEL_CLKGATE);
Chris Wilson34991bd2017-11-11 10:03:36 +00007154 /* ReadHitWriteOnlyDisable:cnl */
7155 val |= RCCUNIT_CLKGATE_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02007156 intel_uncore_write(&dev_priv->uncore, SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007157
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007158 /* Wa_2201832410:cnl */
Jani Nikula5f461662020-11-30 13:15:58 +02007159 val = intel_uncore_read(&dev_priv->uncore, SUBSLICE_UNIT_LEVEL_CLKGATE);
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007160 val |= GWUNIT_CLKGATE_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02007161 intel_uncore_write(&dev_priv->uncore, SUBSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007162
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007163 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08007164 /* WaVFUnitClockGatingDisable:cnl */
Jani Nikula5f461662020-11-30 13:15:58 +02007165 val = intel_uncore_read(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007166 val |= VFUNIT_CLKGATE_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02007167 intel_uncore_write(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007168}
7169
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007170static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7171{
7172 cnp_init_clock_gating(dev_priv);
7173 gen9_init_clock_gating(dev_priv);
7174
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007175 /* WAC6entrylatency:cfl */
Jani Nikula5f461662020-11-30 13:15:58 +02007176 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 +03007177 FBC_LLC_FULLY_OPEN);
7178
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007179 /*
7180 * WaFbcTurnOffFbcWatermark:cfl
7181 * Display WA #0562: cfl
7182 */
Jani Nikula5f461662020-11-30 13:15:58 +02007183 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 +03007184 DISP_FBC_WM_DIS);
7185
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007186 /*
7187 * WaFbcNukeOnHostModify:cfl
7188 * Display WA #0873: cfl
7189 */
Jani Nikula5f461662020-11-30 13:15:58 +02007190 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 -07007191 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7192}
7193
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007194static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007195{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007196 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007197
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007198 /* WAC6entrylatency:kbl */
Jani Nikula5f461662020-11-30 13:15:58 +02007199 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 +03007200 FBC_LLC_FULLY_OPEN);
7201
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007202 /* WaDisableSDEUnitClockGating:kbl */
Matt Roper96c5a152020-08-10 20:21:05 -07007203 if (IS_KBL_GT_REVID(dev_priv, 0, KBL_REVID_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007204 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007205 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007206
7207 /* WaDisableGamClockGating:kbl */
Matt Roper96c5a152020-08-10 20:21:05 -07007208 if (IS_KBL_GT_REVID(dev_priv, 0, KBL_REVID_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007209 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007210 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007211
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007212 /*
7213 * WaFbcTurnOffFbcWatermark:kbl
7214 * Display WA #0562: kbl
7215 */
Jani Nikula5f461662020-11-30 13:15:58 +02007216 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 +03007217 DISP_FBC_WM_DIS);
7218
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007219 /*
7220 * WaFbcNukeOnHostModify:kbl
7221 * Display WA #0873: kbl
7222 */
Jani Nikula5f461662020-11-30 13:15:58 +02007223 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 +03007224 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007225}
7226
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007227static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007228{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007229 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007230
Ville Syrjäläf1421192020-07-16 22:04:25 +03007231 /* WaDisableDopClockGating:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007232 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 +03007233 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7234
Mika Kuoppala44fff992016-06-07 17:19:09 +03007235 /* WAC6entrylatency:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007236 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 +03007237 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007238
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007239 /*
7240 * WaFbcTurnOffFbcWatermark:skl
7241 * Display WA #0562: skl
7242 */
Jani Nikula5f461662020-11-30 13:15:58 +02007243 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 +03007244 DISP_FBC_WM_DIS);
7245
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007246 /*
7247 * WaFbcNukeOnHostModify:skl
7248 * Display WA #0873: skl
7249 */
Jani Nikula5f461662020-11-30 13:15:58 +02007250 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 +03007251 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007252
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007253 /*
7254 * WaFbcHighMemBwCorruptionAvoidance:skl
7255 * Display WA #0883: skl
7256 */
Jani Nikula5f461662020-11-30 13:15:58 +02007257 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 +03007258 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007259}
7260
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007261static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007262{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007263 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007264
Ville Syrjälä885f1822020-07-08 16:12:20 +03007265 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007266 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7267 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007268 HSW_FBCQ_DIS);
7269
Ben Widawskyab57fff2013-12-12 15:28:04 -08007270 /* WaSwitchSolVfFArbitrationPriority:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007271 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 -07007272
Ben Widawskyab57fff2013-12-12 15:28:04 -08007273 /* WaPsrDPAMaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007274 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7275 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007276
Ben Widawskyab57fff2013-12-12 15:28:04 -08007277 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007278 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007279 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7280 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007281 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007282 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007283
Ben Widawskyab57fff2013-12-12 15:28:04 -08007284 /* WaVSRefCountFullforceMissDisable:bdw */
7285 /* WaDSRefCountFullforceMissDisable:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007286 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7287 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ben Widawskyab57fff2013-12-12 15:28:04 -08007288 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007289
Jani Nikula5f461662020-11-30 13:15:58 +02007290 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007291 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007292
7293 /* WaDisableSDEUnitClockGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007294 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 +02007295 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007296
Imre Deak450174f2016-05-03 15:54:21 +03007297 /* WaProgramL3SqcReg1Default:bdw */
7298 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007299
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007300 /* WaKVMNotificationOnConfigChange:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007301 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 +03007302 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7303
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007304 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007305
7306 /* WaDisableDopClockGating:bdw
7307 *
7308 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7309 * clock gating.
7310 */
Jani Nikula5f461662020-11-30 13:15:58 +02007311 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7312 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007313}
7314
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007315static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007316{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007317 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007318 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7319 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007320 HSW_FBCQ_DIS);
7321
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007322 /* This is required by WaCatErrorRejectionIssue:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007323 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7324 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007325 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007326
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007327 /* WaSwitchSolVfFArbitrationPriority:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007328 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 -07007329
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007330 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007331}
7332
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007333static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007334{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007335 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007336
Jani Nikula5f461662020-11-30 13:15:58 +02007337 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007338
Ville Syrjälä885f1822020-07-08 16:12:20 +03007339 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007340 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7341 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007342 ILK_FBCQ_DIS);
7343
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007344 /* WaDisableBackToBackFlipFix:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007345 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007346 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7347 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7348
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007349 if (IS_IVB_GT1(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007350 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007351 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007352 else {
7353 /* must write both registers */
Jani Nikula5f461662020-11-30 13:15:58 +02007354 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Ville Syrjälä412236c2014-01-22 21:32:44 +02007355 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jani Nikula5f461662020-11-30 13:15:58 +02007356 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007357 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007358 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007359
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007360 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007361 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007362 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007363 */
Jani Nikula5f461662020-11-30 13:15:58 +02007364 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007365 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007366
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007367 /* This is required by WaCatErrorRejectionIssue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007368 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7369 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007370 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7371
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007372 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007373
Jani Nikula5f461662020-11-30 13:15:58 +02007374 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
Ben Widawsky20848222012-05-04 18:58:59 -07007375 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7376 snpcr |= GEN6_MBC_SNPCR_MED;
Jani Nikula5f461662020-11-30 13:15:58 +02007377 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007378
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007379 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007380 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007381
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007382 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007383}
7384
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007385static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007386{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007387 /* WaDisableBackToBackFlipFix:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007388 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007389 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7390 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7391
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007392 /* WaDisableDopClockGating:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007393 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007394 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7395
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007396 /* This is required by WaCatErrorRejectionIssue:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007397 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7398 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007399 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7400
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007401 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007402 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007403 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007404 */
Jani Nikula5f461662020-11-30 13:15:58 +02007405 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007406 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007407
Akash Goelc98f5062014-03-24 23:00:07 +05307408 /* WaDisableL3Bank2xClockGate:vlv
7409 * Disabling L3 clock gating- MMIO 940c[25] = 1
7410 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
Jani Nikula5f461662020-11-30 13:15:58 +02007411 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7412 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007413
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007414 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007415 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007416 * Disable clock gating on th GCFG unit to prevent a delay
7417 * in the reporting of vblank events.
7418 */
Jani Nikula5f461662020-11-30 13:15:58 +02007419 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007420}
7421
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007422static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007423{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007424 /* WaVSRefCountFullforceMissDisable:chv */
7425 /* WaDSRefCountFullforceMissDisable:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007426 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7427 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ville Syrjälä232ce332014-04-09 13:28:35 +03007428 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007429
7430 /* WaDisableSemaphoreAndSyncFlipWait:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007431 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007432 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007433
7434 /* WaDisableCSUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007435 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 +03007436 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007437
7438 /* WaDisableSDEUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007439 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 +03007440 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007441
7442 /*
Imre Deak450174f2016-05-03 15:54:21 +03007443 * WaProgramL3SqcReg1Default:chv
7444 * See gfxspecs/Related Documents/Performance Guide/
7445 * LSQC Setting Recommendations.
7446 */
7447 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007448}
7449
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007450static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007451{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007452 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007453
Jani Nikula5f461662020-11-30 13:15:58 +02007454 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7455 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007456 GS_UNIT_CLOCK_GATE_DISABLE |
7457 CL_UNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007458 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007459 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7460 OVRUNIT_CLOCK_GATE_DISABLE |
7461 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007462 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007463 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007464 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007465
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007466 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007467}
7468
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007469static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007470{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007471 struct intel_uncore *uncore = &dev_priv->uncore;
7472
7473 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7474 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7475 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7476 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7477 intel_uncore_write16(uncore, DEUC, 0);
7478 intel_uncore_write(uncore,
7479 MI_ARB_STATE,
7480 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007481}
7482
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007483static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007484{
Jani Nikula5f461662020-11-30 13:15:58 +02007485 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007486 I965_RCC_CLOCK_GATE_DISABLE |
7487 I965_RCPB_CLOCK_GATE_DISABLE |
7488 I965_ISC_CLOCK_GATE_DISABLE |
7489 I965_FBC_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007490 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7491 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä20f94962013-06-07 10:47:02 +03007492 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007493}
7494
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007495static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007496{
Jani Nikula5f461662020-11-30 13:15:58 +02007497 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007498
7499 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7500 DSTATE_DOT_CLOCK_GATING;
Jani Nikula5f461662020-11-30 13:15:58 +02007501 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007502
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007503 if (IS_PINEVIEW(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007504 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007505
7506 /* IIR "flip pending" means done if this bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007507 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007508
7509 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007510 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007511
7512 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007513 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 +03007514
Jani Nikula5f461662020-11-30 13:15:58 +02007515 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007516 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007517}
7518
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007519static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007520{
Jani Nikula5f461662020-11-30 13:15:58 +02007521 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007522
7523 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007524 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007525 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007526
Jani Nikula5f461662020-11-30 13:15:58 +02007527 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007528 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007529
7530 /*
7531 * Have FBC ignore 3D activity since we use software
7532 * render tracking, and otherwise a pure 3D workload
7533 * (even if it just renders a single frame and then does
7534 * abosultely nothing) would not allow FBC to recompress
7535 * until a 2D blit occurs.
7536 */
Jani Nikula5f461662020-11-30 13:15:58 +02007537 intel_uncore_write(&dev_priv->uncore, SCPD0,
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007538 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007539}
7540
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007541static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007542{
Jani Nikula5f461662020-11-30 13:15:58 +02007543 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007544 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7545 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007546}
7547
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007548void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007549{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007550 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007551}
7552
Ville Syrjälä712bf362016-10-31 22:37:23 +02007553void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007554{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007555 if (HAS_PCH_LPT(dev_priv))
7556 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007557}
7558
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007559static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007560{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007561 drm_dbg_kms(&dev_priv->drm,
7562 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007563}
7564
7565/**
7566 * intel_init_clock_gating_hooks - setup the clock gating hooks
7567 * @dev_priv: device private
7568 *
7569 * Setup the hooks that configure which clocks of a given platform can be
7570 * gated and also apply various GT and display specific workarounds for these
7571 * platforms. Note that some GT specific workarounds are applied separately
7572 * when GPU contexts or batchbuffers start their execution.
7573 */
7574void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7575{
Stuart Summersda9427502020-10-14 12:19:34 -07007576 if (IS_DG1(dev_priv))
7577 dev_priv->display.init_clock_gating = dg1_init_clock_gating;
7578 else if (IS_GEN(dev_priv, 12))
José Roberto de Souza35f08372021-01-13 05:37:59 -08007579 dev_priv->display.init_clock_gating = gen12lp_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07007580 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007581 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07007582 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007583 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007584 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007585 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007586 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007587 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007588 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007589 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007590 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007591 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007592 else if (IS_GEMINILAKE(dev_priv))
7593 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007594 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007595 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007596 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007597 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007598 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007599 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007600 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007601 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007602 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007603 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007604 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02007605 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007606 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007607 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007608 else if (IS_G4X(dev_priv))
7609 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007610 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007611 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007612 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007613 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007614 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02007615 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7616 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7617 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007618 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02007619 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7620 else {
7621 MISSING_CASE(INTEL_DEVID(dev_priv));
7622 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7623 }
7624}
7625
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007626/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007627void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007628{
Daniel Vetterc921aba2012-04-26 23:28:17 +02007629 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007630 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08007631 pnv_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007632 else if (IS_GEN(dev_priv, 5))
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08007633 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007634
James Ausmusb068a862019-10-09 10:23:14 -07007635 if (intel_has_sagv(dev_priv))
7636 skl_setup_sagv_block_time(dev_priv);
7637
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007638 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007639 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007640 skl_setup_wm_latency(dev_priv);
Matt Roper98d39492016-05-12 07:06:03 -07007641 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007642 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007643 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007644
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007645 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007646 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007647 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007648 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007649 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007650 dev_priv->display.compute_intermediate_wm =
7651 ilk_compute_intermediate_wm;
7652 dev_priv->display.initial_watermarks =
7653 ilk_initial_watermarks;
7654 dev_priv->display.optimize_watermarks =
7655 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007656 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007657 drm_dbg_kms(&dev_priv->drm,
7658 "Failed to read display plane latency. "
7659 "Disable CxSR\n");
Ville Syrjäläbd602542014-01-07 16:14:10 +02007660 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007661 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007662 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007663 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007664 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007665 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007666 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007667 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03007668 } else if (IS_G4X(dev_priv)) {
7669 g4x_setup_wm_latency(dev_priv);
7670 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
7671 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
7672 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
7673 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007674 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00007675 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007676 dev_priv->is_ddr3,
7677 dev_priv->fsb_freq,
7678 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007679 drm_info(&dev_priv->drm,
7680 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007681 "(found ddr%s fsb freq %d, mem freq %d), "
7682 "disabling CxSR\n",
7683 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7684 dev_priv->fsb_freq, dev_priv->mem_freq);
7685 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007686 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007687 dev_priv->display.update_wm = NULL;
7688 } else
Lucas De Marchi1d218222019-12-24 00:40:04 -08007689 dev_priv->display.update_wm = pnv_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007690 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007691 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007692 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007693 dev_priv->display.update_wm = i9xx_update_wm;
7694 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007695 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03007696 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007697 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007698 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007699 } else {
7700 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007701 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007702 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007703 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007704 drm_err(&dev_priv->drm,
7705 "unexpected fall-through in %s\n", __func__);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007706 }
7707}
7708
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00007709void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01007710{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01007711 dev_priv->runtime_pm.suspended = false;
7712 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007713}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02007714
7715static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
7716{
7717 struct intel_dbuf_state *dbuf_state;
7718
7719 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
7720 if (!dbuf_state)
7721 return NULL;
7722
7723 return &dbuf_state->base;
7724}
7725
7726static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
7727 struct intel_global_state *state)
7728{
7729 kfree(state);
7730}
7731
7732static const struct intel_global_state_funcs intel_dbuf_funcs = {
7733 .atomic_duplicate_state = intel_dbuf_duplicate_state,
7734 .atomic_destroy_state = intel_dbuf_destroy_state,
7735};
7736
7737struct intel_dbuf_state *
7738intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
7739{
7740 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7741 struct intel_global_state *dbuf_state;
7742
7743 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
7744 if (IS_ERR(dbuf_state))
7745 return ERR_CAST(dbuf_state);
7746
7747 return to_intel_dbuf_state(dbuf_state);
7748}
7749
7750int intel_dbuf_init(struct drm_i915_private *dev_priv)
7751{
7752 struct intel_dbuf_state *dbuf_state;
7753
7754 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
7755 if (!dbuf_state)
7756 return -ENOMEM;
7757
7758 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
7759 &dbuf_state->base, &intel_dbuf_funcs);
7760
7761 return 0;
7762}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02007763
7764void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
7765{
7766 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7767 const struct intel_dbuf_state *new_dbuf_state =
7768 intel_atomic_get_new_dbuf_state(state);
7769 const struct intel_dbuf_state *old_dbuf_state =
7770 intel_atomic_get_old_dbuf_state(state);
7771
7772 if (!new_dbuf_state ||
7773 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7774 return;
7775
7776 WARN_ON(!new_dbuf_state->base.changed);
7777
7778 gen9_dbuf_slices_update(dev_priv,
7779 old_dbuf_state->enabled_slices |
7780 new_dbuf_state->enabled_slices);
7781}
7782
7783void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
7784{
7785 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7786 const struct intel_dbuf_state *new_dbuf_state =
7787 intel_atomic_get_new_dbuf_state(state);
7788 const struct intel_dbuf_state *old_dbuf_state =
7789 intel_atomic_get_old_dbuf_state(state);
7790
7791 if (!new_dbuf_state ||
7792 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7793 return;
7794
7795 WARN_ON(!new_dbuf_state->base.changed);
7796
7797 gen9_dbuf_slices_update(dev_priv,
7798 new_dbuf_state->enabled_slices);
7799}