blob: 186b7585041b8bfef8324d696cf1365817f817f3 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Sam Ravnborgd0e93592019-01-26 13:25:24 +010028#include <linux/module.h>
Chris Wilson08ea70a2018-08-12 23:36:31 +010029#include <linux/pm_runtime.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010030
31#include <drm/drm_atomic_helper.h>
32#include <drm/drm_fourcc.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070033#include <drm/drm_plane_helper.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010034
Jani Nikuladf0566a2019-06-13 11:44:16 +030035#include "display/intel_atomic.h"
Ville Syrjälä3df3fe22020-11-06 19:30:42 +020036#include "display/intel_atomic_plane.h"
Stanislav Lisovskiycac91e62020-05-22 16:18:43 +030037#include "display/intel_bw.h"
Ville Syrjälä7785ae02021-04-30 17:39:44 +030038#include "display/intel_de.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030039#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030040#include "display/intel_fbc.h"
41#include "display/intel_sprite.h"
Dave Airlie46d12f92021-02-05 16:48:36 +020042#include "display/skl_universal_plane.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030043
Andi Shyti0dc3c562019-10-20 19:41:39 +010044#include "gt/intel_llc.h"
45
Eugeni Dodonov85208be2012-04-16 22:20:34 -030046#include "i915_drv.h"
Jani Nikulaa10510a2020-02-27 19:00:47 +020047#include "i915_fixed.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030048#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030049#include "i915_trace.h"
Jani Nikula4dd43752021-10-14 13:28:57 +030050#include "intel_pcode.h"
Jani Nikula696173b2019-04-05 14:00:15 +030051#include "intel_pm.h"
Jani Nikula1eecf31e2021-10-13 13:11:59 +030052#include "vlv_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020053#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030054
Jani Nikulaa10510a2020-02-27 19:00:47 +020055/* Stores plane specific WM parameters */
56struct skl_wm_params {
57 bool x_tiled, y_tiled;
58 bool rc_surface;
59 bool is_planar;
60 u32 width;
61 u8 cpp;
62 u32 plane_pixel_rate;
63 u32 y_min_scanlines;
64 u32 plane_bytes_per_line;
65 uint_fixed_16_16_t plane_blocks_per_line;
66 uint_fixed_16_16_t y_tile_minimum;
67 u32 linetime_us;
68 u32 dbuf_block_size;
69};
70
71/* used in computing the new watermarks state */
72struct intel_wm_config {
73 unsigned int num_pipes_active;
74 bool sprites_enabled;
75 bool sprites_scaled;
76};
77
Ville Syrjälä46f16e62016-10-31 22:37:22 +020078static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030079{
Ville Syrjäläb2d73de2021-09-30 22:09:42 +030080 enum pipe pipe;
81
Ville Syrjälä93564042017-08-24 22:10:51 +030082 if (HAS_LLC(dev_priv)) {
83 /*
84 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080085 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030086 *
87 * Must match Sampler, Pixel Back End, and Media. See
88 * WaCompressedResourceSamplerPbeMediaNewHashMode.
89 */
Jani Nikula5f461662020-11-30 13:15:58 +020090 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
91 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
Ville Syrjälä93564042017-08-24 22:10:51 +030092 SKL_DE_COMPRESSED_HASH_MODE);
93 }
94
Ville Syrjäläb2d73de2021-09-30 22:09:42 +030095 for_each_pipe(dev_priv, pipe) {
96 /*
97 * "Plane N strech max must be programmed to 11b (x1)
98 * when Async flips are enabled on that plane."
99 */
Tvrtko Ursulincca08462021-11-26 14:14:24 +0000100 if (!IS_GEMINILAKE(dev_priv) && intel_vtd_active(dev_priv))
Ville Syrjäläb2d73de2021-09-30 22:09:42 +0300101 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
102 SKL_PLANE1_STRETCH_MAX_MASK, SKL_PLANE1_STRETCH_MAX_X1);
103 }
104
Rodrigo Vivi82525c12017-06-08 08:50:00 -0700105 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +0200106 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
107 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300108
Rodrigo Vivi82525c12017-06-08 08:50:00 -0700109 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Jani Nikula5f461662020-11-30 13:15:58 +0200110 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
111 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +0300112
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300113 /*
114 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
115 * Display WA #0859: skl,bxt,kbl,glk,cfl
116 */
Jani Nikula5f461662020-11-30 13:15:58 +0200117 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 +0300118 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300119}
120
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200121static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200122{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200123 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200124
Nick Hoatha7546152015-06-29 14:07:32 +0100125 /* WaDisableSDEUnitClockGating:bxt */
Jani Nikula5f461662020-11-30 13:15:58 +0200126 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Nick Hoatha7546152015-06-29 14:07:32 +0100127 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
128
Imre Deak32608ca2015-03-11 11:10:27 +0200129 /*
130 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200131 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200132 */
Jani Nikula5f461662020-11-30 13:15:58 +0200133 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200134 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200135
136 /*
137 * Wa: Backlight PWM may stop in the asserted state, causing backlight
138 * to stay fully on.
139 */
Jani Nikula5f461662020-11-30 13:15:58 +0200140 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 +0200141 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530142
143 /*
144 * Lower the display internal timeout.
145 * This is needed to avoid any hard hangs when DSI port PLL
146 * is off and a MMIO access is attempted by any privilege
147 * application, using batch buffers or any other means.
148 */
Jani Nikula5f461662020-11-30 13:15:58 +0200149 intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300150
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300151 /*
152 * WaFbcTurnOffFbcWatermark:bxt
153 * Display WA #0562: bxt
154 */
Jani Nikula5f461662020-11-30 13:15:58 +0200155 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 +0300156 DISP_FBC_WM_DIS);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300157
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +0300158 /*
159 * WaFbcHighMemBwCorruptionAvoidance:bxt
160 * Display WA #0883: bxt
161 */
Jani Nikula5f461662020-11-30 13:15:58 +0200162 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 +0300163 ILK_DPFC_DISABLE_DUMMY0);
Imre Deaka82abe42015-03-27 14:00:04 +0200164}
165
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200166static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
167{
168 gen9_init_clock_gating(dev_priv);
169
170 /*
171 * WaDisablePWMClockGating:glk
172 * Backlight PWM may stop in the asserted state, causing backlight
173 * to stay fully on.
174 */
Jani Nikula5f461662020-11-30 13:15:58 +0200175 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 +0200176 PWM1_GATING_DIS | PWM2_GATING_DIS);
177}
178
Lucas De Marchi1d218222019-12-24 00:40:04 -0800179static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200180{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200181 u32 tmp;
182
Jani Nikula5f461662020-11-30 13:15:58 +0200183 tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200184
185 switch (tmp & CLKCFG_FSB_MASK) {
186 case CLKCFG_FSB_533:
187 dev_priv->fsb_freq = 533; /* 133*4 */
188 break;
189 case CLKCFG_FSB_800:
190 dev_priv->fsb_freq = 800; /* 200*4 */
191 break;
192 case CLKCFG_FSB_667:
193 dev_priv->fsb_freq = 667; /* 167*4 */
194 break;
195 case CLKCFG_FSB_400:
196 dev_priv->fsb_freq = 400; /* 100*4 */
197 break;
198 }
199
200 switch (tmp & CLKCFG_MEM_MASK) {
201 case CLKCFG_MEM_533:
202 dev_priv->mem_freq = 533;
203 break;
204 case CLKCFG_MEM_667:
205 dev_priv->mem_freq = 667;
206 break;
207 case CLKCFG_MEM_800:
208 dev_priv->mem_freq = 800;
209 break;
210 }
211
212 /* detect pineview DDR3 setting */
Jani Nikula5f461662020-11-30 13:15:58 +0200213 tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200214 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
215}
216
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800217static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200218{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200219 u16 ddrpll, csipll;
220
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100221 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
222 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200223
224 switch (ddrpll & 0xff) {
225 case 0xc:
226 dev_priv->mem_freq = 800;
227 break;
228 case 0x10:
229 dev_priv->mem_freq = 1066;
230 break;
231 case 0x14:
232 dev_priv->mem_freq = 1333;
233 break;
234 case 0x18:
235 dev_priv->mem_freq = 1600;
236 break;
237 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300238 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
239 ddrpll & 0xff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200240 dev_priv->mem_freq = 0;
241 break;
242 }
243
Daniel Vetterc921aba2012-04-26 23:28:17 +0200244 switch (csipll & 0x3ff) {
245 case 0x00c:
246 dev_priv->fsb_freq = 3200;
247 break;
248 case 0x00e:
249 dev_priv->fsb_freq = 3733;
250 break;
251 case 0x010:
252 dev_priv->fsb_freq = 4266;
253 break;
254 case 0x012:
255 dev_priv->fsb_freq = 4800;
256 break;
257 case 0x014:
258 dev_priv->fsb_freq = 5333;
259 break;
260 case 0x016:
261 dev_priv->fsb_freq = 5866;
262 break;
263 case 0x018:
264 dev_priv->fsb_freq = 6400;
265 break;
266 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300267 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
268 csipll & 0x3ff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200269 dev_priv->fsb_freq = 0;
270 break;
271 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200272}
273
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300274static const struct cxsr_latency cxsr_latency_table[] = {
275 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
276 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
277 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
278 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
279 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
280
281 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
282 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
283 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
284 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
285 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
286
287 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
288 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
289 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
290 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
291 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
292
293 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
294 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
295 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
296 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
297 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
298
299 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
300 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
301 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
302 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
303 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
304
305 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
306 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
307 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
308 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
309 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
310};
311
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100312static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
313 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300314 int fsb,
315 int mem)
316{
317 const struct cxsr_latency *latency;
318 int i;
319
320 if (fsb == 0 || mem == 0)
321 return NULL;
322
323 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
324 latency = &cxsr_latency_table[i];
325 if (is_desktop == latency->is_desktop &&
326 is_ddr3 == latency->is_ddr3 &&
327 fsb == latency->fsb_freq && mem == latency->mem_freq)
328 return latency;
329 }
330
331 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
332
333 return NULL;
334}
335
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200336static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
337{
338 u32 val;
339
Chris Wilson337fa6e2019-04-26 09:17:20 +0100340 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200341
342 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
343 if (enable)
344 val &= ~FORCE_DDR_HIGH_FREQ;
345 else
346 val |= FORCE_DDR_HIGH_FREQ;
347 val &= ~FORCE_DDR_LOW_FREQ;
348 val |= FORCE_DDR_FREQ_REQ_ACK;
349 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
350
351 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
352 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300353 drm_err(&dev_priv->drm,
354 "timed out waiting for Punit DDR DVFS request\n");
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200355
Chris Wilson337fa6e2019-04-26 09:17:20 +0100356 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200357}
358
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200359static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
360{
361 u32 val;
362
Chris Wilson337fa6e2019-04-26 09:17:20 +0100363 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200364
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200365 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200366 if (enable)
367 val |= DSP_MAXFIFO_PM5_ENABLE;
368 else
369 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200370 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200371
Chris Wilson337fa6e2019-04-26 09:17:20 +0100372 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200373}
374
Ville Syrjäläf4998962015-03-10 17:02:21 +0200375#define FW_WM(value, plane) \
376 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
377
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200378static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300379{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200380 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300381 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300382
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100383 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200384 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
385 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
386 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200387 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200388 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
389 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
390 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200391 } else if (IS_PINEVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200392 val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200393 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
394 if (enable)
395 val |= PINEVIEW_SELF_REFRESH_EN;
396 else
397 val &= ~PINEVIEW_SELF_REFRESH_EN;
Jani Nikula5f461662020-11-30 13:15:58 +0200398 intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
399 intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100400 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +0200401 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300402 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
403 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200404 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
405 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100406 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300407 /*
408 * FIXME can't find a bit like this for 915G, and
409 * and yet it does have the related watermark in
410 * FW_BLC_SELF. What's going on?
411 */
Jani Nikula5f461662020-11-30 13:15:58 +0200412 was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300413 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
414 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
Jani Nikula5f461662020-11-30 13:15:58 +0200415 intel_uncore_write(&dev_priv->uncore, INSTPM, val);
416 intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300417 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200418 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300419 }
420
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200421 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
422
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300423 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
424 enableddisabled(enable),
425 enableddisabled(was_enabled));
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200426
427 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300428}
429
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300430/**
431 * intel_set_memory_cxsr - Configure CxSR state
432 * @dev_priv: i915 device
433 * @enable: Allow vs. disallow CxSR
434 *
435 * Allow or disallow the system to enter a special CxSR
436 * (C-state self refresh) state. What typically happens in CxSR mode
437 * is that several display FIFOs may get combined into a single larger
438 * FIFO for a particular plane (so called max FIFO mode) to allow the
439 * system to defer memory fetches longer, and the memory will enter
440 * self refresh.
441 *
442 * Note that enabling CxSR does not guarantee that the system enter
443 * this special mode, nor does it guarantee that the system stays
444 * in that mode once entered. So this just allows/disallows the system
445 * to autonomously utilize the CxSR mode. Other factors such as core
446 * C-states will affect when/if the system actually enters/exits the
447 * CxSR mode.
448 *
449 * Note that on VLV/CHV this actually only controls the max FIFO mode,
450 * and the system is free to enter/exit memory self refresh at any time
451 * even when the use of CxSR has been disallowed.
452 *
453 * While the system is actually in the CxSR/max FIFO mode, some plane
454 * control registers will not get latched on vblank. Thus in order to
455 * guarantee the system will respond to changes in the plane registers
456 * we must always disallow CxSR prior to making changes to those registers.
457 * Unfortunately the system will re-evaluate the CxSR conditions at
458 * frame start which happens after vblank start (which is when the plane
459 * registers would get latched), so we can't proceed with the plane update
460 * during the same frame where we disallowed CxSR.
461 *
462 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
463 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
464 * the hardware w.r.t. HPLL SR when writing to plane registers.
465 * Disallowing just CxSR is sufficient.
466 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200467bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200468{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200469 bool ret;
470
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200471 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200472 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300473 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
474 dev_priv->wm.vlv.cxsr = enable;
475 else if (IS_G4X(dev_priv))
476 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200477 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200478
479 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200480}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200481
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482/*
483 * Latency for FIFO fetches is dependent on several factors:
484 * - memory configuration (speed, channels)
485 * - chipset
486 * - current MCH state
487 * It can be fairly high in some situations, so here we assume a fairly
488 * pessimal value. It's a tradeoff between extra memory fetches (if we
489 * set this value too high, the FIFO will fetch frequently to stay full)
490 * and power consumption (set it too low to save power and we might see
491 * FIFO underruns and display "flicker").
492 *
493 * A value of 5us seems to be a good balance; safe for very low end
494 * platforms but not overly aggressive on lower latency configs.
495 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100496static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300497
Ville Syrjäläb5004722015-03-05 21:19:47 +0200498#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
499 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
500
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200501static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200502{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100503 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200504 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200505 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200506 enum pipe pipe = crtc->pipe;
507 int sprite0_start, sprite1_start;
Kees Cook2713eb42020-02-20 16:05:17 -0800508 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200509
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200510 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200511 case PIPE_A:
Jani Nikula5f461662020-11-30 13:15:58 +0200512 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
513 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200514 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
515 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
516 break;
517 case PIPE_B:
Jani Nikula5f461662020-11-30 13:15:58 +0200518 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
519 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200520 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
521 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
522 break;
523 case PIPE_C:
Jani Nikula5f461662020-11-30 13:15:58 +0200524 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
525 dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200526 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
527 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
528 break;
529 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200530 MISSING_CASE(pipe);
531 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200532 }
533
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200534 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
535 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
536 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
537 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200538}
539
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200540static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
541 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542{
Jani Nikula5f461662020-11-30 13:15:58 +0200543 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300544 int size;
545
546 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200547 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300548 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
549
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300550 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
551 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552
553 return size;
554}
555
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200556static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
557 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300558{
Jani Nikula5f461662020-11-30 13:15:58 +0200559 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300560 int size;
561
562 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200563 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300564 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
565 size >>= 1; /* 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
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200573static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
574 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300575{
Jani Nikula5f461662020-11-30 13:15:58 +0200576 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300577 int size;
578
579 size = dsparb & 0x7f;
580 size >>= 2; /* Convert to cachelines */
581
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300582 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
583 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300584
585 return size;
586}
587
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300588/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800589static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300590 .fifo_size = PINEVIEW_DISPLAY_FIFO,
591 .max_wm = PINEVIEW_MAX_WM,
592 .default_wm = PINEVIEW_DFT_WM,
593 .guard_size = PINEVIEW_GUARD_WM,
594 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300595};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800596
597static const struct intel_watermark_params pnv_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300598 .fifo_size = PINEVIEW_DISPLAY_FIFO,
599 .max_wm = PINEVIEW_MAX_WM,
600 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
601 .guard_size = PINEVIEW_GUARD_WM,
602 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300603};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800604
605static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300606 .fifo_size = PINEVIEW_CURSOR_FIFO,
607 .max_wm = PINEVIEW_CURSOR_MAX_WM,
608 .default_wm = PINEVIEW_CURSOR_DFT_WM,
609 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
610 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300611};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800612
613static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300614 .fifo_size = PINEVIEW_CURSOR_FIFO,
615 .max_wm = PINEVIEW_CURSOR_MAX_WM,
616 .default_wm = PINEVIEW_CURSOR_DFT_WM,
617 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
618 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300619};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800620
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300621static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300622 .fifo_size = I965_CURSOR_FIFO,
623 .max_wm = I965_CURSOR_MAX_WM,
624 .default_wm = I965_CURSOR_DFT_WM,
625 .guard_size = 2,
626 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300627};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800628
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300629static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300630 .fifo_size = I945_FIFO_SIZE,
631 .max_wm = I915_MAX_WM,
632 .default_wm = 1,
633 .guard_size = 2,
634 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300635};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800636
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300637static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300638 .fifo_size = I915_FIFO_SIZE,
639 .max_wm = I915_MAX_WM,
640 .default_wm = 1,
641 .guard_size = 2,
642 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300643};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800644
Ville Syrjälä9d539102014-08-15 01:21:53 +0300645static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300646 .fifo_size = I855GM_FIFO_SIZE,
647 .max_wm = I915_MAX_WM,
648 .default_wm = 1,
649 .guard_size = 2,
650 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300651};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800652
Ville Syrjälä9d539102014-08-15 01:21:53 +0300653static const struct intel_watermark_params i830_bc_wm_info = {
654 .fifo_size = I855GM_FIFO_SIZE,
655 .max_wm = I915_MAX_WM/2,
656 .default_wm = 1,
657 .guard_size = 2,
658 .cacheline_size = I830_FIFO_LINE_SIZE,
659};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800660
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200661static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300662 .fifo_size = I830_FIFO_SIZE,
663 .max_wm = I915_MAX_WM,
664 .default_wm = 1,
665 .guard_size = 2,
666 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300667};
668
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300669/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300670 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
671 * @pixel_rate: Pipe pixel rate in kHz
672 * @cpp: Plane bytes per pixel
673 * @latency: Memory wakeup latency in 0.1us units
674 *
675 * Compute the watermark using the method 1 or "small buffer"
676 * formula. The caller may additonally add extra cachelines
677 * to account for TLB misses and clock crossings.
678 *
679 * This method is concerned with the short term drain rate
680 * of the FIFO, ie. it does not account for blanking periods
681 * which would effectively reduce the average drain rate across
682 * a longer period. The name "small" refers to the fact the
683 * FIFO is relatively small compared to the amount of data
684 * fetched.
685 *
686 * The FIFO level vs. time graph might look something like:
687 *
688 * |\ |\
689 * | \ | \
690 * __---__---__ (- plane active, _ blanking)
691 * -> time
692 *
693 * or perhaps like this:
694 *
695 * |\|\ |\|\
696 * __----__----__ (- plane active, _ blanking)
697 * -> time
698 *
699 * Returns:
700 * The watermark in bytes
701 */
702static unsigned int intel_wm_method1(unsigned int pixel_rate,
703 unsigned int cpp,
704 unsigned int latency)
705{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200706 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300707
Ville Syrjäläd492a292019-04-08 18:27:01 +0300708 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300709 ret = DIV_ROUND_UP_ULL(ret, 10000);
710
711 return ret;
712}
713
714/**
715 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
716 * @pixel_rate: Pipe pixel rate in kHz
717 * @htotal: Pipe horizontal total
718 * @width: Plane width in pixels
719 * @cpp: Plane bytes per pixel
720 * @latency: Memory wakeup latency in 0.1us units
721 *
722 * Compute the watermark using the method 2 or "large buffer"
723 * formula. The caller may additonally add extra cachelines
724 * to account for TLB misses and clock crossings.
725 *
726 * This method is concerned with the long term drain rate
727 * of the FIFO, ie. it does account for blanking periods
728 * which effectively reduce the average drain rate across
729 * a longer period. The name "large" refers to the fact the
730 * FIFO is relatively large compared to the amount of data
731 * fetched.
732 *
733 * The FIFO level vs. time graph might look something like:
734 *
735 * |\___ |\___
736 * | \___ | \___
737 * | \ | \
738 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
739 * -> time
740 *
741 * Returns:
742 * The watermark in bytes
743 */
744static unsigned int intel_wm_method2(unsigned int pixel_rate,
745 unsigned int htotal,
746 unsigned int width,
747 unsigned int cpp,
748 unsigned int latency)
749{
750 unsigned int ret;
751
752 /*
753 * FIXME remove once all users are computing
754 * watermarks in the correct place.
755 */
756 if (WARN_ON_ONCE(htotal == 0))
757 htotal = 1;
758
759 ret = (latency * pixel_rate) / (htotal * 10000);
760 ret = (ret + 1) * width * cpp;
761
762 return ret;
763}
764
765/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300766 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300767 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300768 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000769 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200770 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771 * @latency_ns: memory latency for the platform
772 *
773 * Calculate the watermark level (the level at which the display plane will
774 * start fetching from memory again). Each chip has a different display
775 * FIFO size and allocation, so the caller needs to figure that out and pass
776 * in the correct intel_watermark_params structure.
777 *
778 * As the pixel clock runs, the FIFO will be drained at a rate that depends
779 * on the pixel size. When it reaches the watermark level, it'll start
780 * fetching FIFO line sized based chunks from memory until the FIFO fills
781 * past the watermark point. If the FIFO drains completely, a FIFO underrun
782 * will occur, and a display engine hang could result.
783 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300784static unsigned int intel_calculate_wm(int pixel_rate,
785 const struct intel_watermark_params *wm,
786 int fifo_size, int cpp,
787 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300788{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300789 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300790
791 /*
792 * Note: we need to make sure we don't overflow for various clock &
793 * latency values.
794 * clocks go from a few thousand to several hundred thousand.
795 * latency is usually a few thousand
796 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300797 entries = intel_wm_method1(pixel_rate, cpp,
798 latency_ns / 100);
799 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
800 wm->guard_size;
801 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300802
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300803 wm_size = fifo_size - entries;
804 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300805
806 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300807 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300808 wm_size = wm->max_wm;
809 if (wm_size <= 0)
810 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300811
812 /*
813 * Bspec seems to indicate that the value shouldn't be lower than
814 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
815 * Lets go for 8 which is the burst size since certain platforms
816 * already use a hardcoded 8 (which is what the spec says should be
817 * done).
818 */
819 if (wm_size <= 8)
820 wm_size = 8;
821
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300822 return wm_size;
823}
824
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300825static bool is_disabling(int old, int new, int threshold)
826{
827 return old >= threshold && new < threshold;
828}
829
830static bool is_enabling(int old, int new, int threshold)
831{
832 return old < threshold && new >= threshold;
833}
834
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300835static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
836{
837 return dev_priv->wm.max_level + 1;
838}
839
Ville Syrjälä24304d812017-03-14 17:10:49 +0200840static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
841 const struct intel_plane_state *plane_state)
842{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100843 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200844
845 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100846 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200847 return false;
848
849 /*
850 * Treat cursor with fb as always visible since cursor updates
851 * can happen faster than the vrefresh rate, and the current
852 * watermark code doesn't handle that correctly. Cursor updates
853 * which set/clear the fb or change the cursor size are going
854 * to get throttled by intel_legacy_cursor_update() to work
855 * around this problem with the watermark code.
856 */
857 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100858 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200859 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100860 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200861}
862
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200863static bool intel_crtc_active(struct intel_crtc *crtc)
864{
865 /* Be paranoid as we can arrive here with only partial
866 * state retrieved from the hardware during setup.
867 *
868 * We can ditch the adjusted_mode.crtc_clock check as soon
869 * as Haswell has gained clock readout/fastboot support.
870 *
871 * We can ditch the crtc->primary->state->fb check as soon as we can
872 * properly reconstruct framebuffers.
873 *
874 * FIXME: The intel_crtc->active here should be switched to
875 * crtc->state->active once we have proper CRTC states wired up
876 * for atomic.
877 */
878 return crtc->active && crtc->base.primary->state->fb &&
879 crtc->config->hw.adjusted_mode.crtc_clock;
880}
881
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200882static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300883{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200884 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200886 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200887 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300888 if (enabled)
889 return NULL;
890 enabled = crtc;
891 }
892 }
893
894 return enabled;
895}
896
Dave Airlieef9c66a2021-09-29 01:57:47 +0300897static void pnv_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300898{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200899 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300900 const struct cxsr_latency *latency;
901 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300902 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300903
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000904 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100905 dev_priv->is_ddr3,
906 dev_priv->fsb_freq,
907 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300908 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300909 drm_dbg_kms(&dev_priv->drm,
910 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300911 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300912 return;
913 }
914
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200915 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300916 if (crtc) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200917 const struct drm_display_mode *pipe_mode =
918 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200919 const struct drm_framebuffer *fb =
920 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200921 int cpp = fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +0200922 int clock = pipe_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300923
924 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800925 wm = intel_calculate_wm(clock, &pnv_display_wm,
926 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200927 cpp, latency->display_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200928 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300929 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200930 reg |= FW_WM(wm, SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200931 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300932 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300933
934 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800935 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
936 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300937 4, latency->cursor_sr);
Jani Nikula5f461662020-11-30 13:15:58 +0200938 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300939 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200940 reg |= FW_WM(wm, CURSOR_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200941 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300942
943 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800944 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
945 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200946 cpp, latency->display_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200947 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300948 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200949 reg |= FW_WM(wm, HPLL_SR);
Jani Nikula5f461662020-11-30 13:15:58 +0200950 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300951
952 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800953 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
954 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300955 4, latency->cursor_hpll_disable);
Jani Nikula5f461662020-11-30 13:15:58 +0200956 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300957 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200958 reg |= FW_WM(wm, HPLL_CURSOR);
Jani Nikula5f461662020-11-30 13:15:58 +0200959 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300960 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300961
Imre Deak5209b1f2014-07-01 12:36:17 +0300962 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300963 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300964 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300965 }
966}
967
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300968/*
969 * Documentation says:
970 * "If the line size is small, the TLB fetches can get in the way of the
971 * data fetches, causing some lag in the pixel data return which is not
972 * accounted for in the above formulas. The following adjustment only
973 * needs to be applied if eight whole lines fit in the buffer at once.
974 * The WM is adjusted upwards by the difference between the FIFO size
975 * and the size of 8 whole lines. This adjustment is always performed
976 * in the actual pixel depth regardless of whether FBC is enabled or not."
977 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000978static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300979{
980 int tlb_miss = fifo_size * 64 - width * cpp * 8;
981
982 return max(0, tlb_miss);
983}
984
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300985static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
986 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300987{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300988 enum pipe pipe;
989
990 for_each_pipe(dev_priv, pipe)
991 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
992
Jani Nikula5f461662020-11-30 13:15:58 +0200993 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300994 FW_WM(wm->sr.plane, SR) |
995 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
996 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
997 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +0200998 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300999 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
1000 FW_WM(wm->sr.fbc, FBC_SR) |
1001 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
1002 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
1003 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1004 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001005 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001006 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
1007 FW_WM(wm->sr.cursor, CURSOR_SR) |
1008 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
1009 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001010
Jani Nikula5f461662020-11-30 13:15:58 +02001011 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001012}
1013
Ville Syrjälä15665972015-03-10 16:16:28 +02001014#define FW_WM_VLV(value, plane) \
1015 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1016
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001017static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001018 const struct vlv_wm_values *wm)
1019{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001020 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001021
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001022 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001023 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1024
Jani Nikula5f461662020-11-30 13:15:58 +02001025 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001026 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1027 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1028 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1029 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1030 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001031
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001032 /*
1033 * Zero the (unused) WM1 watermarks, and also clear all the
1034 * high order bits so that there are no out of bounds values
1035 * present in the registers during the reprogramming.
1036 */
Jani Nikula5f461662020-11-30 13:15:58 +02001037 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1038 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1039 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1040 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1041 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001042
Jani Nikula5f461662020-11-30 13:15:58 +02001043 intel_uncore_write(&dev_priv->uncore, DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001044 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001045 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1046 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1047 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001048 intel_uncore_write(&dev_priv->uncore, DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001049 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1050 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1051 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Jani Nikula5f461662020-11-30 13:15:58 +02001052 intel_uncore_write(&dev_priv->uncore, DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001053 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001054
1055 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02001056 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001057 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1058 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001059 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001060 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1061 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Jani Nikula5f461662020-11-30 13:15:58 +02001062 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001063 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1064 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Jani Nikula5f461662020-11-30 13:15:58 +02001065 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001066 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001067 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1068 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1069 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1070 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1071 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1072 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1073 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1074 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1075 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001076 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02001077 intel_uncore_write(&dev_priv->uncore, DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001078 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1079 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Jani Nikula5f461662020-11-30 13:15:58 +02001080 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001081 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001082 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1083 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1084 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1085 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1086 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1087 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001088 }
1089
Jani Nikula5f461662020-11-30 13:15:58 +02001090 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001091}
1092
Ville Syrjälä15665972015-03-10 16:16:28 +02001093#undef FW_WM_VLV
1094
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001095static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1096{
1097 /* all latencies in usec */
1098 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1099 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001100 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001101
Ville Syrjälä79d94302017-04-21 21:14:30 +03001102 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001103}
1104
1105static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1106{
1107 /*
1108 * DSPCNTR[13] supposedly controls whether the
1109 * primary plane can use the FIFO space otherwise
1110 * reserved for the sprite plane. It's not 100% clear
1111 * what the actual FIFO size is, but it looks like we
1112 * can happily set both primary and sprite watermarks
1113 * up to 127 cachelines. So that would seem to mean
1114 * that either DSPCNTR[13] doesn't do anything, or that
1115 * the total FIFO is >= 256 cachelines in size. Either
1116 * way, we don't seem to have to worry about this
1117 * repartitioning as the maximum watermark value the
1118 * register can hold for each plane is lower than the
1119 * minimum FIFO size.
1120 */
1121 switch (plane_id) {
1122 case PLANE_CURSOR:
1123 return 63;
1124 case PLANE_PRIMARY:
1125 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1126 case PLANE_SPRITE0:
1127 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1128 default:
1129 MISSING_CASE(plane_id);
1130 return 0;
1131 }
1132}
1133
1134static int g4x_fbc_fifo_size(int level)
1135{
1136 switch (level) {
1137 case G4X_WM_LEVEL_SR:
1138 return 7;
1139 case G4X_WM_LEVEL_HPLL:
1140 return 15;
1141 default:
1142 MISSING_CASE(level);
1143 return 0;
1144 }
1145}
1146
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001147static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1148 const struct intel_plane_state *plane_state,
1149 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001150{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001151 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001152 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001153 const struct drm_display_mode *pipe_mode =
1154 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001155 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1156 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001157
1158 if (latency == 0)
1159 return USHRT_MAX;
1160
1161 if (!intel_wm_plane_visible(crtc_state, plane_state))
1162 return 0;
1163
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001164 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001165
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001166 /*
Ville Syrjälä52913622021-05-14 15:57:41 +03001167 * WaUse32BppForSRWM:ctg,elk
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001168 *
Ville Syrjälä52913622021-05-14 15:57:41 +03001169 * The spec fails to list this restriction for the
1170 * HPLL watermark, which seems a little strange.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001171 * Let's use 32bpp for the HPLL watermark as well.
1172 */
Ville Syrjälä52913622021-05-14 15:57:41 +03001173 if (plane->id == PLANE_PRIMARY &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001174 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001175 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001176
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001177 clock = pipe_mode->crtc_clock;
1178 htotal = pipe_mode->crtc_htotal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001179
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001180 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001181
1182 if (plane->id == PLANE_CURSOR) {
1183 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1184 } else if (plane->id == PLANE_PRIMARY &&
1185 level == G4X_WM_LEVEL_NORMAL) {
1186 wm = intel_wm_method1(clock, cpp, latency);
1187 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001188 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001189
1190 small = intel_wm_method1(clock, cpp, latency);
1191 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1192
1193 wm = min(small, large);
1194 }
1195
1196 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1197 width, cpp);
1198
1199 wm = DIV_ROUND_UP(wm, 64) + 2;
1200
Chris Wilson1a1f1282017-11-07 14:03:38 +00001201 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001202}
1203
1204static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1205 int level, enum plane_id plane_id, u16 value)
1206{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001207 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001208 bool dirty = false;
1209
1210 for (; level < intel_wm_num_levels(dev_priv); level++) {
1211 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1212
1213 dirty |= raw->plane[plane_id] != value;
1214 raw->plane[plane_id] = value;
1215 }
1216
1217 return dirty;
1218}
1219
1220static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1221 int level, u16 value)
1222{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001223 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001224 bool dirty = false;
1225
1226 /* NORMAL level doesn't have an FBC watermark */
1227 level = max(level, G4X_WM_LEVEL_SR);
1228
1229 for (; level < intel_wm_num_levels(dev_priv); level++) {
1230 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1231
1232 dirty |= raw->fbc != value;
1233 raw->fbc = value;
1234 }
1235
1236 return dirty;
1237}
1238
Maarten Lankhorstec193642019-06-28 10:55:17 +02001239static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1240 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001241 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001242
1243static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1244 const struct intel_plane_state *plane_state)
1245{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001246 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001247 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001248 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1249 enum plane_id plane_id = plane->id;
1250 bool dirty = false;
1251 int level;
1252
1253 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1254 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1255 if (plane_id == PLANE_PRIMARY)
1256 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1257 goto out;
1258 }
1259
1260 for (level = 0; level < num_levels; level++) {
1261 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1262 int wm, max_wm;
1263
1264 wm = g4x_compute_wm(crtc_state, plane_state, level);
1265 max_wm = g4x_plane_fifo_size(plane_id, level);
1266
1267 if (wm > max_wm)
1268 break;
1269
1270 dirty |= raw->plane[plane_id] != wm;
1271 raw->plane[plane_id] = wm;
1272
1273 if (plane_id != PLANE_PRIMARY ||
1274 level == G4X_WM_LEVEL_NORMAL)
1275 continue;
1276
1277 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1278 raw->plane[plane_id]);
1279 max_wm = g4x_fbc_fifo_size(level);
1280
1281 /*
1282 * FBC wm is not mandatory as we
1283 * can always just disable its use.
1284 */
1285 if (wm > max_wm)
1286 wm = USHRT_MAX;
1287
1288 dirty |= raw->fbc != wm;
1289 raw->fbc = wm;
1290 }
1291
1292 /* mark watermarks as invalid */
1293 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1294
1295 if (plane_id == PLANE_PRIMARY)
1296 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1297
1298 out:
1299 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001300 drm_dbg_kms(&dev_priv->drm,
1301 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1302 plane->base.name,
1303 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1304 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1305 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001306
1307 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001308 drm_dbg_kms(&dev_priv->drm,
1309 "FBC watermarks: SR=%d, HPLL=%d\n",
1310 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1311 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001312 }
1313
1314 return dirty;
1315}
1316
1317static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1318 enum plane_id plane_id, int level)
1319{
1320 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1321
1322 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1323}
1324
1325static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1326 int level)
1327{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001328 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001329
1330 if (level > dev_priv->wm.max_level)
1331 return false;
1332
1333 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1334 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1335 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1336}
1337
1338/* mark all levels starting from 'level' as invalid */
1339static void g4x_invalidate_wms(struct intel_crtc *crtc,
1340 struct g4x_wm_state *wm_state, int level)
1341{
1342 if (level <= G4X_WM_LEVEL_NORMAL) {
1343 enum plane_id plane_id;
1344
1345 for_each_plane_id_on_crtc(crtc, plane_id)
1346 wm_state->wm.plane[plane_id] = USHRT_MAX;
1347 }
1348
1349 if (level <= G4X_WM_LEVEL_SR) {
1350 wm_state->cxsr = false;
1351 wm_state->sr.cursor = USHRT_MAX;
1352 wm_state->sr.plane = USHRT_MAX;
1353 wm_state->sr.fbc = USHRT_MAX;
1354 }
1355
1356 if (level <= G4X_WM_LEVEL_HPLL) {
1357 wm_state->hpll_en = false;
1358 wm_state->hpll.cursor = USHRT_MAX;
1359 wm_state->hpll.plane = USHRT_MAX;
1360 wm_state->hpll.fbc = USHRT_MAX;
1361 }
1362}
1363
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001364static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1365 int level)
1366{
1367 if (level < G4X_WM_LEVEL_SR)
1368 return false;
1369
1370 if (level >= G4X_WM_LEVEL_SR &&
1371 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1372 return false;
1373
1374 if (level >= G4X_WM_LEVEL_HPLL &&
1375 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1376 return false;
1377
1378 return true;
1379}
1380
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001381static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1382 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001383{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001384 struct intel_crtc_state *crtc_state =
1385 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001386 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001387 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001388 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001389 const struct intel_plane_state *old_plane_state;
1390 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001391 struct intel_plane *plane;
1392 enum plane_id plane_id;
1393 int i, level;
1394 unsigned int dirty = 0;
1395
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001396 for_each_oldnew_intel_plane_in_state(state, plane,
1397 old_plane_state,
1398 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001399 if (new_plane_state->hw.crtc != &crtc->base &&
1400 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001401 continue;
1402
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001403 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001404 dirty |= BIT(plane->id);
1405 }
1406
1407 if (!dirty)
1408 return 0;
1409
1410 level = G4X_WM_LEVEL_NORMAL;
1411 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1412 goto out;
1413
1414 raw = &crtc_state->wm.g4x.raw[level];
1415 for_each_plane_id_on_crtc(crtc, plane_id)
1416 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1417
1418 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001419 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1420 goto out;
1421
1422 raw = &crtc_state->wm.g4x.raw[level];
1423 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1424 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1425 wm_state->sr.fbc = raw->fbc;
1426
Ville Syrjälä0cf771b2021-05-14 15:57:39 +03001427 wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001428
1429 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001430 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1431 goto out;
1432
1433 raw = &crtc_state->wm.g4x.raw[level];
1434 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1435 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1436 wm_state->hpll.fbc = raw->fbc;
1437
1438 wm_state->hpll_en = wm_state->cxsr;
1439
1440 level++;
1441
1442 out:
1443 if (level == G4X_WM_LEVEL_NORMAL)
1444 return -EINVAL;
1445
1446 /* invalidate the higher levels */
1447 g4x_invalidate_wms(crtc, wm_state, level);
1448
1449 /*
1450 * Determine if the FBC watermark(s) can be used. IF
1451 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001452 * watermark(s) rather than disable the SR/HPLL
1453 * level(s) entirely. 'level-1' is the highest valid
1454 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001455 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001456 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001457
1458 return 0;
1459}
1460
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001461static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1462 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001463{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301464 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001465 struct intel_crtc_state *new_crtc_state =
1466 intel_atomic_get_new_crtc_state(state, crtc);
1467 const struct intel_crtc_state *old_crtc_state =
1468 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001469 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1470 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001471 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001472 enum plane_id plane_id;
1473
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001474 if (!new_crtc_state->hw.active ||
1475 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001476 *intermediate = *optimal;
1477
1478 intermediate->cxsr = false;
1479 intermediate->hpll_en = false;
1480 goto out;
1481 }
1482
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001483 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001484 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001485 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001486 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001487 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1488
1489 for_each_plane_id_on_crtc(crtc, plane_id) {
1490 intermediate->wm.plane[plane_id] =
1491 max(optimal->wm.plane[plane_id],
1492 active->wm.plane[plane_id]);
1493
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301494 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1495 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001496 }
1497
1498 intermediate->sr.plane = max(optimal->sr.plane,
1499 active->sr.plane);
1500 intermediate->sr.cursor = max(optimal->sr.cursor,
1501 active->sr.cursor);
1502 intermediate->sr.fbc = max(optimal->sr.fbc,
1503 active->sr.fbc);
1504
1505 intermediate->hpll.plane = max(optimal->hpll.plane,
1506 active->hpll.plane);
1507 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1508 active->hpll.cursor);
1509 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1510 active->hpll.fbc);
1511
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301512 drm_WARN_ON(&dev_priv->drm,
1513 (intermediate->sr.plane >
1514 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1515 intermediate->sr.cursor >
1516 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1517 intermediate->cxsr);
1518 drm_WARN_ON(&dev_priv->drm,
1519 (intermediate->sr.plane >
1520 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1521 intermediate->sr.cursor >
1522 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1523 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001524
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301525 drm_WARN_ON(&dev_priv->drm,
1526 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1527 intermediate->fbc_en && intermediate->cxsr);
1528 drm_WARN_ON(&dev_priv->drm,
1529 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1530 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001531
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001532out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001533 /*
1534 * If our intermediate WM are identical to the final WM, then we can
1535 * omit the post-vblank programming; only update if it's different.
1536 */
1537 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001538 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001539
1540 return 0;
1541}
1542
1543static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1544 struct g4x_wm_values *wm)
1545{
1546 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001547 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001548
1549 wm->cxsr = true;
1550 wm->hpll_en = true;
1551 wm->fbc_en = true;
1552
1553 for_each_intel_crtc(&dev_priv->drm, crtc) {
1554 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1555
1556 if (!crtc->active)
1557 continue;
1558
1559 if (!wm_state->cxsr)
1560 wm->cxsr = false;
1561 if (!wm_state->hpll_en)
1562 wm->hpll_en = false;
1563 if (!wm_state->fbc_en)
1564 wm->fbc_en = false;
1565
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001566 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001567 }
1568
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001569 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001570 wm->cxsr = false;
1571 wm->hpll_en = false;
1572 wm->fbc_en = false;
1573 }
1574
1575 for_each_intel_crtc(&dev_priv->drm, crtc) {
1576 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1577 enum pipe pipe = crtc->pipe;
1578
1579 wm->pipe[pipe] = wm_state->wm;
1580 if (crtc->active && wm->cxsr)
1581 wm->sr = wm_state->sr;
1582 if (crtc->active && wm->hpll_en)
1583 wm->hpll = wm_state->hpll;
1584 }
1585}
1586
1587static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1588{
1589 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1590 struct g4x_wm_values new_wm = {};
1591
1592 g4x_merge_wm(dev_priv, &new_wm);
1593
1594 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1595 return;
1596
1597 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1598 _intel_set_memory_cxsr(dev_priv, false);
1599
1600 g4x_write_wm_values(dev_priv, &new_wm);
1601
1602 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1603 _intel_set_memory_cxsr(dev_priv, true);
1604
1605 *old_wm = new_wm;
1606}
1607
1608static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001609 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001610{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001611 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1612 const struct intel_crtc_state *crtc_state =
1613 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001614
1615 mutex_lock(&dev_priv->wm.wm_mutex);
1616 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1617 g4x_program_watermarks(dev_priv);
1618 mutex_unlock(&dev_priv->wm.wm_mutex);
1619}
1620
1621static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001622 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001623{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001624 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1625 const struct intel_crtc_state *crtc_state =
1626 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001627
1628 if (!crtc_state->wm.need_postvbl_update)
1629 return;
1630
1631 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001632 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001633 g4x_program_watermarks(dev_priv);
1634 mutex_unlock(&dev_priv->wm.wm_mutex);
1635}
1636
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001637/* latency must be in 0.1us units. */
1638static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001639 unsigned int htotal,
1640 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001641 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001642 unsigned int latency)
1643{
1644 unsigned int ret;
1645
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001646 ret = intel_wm_method2(pixel_rate, htotal,
1647 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001648 ret = DIV_ROUND_UP(ret, 64);
1649
1650 return ret;
1651}
1652
Ville Syrjäläbb726512016-10-31 22:37:24 +02001653static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001654{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001655 /* all latencies in usec */
1656 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1657
Ville Syrjälä58590c12015-09-08 21:05:12 +03001658 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1659
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001660 if (IS_CHERRYVIEW(dev_priv)) {
1661 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1662 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001663
1664 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001665 }
1666}
1667
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001668static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1669 const struct intel_plane_state *plane_state,
1670 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001671{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001672 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001673 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001674 const struct drm_display_mode *pipe_mode =
1675 &crtc_state->hw.pipe_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001676 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001677
1678 if (dev_priv->wm.pri_latency[level] == 0)
1679 return USHRT_MAX;
1680
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001681 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001682 return 0;
1683
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001684 cpp = plane_state->hw.fb->format->cpp[0];
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02001685 clock = pipe_mode->crtc_clock;
1686 htotal = pipe_mode->crtc_htotal;
Ville Syrjäläe339d672016-11-28 19:37:17 +02001687 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001688
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001689 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001690 /*
1691 * FIXME the formula gives values that are
1692 * too big for the cursor FIFO, and hence we
1693 * would never be able to use cursors. For
1694 * now just hardcode the watermark.
1695 */
1696 wm = 63;
1697 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001698 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001699 dev_priv->wm.pri_latency[level] * 10);
1700 }
1701
Chris Wilson1a1f1282017-11-07 14:03:38 +00001702 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001703}
1704
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001705static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1706{
1707 return (active_planes & (BIT(PLANE_SPRITE0) |
1708 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1709}
1710
Ville Syrjälä5012e602017-03-02 19:14:56 +02001711static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001712{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001713 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301714 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001715 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001716 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001717 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001718 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001719 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001720 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001721 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001722 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001723 unsigned int total_rate;
1724 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001725
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001726 /*
1727 * When enabling sprite0 after sprite1 has already been enabled
1728 * we tend to get an underrun unless sprite0 already has some
1729 * FIFO space allcoated. Hence we always allocate at least one
1730 * cacheline for sprite0 whenever sprite1 is enabled.
1731 *
1732 * All other plane enable sequences appear immune to this problem.
1733 */
1734 if (vlv_need_sprite0_fifo_workaround(active_planes))
1735 sprite0_fifo_extra = 1;
1736
Ville Syrjälä5012e602017-03-02 19:14:56 +02001737 total_rate = raw->plane[PLANE_PRIMARY] +
1738 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001739 raw->plane[PLANE_SPRITE1] +
1740 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001741
Ville Syrjälä5012e602017-03-02 19:14:56 +02001742 if (total_rate > fifo_size)
1743 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001744
Ville Syrjälä5012e602017-03-02 19:14:56 +02001745 if (total_rate == 0)
1746 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001747
Ville Syrjälä5012e602017-03-02 19:14:56 +02001748 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001749 unsigned int rate;
1750
Ville Syrjälä5012e602017-03-02 19:14:56 +02001751 if ((active_planes & BIT(plane_id)) == 0) {
1752 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001753 continue;
1754 }
1755
Ville Syrjälä5012e602017-03-02 19:14:56 +02001756 rate = raw->plane[plane_id];
1757 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1758 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001759 }
1760
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001761 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1762 fifo_left -= sprite0_fifo_extra;
1763
Ville Syrjälä5012e602017-03-02 19:14:56 +02001764 fifo_state->plane[PLANE_CURSOR] = 63;
1765
1766 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001767
1768 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001769 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001770 int plane_extra;
1771
1772 if (fifo_left == 0)
1773 break;
1774
Ville Syrjälä5012e602017-03-02 19:14:56 +02001775 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001776 continue;
1777
1778 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001779 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001780 fifo_left -= plane_extra;
1781 }
1782
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301783 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001784
1785 /* give it all to the first plane if none are active */
1786 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301787 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001788 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1789 }
1790
1791 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001792}
1793
Ville Syrjäläff32c542017-03-02 19:14:57 +02001794/* mark all levels starting from 'level' as invalid */
1795static void vlv_invalidate_wms(struct intel_crtc *crtc,
1796 struct vlv_wm_state *wm_state, int level)
1797{
1798 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1799
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001800 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001801 enum plane_id plane_id;
1802
1803 for_each_plane_id_on_crtc(crtc, plane_id)
1804 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1805
1806 wm_state->sr[level].cursor = USHRT_MAX;
1807 wm_state->sr[level].plane = USHRT_MAX;
1808 }
1809}
1810
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001811static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1812{
1813 if (wm > fifo_size)
1814 return USHRT_MAX;
1815 else
1816 return fifo_size - wm;
1817}
1818
Ville Syrjäläff32c542017-03-02 19:14:57 +02001819/*
1820 * Starting from 'level' set all higher
1821 * levels to 'value' in the "raw" watermarks.
1822 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001823static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001824 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001825{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001826 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001827 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001828 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001829
Ville Syrjäläff32c542017-03-02 19:14:57 +02001830 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001831 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001832
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001833 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001834 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001835 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001836
1837 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001838}
1839
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001840static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1841 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001842{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001843 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001844 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001845 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001846 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001847 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001848 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001849
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001850 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001851 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1852 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001853 }
1854
1855 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001856 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001857 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1858 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1859
Ville Syrjäläff32c542017-03-02 19:14:57 +02001860 if (wm > max_wm)
1861 break;
1862
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001863 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001864 raw->plane[plane_id] = wm;
1865 }
1866
1867 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001868 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001869
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001870out:
1871 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001872 drm_dbg_kms(&dev_priv->drm,
1873 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1874 plane->base.name,
1875 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1876 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1877 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001878
1879 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001880}
1881
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001882static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1883 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001884{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001885 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001886 &crtc_state->wm.vlv.raw[level];
1887 const struct vlv_fifo_state *fifo_state =
1888 &crtc_state->wm.vlv.fifo_state;
1889
1890 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1891}
1892
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001893static 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 +02001894{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001895 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1896 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1897 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1898 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001899}
1900
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001901static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1902 struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001903{
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001904 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä670c89e2021-06-09 11:56:30 +03001905 struct intel_crtc_state *crtc_state =
1906 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001907 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001908 const struct vlv_fifo_state *fifo_state =
1909 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä8f27dbf2021-05-14 15:57:40 +03001910 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1911 int num_active_planes = hweight8(active_planes);
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001912 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001913 const struct intel_plane_state *old_plane_state;
1914 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001915 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001916 enum plane_id plane_id;
1917 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001918 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001919
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001920 for_each_oldnew_intel_plane_in_state(state, plane,
1921 old_plane_state,
1922 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001923 if (new_plane_state->hw.crtc != &crtc->base &&
1924 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001925 continue;
1926
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001927 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001928 dirty |= BIT(plane->id);
1929 }
1930
1931 /*
1932 * DSPARB registers may have been reset due to the
1933 * power well being turned off. Make sure we restore
1934 * them to a consistent state even if no primary/sprite
1935 * planes are initially active.
1936 */
1937 if (needs_modeset)
1938 crtc_state->fifo_changed = true;
1939
1940 if (!dirty)
1941 return 0;
1942
1943 /* cursor changes don't warrant a FIFO recompute */
1944 if (dirty & ~BIT(PLANE_CURSOR)) {
1945 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001946 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001947 const struct vlv_fifo_state *old_fifo_state =
1948 &old_crtc_state->wm.vlv.fifo_state;
1949
1950 ret = vlv_compute_fifo(crtc_state);
1951 if (ret)
1952 return ret;
1953
1954 if (needs_modeset ||
1955 memcmp(old_fifo_state, fifo_state,
1956 sizeof(*fifo_state)) != 0)
1957 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001958 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001959
Ville Syrjäläff32c542017-03-02 19:14:57 +02001960 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001961 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001962 /*
1963 * Note that enabling cxsr with no primary/sprite planes
1964 * enabled can wedge the pipe. Hence we only allow cxsr
1965 * with exactly one enabled primary/sprite plane.
1966 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001967 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001968
Ville Syrjälä5012e602017-03-02 19:14:56 +02001969 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001970 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001971 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001972
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001973 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001974 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001975
Ville Syrjäläff32c542017-03-02 19:14:57 +02001976 for_each_plane_id_on_crtc(crtc, plane_id) {
1977 wm_state->wm[level].plane[plane_id] =
1978 vlv_invert_wm_value(raw->plane[plane_id],
1979 fifo_state->plane[plane_id]);
1980 }
1981
1982 wm_state->sr[level].plane =
1983 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001984 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001985 raw->plane[PLANE_SPRITE1]),
1986 sr_fifo_size);
1987
1988 wm_state->sr[level].cursor =
1989 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1990 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001991 }
1992
Ville Syrjäläff32c542017-03-02 19:14:57 +02001993 if (level == 0)
1994 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001995
Ville Syrjäläff32c542017-03-02 19:14:57 +02001996 /* limit to only levels we can actually handle */
1997 wm_state->num_levels = level;
1998
1999 /* invalidate the higher levels */
2000 vlv_invalidate_wms(crtc, wm_state, level);
2001
2002 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002003}
2004
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002005#define VLV_FIFO(plane, value) \
2006 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
2007
Ville Syrjäläff32c542017-03-02 19:14:57 +02002008static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002009 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002010{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002011 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002012 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002013 const struct intel_crtc_state *crtc_state =
2014 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002015 const struct vlv_fifo_state *fifo_state =
2016 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002017 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002018 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002019
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002020 if (!crtc_state->fifo_changed)
2021 return;
2022
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002023 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2024 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2025 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002026
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302027 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2028 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002029
Ville Syrjäläc137d662017-03-02 19:15:06 +02002030 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2031
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002032 /*
2033 * uncore.lock serves a double purpose here. It allows us to
2034 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2035 * it protects the DSPARB registers from getting clobbered by
2036 * parallel updates from multiple pipes.
2037 *
2038 * intel_pipe_update_start() has already disabled interrupts
2039 * for us, so a plain spin_lock() is sufficient here.
2040 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002041 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002042
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002043 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002044 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002045 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2046 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002047
2048 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2049 VLV_FIFO(SPRITEB, 0xff));
2050 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2051 VLV_FIFO(SPRITEB, sprite1_start));
2052
2053 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2054 VLV_FIFO(SPRITEB_HI, 0x1));
2055 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2056 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2057
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002058 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2059 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002060 break;
2061 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002062 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2063 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002064
2065 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2066 VLV_FIFO(SPRITED, 0xff));
2067 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2068 VLV_FIFO(SPRITED, sprite1_start));
2069
2070 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2071 VLV_FIFO(SPRITED_HI, 0xff));
2072 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2073 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2074
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002075 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2076 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002077 break;
2078 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002079 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2080 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002081
2082 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2083 VLV_FIFO(SPRITEF, 0xff));
2084 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2085 VLV_FIFO(SPRITEF, sprite1_start));
2086
2087 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2088 VLV_FIFO(SPRITEF_HI, 0xff));
2089 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2090 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2091
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002092 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2093 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002094 break;
2095 default:
2096 break;
2097 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002098
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002099 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002100
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002101 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002102}
2103
2104#undef VLV_FIFO
2105
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002106static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
2107 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002108{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002109 struct intel_crtc_state *new_crtc_state =
2110 intel_atomic_get_new_crtc_state(state, crtc);
2111 const struct intel_crtc_state *old_crtc_state =
2112 intel_atomic_get_old_crtc_state(state, crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002113 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2114 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002115 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002116 int level;
2117
Ville Syrjälä670c89e2021-06-09 11:56:30 +03002118 if (!new_crtc_state->hw.active ||
2119 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002120 *intermediate = *optimal;
2121
2122 intermediate->cxsr = false;
2123 goto out;
2124 }
2125
Ville Syrjälä4841da52017-03-02 19:14:59 +02002126 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002127 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002128 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002129
2130 for (level = 0; level < intermediate->num_levels; level++) {
2131 enum plane_id plane_id;
2132
2133 for_each_plane_id_on_crtc(crtc, plane_id) {
2134 intermediate->wm[level].plane[plane_id] =
2135 min(optimal->wm[level].plane[plane_id],
2136 active->wm[level].plane[plane_id]);
2137 }
2138
2139 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2140 active->sr[level].plane);
2141 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2142 active->sr[level].cursor);
2143 }
2144
2145 vlv_invalidate_wms(crtc, intermediate, level);
2146
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002147out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002148 /*
2149 * If our intermediate WM are identical to the final WM, then we can
2150 * omit the post-vblank programming; only update if it's different.
2151 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002152 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002153 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002154
2155 return 0;
2156}
2157
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002158static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002159 struct vlv_wm_values *wm)
2160{
2161 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002162 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002163
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002164 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002165 wm->cxsr = true;
2166
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002167 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002168 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002169
2170 if (!crtc->active)
2171 continue;
2172
2173 if (!wm_state->cxsr)
2174 wm->cxsr = false;
2175
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002176 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002177 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2178 }
2179
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002180 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002181 wm->cxsr = false;
2182
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002183 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002184 wm->level = VLV_WM_LEVEL_PM2;
2185
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002186 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002187 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002188 enum pipe pipe = crtc->pipe;
2189
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002190 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002191 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002192 wm->sr = wm_state->sr[wm->level];
2193
Ville Syrjälä1b313892016-11-28 19:37:08 +02002194 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2195 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2196 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2197 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002198 }
2199}
2200
Ville Syrjäläff32c542017-03-02 19:14:57 +02002201static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002202{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002203 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2204 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002205
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002206 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002207
Ville Syrjäläff32c542017-03-02 19:14:57 +02002208 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002209 return;
2210
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002211 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002212 chv_set_memory_dvfs(dev_priv, false);
2213
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002214 if (is_disabling(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, false);
2216
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002217 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002218 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002219
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002220 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002221
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002222 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002223 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002224
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002225 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002226 chv_set_memory_pm5(dev_priv, true);
2227
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002228 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002229 chv_set_memory_dvfs(dev_priv, true);
2230
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002231 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002232}
2233
Ville Syrjäläff32c542017-03-02 19:14:57 +02002234static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002235 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002236{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002237 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2238 const struct intel_crtc_state *crtc_state =
2239 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002240
2241 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002242 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2243 vlv_program_watermarks(dev_priv);
2244 mutex_unlock(&dev_priv->wm.wm_mutex);
2245}
2246
2247static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002248 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002249{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002250 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2251 const struct intel_crtc_state *crtc_state =
2252 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002253
2254 if (!crtc_state->wm.need_postvbl_update)
2255 return;
2256
2257 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002258 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002259 vlv_program_watermarks(dev_priv);
2260 mutex_unlock(&dev_priv->wm.wm_mutex);
2261}
2262
Dave Airlieef9c66a2021-09-29 01:57:47 +03002263static void i965_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002264{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002265 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002266 int srwm = 1;
2267 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002268 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002269
2270 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002271 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002272 if (crtc) {
2273 /* self-refresh has much higher latency */
2274 static const int sr_latency_ns = 12000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002275 const struct drm_display_mode *pipe_mode =
2276 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002277 const struct drm_framebuffer *fb =
2278 crtc->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002279 int clock = pipe_mode->crtc_clock;
2280 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002281 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002282 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002283 int entries;
2284
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002285 entries = intel_wm_method2(clock, htotal,
2286 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002287 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2288 srwm = I965_FIFO_SIZE - entries;
2289 if (srwm < 0)
2290 srwm = 1;
2291 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002292 drm_dbg_kms(&dev_priv->drm,
2293 "self-refresh entries: %d, wm: %d\n",
2294 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002295
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002296 entries = intel_wm_method2(clock, htotal,
2297 crtc->base.cursor->state->crtc_w, 4,
2298 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002299 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002300 i965_cursor_wm_info.cacheline_size) +
2301 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002302
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002303 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002304 if (cursor_sr > i965_cursor_wm_info.max_wm)
2305 cursor_sr = i965_cursor_wm_info.max_wm;
2306
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002307 drm_dbg_kms(&dev_priv->drm,
2308 "self-refresh watermark: display plane %d "
2309 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002310
Imre Deak98584252014-06-13 14:54:20 +03002311 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002312 } else {
Imre Deak98584252014-06-13 14:54:20 +03002313 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002314 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002315 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002316 }
2317
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002318 drm_dbg_kms(&dev_priv->drm,
2319 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2320 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002321
2322 /* 965 has limitations... */
Jani Nikula5f461662020-11-30 13:15:58 +02002323 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002324 FW_WM(8, CURSORB) |
2325 FW_WM(8, PLANEB) |
2326 FW_WM(8, PLANEA));
Jani Nikula5f461662020-11-30 13:15:58 +02002327 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02002328 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002329 /* update cursor SR watermark */
Jani Nikula5f461662020-11-30 13:15:58 +02002330 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002331
2332 if (cxsr_enabled)
2333 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002334}
2335
Ville Syrjäläf4998962015-03-10 17:02:21 +02002336#undef FW_WM
2337
Dave Airlieef9c66a2021-09-29 01:57:47 +03002338static void i9xx_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002339{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002340 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002341 u32 fwater_lo;
2342 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002343 int cwm, srwm = 1;
2344 int fifo_size;
2345 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002346 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002347
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002348 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002349 wm_info = &i945_wm_info;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002350 else if (DISPLAY_VER(dev_priv) != 2)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002351 wm_info = &i915_wm_info;
2352 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002353 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002354
Dave Airlie758b2fc2021-09-29 01:57:46 +03002355 if (DISPLAY_VER(dev_priv) == 2)
2356 fifo_size = i830_get_fifo_size(dev_priv, PLANE_A);
2357 else
2358 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_A);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002359 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002360 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002361 const struct drm_display_mode *pipe_mode =
2362 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002363 const struct drm_framebuffer *fb =
2364 crtc->base.primary->state->fb;
2365 int cpp;
2366
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002367 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002368 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002369 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002370 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002371
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002372 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002373 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002374 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002375 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002376 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002377 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002378 if (planea_wm > (long)wm_info->max_wm)
2379 planea_wm = wm_info->max_wm;
2380 }
2381
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002382 if (DISPLAY_VER(dev_priv) == 2)
Ville Syrjälä9d539102014-08-15 01:21:53 +03002383 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002384
Dave Airlie758b2fc2021-09-29 01:57:46 +03002385 if (DISPLAY_VER(dev_priv) == 2)
2386 fifo_size = i830_get_fifo_size(dev_priv, PLANE_B);
2387 else
2388 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_B);
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002389 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002390 if (intel_crtc_active(crtc)) {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002391 const struct drm_display_mode *pipe_mode =
2392 &crtc->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002393 const struct drm_framebuffer *fb =
2394 crtc->base.primary->state->fb;
2395 int cpp;
2396
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002397 if (DISPLAY_VER(dev_priv) == 2)
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002398 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002399 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002400 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002401
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002402 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002403 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002404 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002405 if (enabled == NULL)
2406 enabled = crtc;
2407 else
2408 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002409 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002410 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002411 if (planeb_wm > (long)wm_info->max_wm)
2412 planeb_wm = wm_info->max_wm;
2413 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002414
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002415 drm_dbg_kms(&dev_priv->drm,
2416 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002417
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002418 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002419 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002420
Ville Syrjäläefc26112016-10-31 22:37:04 +02002421 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002422
2423 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002424 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002425 enabled = NULL;
2426 }
2427
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002428 /*
2429 * Overlay gets an aggressive default since video jitter is bad.
2430 */
2431 cwm = 2;
2432
2433 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002434 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002435
2436 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002437 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002438 /* self-refresh has much higher latency */
2439 static const int sr_latency_ns = 6000;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002440 const struct drm_display_mode *pipe_mode =
2441 &enabled->config->hw.pipe_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002442 const struct drm_framebuffer *fb =
2443 enabled->base.primary->state->fb;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002444 int clock = pipe_mode->crtc_clock;
2445 int htotal = pipe_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002446 int hdisplay = enabled->config->pipe_src_w;
2447 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002448 int entries;
2449
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002450 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002451 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002452 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002453 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002454
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002455 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2456 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002457 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002458 drm_dbg_kms(&dev_priv->drm,
2459 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002460 srwm = wm_info->fifo_size - entries;
2461 if (srwm < 0)
2462 srwm = 1;
2463
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002464 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02002465 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002466 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002467 else
Jani Nikula5f461662020-11-30 13:15:58 +02002468 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002469 }
2470
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002471 drm_dbg_kms(&dev_priv->drm,
2472 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2473 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002474
2475 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2476 fwater_hi = (cwm & 0x1f);
2477
2478 /* Set request length to 8 cachelines per fetch */
2479 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2480 fwater_hi = fwater_hi | (1 << 8);
2481
Jani Nikula5f461662020-11-30 13:15:58 +02002482 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2483 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002484
Imre Deak5209b1f2014-07-01 12:36:17 +03002485 if (enabled)
2486 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002487}
2488
Dave Airlieef9c66a2021-09-29 01:57:47 +03002489static void i845_update_wm(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002490{
Ville Syrjäläefc26112016-10-31 22:37:04 +02002491 struct intel_crtc *crtc;
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002492 const struct drm_display_mode *pipe_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002493 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002494 int planea_wm;
2495
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002496 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002497 if (crtc == NULL)
2498 return;
2499
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002500 pipe_mode = &crtc->config->hw.pipe_mode;
2501 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002502 &i845_wm_info,
Dave Airlie758b2fc2021-09-29 01:57:46 +03002503 i845_get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002504 4, pessimal_latency_ns);
Jani Nikula5f461662020-11-30 13:15:58 +02002505 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002506 fwater_lo |= (3<<8) | planea_wm;
2507
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002508 drm_dbg_kms(&dev_priv->drm,
2509 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002510
Jani Nikula5f461662020-11-30 13:15:58 +02002511 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002512}
2513
Ville Syrjälä37126462013-08-01 16:18:55 +03002514/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002515static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2516 unsigned int cpp,
2517 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002518{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002519 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002520
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002521 ret = intel_wm_method1(pixel_rate, cpp, latency);
2522 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002523
2524 return ret;
2525}
2526
Ville Syrjälä37126462013-08-01 16:18:55 +03002527/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002528static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2529 unsigned int htotal,
2530 unsigned int width,
2531 unsigned int cpp,
2532 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002533{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002534 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002535
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002536 ret = intel_wm_method2(pixel_rate, htotal,
2537 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002538 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002539
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002540 return ret;
2541}
2542
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002543static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002544{
Matt Roper15126882015-12-03 11:37:40 -08002545 /*
2546 * Neither of these should be possible since this function shouldn't be
2547 * called if the CRTC is off or the plane is invisible. But let's be
2548 * extra paranoid to avoid a potential divide-by-zero if we screw up
2549 * elsewhere in the driver.
2550 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002551 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002552 return 0;
2553 if (WARN_ON(!horiz_pixels))
2554 return 0;
2555
Ville Syrjäläac484962016-01-20 21:05:26 +02002556 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002557}
2558
Imre Deak820c1982013-12-17 14:46:36 +02002559struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002560 u16 pri;
2561 u16 spr;
2562 u16 cur;
2563 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002564};
2565
Ville Syrjälä37126462013-08-01 16:18:55 +03002566/*
2567 * For both WM_PIPE and WM_LP.
2568 * mem_value must be in 0.1us units.
2569 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002570static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2571 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002572 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002573{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002574 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002575 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002576
Ville Syrjälä03981c62018-11-14 19:34:40 +02002577 if (mem_value == 0)
2578 return U32_MAX;
2579
Maarten Lankhorstec193642019-06-28 10:55:17 +02002580 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002581 return 0;
2582
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002583 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002584
Maarten Lankhorstec193642019-06-28 10:55:17 +02002585 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002586
2587 if (!is_lp)
2588 return method1;
2589
Maarten Lankhorstec193642019-06-28 10:55:17 +02002590 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002591 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002592 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002593 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002594
2595 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002596}
2597
Ville Syrjälä37126462013-08-01 16:18:55 +03002598/*
2599 * For both WM_PIPE and WM_LP.
2600 * mem_value must be in 0.1us units.
2601 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002602static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2603 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002604 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002605{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002606 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002607 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002608
Ville Syrjälä03981c62018-11-14 19:34:40 +02002609 if (mem_value == 0)
2610 return U32_MAX;
2611
Maarten Lankhorstec193642019-06-28 10:55:17 +02002612 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002613 return 0;
2614
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002615 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002616
Maarten Lankhorstec193642019-06-28 10:55:17 +02002617 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2618 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002619 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002620 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002621 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002622 return min(method1, method2);
2623}
2624
Ville Syrjälä37126462013-08-01 16:18:55 +03002625/*
2626 * For both WM_PIPE and WM_LP.
2627 * mem_value must be in 0.1us units.
2628 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002629static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2630 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002631 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002632{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002633 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002634
Ville Syrjälä03981c62018-11-14 19:34:40 +02002635 if (mem_value == 0)
2636 return U32_MAX;
2637
Maarten Lankhorstec193642019-06-28 10:55:17 +02002638 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002639 return 0;
2640
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002641 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002642
Maarten Lankhorstec193642019-06-28 10:55:17 +02002643 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02002644 crtc_state->hw.pipe_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002645 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002646 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002647}
2648
Paulo Zanonicca32e92013-05-31 11:45:06 -03002649/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002650static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2651 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002652 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002653{
Ville Syrjälä83054942016-11-18 21:53:00 +02002654 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002655
Maarten Lankhorstec193642019-06-28 10:55:17 +02002656 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002657 return 0;
2658
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002659 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002660
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002661 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2662 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002663}
2664
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002665static unsigned int
2666ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002667{
Matt Roper7dadd282021-03-19 21:42:43 -07002668 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002669 return 3072;
Matt Roper7dadd282021-03-19 21:42:43 -07002670 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002671 return 768;
2672 else
2673 return 512;
2674}
2675
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002676static unsigned int
2677ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2678 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002679{
Matt Roper7dadd282021-03-19 21:42:43 -07002680 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002681 /* BDW primary/sprite plane watermarks */
2682 return level == 0 ? 255 : 2047;
Matt Roper7dadd282021-03-19 21:42:43 -07002683 else if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002684 /* IVB/HSW primary/sprite plane watermarks */
2685 return level == 0 ? 127 : 1023;
2686 else if (!is_sprite)
2687 /* ILK/SNB primary plane watermarks */
2688 return level == 0 ? 127 : 511;
2689 else
2690 /* ILK/SNB sprite plane watermarks */
2691 return level == 0 ? 63 : 255;
2692}
2693
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002694static unsigned int
2695ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002696{
Matt Roper7dadd282021-03-19 21:42:43 -07002697 if (DISPLAY_VER(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002698 return level == 0 ? 63 : 255;
2699 else
2700 return level == 0 ? 31 : 63;
2701}
2702
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002703static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002704{
Matt Roper7dadd282021-03-19 21:42:43 -07002705 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002706 return 31;
2707 else
2708 return 15;
2709}
2710
Ville Syrjälä158ae642013-08-07 13:28:19 +03002711/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002712static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002713 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002714 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002715 enum intel_ddb_partitioning ddb_partitioning,
2716 bool is_sprite)
2717{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002718 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002719
2720 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002721 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002722 return 0;
2723
2724 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002725 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002726 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002727
2728 /*
2729 * For some reason the non self refresh
2730 * FIFO size is only half of the self
2731 * refresh FIFO size on ILK/SNB.
2732 */
Matt Roper7dadd282021-03-19 21:42:43 -07002733 if (DISPLAY_VER(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002734 fifo_size /= 2;
2735 }
2736
Ville Syrjälä240264f2013-08-07 13:29:12 +03002737 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002738 /* level 0 is always calculated with 1:1 split */
2739 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2740 if (is_sprite)
2741 fifo_size *= 5;
2742 fifo_size /= 6;
2743 } else {
2744 fifo_size /= 2;
2745 }
2746 }
2747
2748 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002749 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002750}
2751
2752/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002753static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002754 int level,
2755 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002756{
2757 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002758 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002759 return 64;
2760
2761 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002762 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002763}
2764
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002765static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002766 int level,
2767 const struct intel_wm_config *config,
2768 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002769 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002770{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002771 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2772 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2773 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2774 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002775}
2776
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002777static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002778 int level,
2779 struct ilk_wm_maximums *max)
2780{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002781 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2782 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2783 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2784 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002785}
2786
Ville Syrjäläd9395652013-10-09 19:18:10 +03002787static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002788 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002789 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002790{
2791 bool ret;
2792
2793 /* already determined to be invalid? */
2794 if (!result->enable)
2795 return false;
2796
2797 result->enable = result->pri_val <= max->pri &&
2798 result->spr_val <= max->spr &&
2799 result->cur_val <= max->cur;
2800
2801 ret = result->enable;
2802
2803 /*
2804 * HACK until we can pre-compute everything,
2805 * and thus fail gracefully if LP0 watermarks
2806 * are exceeded...
2807 */
2808 if (level == 0 && !result->enable) {
2809 if (result->pri_val > max->pri)
2810 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2811 level, result->pri_val, max->pri);
2812 if (result->spr_val > max->spr)
2813 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2814 level, result->spr_val, max->spr);
2815 if (result->cur_val > max->cur)
2816 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2817 level, result->cur_val, max->cur);
2818
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002819 result->pri_val = min_t(u32, result->pri_val, max->pri);
2820 result->spr_val = min_t(u32, result->spr_val, max->spr);
2821 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002822 result->enable = true;
2823 }
2824
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002825 return ret;
2826}
2827
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002828static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002829 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002830 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002831 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002832 const struct intel_plane_state *pristate,
2833 const struct intel_plane_state *sprstate,
2834 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002835 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002836{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002837 u16 pri_latency = dev_priv->wm.pri_latency[level];
2838 u16 spr_latency = dev_priv->wm.spr_latency[level];
2839 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002840
2841 /* WM1+ latency values stored in 0.5us units */
2842 if (level > 0) {
2843 pri_latency *= 5;
2844 spr_latency *= 5;
2845 cur_latency *= 5;
2846 }
2847
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002848 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002849 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002850 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002851 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002852 }
2853
2854 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002855 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002856
2857 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002858 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002859
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002860 result->enable = true;
2861}
2862
Ville Syrjäläbb726512016-10-31 22:37:24 +02002863static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002864 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002865{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002866 struct intel_uncore *uncore = &dev_priv->uncore;
2867
Matt Roper7dadd282021-03-19 21:42:43 -07002868 if (DISPLAY_VER(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002869 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002870 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002871 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roperd3252e12021-08-20 15:57:10 -07002872 int mult = IS_DG2(dev_priv) ? 2 : 1;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002873
2874 /* read the first set of memory latencies[0:3] */
2875 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002876 ret = sandybridge_pcode_read(dev_priv,
2877 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002878 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002879
2880 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002881 drm_err(&dev_priv->drm,
2882 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002883 return;
2884 }
2885
Matt Roperd3252e12021-08-20 15:57:10 -07002886 wm[0] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2887 wm[1] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2888 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2889 wm[2] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2890 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2891 wm[3] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2892 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002893
2894 /* read the second set of memory latencies[4:7] */
2895 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002896 ret = sandybridge_pcode_read(dev_priv,
2897 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002898 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002899 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002900 drm_err(&dev_priv->drm,
2901 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002902 return;
2903 }
2904
Matt Roperd3252e12021-08-20 15:57:10 -07002905 wm[4] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2906 wm[5] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2907 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2908 wm[6] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2909 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2910 wm[7] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2911 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002912
Vandana Kannan367294b2014-11-04 17:06:46 +00002913 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002914 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2915 * need to be disabled. We make sure to sanitize the values out
2916 * of the punit to satisfy this requirement.
2917 */
2918 for (level = 1; level <= max_level; level++) {
2919 if (wm[level] == 0) {
2920 for (i = level + 1; i <= max_level; i++)
2921 wm[i] = 0;
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002922
2923 max_level = level - 1;
2924
Paulo Zanoni0727e402016-09-22 18:00:30 -03002925 break;
2926 }
2927 }
2928
2929 /*
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002930 * WaWmMemoryReadLatency
Damien Lespiau6f972352015-02-09 19:33:07 +00002931 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002932 * punit doesn't take into account the read latency so we need
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002933 * to add proper adjustement to each valid level we retrieve
2934 * from the punit when level 0 response data is 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002935 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002936 if (wm[0] == 0) {
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002937 u8 adjust = DISPLAY_VER(dev_priv) >= 12 ? 3 : 2;
2938
Lucas De Marchi0bc3a4e2021-06-22 14:22:10 -07002939 for (level = 0; level <= max_level; level++)
Lucas De Marchicbeeb002021-06-22 14:22:09 -07002940 wm[level] += adjust;
Paulo Zanoni0727e402016-09-22 18:00:30 -03002941 }
2942
Mahesh Kumar86b59282018-08-31 16:39:42 +05302943 /*
2944 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2945 * If we could not get dimm info enable this WA to prevent from
2946 * any underrun. If not able to get Dimm info assume 16GB dimm
2947 * to avoid any underrun.
2948 */
José Roberto de Souza66a24502021-01-28 08:43:12 -08002949 if (dev_priv->dram_info.wm_lv_0_adjust_needed)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302950 wm[0] += 1;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002951 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002952 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002953
2954 wm[0] = (sskpd >> 56) & 0xFF;
2955 if (wm[0] == 0)
2956 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002957 wm[1] = (sskpd >> 4) & 0xFF;
2958 wm[2] = (sskpd >> 12) & 0xFF;
2959 wm[3] = (sskpd >> 20) & 0x1FF;
2960 wm[4] = (sskpd >> 32) & 0x1FF;
Matt Roper7dadd282021-03-19 21:42:43 -07002961 } else if (DISPLAY_VER(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002962 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002963
2964 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2965 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2966 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2967 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Matt Roper7dadd282021-03-19 21:42:43 -07002968 } else if (DISPLAY_VER(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002969 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002970
2971 /* ILK primary LP0 latency is 700 ns */
2972 wm[0] = 7;
2973 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2974 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002975 } else {
2976 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002977 }
2978}
2979
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002980static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002981 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002982{
2983 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002984 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002985 wm[0] = 13;
2986}
2987
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002988static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002989 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002990{
2991 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07002992 if (DISPLAY_VER(dev_priv) == 5)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002993 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002994}
2995
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002996int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002997{
2998 /* how many WM levels are we expecting */
Matt Roper7959ffe2021-05-18 17:06:11 -07002999 if (HAS_HW_SAGV_WM(dev_priv))
3000 return 5;
3001 else if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003002 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003003 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03003004 return 4;
Matt Roper7dadd282021-03-19 21:42:43 -07003005 else if (DISPLAY_VER(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03003006 return 3;
3007 else
3008 return 2;
3009}
Daniel Vetter7526ed72014-09-29 15:07:19 +02003010
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003011static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003012 const char *name,
Linus Torvaldse7c6e402021-04-27 17:05:53 -07003013 const u16 wm[])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003014{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003015 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003016
3017 for (level = 0; level <= max_level; level++) {
3018 unsigned int latency = wm[level];
3019
3020 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003021 drm_dbg_kms(&dev_priv->drm,
3022 "%s WM%d latency not provided\n",
3023 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003024 continue;
3025 }
3026
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003027 /*
3028 * - latencies are in us on gen9.
3029 * - before then, WM1+ latency values are in 0.5us units
3030 */
Matt Roper7dadd282021-03-19 21:42:43 -07003031 if (DISPLAY_VER(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003032 latency *= 10;
3033 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003034 latency *= 5;
3035
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003036 drm_dbg_kms(&dev_priv->drm,
3037 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3038 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003039 }
3040}
3041
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003042static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003043 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003044{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003045 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003046
3047 if (wm[0] >= min)
3048 return false;
3049
3050 wm[0] = max(wm[0], min);
3051 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003052 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003053
3054 return true;
3055}
3056
Ville Syrjäläbb726512016-10-31 22:37:24 +02003057static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003058{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003059 bool changed;
3060
3061 /*
3062 * The BIOS provided WM memory latency values are often
3063 * inadequate for high resolution displays. Adjust them.
3064 */
3065 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3066 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3067 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3068
3069 if (!changed)
3070 return;
3071
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003072 drm_dbg_kms(&dev_priv->drm,
3073 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003074 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3075 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3076 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003077}
3078
Ville Syrjälä03981c62018-11-14 19:34:40 +02003079static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3080{
3081 /*
3082 * On some SNB machines (Thinkpad X220 Tablet at least)
3083 * LP3 usage can cause vblank interrupts to be lost.
3084 * The DEIIR bit will go high but it looks like the CPU
3085 * never gets interrupted.
3086 *
3087 * It's not clear whether other interrupt source could
3088 * be affected or if this is somehow limited to vblank
3089 * interrupts only. To play it safe we disable LP3
3090 * watermarks entirely.
3091 */
3092 if (dev_priv->wm.pri_latency[3] == 0 &&
3093 dev_priv->wm.spr_latency[3] == 0 &&
3094 dev_priv->wm.cur_latency[3] == 0)
3095 return;
3096
3097 dev_priv->wm.pri_latency[3] = 0;
3098 dev_priv->wm.spr_latency[3] = 0;
3099 dev_priv->wm.cur_latency[3] = 0;
3100
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003101 drm_dbg_kms(&dev_priv->drm,
3102 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003103 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);
3106}
3107
Ville Syrjäläbb726512016-10-31 22:37:24 +02003108static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003109{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003110 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003111
3112 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3113 sizeof(dev_priv->wm.pri_latency));
3114 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3115 sizeof(dev_priv->wm.pri_latency));
3116
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003117 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003118 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003119
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003120 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3121 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3122 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003123
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003124 if (DISPLAY_VER(dev_priv) == 6) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003125 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003126 snb_wm_lp3_irq_quirk(dev_priv);
3127 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003128}
3129
Ville Syrjäläbb726512016-10-31 22:37:24 +02003130static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003131{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003132 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003133 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003134}
3135
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003136static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003137 struct intel_pipe_wm *pipe_wm)
3138{
3139 /* LP0 watermark maximums depend on this pipe alone */
3140 const struct intel_wm_config config = {
3141 .num_pipes_active = 1,
3142 .sprites_enabled = pipe_wm->sprites_enabled,
3143 .sprites_scaled = pipe_wm->sprites_scaled,
3144 };
3145 struct ilk_wm_maximums max;
3146
3147 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003148 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003149
3150 /* At least LP0 must be valid */
3151 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003152 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003153 return false;
3154 }
3155
3156 return true;
3157}
3158
Matt Roper261a27d2015-10-08 15:28:25 -07003159/* Compute new watermarks for the pipe */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003160static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
3161 struct intel_crtc *crtc)
Matt Roper261a27d2015-10-08 15:28:25 -07003162{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003163 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3164 struct intel_crtc_state *crtc_state =
3165 intel_atomic_get_new_crtc_state(state, crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003166 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003167 struct intel_plane *plane;
3168 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003169 const struct intel_plane_state *pristate = NULL;
3170 const struct intel_plane_state *sprstate = NULL;
3171 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003172 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003173 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003174
Maarten Lankhorstec193642019-06-28 10:55:17 +02003175 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003176
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003177 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3178 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3179 pristate = plane_state;
3180 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3181 sprstate = plane_state;
3182 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3183 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003184 }
3185
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003186 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003187 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003188 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3189 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3190 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3191 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003192 }
3193
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003194 usable_level = max_level;
3195
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003196 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Matt Roper7dadd282021-03-19 21:42:43 -07003197 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003198 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003199
3200 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003201 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003202 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003203
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003204 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003205 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003206 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003207
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003208 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003209 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003210
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003211 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003212
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003213 for (level = 1; level <= usable_level; level++) {
3214 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003215
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003216 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003217 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003218
3219 /*
3220 * Disable any watermark level that exceeds the
3221 * register maximums since such watermarks are
3222 * always invalid.
3223 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003224 if (!ilk_validate_wm_level(level, &max, wm)) {
3225 memset(wm, 0, sizeof(*wm));
3226 break;
3227 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003228 }
3229
Matt Roper86c8bbb2015-09-24 15:53:16 -07003230 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003231}
3232
3233/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003234 * Build a set of 'intermediate' watermark values that satisfy both the old
3235 * state and the new state. These can be programmed to the hardware
3236 * immediately.
3237 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003238static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
3239 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08003240{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003241 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3242 struct intel_crtc_state *new_crtc_state =
3243 intel_atomic_get_new_crtc_state(state, crtc);
3244 const struct intel_crtc_state *old_crtc_state =
3245 intel_atomic_get_old_crtc_state(state, crtc);
3246 struct intel_pipe_wm *a = &new_crtc_state->wm.ilk.intermediate;
3247 const struct intel_pipe_wm *b = &old_crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003248 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003249
3250 /*
3251 * Start with the final, target watermarks, then combine with the
3252 * currently active watermarks to get values that are safe both before
3253 * and after the vblank.
3254 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003255 *a = new_crtc_state->wm.ilk.optimal;
3256 if (!new_crtc_state->hw.active ||
3257 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) ||
3258 state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003259 return 0;
3260
Matt Ropered4a6a72016-02-23 17:20:13 -08003261 a->pipe_enabled |= b->pipe_enabled;
3262 a->sprites_enabled |= b->sprites_enabled;
3263 a->sprites_scaled |= b->sprites_scaled;
3264
3265 for (level = 0; level <= max_level; level++) {
3266 struct intel_wm_level *a_wm = &a->wm[level];
3267 const struct intel_wm_level *b_wm = &b->wm[level];
3268
3269 a_wm->enable &= b_wm->enable;
3270 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3271 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3272 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3273 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3274 }
3275
3276 /*
3277 * We need to make sure that these merged watermark values are
3278 * actually a valid configuration themselves. If they're not,
3279 * there's no safe way to transition from the old state to
3280 * the new state, so we need to fail the atomic transaction.
3281 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003282 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003283 return -EINVAL;
3284
3285 /*
3286 * If our intermediate WM are identical to the final WM, then we can
3287 * omit the post-vblank programming; only update if it's different.
3288 */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003289 if (memcmp(a, &new_crtc_state->wm.ilk.optimal, sizeof(*a)) != 0)
3290 new_crtc_state->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003291
3292 return 0;
3293}
3294
3295/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003296 * Merge the watermarks from all active pipes for a specific level.
3297 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003298static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003299 int level,
3300 struct intel_wm_level *ret_wm)
3301{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003302 const struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003303
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003304 ret_wm->enable = true;
3305
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003306 for_each_intel_crtc(&dev_priv->drm, crtc) {
3307 const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003308 const struct intel_wm_level *wm = &active->wm[level];
3309
3310 if (!active->pipe_enabled)
3311 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003312
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003313 /*
3314 * The watermark values may have been used in the past,
3315 * so we must maintain them in the registers for some
3316 * time even if the level is now disabled.
3317 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003318 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003319 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003320
3321 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3322 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3323 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3324 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3325 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003326}
3327
3328/*
3329 * Merge all low power watermarks for all active pipes.
3330 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003331static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003332 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003333 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003334 struct intel_pipe_wm *merged)
3335{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003336 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003337 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003338
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003339 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Matt Roper7dadd282021-03-19 21:42:43 -07003340 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003341 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003342 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003343
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003344 /* ILK: FBC WM must be disabled always */
Matt Roper7dadd282021-03-19 21:42:43 -07003345 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003346
3347 /* merge each WM1+ level */
3348 for (level = 1; level <= max_level; level++) {
3349 struct intel_wm_level *wm = &merged->wm[level];
3350
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003351 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003352
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003353 if (level > last_enabled_level)
3354 wm->enable = false;
3355 else if (!ilk_validate_wm_level(level, max, wm))
3356 /* make sure all following levels get disabled */
3357 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003358
3359 /*
3360 * The spec says it is preferred to disable
3361 * FBC WMs instead of disabling a WM level.
3362 */
3363 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003364 if (wm->enable)
3365 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003366 wm->fbc_val = 0;
3367 }
3368 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003369
3370 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3371 /*
3372 * FIXME this is racy. FBC might get enabled later.
3373 * What we should check here is whether FBC can be
3374 * enabled sometime later.
3375 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003376 if (DISPLAY_VER(dev_priv) == 5 && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003377 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003378 for (level = 2; level <= max_level; level++) {
3379 struct intel_wm_level *wm = &merged->wm[level];
3380
3381 wm->enable = false;
3382 }
3383 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003384}
3385
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003386static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3387{
3388 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3389 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3390}
3391
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003392/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003393static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3394 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003395{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003396 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003397 return 2 * level;
3398 else
3399 return dev_priv->wm.pri_latency[level];
3400}
3401
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003402static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003403 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003404 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003405 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003406{
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003407 struct intel_crtc *crtc;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003408 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003409
Ville Syrjälä0362c782013-10-09 19:17:57 +03003410 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003411 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003412
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003413 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003414 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003415 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003416
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003417 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003418
Ville Syrjälä0362c782013-10-09 19:17:57 +03003419 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003420
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003421 /*
3422 * Maintain the watermark values even if the level is
3423 * disabled. Doing otherwise could cause underruns.
3424 */
3425 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003426 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003427 (r->pri_val << WM1_LP_SR_SHIFT) |
3428 r->cur_val;
3429
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003430 if (r->enable)
3431 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3432
Matt Roper7dadd282021-03-19 21:42:43 -07003433 if (DISPLAY_VER(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003434 results->wm_lp[wm_lp - 1] |=
3435 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3436 else
3437 results->wm_lp[wm_lp - 1] |=
3438 r->fbc_val << WM1_LP_FBC_SHIFT;
3439
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003440 /*
3441 * Always set WM1S_LP_EN when spr_val != 0, even if the
3442 * level is disabled. Doing otherwise could cause underruns.
3443 */
Matt Roper7dadd282021-03-19 21:42:43 -07003444 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303445 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003446 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3447 } else
3448 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003449 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003450
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003451 /* LP0 register values */
Ville Syrjälä670c89e2021-06-09 11:56:30 +03003452 for_each_intel_crtc(&dev_priv->drm, crtc) {
3453 enum pipe pipe = crtc->pipe;
3454 const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003455 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003456
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303457 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003458 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003459
3460 results->wm_pipe[pipe] =
3461 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3462 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3463 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003464 }
3465}
3466
Paulo Zanoni861f3382013-05-31 10:19:21 -03003467/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3468 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003469static struct intel_pipe_wm *
3470ilk_find_best_result(struct drm_i915_private *dev_priv,
3471 struct intel_pipe_wm *r1,
3472 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003473{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003474 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003475 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003476
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003477 for (level = 1; level <= max_level; level++) {
3478 if (r1->wm[level].enable)
3479 level1 = level;
3480 if (r2->wm[level].enable)
3481 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003482 }
3483
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003484 if (level1 == level2) {
3485 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003486 return r2;
3487 else
3488 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003489 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003490 return r1;
3491 } else {
3492 return r2;
3493 }
3494}
3495
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003496/* dirty bits used to track which watermarks need changes */
3497#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003498#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3499#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3500#define WM_DIRTY_FBC (1 << 24)
3501#define WM_DIRTY_DDB (1 << 25)
3502
Damien Lespiau055e3932014-08-18 13:49:10 +01003503static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003504 const struct ilk_wm_values *old,
3505 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003506{
3507 unsigned int dirty = 0;
3508 enum pipe pipe;
3509 int wm_lp;
3510
Damien Lespiau055e3932014-08-18 13:49:10 +01003511 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003512 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3513 dirty |= WM_DIRTY_PIPE(pipe);
3514 /* Must disable LP1+ watermarks too */
3515 dirty |= WM_DIRTY_LP_ALL;
3516 }
3517 }
3518
3519 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3520 dirty |= WM_DIRTY_FBC;
3521 /* Must disable LP1+ watermarks too */
3522 dirty |= WM_DIRTY_LP_ALL;
3523 }
3524
3525 if (old->partitioning != new->partitioning) {
3526 dirty |= WM_DIRTY_DDB;
3527 /* Must disable LP1+ watermarks too */
3528 dirty |= WM_DIRTY_LP_ALL;
3529 }
3530
3531 /* LP1+ watermarks already deemed dirty, no need to continue */
3532 if (dirty & WM_DIRTY_LP_ALL)
3533 return dirty;
3534
3535 /* Find the lowest numbered LP1+ watermark in need of an update... */
3536 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3537 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3538 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3539 break;
3540 }
3541
3542 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3543 for (; wm_lp <= 3; wm_lp++)
3544 dirty |= WM_DIRTY_LP(wm_lp);
3545
3546 return dirty;
3547}
3548
Ville Syrjälä8553c182013-12-05 15:51:39 +02003549static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3550 unsigned int dirty)
3551{
Imre Deak820c1982013-12-17 14:46:36 +02003552 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003553 bool changed = false;
3554
3555 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3556 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003557 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003558 changed = true;
3559 }
3560 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3561 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003562 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003563 changed = true;
3564 }
3565 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3566 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
Jani Nikula5f461662020-11-30 13:15:58 +02003567 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003568 changed = true;
3569 }
3570
3571 /*
3572 * Don't touch WM1S_LP_EN here.
3573 * Doing so could cause underruns.
3574 */
3575
3576 return changed;
3577}
3578
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003579/*
3580 * The spec says we shouldn't write when we don't need, because every write
3581 * causes WMs to be re-evaluated, expending some power.
3582 */
Imre Deak820c1982013-12-17 14:46:36 +02003583static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3584 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003585{
Imre Deak820c1982013-12-17 14:46:36 +02003586 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003587 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003588 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003589
Damien Lespiau055e3932014-08-18 13:49:10 +01003590 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003591 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003592 return;
3593
Ville Syrjälä8553c182013-12-05 15:51:39 +02003594 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003595
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003596 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Jani Nikula5f461662020-11-30 13:15:58 +02003597 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003598 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Jani Nikula5f461662020-11-30 13:15:58 +02003599 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003600 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Jani Nikula5f461662020-11-30 13:15:58 +02003601 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003602
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003603 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003604 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02003605 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003606 if (results->partitioning == INTEL_DDB_PART_1_2)
3607 val &= ~WM_MISC_DATA_PARTITION_5_6;
3608 else
3609 val |= WM_MISC_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003610 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003611 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02003612 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003613 if (results->partitioning == INTEL_DDB_PART_1_2)
3614 val &= ~DISP_DATA_PARTITION_5_6;
3615 else
3616 val |= DISP_DATA_PARTITION_5_6;
Jani Nikula5f461662020-11-30 13:15:58 +02003617 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003618 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003619 }
3620
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003621 if (dirty & WM_DIRTY_FBC) {
Jani Nikula5f461662020-11-30 13:15:58 +02003622 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003623 if (results->enable_fbc_wm)
3624 val &= ~DISP_FBC_WM_DIS;
3625 else
3626 val |= DISP_FBC_WM_DIS;
Jani Nikula5f461662020-11-30 13:15:58 +02003627 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
Paulo Zanonicca32e92013-05-31 11:45:06 -03003628 }
3629
Imre Deak954911e2013-12-17 14:46:34 +02003630 if (dirty & WM_DIRTY_LP(1) &&
3631 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003632 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
Imre Deak954911e2013-12-17 14:46:34 +02003633
Matt Roper7dadd282021-03-19 21:42:43 -07003634 if (DISPLAY_VER(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003635 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003636 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003637 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003638 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003639 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003640
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003641 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Jani Nikula5f461662020-11-30 13:15:58 +02003642 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003643 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Jani Nikula5f461662020-11-30 13:15:58 +02003644 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003645 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Jani Nikula5f461662020-11-30 13:15:58 +02003646 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003647
3648 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003649}
3650
Ville Syrjälä60aca572019-11-27 21:05:51 +02003651bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003652{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003653 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3654}
3655
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003656u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303657{
Ville Syrjäläb88da662021-04-16 20:10:09 +03003658 u8 enabled_slices = 0;
3659 enum dbuf_slice slice;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303660
Ville Syrjäläb88da662021-04-16 20:10:09 +03003661 for_each_dbuf_slice(dev_priv, slice) {
3662 if (intel_uncore_read(&dev_priv->uncore,
3663 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3664 enabled_slices |= BIT(slice);
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003665 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303666
Ville Syrjäläb88da662021-04-16 20:10:09 +03003667 return enabled_slices;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303668}
3669
Matt Roper024c9042015-09-24 15:53:11 -07003670/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003671 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3672 * so assume we'll always need it in order to avoid underruns.
3673 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003674static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003675{
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003676 return DISPLAY_VER(dev_priv) == 9;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003677}
3678
Paulo Zanoni56feca92016-09-22 18:00:28 -03003679static bool
3680intel_has_sagv(struct drm_i915_private *dev_priv)
3681{
Matt Roper70bfb302021-04-07 13:39:45 -07003682 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003683 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003684}
3685
James Ausmusb068a862019-10-09 10:23:14 -07003686static void
3687skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3688{
Matt Roper7dadd282021-03-19 21:42:43 -07003689 if (DISPLAY_VER(dev_priv) >= 12) {
James Ausmusda80f042019-10-09 10:23:15 -07003690 u32 val = 0;
3691 int ret;
3692
3693 ret = sandybridge_pcode_read(dev_priv,
3694 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3695 &val, NULL);
3696 if (!ret) {
3697 dev_priv->sagv_block_time_us = val;
3698 return;
3699 }
3700
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003701 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003702 } else if (DISPLAY_VER(dev_priv) == 11) {
James Ausmusb068a862019-10-09 10:23:14 -07003703 dev_priv->sagv_block_time_us = 10;
3704 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003705 } else if (DISPLAY_VER(dev_priv) == 10) {
James Ausmusb068a862019-10-09 10:23:14 -07003706 dev_priv->sagv_block_time_us = 20;
3707 return;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07003708 } else if (DISPLAY_VER(dev_priv) == 9) {
James Ausmusb068a862019-10-09 10:23:14 -07003709 dev_priv->sagv_block_time_us = 30;
3710 return;
3711 } else {
Matt Roper7dadd282021-03-19 21:42:43 -07003712 MISSING_CASE(DISPLAY_VER(dev_priv));
James Ausmusb068a862019-10-09 10:23:14 -07003713 }
3714
3715 /* Default to an unusable block time */
3716 dev_priv->sagv_block_time_us = -1;
3717}
3718
Lyude656d1b82016-08-17 15:55:54 -04003719/*
3720 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3721 * depending on power and performance requirements. The display engine access
3722 * to system memory is blocked during the adjustment time. Because of the
3723 * blocking time, having this enabled can cause full system hangs and/or pipe
3724 * underruns if we don't meet all of the following requirements:
3725 *
3726 * - <= 1 pipe enabled
3727 * - All planes can enable watermarks for latencies >= SAGV engine block time
3728 * - We're not using an interlaced display configuration
3729 */
Ville Syrjälä71024042020-09-25 15:17:48 +03003730static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003731intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003732{
3733 int ret;
3734
Paulo Zanoni56feca92016-09-22 18:00:28 -03003735 if (!intel_has_sagv(dev_priv))
3736 return 0;
3737
3738 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003739 return 0;
3740
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003741 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003742 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3743 GEN9_SAGV_ENABLE);
3744
Ville Syrjäläff61a972018-12-21 19:14:34 +02003745 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003746
3747 /*
3748 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003749 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003750 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003751 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003752 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003753 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003754 return 0;
3755 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003756 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003757 return ret;
3758 }
3759
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003760 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003761 return 0;
3762}
3763
Ville Syrjälä71024042020-09-25 15:17:48 +03003764static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003765intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003766{
Imre Deakb3b8e992016-12-05 18:27:38 +02003767 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003768
Paulo Zanoni56feca92016-09-22 18:00:28 -03003769 if (!intel_has_sagv(dev_priv))
3770 return 0;
3771
3772 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003773 return 0;
3774
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003775 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003776 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003777 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3778 GEN9_SAGV_DISABLE,
3779 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3780 1);
Lyude656d1b82016-08-17 15:55:54 -04003781 /*
3782 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003783 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003784 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003785 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003786 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003787 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003788 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003789 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003790 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003791 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003792 }
3793
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003794 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003795 return 0;
3796}
3797
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003798void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3799{
3800 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003801 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003802 const struct intel_bw_state *old_bw_state;
3803 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003804
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003805 /*
3806 * Just return if we can't control SAGV or don't have it.
3807 * This is different from situation when we have SAGV but just can't
3808 * afford it due to DBuf limitation - in case if SAGV is completely
3809 * disabled in a BIOS, we are not even allowed to send a PCode request,
3810 * as it will throw an error. So have to check it here.
3811 */
3812 if (!intel_has_sagv(dev_priv))
3813 return;
3814
3815 new_bw_state = intel_atomic_get_new_bw_state(state);
3816 if (!new_bw_state)
3817 return;
3818
Matt Roper7dadd282021-03-19 21:42:43 -07003819 if (DISPLAY_VER(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003820 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003821 return;
3822 }
3823
3824 old_bw_state = intel_atomic_get_old_bw_state(state);
3825 /*
3826 * Nothing to mask
3827 */
3828 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3829 return;
3830
3831 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3832
3833 /*
3834 * If new mask is zero - means there is nothing to mask,
3835 * we can only unmask, which should be done in unmask.
3836 */
3837 if (!new_mask)
3838 return;
3839
3840 /*
3841 * Restrict required qgv points before updating the configuration.
3842 * According to BSpec we can't mask and unmask qgv points at the same
3843 * time. Also masking should be done before updating the configuration
3844 * and unmasking afterwards.
3845 */
3846 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003847}
3848
3849void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3850{
3851 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003852 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003853 const struct intel_bw_state *old_bw_state;
3854 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003855
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003856 /*
3857 * Just return if we can't control SAGV or don't have it.
3858 * This is different from situation when we have SAGV but just can't
3859 * afford it due to DBuf limitation - in case if SAGV is completely
3860 * disabled in a BIOS, we are not even allowed to send a PCode request,
3861 * as it will throw an error. So have to check it here.
3862 */
3863 if (!intel_has_sagv(dev_priv))
3864 return;
3865
3866 new_bw_state = intel_atomic_get_new_bw_state(state);
3867 if (!new_bw_state)
3868 return;
3869
Matt Roper7dadd282021-03-19 21:42:43 -07003870 if (DISPLAY_VER(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003871 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003872 return;
3873 }
3874
3875 old_bw_state = intel_atomic_get_old_bw_state(state);
3876 /*
3877 * Nothing to unmask
3878 */
3879 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3880 return;
3881
3882 new_mask = new_bw_state->qgv_points_mask;
3883
3884 /*
3885 * Allow required qgv points after updating the configuration.
3886 * According to BSpec we can't mask and unmask qgv points at the same
3887 * time. Also masking should be done before updating the configuration
3888 * and unmasking afterwards.
3889 */
3890 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003891}
3892
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003893static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003894{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003895 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003896 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä9c312122020-11-06 19:30:40 +02003897 enum plane_id plane_id;
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003898 int max_level = INT_MAX;
Lyude656d1b82016-08-17 15:55:54 -04003899
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003900 if (!intel_has_sagv(dev_priv))
3901 return false;
3902
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003903 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003904 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003905
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02003906 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003907 return false;
3908
Ville Syrjälä9c312122020-11-06 19:30:40 +02003909 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003910 const struct skl_plane_wm *wm =
Ville Syrjälä9c312122020-11-06 19:30:40 +02003911 &crtc_state->wm.skl.optimal.planes[plane_id];
3912 int level;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003913
Lyude656d1b82016-08-17 15:55:54 -04003914 /* Skip this plane if it's not enabled */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003915 if (!wm->wm[0].enable)
Lyude656d1b82016-08-17 15:55:54 -04003916 continue;
3917
3918 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003919 for (level = ilk_wm_max_level(dev_priv);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003920 !wm->wm[level].enable; --level)
Lyude656d1b82016-08-17 15:55:54 -04003921 { }
3922
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003923 /* Highest common enabled wm level for all planes */
3924 max_level = min(level, max_level);
3925 }
3926
3927 /* No enabled planes? */
3928 if (max_level == INT_MAX)
3929 return true;
3930
3931 for_each_plane_id_on_crtc(crtc, plane_id) {
3932 const struct skl_plane_wm *wm =
3933 &crtc_state->wm.skl.optimal.planes[plane_id];
3934
Lyude656d1b82016-08-17 15:55:54 -04003935 /*
Ville Syrjäläcdf64622021-03-05 17:36:06 +02003936 * All enabled planes must have enabled a common wm level that
3937 * can tolerate memory latencies higher than sagv_block_time_us
Lyude656d1b82016-08-17 15:55:54 -04003938 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003939 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
Lyude656d1b82016-08-17 15:55:54 -04003940 return false;
3941 }
3942
3943 return true;
3944}
3945
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003946static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3947{
3948 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3949 enum plane_id plane_id;
3950
3951 if (!crtc_state->hw.active)
3952 return true;
3953
3954 for_each_plane_id_on_crtc(crtc, plane_id) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003955 const struct skl_plane_wm *wm =
3956 &crtc_state->wm.skl.optimal.planes[plane_id];
3957
Ville Syrjälä5dac8082021-03-05 17:36:10 +02003958 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003959 return false;
3960 }
3961
3962 return true;
3963}
3964
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003965static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3966{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003967 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3968 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3969
Matt Roper7dadd282021-03-19 21:42:43 -07003970 if (DISPLAY_VER(dev_priv) >= 12)
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003971 return tgl_crtc_can_enable_sagv(crtc_state);
3972 else
3973 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003974}
3975
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003976bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3977 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003978{
Matt Roper7dadd282021-03-19 21:42:43 -07003979 if (DISPLAY_VER(dev_priv) < 11 &&
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003980 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003981 return false;
3982
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003983 return bw_state->pipe_sagv_reject == 0;
3984}
3985
3986static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3987{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003988 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003989 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003990 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003991 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003992 struct intel_bw_state *new_bw_state = NULL;
3993 const struct intel_bw_state *old_bw_state = NULL;
3994 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003995
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003996 for_each_new_intel_crtc_in_state(state, crtc,
3997 new_crtc_state, i) {
3998 new_bw_state = intel_atomic_get_bw_state(state);
3999 if (IS_ERR(new_bw_state))
4000 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004001
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004002 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004003
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004004 if (intel_crtc_can_enable_sagv(new_crtc_state))
4005 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
4006 else
4007 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
4008 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004009
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004010 if (!new_bw_state)
4011 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004012
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004013 new_bw_state->active_pipes =
4014 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03004015
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03004016 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4017 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4018 if (ret)
4019 return ret;
4020 }
4021
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004022 for_each_new_intel_crtc_in_state(state, crtc,
4023 new_crtc_state, i) {
4024 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4025
4026 /*
4027 * We store use_sagv_wm in the crtc state rather than relying on
4028 * that bw state since we have no convenient way to get at the
4029 * latter from the plane commit hooks (especially in the legacy
4030 * cursor case)
4031 */
Matt Roper7959ffe2021-05-18 17:06:11 -07004032 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4033 DISPLAY_VER(dev_priv) >= 12 &&
4034 intel_can_enable_sagv(dev_priv, new_bw_state);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004035 }
4036
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004037 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4038 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004039 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4040 if (ret)
4041 return ret;
4042 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4043 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4044 if (ret)
4045 return ret;
4046 }
4047
4048 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004049}
4050
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004051static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4052{
Ville Syrjäläb88da662021-04-16 20:10:09 +03004053 return INTEL_INFO(dev_priv)->dbuf.size /
4054 hweight8(INTEL_INFO(dev_priv)->dbuf.slice_mask);
Ville Syrjälä944a5e32021-01-22 22:56:28 +02004055}
4056
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004057static void
4058skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4059 struct skl_ddb_entry *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304060{
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004061 int slice_size = intel_dbuf_slice_size(dev_priv);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004062
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004063 if (!slice_mask) {
4064 ddb->start = 0;
4065 ddb->end = 0;
4066 return;
4067 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004068
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004069 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4070 ddb->end = fls(slice_mask) * slice_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004071
Ville Syrjälä96dc6ed2021-01-22 22:56:29 +02004072 WARN_ON(ddb->start >= ddb->end);
Ville Syrjäläb88da662021-04-16 20:10:09 +03004073 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->dbuf.size);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004074}
4075
Ville Syrjälä835c1762021-05-18 17:06:16 -07004076static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
4077{
4078 struct skl_ddb_entry ddb;
4079
4080 if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
4081 slice_mask = BIT(DBUF_S1);
4082 else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
4083 slice_mask = BIT(DBUF_S3);
4084
4085 skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
4086
4087 return ddb.start;
4088}
4089
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004090u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4091 const struct skl_ddb_entry *entry)
4092{
Ville Syrjälä6390e5a2021-04-16 20:10:07 +03004093 int slice_size = intel_dbuf_slice_size(dev_priv);
4094 enum dbuf_slice start_slice, end_slice;
4095 u8 slice_mask = 0;
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004096
4097 if (!skl_ddb_entry_size(entry))
4098 return 0;
4099
4100 start_slice = entry->start / slice_size;
4101 end_slice = (entry->end - 1) / slice_size;
4102
4103 /*
4104 * Per plane DDB entry can in a really worst case be on multiple slices
4105 * but single entry is anyway contigious.
4106 */
4107 while (start_slice <= end_slice) {
4108 slice_mask |= BIT(start_slice);
4109 start_slice++;
4110 }
4111
4112 return slice_mask;
4113}
4114
Ville Syrjälä2791a402021-01-22 22:56:26 +02004115static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4116{
4117 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4118 int hdisplay, vdisplay;
4119
4120 if (!crtc_state->hw.active)
4121 return 0;
4122
4123 /*
4124 * Watermark/ddb requirement highly depends upon width of the
4125 * framebuffer, So instead of allocating DDB equally among pipes
4126 * distribute DDB based on resolution/width of the display.
4127 */
4128 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4129
4130 return hdisplay;
4131}
4132
Ville Syrjäläef79d622021-01-22 22:56:32 +02004133static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4134 enum pipe for_pipe,
4135 unsigned int *weight_start,
4136 unsigned int *weight_end,
4137 unsigned int *weight_total)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004138{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004139 struct drm_i915_private *dev_priv =
4140 to_i915(dbuf_state->base.state->base.dev);
4141 enum pipe pipe;
Ville Syrjälä53630962021-01-22 22:56:31 +02004142
4143 *weight_start = 0;
4144 *weight_end = 0;
4145 *weight_total = 0;
4146
Ville Syrjäläef79d622021-01-22 22:56:32 +02004147 for_each_pipe(dev_priv, pipe) {
4148 int weight = dbuf_state->weight[pipe];
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004149
4150 /*
4151 * Do not account pipes using other slice sets
4152 * luckily as of current BSpec slice sets do not partially
4153 * intersect(pipes share either same one slice or same slice set
4154 * i.e no partial intersection), so it is enough to check for
4155 * equality for now.
4156 */
Ville Syrjäläef79d622021-01-22 22:56:32 +02004157 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304158 continue;
4159
Ville Syrjälä53630962021-01-22 22:56:31 +02004160 *weight_total += weight;
Ville Syrjälä53630962021-01-22 22:56:31 +02004161 if (pipe < for_pipe) {
4162 *weight_start += weight;
4163 *weight_end += weight;
4164 } else if (pipe == for_pipe) {
4165 *weight_end += weight;
4166 }
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304167 }
Ville Syrjälä53630962021-01-22 22:56:31 +02004168}
4169
4170static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02004171skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
Ville Syrjälä53630962021-01-22 22:56:31 +02004172{
Ville Syrjäläef79d622021-01-22 22:56:32 +02004173 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4174 unsigned int weight_total, weight_start, weight_end;
Ville Syrjälä53630962021-01-22 22:56:31 +02004175 const struct intel_dbuf_state *old_dbuf_state =
4176 intel_atomic_get_old_dbuf_state(state);
4177 struct intel_dbuf_state *new_dbuf_state =
4178 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02004179 struct intel_crtc_state *crtc_state;
Ville Syrjälä53630962021-01-22 22:56:31 +02004180 struct skl_ddb_entry ddb_slices;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004181 enum pipe pipe = crtc->pipe;
Manasi Navaree2bebb92021-06-03 14:53:38 -07004182 unsigned int mbus_offset = 0;
Ville Syrjälä53630962021-01-22 22:56:31 +02004183 u32 ddb_range_size;
4184 u32 dbuf_slice_mask;
4185 u32 start, end;
4186 int ret;
4187
Ville Syrjäläef79d622021-01-22 22:56:32 +02004188 if (new_dbuf_state->weight[pipe] == 0) {
4189 new_dbuf_state->ddb[pipe].start = 0;
4190 new_dbuf_state->ddb[pipe].end = 0;
4191 goto out;
Ville Syrjälä53630962021-01-22 22:56:31 +02004192 }
4193
Ville Syrjäläef79d622021-01-22 22:56:32 +02004194 dbuf_slice_mask = new_dbuf_state->slices[pipe];
Ville Syrjälä53630962021-01-22 22:56:31 +02004195
4196 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
Ville Syrjälä835c1762021-05-18 17:06:16 -07004197 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_slice_mask);
Ville Syrjälä53630962021-01-22 22:56:31 +02004198 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4199
Ville Syrjäläef79d622021-01-22 22:56:32 +02004200 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4201 &weight_start, &weight_end, &weight_total);
Ville Syrjälä53630962021-01-22 22:56:31 +02004202
4203 start = ddb_range_size * weight_start / weight_total;
4204 end = ddb_range_size * weight_end / weight_total;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004205
Ville Syrjälä835c1762021-05-18 17:06:16 -07004206 new_dbuf_state->ddb[pipe].start = ddb_slices.start - mbus_offset + start;
4207 new_dbuf_state->ddb[pipe].end = ddb_slices.start - mbus_offset + end;
Ville Syrjäläef79d622021-01-22 22:56:32 +02004208out:
Ville Syrjälä835c1762021-05-18 17:06:16 -07004209 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
4210 skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
Ville Syrjäläef79d622021-01-22 22:56:32 +02004211 &new_dbuf_state->ddb[pipe]))
4212 return 0;
4213
4214 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4215 if (ret)
4216 return ret;
4217
4218 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4219 if (IS_ERR(crtc_state))
4220 return PTR_ERR(crtc_state);
4221
Ville Syrjälä835c1762021-05-18 17:06:16 -07004222 /*
4223 * Used for checking overlaps, so we need absolute
4224 * offsets instead of MBUS relative offsets.
4225 */
4226 crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
4227 crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004228
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004229 drm_dbg_kms(&dev_priv->drm,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004230 "[CRTC:%d:%s] dbuf slices 0x%x -> 0x%x, ddb (%d - %d) -> (%d - %d), active pipes 0x%x -> 0x%x\n",
Ville Syrjälä53630962021-01-22 22:56:31 +02004231 crtc->base.base.id, crtc->base.name,
Ville Syrjäläef79d622021-01-22 22:56:32 +02004232 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4233 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4234 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4235 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004236
4237 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004238}
4239
Ville Syrjälädf331de2019-03-19 18:03:11 +02004240static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4241 int width, const struct drm_format_info *format,
4242 u64 modifier, unsigned int rotation,
4243 u32 plane_pixel_rate, struct skl_wm_params *wp,
4244 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004245static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004246 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004247 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004248 const struct skl_wm_params *wp,
4249 const struct skl_wm_level *result_prev,
4250 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004251
Ville Syrjälädf331de2019-03-19 18:03:11 +02004252static unsigned int
4253skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4254 int num_active)
4255{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004256 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004257 int level, max_level = ilk_wm_max_level(dev_priv);
4258 struct skl_wm_level wm = {};
4259 int ret, min_ddb_alloc = 0;
4260 struct skl_wm_params wp;
4261
4262 ret = skl_compute_wm_params(crtc_state, 256,
4263 drm_format_info(DRM_FORMAT_ARGB8888),
4264 DRM_FORMAT_MOD_LINEAR,
4265 DRM_MODE_ROTATE_0,
4266 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304267 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004268
4269 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004270 unsigned int latency = dev_priv->wm.skl_latency[level];
4271
4272 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004273 if (wm.min_ddb_alloc == U16_MAX)
4274 break;
4275
4276 min_ddb_alloc = wm.min_ddb_alloc;
4277 }
4278
4279 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004280}
4281
Mahesh Kumar37cde112018-04-26 19:55:17 +05304282static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4283 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004284{
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004285 entry->start = reg & DDB_ENTRY_MASK;
4286 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304287
Damien Lespiau16160e32014-11-04 17:06:53 +00004288 if (entry->end)
4289 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004290}
4291
Mahesh Kumarddf34312018-04-09 09:11:03 +05304292static void
4293skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4294 const enum pipe pipe,
4295 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004296 struct skl_ddb_entry *ddb_y,
4297 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304298{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004299 u32 val, val2;
4300 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304301
4302 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4303 if (plane_id == PLANE_CURSOR) {
Jani Nikula5f461662020-11-30 13:15:58 +02004304 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004305 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304306 return;
4307 }
4308
Jani Nikula5f461662020-11-30 13:15:58 +02004309 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304310
4311 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004312 if (val & PLANE_CTL_ENABLE)
4313 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4314 val & PLANE_CTL_ORDER_RGBX,
4315 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304316
Matt Roper7dadd282021-03-19 21:42:43 -07004317 if (DISPLAY_VER(dev_priv) >= 11) {
Jani Nikula5f461662020-11-30 13:15:58 +02004318 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004319 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4320 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02004321 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4322 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304323
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004324 if (fourcc &&
4325 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004326 swap(val, val2);
4327
4328 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4329 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304330 }
4331}
4332
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004333void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4334 struct skl_ddb_entry *ddb_y,
4335 struct skl_ddb_entry *ddb_uv)
4336{
4337 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4338 enum intel_display_power_domain power_domain;
4339 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004340 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004341 enum plane_id plane_id;
4342
4343 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004344 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4345 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004346 return;
4347
4348 for_each_plane_id_on_crtc(crtc, plane_id)
4349 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4350 plane_id,
4351 &ddb_y[plane_id],
4352 &ddb_uv[plane_id]);
4353
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004354 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004355}
4356
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004357/*
4358 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4359 * The bspec defines downscale amount as:
4360 *
4361 * """
4362 * Horizontal down scale amount = maximum[1, Horizontal source size /
4363 * Horizontal destination size]
4364 * Vertical down scale amount = maximum[1, Vertical source size /
4365 * Vertical destination size]
4366 * Total down scale amount = Horizontal down scale amount *
4367 * Vertical down scale amount
4368 * """
4369 *
4370 * Return value is provided in 16.16 fixed point form to retain fractional part.
4371 * Caller should take care of dividing & rounding off the value.
4372 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304373static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004374skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4375 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004376{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304377 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004378 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304379 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4380 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004381
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304382 if (drm_WARN_ON(&dev_priv->drm,
4383 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304384 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004385
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004386 /*
4387 * Src coordinates are already rotated by 270 degrees for
4388 * the 90/270 degree plane rotation cases (to match the
4389 * GTT mapping), hence no need to account for rotation here.
4390 *
4391 * n.b., src is 16.16 fixed point, dst is whole integer.
4392 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004393 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4394 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4395 dst_w = drm_rect_width(&plane_state->uapi.dst);
4396 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004397
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304398 fp_w_ratio = div_fixed16(src_w, dst_w);
4399 fp_h_ratio = div_fixed16(src_h, dst_h);
4400 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4401 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004402
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304403 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004404}
4405
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004406struct dbuf_slice_conf_entry {
4407 u8 active_pipes;
4408 u8 dbuf_mask[I915_MAX_PIPES];
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004409 bool join_mbus;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004410};
4411
4412/*
4413 * Table taken from Bspec 12716
4414 * Pipes do have some preferred DBuf slice affinity,
4415 * plus there are some hardcoded requirements on how
4416 * those should be distributed for multipipe scenarios.
4417 * For more DBuf slices algorithm can get even more messy
4418 * and less readable, so decided to use a table almost
4419 * as is from BSpec itself - that way it is at least easier
4420 * to compare, change and check.
4421 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004422static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004423/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4424{
4425 {
4426 .active_pipes = BIT(PIPE_A),
4427 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004428 [PIPE_A] = BIT(DBUF_S1),
4429 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004430 },
4431 {
4432 .active_pipes = BIT(PIPE_B),
4433 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004434 [PIPE_B] = BIT(DBUF_S1),
4435 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004436 },
4437 {
4438 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4439 .dbuf_mask = {
4440 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004441 [PIPE_B] = BIT(DBUF_S2),
4442 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004443 },
4444 {
4445 .active_pipes = BIT(PIPE_C),
4446 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004447 [PIPE_C] = BIT(DBUF_S2),
4448 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004449 },
4450 {
4451 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4452 .dbuf_mask = {
4453 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004454 [PIPE_C] = BIT(DBUF_S2),
4455 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004456 },
4457 {
4458 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4459 .dbuf_mask = {
4460 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004461 [PIPE_C] = BIT(DBUF_S2),
4462 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004463 },
4464 {
4465 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4466 .dbuf_mask = {
4467 [PIPE_A] = BIT(DBUF_S1),
4468 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004469 [PIPE_C] = BIT(DBUF_S2),
4470 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004471 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004472 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004473};
4474
4475/*
4476 * Table taken from Bspec 49255
4477 * Pipes do have some preferred DBuf slice affinity,
4478 * plus there are some hardcoded requirements on how
4479 * those should be distributed for multipipe scenarios.
4480 * For more DBuf slices algorithm can get even more messy
4481 * and less readable, so decided to use a table almost
4482 * as is from BSpec itself - that way it is at least easier
4483 * to compare, change and check.
4484 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004485static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004486/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4487{
4488 {
4489 .active_pipes = BIT(PIPE_A),
4490 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004491 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4492 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004493 },
4494 {
4495 .active_pipes = BIT(PIPE_B),
4496 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004497 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4498 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004499 },
4500 {
4501 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4502 .dbuf_mask = {
4503 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004504 [PIPE_B] = BIT(DBUF_S1),
4505 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004506 },
4507 {
4508 .active_pipes = BIT(PIPE_C),
4509 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004510 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4511 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004512 },
4513 {
4514 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4515 .dbuf_mask = {
4516 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004517 [PIPE_C] = BIT(DBUF_S2),
4518 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004519 },
4520 {
4521 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4522 .dbuf_mask = {
4523 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004524 [PIPE_C] = BIT(DBUF_S2),
4525 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004526 },
4527 {
4528 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4529 .dbuf_mask = {
4530 [PIPE_A] = BIT(DBUF_S1),
4531 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004532 [PIPE_C] = BIT(DBUF_S2),
4533 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004534 },
4535 {
4536 .active_pipes = BIT(PIPE_D),
4537 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004538 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4539 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004540 },
4541 {
4542 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4543 .dbuf_mask = {
4544 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004545 [PIPE_D] = BIT(DBUF_S2),
4546 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004547 },
4548 {
4549 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4550 .dbuf_mask = {
4551 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004552 [PIPE_D] = BIT(DBUF_S2),
4553 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004554 },
4555 {
4556 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4557 .dbuf_mask = {
4558 [PIPE_A] = BIT(DBUF_S1),
4559 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004560 [PIPE_D] = BIT(DBUF_S2),
4561 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004562 },
4563 {
4564 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4565 .dbuf_mask = {
4566 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004567 [PIPE_D] = BIT(DBUF_S2),
4568 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004569 },
4570 {
4571 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4572 .dbuf_mask = {
4573 [PIPE_A] = BIT(DBUF_S1),
4574 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004575 [PIPE_D] = BIT(DBUF_S2),
4576 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004577 },
4578 {
4579 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4580 .dbuf_mask = {
4581 [PIPE_B] = BIT(DBUF_S1),
4582 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004583 [PIPE_D] = BIT(DBUF_S2),
4584 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004585 },
4586 {
4587 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4588 .dbuf_mask = {
4589 [PIPE_A] = BIT(DBUF_S1),
4590 [PIPE_B] = BIT(DBUF_S1),
4591 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004592 [PIPE_D] = BIT(DBUF_S2),
4593 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004594 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004595 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004596};
4597
Matt Roper49f75632021-07-21 15:30:40 -07004598static const struct dbuf_slice_conf_entry dg2_allowed_dbufs[] = {
4599 {
4600 .active_pipes = BIT(PIPE_A),
4601 .dbuf_mask = {
4602 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4603 },
4604 },
4605 {
4606 .active_pipes = BIT(PIPE_B),
4607 .dbuf_mask = {
4608 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4609 },
4610 },
4611 {
4612 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4613 .dbuf_mask = {
4614 [PIPE_A] = BIT(DBUF_S1),
4615 [PIPE_B] = BIT(DBUF_S2),
4616 },
4617 },
4618 {
4619 .active_pipes = BIT(PIPE_C),
4620 .dbuf_mask = {
4621 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4622 },
4623 },
4624 {
4625 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4626 .dbuf_mask = {
4627 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4628 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4629 },
4630 },
4631 {
4632 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4633 .dbuf_mask = {
4634 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4635 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4636 },
4637 },
4638 {
4639 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4640 .dbuf_mask = {
4641 [PIPE_A] = BIT(DBUF_S1),
4642 [PIPE_B] = BIT(DBUF_S2),
4643 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4644 },
4645 },
4646 {
4647 .active_pipes = BIT(PIPE_D),
4648 .dbuf_mask = {
4649 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4650 },
4651 },
4652 {
4653 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4654 .dbuf_mask = {
4655 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4656 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4657 },
4658 },
4659 {
4660 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4661 .dbuf_mask = {
4662 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4663 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4664 },
4665 },
4666 {
4667 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4668 .dbuf_mask = {
4669 [PIPE_A] = BIT(DBUF_S1),
4670 [PIPE_B] = BIT(DBUF_S2),
4671 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4672 },
4673 },
4674 {
4675 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4676 .dbuf_mask = {
4677 [PIPE_C] = BIT(DBUF_S3),
4678 [PIPE_D] = BIT(DBUF_S4),
4679 },
4680 },
4681 {
4682 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4683 .dbuf_mask = {
4684 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4685 [PIPE_C] = BIT(DBUF_S3),
4686 [PIPE_D] = BIT(DBUF_S4),
4687 },
4688 },
4689 {
4690 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4691 .dbuf_mask = {
4692 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4693 [PIPE_C] = BIT(DBUF_S3),
4694 [PIPE_D] = BIT(DBUF_S4),
4695 },
4696 },
4697 {
4698 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4699 .dbuf_mask = {
4700 [PIPE_A] = BIT(DBUF_S1),
4701 [PIPE_B] = BIT(DBUF_S2),
4702 [PIPE_C] = BIT(DBUF_S3),
4703 [PIPE_D] = BIT(DBUF_S4),
4704 },
4705 },
4706 {}
4707};
4708
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004709static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
4710 {
4711 .active_pipes = BIT(PIPE_A),
4712 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004713 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004714 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004715 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004716 },
4717 {
4718 .active_pipes = BIT(PIPE_B),
4719 .dbuf_mask = {
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004720 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004721 },
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004722 .join_mbus = true,
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004723 },
4724 {
4725 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4726 .dbuf_mask = {
4727 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4728 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4729 },
4730 },
4731 {
4732 .active_pipes = BIT(PIPE_C),
4733 .dbuf_mask = {
4734 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4735 },
4736 },
4737 {
4738 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4739 .dbuf_mask = {
4740 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4741 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4742 },
4743 },
4744 {
4745 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4746 .dbuf_mask = {
4747 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4748 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4749 },
4750 },
4751 {
4752 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4753 .dbuf_mask = {
4754 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4755 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4756 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4757 },
4758 },
4759 {
4760 .active_pipes = BIT(PIPE_D),
4761 .dbuf_mask = {
4762 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4763 },
4764 },
4765 {
4766 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4767 .dbuf_mask = {
4768 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4769 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4770 },
4771 },
4772 {
4773 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4774 .dbuf_mask = {
4775 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4776 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4777 },
4778 },
4779 {
4780 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4781 .dbuf_mask = {
4782 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4783 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4784 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4785 },
4786 },
4787 {
4788 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4789 .dbuf_mask = {
4790 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4791 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4792 },
4793 },
4794 {
4795 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4796 .dbuf_mask = {
4797 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4798 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4799 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4800 },
4801 },
4802 {
4803 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4804 .dbuf_mask = {
4805 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4806 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4807 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4808 },
4809 },
4810 {
4811 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4812 .dbuf_mask = {
4813 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4814 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4815 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4816 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4817 },
4818 },
4819 {}
4820
4821};
4822
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07004823static bool check_mbus_joined(u8 active_pipes,
4824 const struct dbuf_slice_conf_entry *dbuf_slices)
4825{
4826 int i;
4827
4828 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
4829 if (dbuf_slices[i].active_pipes == active_pipes)
4830 return dbuf_slices[i].join_mbus;
4831 }
4832 return false;
4833}
4834
4835static bool adlp_check_mbus_joined(u8 active_pipes)
4836{
4837 return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
4838}
4839
Ville Syrjälä05e81552020-02-25 19:11:09 +02004840static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4841 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004842{
4843 int i;
4844
Ville Syrjälä05e81552020-02-25 19:11:09 +02004845 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004846 if (dbuf_slices[i].active_pipes == active_pipes)
4847 return dbuf_slices[i].dbuf_mask[pipe];
4848 }
4849 return 0;
4850}
4851
4852/*
4853 * This function finds an entry with same enabled pipe configuration and
4854 * returns correspondent DBuf slice mask as stated in BSpec for particular
4855 * platform.
4856 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004857static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004858{
4859 /*
4860 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4861 * required calculating "pipe ratio" in order to determine
4862 * if one or two slices can be used for single pipe configurations
4863 * as additional constraint to the existing table.
4864 * However based on recent info, it should be not "pipe ratio"
4865 * but rather ratio between pixel_rate and cdclk with additional
4866 * constants, so for now we are using only table until this is
4867 * clarified. Also this is the reason why crtc_state param is
4868 * still here - we will need it once those additional constraints
4869 * pop up.
4870 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004871 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004872}
4873
Ville Syrjälä05e81552020-02-25 19:11:09 +02004874static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004875{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004876 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004877}
4878
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004879static u32 adlp_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4880{
4881 return compute_dbuf_slices(pipe, active_pipes, adlp_allowed_dbufs);
4882}
4883
Matt Roper49f75632021-07-21 15:30:40 -07004884static u32 dg2_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4885{
4886 return compute_dbuf_slices(pipe, active_pipes, dg2_allowed_dbufs);
4887}
4888
Ville Syrjälä2d42f322021-01-22 22:56:27 +02004889static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004890{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004891 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4892 enum pipe pipe = crtc->pipe;
4893
Matt Roper49f75632021-07-21 15:30:40 -07004894 if (IS_DG2(dev_priv))
4895 return dg2_compute_dbuf_slices(pipe, active_pipes);
4896 else if (IS_ALDERLAKE_P(dev_priv))
Vandita Kulkarni247bdac2021-05-18 17:06:15 -07004897 return adlp_compute_dbuf_slices(pipe, active_pipes);
4898 else if (DISPLAY_VER(dev_priv) == 12)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004899 return tgl_compute_dbuf_slices(pipe, active_pipes);
Lucas De Marchi93e7e612021-04-12 22:09:53 -07004900 else if (DISPLAY_VER(dev_priv) == 11)
Ville Syrjälä05e81552020-02-25 19:11:09 +02004901 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004902 /*
4903 * For anything else just return one slice yet.
4904 * Should be extended for other platforms.
4905 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004906 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004907}
4908
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004909static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004910skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4911 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004912 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004913{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004914 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004915 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004916 u32 data_rate;
4917 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304918 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004919 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004920
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004921 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004922 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004923
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004924 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004925 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004926
4927 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004928 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004929 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004930
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004931 /*
4932 * Src coordinates are already rotated by 270 degrees for
4933 * the 90/270 degree plane rotation cases (to match the
4934 * GTT mapping), hence no need to account for rotation here.
4935 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004936 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4937 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004938
Mahesh Kumarb879d582018-04-09 09:11:01 +05304939 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004940 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304941 width /= 2;
4942 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004943 }
4944
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004945 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304946
Maarten Lankhorstec193642019-06-28 10:55:17 +02004947 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004948
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004949 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4950
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004951 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004952 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004953}
4954
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004955static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004956skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4957 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004958{
Ville Syrjäläab016302020-11-06 19:30:41 +02004959 struct intel_crtc_state *crtc_state =
4960 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004961 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004962 struct intel_plane *plane;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004963 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02004964 enum plane_id plane_id;
4965 int i;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004966
Matt Ropera1de91e2016-05-12 07:05:57 -07004967 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004968 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4969 if (plane->pipe != crtc->pipe)
4970 continue;
4971
4972 plane_id = plane->id;
Matt Roper024c9042015-09-24 15:53:11 -07004973
Mahesh Kumarb879d582018-04-09 09:11:01 +05304974 /* packed/y */
Ville Syrjäläab016302020-11-06 19:30:41 +02004975 crtc_state->plane_data_rate[plane_id] =
4976 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Matt Roper9c74d822016-05-12 07:05:58 -07004977
Mahesh Kumarb879d582018-04-09 09:11:01 +05304978 /* uv-plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02004979 crtc_state->uv_plane_data_rate[plane_id] =
4980 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4981 }
4982
4983 for_each_plane_id_on_crtc(crtc, plane_id) {
4984 total_data_rate += crtc_state->plane_data_rate[plane_id];
4985 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004986 }
4987
4988 return total_data_rate;
4989}
4990
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004991static u64
Ville Syrjäläab016302020-11-06 19:30:41 +02004992icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4993 struct intel_crtc *crtc)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004994{
Ville Syrjäläab016302020-11-06 19:30:41 +02004995 struct intel_crtc_state *crtc_state =
4996 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004997 const struct intel_plane_state *plane_state;
Ville Syrjäläab016302020-11-06 19:30:41 +02004998 struct intel_plane *plane;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004999 u64 total_data_rate = 0;
Ville Syrjäläab016302020-11-06 19:30:41 +02005000 enum plane_id plane_id;
5001 int i;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005002
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005003 /* Calculate and cache data rate for each plane */
Ville Syrjäläab016302020-11-06 19:30:41 +02005004 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5005 if (plane->pipe != crtc->pipe)
5006 continue;
5007
5008 plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005009
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005010 if (!plane_state->planar_linked_plane) {
Ville Syrjäläab016302020-11-06 19:30:41 +02005011 crtc_state->plane_data_rate[plane_id] =
5012 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005013 } else {
5014 enum plane_id y_plane_id;
5015
5016 /*
5017 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005018 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005019 * and needs the master plane state which may be
5020 * NULL if we try get_new_plane_state(), so we
5021 * always calculate from the master.
5022 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005023 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005024 continue;
5025
5026 /* Y plane rate is calculated on the slave */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005027 y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjäläab016302020-11-06 19:30:41 +02005028 crtc_state->plane_data_rate[y_plane_id] =
5029 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005030
Ville Syrjäläab016302020-11-06 19:30:41 +02005031 crtc_state->plane_data_rate[plane_id] =
5032 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005033 }
5034 }
5035
Ville Syrjäläab016302020-11-06 19:30:41 +02005036 for_each_plane_id_on_crtc(crtc, plane_id)
5037 total_data_rate += crtc_state->plane_data_rate[plane_id];
5038
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005039 return total_data_rate;
5040}
5041
Ville Syrjälä5516e892021-02-26 17:32:03 +02005042const struct skl_wm_level *
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005043skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005044 enum plane_id plane_id,
5045 int level)
5046{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005047 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5048
5049 if (level == 0 && pipe_wm->use_sagv_wm)
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005050 return &wm->sagv.wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005051
5052 return &wm->wm[level];
5053}
5054
Ville Syrjälä5516e892021-02-26 17:32:03 +02005055const struct skl_wm_level *
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005056skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
5057 enum plane_id plane_id)
5058{
5059 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5060
5061 if (pipe_wm->use_sagv_wm)
5062 return &wm->sagv.trans_wm;
5063
5064 return &wm->trans_wm;
5065}
5066
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005067/*
5068 * We only disable the watermarks for each plane if
5069 * they exceed the ddb allocation of said plane. This
5070 * is done so that we don't end up touching cursor
5071 * watermarks needlessly when some other plane reduces
5072 * our max possible watermark level.
5073 *
5074 * Bspec has this to say about the PLANE_WM enable bit:
5075 * "All the watermarks at this level for all enabled
5076 * planes must be enabled before the level will be used."
5077 * So this is actually safe to do.
5078 */
5079static void
5080skl_check_wm_level(struct skl_wm_level *wm, u64 total)
5081{
5082 if (wm->min_ddb_alloc > total)
5083 memset(wm, 0, sizeof(*wm));
5084}
5085
5086static void
5087skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
5088 u64 total, u64 uv_total)
5089{
5090 if (wm->min_ddb_alloc > total ||
5091 uv_wm->min_ddb_alloc > uv_total) {
5092 memset(wm, 0, sizeof(*wm));
5093 memset(uv_wm, 0, sizeof(*uv_wm));
5094 }
5095}
5096
Matt Roperc107acf2016-05-12 07:06:01 -07005097static int
Ville Syrjäläef79d622021-01-22 22:56:32 +02005098skl_allocate_plane_ddb(struct intel_atomic_state *state,
5099 struct intel_crtc *crtc)
Damien Lespiaub9cec072014-11-04 17:06:43 +00005100{
Ville Syrjäläef79d622021-01-22 22:56:32 +02005101 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläffc90032020-11-06 19:30:37 +02005102 struct intel_crtc_state *crtc_state =
5103 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005104 const struct intel_dbuf_state *dbuf_state =
Ville Syrjälä47a14952021-01-22 22:56:30 +02005105 intel_atomic_get_new_dbuf_state(state);
Ville Syrjäläef79d622021-01-22 22:56:32 +02005106 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
5107 int num_active = hweight8(dbuf_state->active_pipes);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005108 u16 alloc_size, start = 0;
5109 u16 total[I915_MAX_PLANES] = {};
5110 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02005111 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005112 enum plane_id plane_id;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005113 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08005114 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005115
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005116 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005117 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
5118 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005119
Ville Syrjäläef79d622021-01-22 22:56:32 +02005120 if (!crtc_state->hw.active)
Matt Roperc107acf2016-05-12 07:06:01 -07005121 return 0;
Matt Roperc107acf2016-05-12 07:06:01 -07005122
Matt Roper7dadd282021-03-19 21:42:43 -07005123 if (DISPLAY_VER(dev_priv) >= 11)
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005124 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005125 icl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005126 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005127 total_data_rate =
Ville Syrjäläab016302020-11-06 19:30:41 +02005128 skl_get_total_relative_data_rate(state, crtc);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07005129
Damien Lespiau34bb56a2014-11-04 17:07:01 +00005130 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05305131 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005132 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005133
Matt Roperd8e87492018-12-11 09:31:07 -08005134 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005135 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08005136 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005137 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08005138 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02005139 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005140
Matt Ropera1de91e2016-05-12 07:05:57 -07005141 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07005142 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005143
Matt Roperd8e87492018-12-11 09:31:07 -08005144 /*
5145 * Find the highest watermark level for which we can satisfy the block
5146 * requirement of active planes.
5147 */
5148 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08005149 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005150 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005151 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005152 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005153
5154 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05305155 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305156 drm_WARN_ON(&dev_priv->drm,
5157 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02005158 blocks = U32_MAX;
5159 break;
5160 }
5161 continue;
5162 }
5163
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005164 blocks += wm->wm[level].min_ddb_alloc;
5165 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08005166 }
5167
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02005168 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08005169 alloc_size -= blocks;
5170 break;
5171 }
5172 }
5173
5174 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005175 drm_dbg_kms(&dev_priv->drm,
5176 "Requested display configuration exceeds system DDB limitations");
5177 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
5178 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08005179 return -EINVAL;
5180 }
5181
5182 /*
5183 * Grant each plane the blocks it requires at the highest achievable
5184 * watermark level, plus an extra share of the leftover blocks
5185 * proportional to its relative data rate.
5186 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005187 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005188 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005189 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005190 u64 rate;
5191 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005192
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005193 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02005194 continue;
5195
Damien Lespiaub9cec072014-11-04 17:06:43 +00005196 /*
Matt Roperd8e87492018-12-11 09:31:07 -08005197 * We've accounted for all active planes; remaining planes are
5198 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00005199 */
Matt Roperd8e87492018-12-11 09:31:07 -08005200 if (total_data_rate == 0)
5201 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005202
Ville Syrjäläab016302020-11-06 19:30:41 +02005203 rate = crtc_state->plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005204 extra = min_t(u16, alloc_size,
5205 DIV64_U64_ROUND_UP(alloc_size * rate,
5206 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005207 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005208 alloc_size -= extra;
5209 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005210
Matt Roperd8e87492018-12-11 09:31:07 -08005211 if (total_data_rate == 0)
5212 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07005213
Ville Syrjäläab016302020-11-06 19:30:41 +02005214 rate = crtc_state->uv_plane_data_rate[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005215 extra = min_t(u16, alloc_size,
5216 DIV64_U64_ROUND_UP(alloc_size * rate,
5217 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005218 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08005219 alloc_size -= extra;
5220 total_data_rate -= rate;
5221 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305222 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08005223
5224 /* Set the actual DDB start/end points for each plane */
5225 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005226 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005227 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005228 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005229 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005230 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08005231
5232 if (plane_id == PLANE_CURSOR)
5233 continue;
5234
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005235 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05305236 drm_WARN_ON(&dev_priv->drm,
Matt Roper7dadd282021-03-19 21:42:43 -07005237 DISPLAY_VER(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005238
Matt Roperd8e87492018-12-11 09:31:07 -08005239 /* Leave disabled planes at (0,0) */
5240 if (total[plane_id]) {
5241 plane_alloc->start = start;
5242 start += total[plane_id];
5243 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07005244 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005245
Matt Roperd8e87492018-12-11 09:31:07 -08005246 if (uv_total[plane_id]) {
5247 uv_plane_alloc->start = start;
5248 start += uv_total[plane_id];
5249 uv_plane_alloc->end = start;
5250 }
5251 }
5252
5253 /*
5254 * When we calculated watermark values we didn't know how high
5255 * of a level we'd actually be able to hit, so we just marked
5256 * all levels as "enabled." Go back now and disable the ones
5257 * that aren't actually possible.
5258 */
5259 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005260 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005261 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005262 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02005263
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005264 skl_check_nv12_wm_level(&wm->wm[level], &wm->uv_wm[level],
5265 total[plane_id], uv_total[plane_id]);
Ville Syrjälä290248c2019-02-13 18:54:24 +02005266
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005267 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005268 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005269 * Underruns with WM1+ disabled
5270 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005271 if (DISPLAY_VER(dev_priv) == 11 &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005272 level == 1 && wm->wm[0].enable) {
5273 wm->wm[level].blocks = wm->wm[0].blocks;
5274 wm->wm[level].lines = wm->wm[0].lines;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005275 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005276 }
Matt Roperd8e87492018-12-11 09:31:07 -08005277 }
5278 }
5279
5280 /*
Ville Syrjälädf4a50a2021-02-26 17:31:59 +02005281 * Go back and disable the transition and SAGV watermarks
5282 * if it turns out we don't have enough DDB blocks for them.
Matt Roperd8e87492018-12-11 09:31:07 -08005283 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005284 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005285 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005286 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005287
Ville Syrjäläa5941b42021-03-05 17:36:09 +02005288 skl_check_wm_level(&wm->trans_wm, total[plane_id]);
5289 skl_check_wm_level(&wm->sagv.wm0, total[plane_id]);
5290 skl_check_wm_level(&wm->sagv.trans_wm, total[plane_id]);
Damien Lespiaub9cec072014-11-04 17:06:43 +00005291 }
5292
Matt Roperc107acf2016-05-12 07:06:01 -07005293 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005294}
5295
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005296/*
5297 * 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 +02005298 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005299 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5300 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5301*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005302static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005303skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5304 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005305{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005306 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305307 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005308
5309 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305310 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005311
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305312 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005313 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005314
Matt Roper2b5a4562021-03-22 16:38:40 -07005315 if (DISPLAY_VER(dev_priv) >= 10)
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005316 ret = add_fixed16_u32(ret, 1);
5317
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005318 return ret;
5319}
5320
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005321static uint_fixed_16_16_t
5322skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5323 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005324{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005325 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305326 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005327
5328 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305329 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005330
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005331 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305332 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5333 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305334 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005335 return ret;
5336}
5337
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305338static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005339intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305340{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305341 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005342 u32 pixel_rate;
5343 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305344 uint_fixed_16_16_t linetime_us;
5345
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005346 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305347 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305348
Maarten Lankhorstec193642019-06-28 10:55:17 +02005349 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305350
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305351 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305352 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305353
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005354 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305355 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305356
5357 return linetime_us;
5358}
5359
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305360static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005361skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5362 int width, const struct drm_format_info *format,
5363 u64 modifier, unsigned int rotation,
5364 u32 plane_pixel_rate, struct skl_wm_params *wp,
5365 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305366{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005367 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005368 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005369 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305370
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305371 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005372 if (color_plane == 1 &&
5373 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005374 drm_dbg_kms(&dev_priv->drm,
5375 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305376 return -EINVAL;
5377 }
5378
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005379 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5380 modifier == I915_FORMAT_MOD_Yf_TILED ||
5381 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5382 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5383 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5384 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5385 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005386 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305387
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005388 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005389 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305390 wp->width /= 2;
5391
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005392 wp->cpp = format->cpp[color_plane];
5393 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305394
Matt Roper7dadd282021-03-19 21:42:43 -07005395 if (DISPLAY_VER(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005396 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005397 wp->dbuf_block_size = 256;
5398 else
5399 wp->dbuf_block_size = 512;
5400
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005401 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305402 switch (wp->cpp) {
5403 case 1:
5404 wp->y_min_scanlines = 16;
5405 break;
5406 case 2:
5407 wp->y_min_scanlines = 8;
5408 break;
5409 case 4:
5410 wp->y_min_scanlines = 4;
5411 break;
5412 default:
5413 MISSING_CASE(wp->cpp);
5414 return -EINVAL;
5415 }
5416 } else {
5417 wp->y_min_scanlines = 4;
5418 }
5419
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005420 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305421 wp->y_min_scanlines *= 2;
5422
5423 wp->plane_bytes_per_line = wp->width * wp->cpp;
5424 if (wp->y_tiled) {
5425 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005426 wp->y_min_scanlines,
5427 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305428
Matt Roper2b5a4562021-03-22 16:38:40 -07005429 if (DISPLAY_VER(dev_priv) >= 10)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305430 interm_pbpl++;
5431
5432 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5433 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305434 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005435 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005436 wp->dbuf_block_size);
5437
Matt Roper2b5a4562021-03-22 16:38:40 -07005438 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005439 interm_pbpl++;
5440
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305441 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5442 }
5443
5444 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5445 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005446
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305447 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005448 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305449
5450 return 0;
5451}
5452
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005453static int
5454skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5455 const struct intel_plane_state *plane_state,
5456 struct skl_wm_params *wp, int color_plane)
5457{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005458 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005459 int width;
5460
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005461 /*
5462 * Src coordinates are already rotated by 270 degrees for
5463 * the 90/270 degree plane rotation cases (to match the
5464 * GTT mapping), hence no need to account for rotation here.
5465 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005466 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005467
5468 return skl_compute_wm_params(crtc_state, width,
5469 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005470 plane_state->hw.rotation,
Ville Syrjälä3df3fe22020-11-06 19:30:42 +02005471 intel_plane_pixel_rate(crtc_state, plane_state),
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005472 wp, color_plane);
5473}
5474
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005475static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5476{
Matt Roper2b5a4562021-03-22 16:38:40 -07005477 if (DISPLAY_VER(dev_priv) >= 10)
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005478 return true;
5479
5480 /* The number of lines are ignored for the level 0 watermark. */
5481 return level > 0;
5482}
5483
Matt Roper1003cee2021-05-14 08:36:54 -07005484static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5485{
5486 if (DISPLAY_VER(dev_priv) >= 13)
5487 return 255;
5488 else
5489 return 31;
5490}
5491
Maarten Lankhorstec193642019-06-28 10:55:17 +02005492static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005493 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005494 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005495 const struct skl_wm_params *wp,
5496 const struct skl_wm_level *result_prev,
5497 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005498{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005499 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305500 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305501 uint_fixed_16_16_t selected_result;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005502 u32 blocks, lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005503
Ville Syrjälä0aded172019-02-05 17:50:53 +02005504 if (latency == 0) {
5505 /* reject it */
5506 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005507 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005508 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005509
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005510 /*
5511 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5512 * Display WA #1141: kbl,cfl
5513 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005514 if ((IS_KABYLAKE(dev_priv) ||
5515 IS_COFFEELAKE(dev_priv) ||
5516 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005517 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305518 latency += 4;
5519
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005520 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005521 latency += 15;
5522
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305523 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005524 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305525 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005526 crtc_state->hw.pipe_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005527 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305528 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005529
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305530 if (wp->y_tiled) {
5531 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005532 } else {
Maarten Lankhorstbafcdad2020-11-12 21:17:18 +02005533 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005534 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005535 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005536 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005537 } else if (latency >= wp->linetime_us) {
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005538 if (DISPLAY_VER(dev_priv) == 9)
Paulo Zanoni077b5822018-10-04 16:15:57 -07005539 selected_result = min_fixed16(method1, method2);
5540 else
5541 selected_result = method2;
5542 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005543 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005544 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005545 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005546
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005547 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5548 lines = div_round_up_fixed16(selected_result,
5549 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005550
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005551 if (DISPLAY_VER(dev_priv) == 9) {
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005552 /* Display WA #1125: skl,bxt,kbl */
5553 if (level == 0 && wp->rc_surface)
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005554 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005555
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005556 /* Display WA #1126: skl,bxt,kbl */
5557 if (level >= 1 && level <= 7) {
5558 if (wp->y_tiled) {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005559 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5560 lines += wp->y_min_scanlines;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005561 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005562 blocks++;
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005563 }
5564
5565 /*
5566 * Make sure result blocks for higher latency levels are
5567 * atleast as high as level below the current level.
5568 * Assumption in DDB algorithm optimization for special
5569 * cases. Also covers Display WA #1125 for RC.
5570 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005571 if (result_prev->blocks > blocks)
5572 blocks = result_prev->blocks;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005573 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005574 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005575
Matt Roper7dadd282021-03-19 21:42:43 -07005576 if (DISPLAY_VER(dev_priv) >= 11) {
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005577 if (wp->y_tiled) {
5578 int extra_lines;
5579
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005580 if (lines % wp->y_min_scanlines == 0)
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005581 extra_lines = wp->y_min_scanlines;
5582 else
5583 extra_lines = wp->y_min_scanlines * 2 -
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005584 lines % wp->y_min_scanlines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005585
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005586 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005587 wp->plane_blocks_per_line);
5588 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005589 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005590 }
5591 }
5592
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005593 if (!skl_wm_has_lines(dev_priv, level))
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005594 lines = 0;
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005595
Matt Roper1003cee2021-05-14 08:36:54 -07005596 if (lines > skl_wm_max_lines(dev_priv)) {
Ville Syrjälä0aded172019-02-05 17:50:53 +02005597 /* reject it */
5598 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005599 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005600 }
Matt Roperd8e87492018-12-11 09:31:07 -08005601
5602 /*
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005603 * If lines is valid, assume we can use this watermark level
Matt Roperd8e87492018-12-11 09:31:07 -08005604 * for now. We'll come back and disable it after we calculate the
5605 * DDB allocation if it turns out we don't actually have enough
5606 * blocks to satisfy it.
5607 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005608 result->blocks = blocks;
5609 result->lines = lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005610 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005611 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5612 result->enable = true;
Ville Syrjälä9c312122020-11-06 19:30:40 +02005613
Matt Roper7dadd282021-03-19 21:42:43 -07005614 if (DISPLAY_VER(dev_priv) < 12)
Ville Syrjälä9c312122020-11-06 19:30:40 +02005615 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005616}
5617
Matt Roperd8e87492018-12-11 09:31:07 -08005618static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005619skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305620 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005621 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005622{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005623 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305624 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005625 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005626
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305627 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005628 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005629 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305630
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005631 skl_compute_plane_wm(crtc_state, level, latency,
5632 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005633
5634 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305635 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005636}
5637
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005638static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5639 const struct skl_wm_params *wm_params,
5640 struct skl_plane_wm *plane_wm)
5641{
5642 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläa68aa482021-02-26 17:32:01 +02005643 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005644 struct skl_wm_level *levels = plane_wm->wm;
5645 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5646
5647 skl_compute_plane_wm(crtc_state, 0, latency,
5648 wm_params, &levels[0],
5649 sagv_wm);
5650}
5651
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005652static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5653 struct skl_wm_level *trans_wm,
5654 const struct skl_wm_level *wm0,
5655 const struct skl_wm_params *wp)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005656{
Ville Syrjäläc834d032020-02-28 22:35:52 +02005657 u16 trans_min, trans_amount, trans_y_tile_min;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005658 u16 wm0_blocks, trans_offset, blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005659
Kumar, Maheshca476672017-08-17 19:15:24 +05305660 /* Transition WM don't make any sense if ipc is disabled */
5661 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005662 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305663
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005664 /*
5665 * WaDisableTWM:skl,kbl,cfl,bxt
5666 * Transition WM are not recommended by HW team for GEN9
5667 */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005668 if (DISPLAY_VER(dev_priv) == 9)
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005669 return;
5670
Matt Roper7dadd282021-03-19 21:42:43 -07005671 if (DISPLAY_VER(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305672 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005673 else
5674 trans_min = 14;
5675
5676 /* Display WA #1140: glk,cnl */
Lucas De Marchi93e7e612021-04-12 22:09:53 -07005677 if (DISPLAY_VER(dev_priv) == 10)
Ville Syrjäläc834d032020-02-28 22:35:52 +02005678 trans_amount = 0;
5679 else
5680 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305681
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005682 trans_offset = trans_min + trans_amount;
Kumar, Maheshca476672017-08-17 19:15:24 +05305683
Paulo Zanonicbacc792018-10-04 16:15:58 -07005684 /*
5685 * The spec asks for Selected Result Blocks for wm0 (the real value),
5686 * not Result Blocks (the integer value). Pay attention to the capital
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005687 * letters. The value wm_l0->blocks is actually Result Blocks, but
Paulo Zanonicbacc792018-10-04 16:15:58 -07005688 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5689 * and since we later will have to get the ceiling of the sum in the
5690 * transition watermarks calculation, we can just pretend Selected
5691 * Result Blocks is Result Blocks minus 1 and it should work for the
5692 * current platforms.
5693 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005694 wm0_blocks = wm0->blocks - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005695
Kumar, Maheshca476672017-08-17 19:15:24 +05305696 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005697 trans_y_tile_min =
5698 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005699 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305700 } else {
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005701 blocks = wm0_blocks + trans_offset;
Kumar, Maheshca476672017-08-17 19:15:24 +05305702 }
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005703 blocks++;
Kumar, Maheshca476672017-08-17 19:15:24 +05305704
Matt Roperd8e87492018-12-11 09:31:07 -08005705 /*
5706 * Just assume we can enable the transition watermark. After
5707 * computing the DDB we'll come back and disable it if that
5708 * assumption turns out to be false.
5709 */
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005710 trans_wm->blocks = blocks;
5711 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5712 trans_wm->enable = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005713}
5714
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005715static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005716 const struct intel_plane_state *plane_state,
5717 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005718{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005719 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5720 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälädbf71382020-11-06 19:30:38 +02005721 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005722 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005723 int ret;
5724
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005725 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005726 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005727 if (ret)
5728 return ret;
5729
Ville Syrjälä67155a62019-03-12 22:58:37 +02005730 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005731
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005732 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5733 &wm->wm[0], &wm_params);
5734
Matt Roper7dadd282021-03-19 21:42:43 -07005735 if (DISPLAY_VER(dev_priv) >= 12) {
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005736 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5737
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005738 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5739 &wm->sagv.wm0, &wm_params);
5740 }
Ville Syrjälä83158472018-11-27 18:57:26 +02005741
5742 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005743}
5744
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005745static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005746 const struct intel_plane_state *plane_state,
5747 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005748{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005749 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005750 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005751 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005752
Ville Syrjälä83158472018-11-27 18:57:26 +02005753 wm->is_planar = true;
5754
5755 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005756 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005757 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005758 if (ret)
5759 return ret;
5760
Ville Syrjälä67155a62019-03-12 22:58:37 +02005761 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005762
5763 return 0;
5764}
5765
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005766static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005767 const struct intel_plane_state *plane_state)
5768{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005769 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä83158472018-11-27 18:57:26 +02005770 enum plane_id plane_id = plane->id;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005771 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5772 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005773 int ret;
5774
Ville Syrjälädbf71382020-11-06 19:30:38 +02005775 memset(wm, 0, sizeof(*wm));
5776
Ville Syrjälä83158472018-11-27 18:57:26 +02005777 if (!intel_wm_plane_visible(crtc_state, plane_state))
5778 return 0;
5779
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005780 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005781 plane_id, 0);
5782 if (ret)
5783 return ret;
5784
5785 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005786 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005787 plane_id);
5788 if (ret)
5789 return ret;
5790 }
5791
5792 return 0;
5793}
5794
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005795static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005796 const struct intel_plane_state *plane_state)
5797{
Ville Syrjälädbf71382020-11-06 19:30:38 +02005798 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5799 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5800 enum plane_id plane_id = plane->id;
5801 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
Ville Syrjälä83158472018-11-27 18:57:26 +02005802 int ret;
5803
5804 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005805 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005806 return 0;
5807
Ville Syrjäläf99b8052021-03-27 02:59:45 +02005808 memset(wm, 0, sizeof(*wm));
5809
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005810 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005811 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005812 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005813
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305814 drm_WARN_ON(&dev_priv->drm,
5815 !intel_wm_plane_visible(crtc_state, plane_state));
5816 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5817 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005818
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005819 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005820 y_plane_id, 0);
5821 if (ret)
5822 return ret;
5823
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005824 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005825 plane_id, 1);
5826 if (ret)
5827 return ret;
5828 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005829 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005830 plane_id, 0);
5831 if (ret)
5832 return ret;
5833 }
5834
5835 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005836}
5837
Ville Syrjäläffc90032020-11-06 19:30:37 +02005838static int skl_build_pipe_wm(struct intel_atomic_state *state,
5839 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005840{
Ville Syrjäläffc90032020-11-06 19:30:37 +02005841 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5842 struct intel_crtc_state *crtc_state =
5843 intel_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005844 const struct intel_plane_state *plane_state;
Ville Syrjälädbf71382020-11-06 19:30:38 +02005845 struct intel_plane *plane;
5846 int ret, i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005847
Ville Syrjälädbf71382020-11-06 19:30:38 +02005848 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5849 /*
5850 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5851 * instead but we don't populate that correctly for NV12 Y
5852 * planes so for now hack this.
5853 */
5854 if (plane->pipe != crtc->pipe)
5855 continue;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305856
Matt Roper7dadd282021-03-19 21:42:43 -07005857 if (DISPLAY_VER(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005858 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005859 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005860 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305861 if (ret)
5862 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005863 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305864
Ville Syrjälädbf71382020-11-06 19:30:38 +02005865 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5866
Matt Roper55994c22016-05-12 07:06:08 -07005867 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005868}
5869
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005870static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5871 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005872 const struct skl_ddb_entry *entry)
5873{
5874 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005875 intel_de_write_fw(dev_priv, reg,
5876 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005877 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005878 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005879}
5880
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005881static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5882 i915_reg_t reg,
5883 const struct skl_wm_level *level)
5884{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005885 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005886
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005887 if (level->enable)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005888 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005889 if (level->ignore_lines)
5890 val |= PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005891 val |= level->blocks;
Matt Roper1003cee2021-05-14 08:36:54 -07005892 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005893
Jani Nikula9b6320a2020-01-23 16:00:04 +02005894 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005895}
5896
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005897void skl_write_plane_wm(struct intel_plane *plane,
5898 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005899{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005900 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005901 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005902 enum plane_id plane_id = plane->id;
5903 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005904 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5905 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005906 const struct skl_ddb_entry *ddb_y =
5907 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5908 const struct skl_ddb_entry *ddb_uv =
5909 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005910
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005911 for (level = 0; level <= max_level; level++)
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005912 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005913 skl_plane_wm_level(pipe_wm, plane_id, level));
5914
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005915 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005916 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005917
Matt Roper7959ffe2021-05-18 17:06:11 -07005918 if (HAS_HW_SAGV_WM(dev_priv)) {
5919 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5920 &wm->sagv.wm0);
5921 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5922 &wm->sagv.trans_wm);
5923 }
5924
Matt Roper7dadd282021-03-19 21:42:43 -07005925 if (DISPLAY_VER(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005926 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005927 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5928 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305929 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005930
5931 if (wm->is_planar)
5932 swap(ddb_y, ddb_uv);
5933
5934 skl_ddb_entry_write(dev_priv,
5935 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5936 skl_ddb_entry_write(dev_priv,
5937 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005938}
5939
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005940void skl_write_cursor_wm(struct intel_plane *plane,
5941 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005942{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005943 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005944 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005945 enum plane_id plane_id = plane->id;
5946 enum pipe pipe = plane->pipe;
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005947 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005948 const struct skl_ddb_entry *ddb =
5949 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005950
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005951 for (level = 0; level <= max_level; level++)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005952 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02005953 skl_plane_wm_level(pipe_wm, plane_id, level));
5954
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005955 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5956 skl_plane_trans_wm(pipe_wm, plane_id));
Lyude27082492016-08-24 07:48:10 +02005957
Matt Roper7959ffe2021-05-18 17:06:11 -07005958 if (HAS_HW_SAGV_WM(dev_priv)) {
5959 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5960
5961 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5962 &wm->sagv.wm0);
5963 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5964 &wm->sagv.trans_wm);
5965 }
5966
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005967 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005968}
5969
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005970bool skl_wm_level_equals(const struct skl_wm_level *l1,
5971 const struct skl_wm_level *l2)
5972{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005973 return l1->enable == l2->enable &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005974 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjälä5dac8082021-03-05 17:36:10 +02005975 l1->lines == l2->lines &&
5976 l1->blocks == l2->blocks;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005977}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005978
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005979static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5980 const struct skl_plane_wm *wm1,
5981 const struct skl_plane_wm *wm2)
5982{
5983 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005984
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005985 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005986 /*
5987 * We don't check uv_wm as the hardware doesn't actually
5988 * use it. It only gets used for calculating the required
5989 * ddb allocation.
5990 */
5991 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005992 return false;
5993 }
5994
Ville Syrjäläf11449d2021-02-26 17:32:00 +02005995 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02005996 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5997 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005998}
5999
Jani Nikula81b55ef2020-04-20 17:04:38 +03006000static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
6001 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006002{
Lyude27082492016-08-24 07:48:10 +02006003 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006004}
6005
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006006static void skl_ddb_entry_union(struct skl_ddb_entry *a,
6007 const struct skl_ddb_entry *b)
6008{
6009 if (a->end && b->end) {
6010 a->start = min(a->start, b->start);
6011 a->end = max(a->end, b->end);
6012 } else if (b->end) {
6013 a->start = b->start;
6014 a->end = b->end;
6015 }
6016}
6017
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006018bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03006019 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006020 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006021{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006022 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006023
Ville Syrjälä53cc68802018-11-01 17:05:59 +02006024 for (i = 0; i < num_entries; i++) {
6025 if (i != ignore_idx &&
6026 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02006027 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03006028 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006029
Lyude27082492016-08-24 07:48:10 +02006030 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00006031}
6032
Jani Nikulabb7791b2016-10-04 12:29:17 +03006033static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006034skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
6035 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006036{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006037 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
6038 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006039 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6040 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006041
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006042 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6043 struct intel_plane_state *plane_state;
6044 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006045
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006046 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
6047 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
6048 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
6049 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006050 continue;
6051
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006052 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006053 if (IS_ERR(plane_state))
6054 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02006055
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006056 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006057 }
6058
6059 return 0;
6060}
6061
Ville Syrjäläef79d622021-01-22 22:56:32 +02006062static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
6063{
6064 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
6065 u8 enabled_slices;
6066 enum pipe pipe;
6067
6068 /*
6069 * FIXME: For now we always enable slice S1 as per
6070 * the Bspec display initialization sequence.
6071 */
6072 enabled_slices = BIT(DBUF_S1);
6073
6074 for_each_pipe(dev_priv, pipe)
6075 enabled_slices |= dbuf_state->slices[pipe];
6076
6077 return enabled_slices;
6078}
6079
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006080static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006081skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07006082{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006083 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6084 const struct intel_dbuf_state *old_dbuf_state;
Ville Syrjäläef79d622021-01-22 22:56:32 +02006085 struct intel_dbuf_state *new_dbuf_state = NULL;
Ville Syrjälä70b1a262020-02-25 19:11:16 +02006086 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006087 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306088 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306089 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07006090
Ville Syrjäläef79d622021-01-22 22:56:32 +02006091 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6092 new_dbuf_state = intel_atomic_get_dbuf_state(state);
6093 if (IS_ERR(new_dbuf_state))
6094 return PTR_ERR(new_dbuf_state);
6095
6096 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6097 break;
6098 }
6099
6100 if (!new_dbuf_state)
6101 return 0;
6102
6103 new_dbuf_state->active_pipes =
6104 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6105
6106 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
6107 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6108 if (ret)
6109 return ret;
6110 }
6111
6112 for_each_intel_crtc(&dev_priv->drm, crtc) {
6113 enum pipe pipe = crtc->pipe;
6114
6115 new_dbuf_state->slices[pipe] =
6116 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes);
6117
6118 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
6119 continue;
6120
6121 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6122 if (ret)
6123 return ret;
6124 }
6125
6126 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
6127
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006128 if (IS_ALDERLAKE_P(dev_priv))
6129 new_dbuf_state->joined_mbus = adlp_check_mbus_joined(new_dbuf_state->active_pipes);
6130
6131 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
6132 old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006133 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
6134 if (ret)
6135 return ret;
6136
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006137 if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6138 /* TODO: Implement vblank synchronized MBUS joining changes */
6139 ret = intel_modeset_all_pipes(state);
6140 if (ret)
6141 return ret;
6142 }
6143
Ville Syrjäläef79d622021-01-22 22:56:32 +02006144 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006145 "Enabled dbuf slices 0x%x -> 0x%x (total dbuf slices 0x%x), mbus joined? %s->%s\n",
Ville Syrjäläef79d622021-01-22 22:56:32 +02006146 old_dbuf_state->enabled_slices,
6147 new_dbuf_state->enabled_slices,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006148 INTEL_INFO(dev_priv)->dbuf.slice_mask,
6149 yesno(old_dbuf_state->joined_mbus),
6150 yesno(new_dbuf_state->joined_mbus));
Ville Syrjäläef79d622021-01-22 22:56:32 +02006151 }
6152
6153 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6154 enum pipe pipe = crtc->pipe;
6155
6156 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
6157
6158 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
6159 continue;
6160
6161 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6162 if (ret)
6163 return ret;
6164 }
6165
6166 for_each_intel_crtc(&dev_priv->drm, crtc) {
6167 ret = skl_crtc_allocate_ddb(state, crtc);
6168 if (ret)
6169 return ret;
6170 }
6171
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006172 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006173 new_crtc_state, i) {
Ville Syrjäläef79d622021-01-22 22:56:32 +02006174 ret = skl_allocate_plane_ddb(state, crtc);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006175 if (ret)
6176 return ret;
6177
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006178 ret = skl_ddb_add_affected_planes(old_crtc_state,
6179 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07006180 if (ret)
6181 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07006182 }
6183
6184 return 0;
6185}
6186
Ville Syrjäläab98e942019-02-08 22:05:27 +02006187static char enast(bool enable)
6188{
6189 return enable ? '*' : ' ';
6190}
6191
Matt Roper2722efb2016-08-17 15:55:55 -04006192static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006193skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006194{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006195 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6196 const struct intel_crtc_state *old_crtc_state;
6197 const struct intel_crtc_state *new_crtc_state;
6198 struct intel_plane *plane;
6199 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01006200 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006201
Jani Nikulabdbf43d2019-10-28 12:38:15 +02006202 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02006203 return;
6204
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006205 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6206 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02006207 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
6208
6209 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
6210 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
6211
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006212 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6213 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006214 const struct skl_ddb_entry *old, *new;
6215
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006216 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
6217 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006218
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006219 if (skl_ddb_entry_equal(old, new))
6220 continue;
6221
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006222 drm_dbg_kms(&dev_priv->drm,
6223 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
6224 plane->base.base.id, plane->base.name,
6225 old->start, old->end, new->start, new->end,
6226 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006227 }
6228
6229 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6230 enum plane_id plane_id = plane->id;
6231 const struct skl_plane_wm *old_wm, *new_wm;
6232
6233 old_wm = &old_pipe_wm->planes[plane_id];
6234 new_wm = &new_pipe_wm->planes[plane_id];
6235
6236 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
6237 continue;
6238
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006239 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006240 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
6241 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006242 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006243 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
6244 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
6245 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
6246 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
6247 enast(old_wm->trans_wm.enable),
6248 enast(old_wm->sagv.wm0.enable),
6249 enast(old_wm->sagv.trans_wm.enable),
6250 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
6251 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
6252 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
6253 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
6254 enast(new_wm->trans_wm.enable),
6255 enast(new_wm->sagv.wm0.enable),
6256 enast(new_wm->sagv.trans_wm.enable));
Ville Syrjäläab98e942019-02-08 22:05:27 +02006257
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006258 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006259 "[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d"
6260 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006261 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006262 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
6263 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
6264 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
6265 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
6266 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
6267 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
6268 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
6269 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
6270 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
6271 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
6272 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
6273 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
6274 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
6275 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
6276 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
6277 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
6278 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
6279 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
6280 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
6281 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
6282 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
6283 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006284
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006285 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006286 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6287 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006288 plane->base.base.id, plane->base.name,
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006289 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
6290 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
6291 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
6292 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
6293 old_wm->trans_wm.blocks,
6294 old_wm->sagv.wm0.blocks,
6295 old_wm->sagv.trans_wm.blocks,
6296 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
6297 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
6298 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
6299 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
6300 new_wm->trans_wm.blocks,
6301 new_wm->sagv.wm0.blocks,
6302 new_wm->sagv.trans_wm.blocks);
Ville Syrjäläab98e942019-02-08 22:05:27 +02006303
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006304 drm_dbg_kms(&dev_priv->drm,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006305 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6306 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006307 plane->base.base.id, plane->base.name,
6308 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6309 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6310 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6311 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6312 old_wm->trans_wm.min_ddb_alloc,
Ville Syrjäläa68aa482021-02-26 17:32:01 +02006313 old_wm->sagv.wm0.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006314 old_wm->sagv.trans_wm.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006315 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6316 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6317 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6318 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006319 new_wm->trans_wm.min_ddb_alloc,
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006320 new_wm->sagv.wm0.min_ddb_alloc,
6321 new_wm->sagv.trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006322 }
6323 }
6324}
6325
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006326static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6327 const struct skl_pipe_wm *old_pipe_wm,
6328 const struct skl_pipe_wm *new_pipe_wm)
6329{
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006330 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6331 int level, max_level = ilk_wm_max_level(i915);
6332
6333 for (level = 0; level <= max_level; level++) {
6334 /*
6335 * We don't check uv_wm as the hardware doesn't actually
6336 * use it. It only gets used for calculating the required
6337 * ddb allocation.
6338 */
Ville Syrjälä93fe8622021-03-25 02:44:14 +02006339 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6340 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006341 return false;
6342 }
6343
Matt Roper7959ffe2021-05-18 17:06:11 -07006344 if (HAS_HW_SAGV_WM(i915)) {
6345 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6346 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6347
6348 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6349 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6350 return false;
6351 }
6352
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006353 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6354 skl_plane_trans_wm(new_pipe_wm, plane->id));
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006355}
6356
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006357/*
6358 * To make sure the cursor watermark registers are always consistent
6359 * with our computed state the following scenario needs special
6360 * treatment:
6361 *
6362 * 1. enable cursor
6363 * 2. move cursor entirely offscreen
6364 * 3. disable cursor
6365 *
6366 * Step 2. does call .disable_plane() but does not zero the watermarks
6367 * (since we consider an offscreen cursor still active for the purposes
6368 * of watermarks). Step 3. would not normally call .disable_plane()
6369 * because the actual plane visibility isn't changing, and we don't
6370 * deallocate the cursor ddb until the pipe gets disabled. So we must
6371 * force step 3. to call .disable_plane() to update the watermark
6372 * registers properly.
6373 *
6374 * Other planes do not suffer from this issues as their watermarks are
6375 * calculated based on the actual plane visibility. The only time this
6376 * can trigger for the other planes is during the initial readout as the
6377 * default value of the watermarks registers is not zero.
6378 */
6379static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6380 struct intel_crtc *crtc)
6381{
6382 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6383 const struct intel_crtc_state *old_crtc_state =
6384 intel_atomic_get_old_crtc_state(state, crtc);
6385 struct intel_crtc_state *new_crtc_state =
6386 intel_atomic_get_new_crtc_state(state, crtc);
6387 struct intel_plane *plane;
6388
6389 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6390 struct intel_plane_state *plane_state;
6391 enum plane_id plane_id = plane->id;
6392
6393 /*
6394 * Force a full wm update for every plane on modeset.
6395 * Required because the reset value of the wm registers
6396 * is non-zero, whereas we want all disabled planes to
6397 * have zero watermarks. So if we turn off the relevant
6398 * power well the hardware state will go out of sync
6399 * with the software state.
6400 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006401 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjälä2871b2f2021-02-26 17:31:58 +02006402 skl_plane_selected_wm_equals(plane,
6403 &old_crtc_state->wm.skl.optimal,
6404 &new_crtc_state->wm.skl.optimal))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006405 continue;
6406
6407 plane_state = intel_atomic_get_plane_state(state, plane);
6408 if (IS_ERR(plane_state))
6409 return PTR_ERR(plane_state);
6410
6411 new_crtc_state->update_planes |= BIT(plane_id);
6412 }
6413
6414 return 0;
6415}
6416
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306417static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006418skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306419{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006420 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006421 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306422 int ret, i;
6423
Ville Syrjäläffc90032020-11-06 19:30:37 +02006424 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6425 ret = skl_build_pipe_wm(state, crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006426 if (ret)
6427 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006428 }
6429
Matt Roperd8e87492018-12-11 09:31:07 -08006430 ret = skl_compute_ddb(state);
6431 if (ret)
6432 return ret;
6433
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006434 ret = intel_compute_sagv_mask(state);
6435 if (ret)
6436 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006437
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006438 /*
6439 * skl_compute_ddb() will have adjusted the final watermarks
6440 * based on how much ddb is available. Now we can actually
6441 * check if the final watermarks changed.
6442 */
Ville Syrjäläffc90032020-11-06 19:30:37 +02006443 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006444 ret = skl_wm_add_affected_planes(state, crtc);
6445 if (ret)
6446 return ret;
6447 }
6448
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006449 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006450
Matt Roper98d39492016-05-12 07:06:03 -07006451 return 0;
6452}
6453
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006454static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006455 struct intel_wm_config *config)
6456{
6457 struct intel_crtc *crtc;
6458
6459 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006460 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006461 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6462
6463 if (!wm->pipe_enabled)
6464 continue;
6465
6466 config->sprites_enabled |= wm->sprites_enabled;
6467 config->sprites_scaled |= wm->sprites_scaled;
6468 config->num_pipes_active++;
6469 }
6470}
6471
Matt Ropered4a6a72016-02-23 17:20:13 -08006472static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006473{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006474 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006475 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006476 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006477 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006478 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006479
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006480 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006481
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006482 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6483 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006484
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006485 /* 5/6 split only in single pipe config on IVB+ */
Matt Roper7dadd282021-03-19 21:42:43 -07006486 if (DISPLAY_VER(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006487 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006488 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6489 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006490
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006491 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006492 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006493 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006494 }
6495
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006496 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006497 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006498
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006499 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006500
Imre Deak820c1982013-12-17 14:46:36 +02006501 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006502}
6503
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006504static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006505 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006506{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006507 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6508 const struct intel_crtc_state *crtc_state =
6509 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006510
Matt Ropered4a6a72016-02-23 17:20:13 -08006511 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006512 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006513 ilk_program_watermarks(dev_priv);
6514 mutex_unlock(&dev_priv->wm.wm_mutex);
6515}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006516
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006517static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006518 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006519{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006520 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6521 const struct intel_crtc_state *crtc_state =
6522 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006523
6524 if (!crtc_state->wm.need_postvbl_update)
6525 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006526
6527 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006528 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6529 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006530 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006531}
6532
Jani Nikula81b55ef2020-04-20 17:04:38 +03006533static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006534{
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006535 level->enable = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006536 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
Ville Syrjälä5dac8082021-03-05 17:36:10 +02006537 level->blocks = val & PLANE_WM_BLOCKS_MASK;
Matt Roper1003cee2021-05-14 08:36:54 -07006538 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
Pradeep Bhat30789992014-11-04 17:06:45 +00006539}
6540
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006541void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006542 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006543{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006544 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6545 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006546 int level, max_level;
6547 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006548 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006549
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006550 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006551
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006552 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006553 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006554
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006555 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006556 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006557 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006558 else
Jani Nikula5f461662020-11-30 13:15:58 +02006559 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006560
6561 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6562 }
6563
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006564 if (plane_id != PLANE_CURSOR)
Jani Nikula5f461662020-11-30 13:15:58 +02006565 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006566 else
Jani Nikula5f461662020-11-30 13:15:58 +02006567 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006568
6569 skl_wm_level_from_reg_val(val, &wm->trans_wm);
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006570
Matt Roper7959ffe2021-05-18 17:06:11 -07006571 if (HAS_HW_SAGV_WM(dev_priv)) {
6572 if (plane_id != PLANE_CURSOR)
6573 val = intel_uncore_read(&dev_priv->uncore,
6574 PLANE_WM_SAGV(pipe, plane_id));
6575 else
6576 val = intel_uncore_read(&dev_priv->uncore,
6577 CUR_WM_SAGV(pipe));
6578
6579 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6580
6581 if (plane_id != PLANE_CURSOR)
6582 val = intel_uncore_read(&dev_priv->uncore,
6583 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6584 else
6585 val = intel_uncore_read(&dev_priv->uncore,
6586 CUR_WM_SAGV_TRANS(pipe));
6587
6588 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6589 } else if (DISPLAY_VER(dev_priv) >= 12) {
Ville Syrjälä5f25e6a2021-02-26 17:32:02 +02006590 wm->sagv.wm0 = wm->wm[0];
6591 wm->sagv.trans_wm = wm->trans_wm;
6592 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006593 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006594}
6595
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006596void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006597{
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006598 struct intel_dbuf_state *dbuf_state =
6599 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006600 struct intel_crtc *crtc;
Pradeep Bhat30789992014-11-04 17:06:45 +00006601
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006602 if (IS_ALDERLAKE_P(dev_priv))
6603 dbuf_state->joined_mbus = intel_de_read(dev_priv, MBUS_CTL) & MBUS_JOIN;
6604
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006605 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006606 struct intel_crtc_state *crtc_state =
6607 to_intel_crtc_state(crtc->base.state);
6608 enum pipe pipe = crtc->pipe;
Ville Syrjälä835c1762021-05-18 17:06:16 -07006609 unsigned int mbus_offset;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006610 enum plane_id plane_id;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006611
Maarten Lankhorstec193642019-06-28 10:55:17 +02006612 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
Ville Syrjälädbf71382020-11-06 19:30:38 +02006613 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006614
6615 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6616
6617 for_each_plane_id_on_crtc(crtc, plane_id) {
6618 struct skl_ddb_entry *ddb_y =
6619 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6620 struct skl_ddb_entry *ddb_uv =
6621 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
6622
6623 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6624 plane_id, ddb_y, ddb_uv);
6625
6626 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6627 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_uv);
6628 }
6629
6630 dbuf_state->slices[pipe] =
6631 skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes);
6632
6633 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6634
Ville Syrjälä835c1762021-05-18 17:06:16 -07006635 /*
6636 * Used for checking overlaps, so we need absolute
6637 * offsets instead of MBUS relative offsets.
6638 */
6639 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_state->slices[pipe]);
6640 crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
6641 crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006642
6643 drm_dbg_kms(&dev_priv->drm,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006644 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x, mbus joined: %s\n",
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006645 crtc->base.base.id, crtc->base.name,
6646 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07006647 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
6648 yesno(dbuf_state->joined_mbus));
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006649 }
Ville Syrjälä33c9c502021-01-22 22:56:33 +02006650
6651 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
Pradeep Bhat30789992014-11-04 17:06:45 +00006652}
6653
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006654static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006655{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006656 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006657 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006658 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006659 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6660 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006661 enum pipe pipe = crtc->pipe;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006662
Jani Nikula5f461662020-11-30 13:15:58 +02006663 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006664
Ville Syrjälä15606532016-05-13 17:55:17 +03006665 memset(active, 0, sizeof(*active));
6666
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006667 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006668
6669 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006670 u32 tmp = hw->wm_pipe[pipe];
6671
6672 /*
6673 * For active pipes LP0 watermark is marked as
6674 * enabled, and LP1+ watermaks as disabled since
6675 * we can't really reverse compute them in case
6676 * multiple pipes are active.
6677 */
6678 active->wm[0].enable = true;
6679 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6680 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6681 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006682 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006683 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006684
6685 /*
6686 * For inactive pipes, all watermark levels
6687 * should be marked as enabled but zeroed,
6688 * which is what we'd compute them to.
6689 */
6690 for (level = 0; level <= max_level; level++)
6691 active->wm[level].enable = true;
6692 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006693
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006694 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006695}
6696
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006697#define _FW_WM(value, plane) \
6698 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6699#define _FW_WM_VLV(value, plane) \
6700 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6701
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006702static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6703 struct g4x_wm_values *wm)
6704{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006705 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006706
Jani Nikula5f461662020-11-30 13:15:58 +02006707 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006708 wm->sr.plane = _FW_WM(tmp, SR);
6709 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6710 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6711 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6712
Jani Nikula5f461662020-11-30 13:15:58 +02006713 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006714 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6715 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6716 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6717 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6718 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6719 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6720
Jani Nikula5f461662020-11-30 13:15:58 +02006721 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006722 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6723 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6724 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6725 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6726}
6727
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006728static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6729 struct vlv_wm_values *wm)
6730{
6731 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006732 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006733
6734 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02006735 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006736
Ville Syrjälä1b313892016-11-28 19:37:08 +02006737 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006738 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006739 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006740 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006741 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006742 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006743 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006744 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6745 }
6746
Jani Nikula5f461662020-11-30 13:15:58 +02006747 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006748 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006749 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6750 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6751 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006752
Jani Nikula5f461662020-11-30 13:15:58 +02006753 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006754 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6755 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6756 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006757
Jani Nikula5f461662020-11-30 13:15:58 +02006758 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006759 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6760
6761 if (IS_CHERRYVIEW(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02006762 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006763 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6764 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006765
Jani Nikula5f461662020-11-30 13:15:58 +02006766 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006767 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6768 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006769
Jani Nikula5f461662020-11-30 13:15:58 +02006770 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006771 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6772 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006773
Jani Nikula5f461662020-11-30 13:15:58 +02006774 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006775 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006776 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6777 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6778 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6779 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6780 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6781 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6782 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6783 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6784 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006785 } else {
Jani Nikula5f461662020-11-30 13:15:58 +02006786 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006787 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6788 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006789
Jani Nikula5f461662020-11-30 13:15:58 +02006790 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006791 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006792 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6793 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6794 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6795 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6796 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6797 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006798 }
6799}
6800
6801#undef _FW_WM
6802#undef _FW_WM_VLV
6803
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006804void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006805{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006806 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6807 struct intel_crtc *crtc;
6808
6809 g4x_read_wm_values(dev_priv, wm);
6810
Jani Nikula5f461662020-11-30 13:15:58 +02006811 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006812
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006813 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006814 struct intel_crtc_state *crtc_state =
6815 to_intel_crtc_state(crtc->base.state);
6816 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6817 struct g4x_pipe_wm *raw;
6818 enum pipe pipe = crtc->pipe;
6819 enum plane_id plane_id;
6820 int level, max_level;
6821
6822 active->cxsr = wm->cxsr;
6823 active->hpll_en = wm->hpll_en;
6824 active->fbc_en = wm->fbc_en;
6825
6826 active->sr = wm->sr;
6827 active->hpll = wm->hpll;
6828
6829 for_each_plane_id_on_crtc(crtc, plane_id) {
6830 active->wm.plane[plane_id] =
6831 wm->pipe[pipe].plane[plane_id];
6832 }
6833
6834 if (wm->cxsr && wm->hpll_en)
6835 max_level = G4X_WM_LEVEL_HPLL;
6836 else if (wm->cxsr)
6837 max_level = G4X_WM_LEVEL_SR;
6838 else
6839 max_level = G4X_WM_LEVEL_NORMAL;
6840
6841 level = G4X_WM_LEVEL_NORMAL;
6842 raw = &crtc_state->wm.g4x.raw[level];
6843 for_each_plane_id_on_crtc(crtc, plane_id)
6844 raw->plane[plane_id] = active->wm.plane[plane_id];
6845
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006846 level = G4X_WM_LEVEL_SR;
6847 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006848 goto out;
6849
6850 raw = &crtc_state->wm.g4x.raw[level];
6851 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6852 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6853 raw->plane[PLANE_SPRITE0] = 0;
6854 raw->fbc = active->sr.fbc;
6855
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006856 level = G4X_WM_LEVEL_HPLL;
6857 if (level > max_level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006858 goto out;
6859
6860 raw = &crtc_state->wm.g4x.raw[level];
6861 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6862 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6863 raw->plane[PLANE_SPRITE0] = 0;
6864 raw->fbc = active->hpll.fbc;
6865
Ville Syrjäläab98ebb2021-05-14 15:57:42 +03006866 level++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006867 out:
6868 for_each_plane_id_on_crtc(crtc, plane_id)
6869 g4x_raw_plane_wm_set(crtc_state, level,
6870 plane_id, USHRT_MAX);
6871 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6872
6873 crtc_state->wm.g4x.optimal = *active;
6874 crtc_state->wm.g4x.intermediate = *active;
6875
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006876 drm_dbg_kms(&dev_priv->drm,
6877 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6878 pipe_name(pipe),
6879 wm->pipe[pipe].plane[PLANE_PRIMARY],
6880 wm->pipe[pipe].plane[PLANE_CURSOR],
6881 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006882 }
6883
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006884 drm_dbg_kms(&dev_priv->drm,
6885 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6886 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6887 drm_dbg_kms(&dev_priv->drm,
6888 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6889 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6890 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6891 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006892}
6893
6894void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6895{
6896 struct intel_plane *plane;
6897 struct intel_crtc *crtc;
6898
6899 mutex_lock(&dev_priv->wm.wm_mutex);
6900
6901 for_each_intel_plane(&dev_priv->drm, plane) {
6902 struct intel_crtc *crtc =
6903 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6904 struct intel_crtc_state *crtc_state =
6905 to_intel_crtc_state(crtc->base.state);
6906 struct intel_plane_state *plane_state =
6907 to_intel_plane_state(plane->base.state);
6908 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6909 enum plane_id plane_id = plane->id;
6910 int level;
6911
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006912 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006913 continue;
6914
6915 for (level = 0; level < 3; level++) {
6916 struct g4x_pipe_wm *raw =
6917 &crtc_state->wm.g4x.raw[level];
6918
6919 raw->plane[plane_id] = 0;
6920 wm_state->wm.plane[plane_id] = 0;
6921 }
6922
6923 if (plane_id == PLANE_PRIMARY) {
6924 for (level = 0; level < 3; level++) {
6925 struct g4x_pipe_wm *raw =
6926 &crtc_state->wm.g4x.raw[level];
6927 raw->fbc = 0;
6928 }
6929
6930 wm_state->sr.fbc = 0;
6931 wm_state->hpll.fbc = 0;
6932 wm_state->fbc_en = false;
6933 }
6934 }
6935
6936 for_each_intel_crtc(&dev_priv->drm, crtc) {
6937 struct intel_crtc_state *crtc_state =
6938 to_intel_crtc_state(crtc->base.state);
6939
6940 crtc_state->wm.g4x.intermediate =
6941 crtc_state->wm.g4x.optimal;
6942 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6943 }
6944
6945 g4x_program_watermarks(dev_priv);
6946
6947 mutex_unlock(&dev_priv->wm.wm_mutex);
6948}
6949
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006950void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006951{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006952 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006953 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006954 u32 val;
6955
6956 vlv_read_wm_values(dev_priv, wm);
6957
Jani Nikula5f461662020-11-30 13:15:58 +02006958 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006959 wm->level = VLV_WM_LEVEL_PM2;
6960
6961 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006962 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006963
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006964 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006965 if (val & DSP_MAXFIFO_PM5_ENABLE)
6966 wm->level = VLV_WM_LEVEL_PM5;
6967
Ville Syrjälä58590c12015-09-08 21:05:12 +03006968 /*
6969 * If DDR DVFS is disabled in the BIOS, Punit
6970 * will never ack the request. So if that happens
6971 * assume we don't have to enable/disable DDR DVFS
6972 * dynamically. To test that just set the REQ_ACK
6973 * bit to poke the Punit, but don't change the
6974 * HIGH/LOW bits so that we don't actually change
6975 * the current state.
6976 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006977 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006978 val |= FORCE_DDR_FREQ_REQ_ACK;
6979 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6980
6981 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6982 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006983 drm_dbg_kms(&dev_priv->drm,
6984 "Punit not acking DDR DVFS request, "
6985 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006986 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6987 } else {
6988 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6989 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6990 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6991 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006992
Chris Wilson337fa6e2019-04-26 09:17:20 +01006993 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006994 }
6995
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006996 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006997 struct intel_crtc_state *crtc_state =
6998 to_intel_crtc_state(crtc->base.state);
6999 struct vlv_wm_state *active = &crtc->wm.active.vlv;
7000 const struct vlv_fifo_state *fifo_state =
7001 &crtc_state->wm.vlv.fifo_state;
7002 enum pipe pipe = crtc->pipe;
7003 enum plane_id plane_id;
7004 int level;
7005
7006 vlv_get_fifo_size(crtc_state);
7007
7008 active->num_levels = wm->level + 1;
7009 active->cxsr = wm->cxsr;
7010
Ville Syrjäläff32c542017-03-02 19:14:57 +02007011 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03007012 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02007013 &crtc_state->wm.vlv.raw[level];
7014
7015 active->sr[level].plane = wm->sr.plane;
7016 active->sr[level].cursor = wm->sr.cursor;
7017
7018 for_each_plane_id_on_crtc(crtc, plane_id) {
7019 active->wm[level].plane[plane_id] =
7020 wm->pipe[pipe].plane[plane_id];
7021
7022 raw->plane[plane_id] =
7023 vlv_invert_wm_value(active->wm[level].plane[plane_id],
7024 fifo_state->plane[plane_id]);
7025 }
7026 }
7027
7028 for_each_plane_id_on_crtc(crtc, plane_id)
7029 vlv_raw_plane_wm_set(crtc_state, level,
7030 plane_id, USHRT_MAX);
7031 vlv_invalidate_wms(crtc, active, level);
7032
7033 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007034 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007035
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007036 drm_dbg_kms(&dev_priv->drm,
7037 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
7038 pipe_name(pipe),
7039 wm->pipe[pipe].plane[PLANE_PRIMARY],
7040 wm->pipe[pipe].plane[PLANE_CURSOR],
7041 wm->pipe[pipe].plane[PLANE_SPRITE0],
7042 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007043 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007044
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007045 drm_dbg_kms(&dev_priv->drm,
7046 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
7047 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03007048}
7049
Ville Syrjälä602ae832017-03-02 19:15:02 +02007050void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
7051{
7052 struct intel_plane *plane;
7053 struct intel_crtc *crtc;
7054
7055 mutex_lock(&dev_priv->wm.wm_mutex);
7056
7057 for_each_intel_plane(&dev_priv->drm, plane) {
7058 struct intel_crtc *crtc =
7059 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
7060 struct intel_crtc_state *crtc_state =
7061 to_intel_crtc_state(crtc->base.state);
7062 struct intel_plane_state *plane_state =
7063 to_intel_plane_state(plane->base.state);
7064 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
7065 const struct vlv_fifo_state *fifo_state =
7066 &crtc_state->wm.vlv.fifo_state;
7067 enum plane_id plane_id = plane->id;
7068 int level;
7069
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01007070 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02007071 continue;
7072
7073 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03007074 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02007075 &crtc_state->wm.vlv.raw[level];
7076
7077 raw->plane[plane_id] = 0;
7078
7079 wm_state->wm[level].plane[plane_id] =
7080 vlv_invert_wm_value(raw->plane[plane_id],
7081 fifo_state->plane[plane_id]);
7082 }
7083 }
7084
7085 for_each_intel_crtc(&dev_priv->drm, crtc) {
7086 struct intel_crtc_state *crtc_state =
7087 to_intel_crtc_state(crtc->base.state);
7088
7089 crtc_state->wm.vlv.intermediate =
7090 crtc_state->wm.vlv.optimal;
7091 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
7092 }
7093
7094 vlv_program_watermarks(dev_priv);
7095
7096 mutex_unlock(&dev_priv->wm.wm_mutex);
7097}
7098
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007099/*
7100 * FIXME should probably kill this and improve
7101 * the real watermark readout/sanitation instead
7102 */
7103static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
7104{
Jani Nikula5f461662020-11-30 13:15:58 +02007105 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM1_LP_SR_EN);
7106 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM1_LP_SR_EN);
7107 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 +02007108
7109 /*
7110 * Don't touch WM1S_LP_EN here.
7111 * Doing so could cause underruns.
7112 */
7113}
7114
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007115void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007116{
Imre Deak820c1982013-12-17 14:46:36 +02007117 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007118 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007119
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02007120 ilk_init_lp_watermarks(dev_priv);
7121
Matt Ropercd1d3ee2018-12-10 13:54:14 -08007122 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007123 ilk_pipe_wm_get_hw_state(crtc);
7124
Jani Nikula5f461662020-11-30 13:15:58 +02007125 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
7126 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
7127 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007128
Jani Nikula5f461662020-11-30 13:15:58 +02007129 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
Matt Roper7dadd282021-03-19 21:42:43 -07007130 if (DISPLAY_VER(dev_priv) >= 7) {
Jani Nikula5f461662020-11-30 13:15:58 +02007131 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
7132 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02007133 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007134
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007135 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007136 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 +02007137 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01007138 else if (IS_IVYBRIDGE(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007139 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 +02007140 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007141
7142 hw->enable_fbc_wm =
Jani Nikula5f461662020-11-30 13:15:58 +02007143 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03007144}
7145
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307146void intel_enable_ipc(struct drm_i915_private *dev_priv)
7147{
7148 u32 val;
7149
José Roberto de Souzafd847b82018-09-18 13:47:11 -07007150 if (!HAS_IPC(dev_priv))
7151 return;
7152
Jani Nikula5f461662020-11-30 13:15:58 +02007153 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307154
7155 if (dev_priv->ipc_enabled)
7156 val |= DISP_IPC_ENABLE;
7157 else
7158 val &= ~DISP_IPC_ENABLE;
7159
Jani Nikula5f461662020-11-30 13:15:58 +02007160 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307161}
7162
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007163static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
7164{
7165 /* Display WA #0477 WaDisableIPC: skl */
7166 if (IS_SKYLAKE(dev_priv))
7167 return false;
7168
7169 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01007170 if (IS_KABYLAKE(dev_priv) ||
7171 IS_COFFEELAKE(dev_priv) ||
7172 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007173 return dev_priv->dram_info.symmetric_memory;
7174
7175 return true;
7176}
7177
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307178void intel_init_ipc(struct drm_i915_private *dev_priv)
7179{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307180 if (!HAS_IPC(dev_priv))
7181 return;
7182
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03007183 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07007184
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05307185 intel_enable_ipc(dev_priv);
7186}
7187
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007188static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007189{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007190 /*
7191 * On Ibex Peak and Cougar Point, we need to disable clock
7192 * gating for the panel power sequencer or it will fail to
7193 * start up when no ports are active.
7194 */
Jani Nikula5f461662020-11-30 13:15:58 +02007195 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007196}
7197
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007198static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007199{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007200 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007201
Damien Lespiau055e3932014-08-18 13:49:10 +01007202 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007203 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
7204 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007205 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007206
Jani Nikula5f461662020-11-30 13:15:58 +02007207 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
7208 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007209 }
7210}
7211
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007212static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007213{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007214 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007215
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007216 /*
7217 * Required for FBC
7218 * WaFbcDisableDpfcClockGating:ilk
7219 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007220 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7221 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7222 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007223
Jani Nikula5f461662020-11-30 13:15:58 +02007224 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007225 MARIUNIT_CLOCK_GATE_DISABLE |
7226 SVSMUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007227 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007228 VFMUNIT_CLOCK_GATE_DISABLE);
7229
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007230 /*
7231 * According to the spec the following bits should be set in
7232 * order to enable memory self-refresh
7233 * The bit 22/21 of 0x42004
7234 * The bit 5 of 0x42020
7235 * The bit 15 of 0x45000
7236 */
Jani Nikula5f461662020-11-30 13:15:58 +02007237 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7238 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007239 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007240 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007241 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
7242 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007243 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007244
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007245 /*
7246 * Based on the document from hardware guys the following bits
7247 * should be set unconditionally in order to enable FBC.
7248 * The bit 22 of 0x42000
7249 * The bit 22 of 0x42004
7250 * The bit 7,8,9 of 0x42020.
7251 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007252 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007253 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Jani Nikula5f461662020-11-30 13:15:58 +02007254 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7255 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007256 ILK_FBCQ_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007257 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7258 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007259 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007260 }
7261
Jani Nikula5f461662020-11-30 13:15:58 +02007262 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007263
Jani Nikula5f461662020-11-30 13:15:58 +02007264 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7265 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007266 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05307267
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007268 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007269
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007270 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007271}
7272
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007273static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007274{
Ville Syrjäläd048a262019-08-21 20:30:31 +03007275 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007276 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007277
7278 /*
7279 * On Ibex Peak and Cougar Point, we need to disable clock
7280 * gating for the panel power sequencer or it will fail to
7281 * start up when no ports are active.
7282 */
Jani Nikula5f461662020-11-30 13:15:58 +02007283 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnescd664072013-10-02 10:34:19 -07007284 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7285 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007286 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
Daniel Vetter3107bd42012-10-31 22:52:31 +01007287 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007288 /* The below fixes the weird display corruption, a few pixels shifted
7289 * downward, on (only) LVDS of some HP laptops with IVY.
7290 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007291 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007292 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007293 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7294 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007295 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007296 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007297 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7298 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Jani Nikula5f461662020-11-30 13:15:58 +02007299 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007300 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007301 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007302 for_each_pipe(dev_priv, pipe) {
Jani Nikula5f461662020-11-30 13:15:58 +02007303 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
Daniel Vetter3107bd42012-10-31 22:52:31 +01007304 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7305 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007306}
7307
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007308static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007309{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007310 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007311
Jani Nikula5f461662020-11-30 13:15:58 +02007312 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007313 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007314 drm_dbg_kms(&dev_priv->drm,
7315 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7316 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007317}
7318
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007319static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007320{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007321 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007322
Jani Nikula5f461662020-11-30 13:15:58 +02007323 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007324
Jani Nikula5f461662020-11-30 13:15:58 +02007325 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7326 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007327 ILK_ELPIN_409_SELECT);
7328
Jani Nikula5f461662020-11-30 13:15:58 +02007329 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7330 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007331 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7332 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7333
7334 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7335 * gating disable must be set. Failure to set it results in
7336 * flickering pixels due to Z write ordering failures after
7337 * some amount of runtime in the Mesa "fire" demo, and Unigine
7338 * Sanctuary and Tropics, and apparently anything else with
7339 * alpha test or pixel discard.
7340 *
7341 * According to the spec, bit 11 (RCCUNIT) must also be set,
7342 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007343 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007344 * WaDisableRCCUnitClockGating:snb
7345 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007346 */
Jani Nikula5f461662020-11-30 13:15:58 +02007347 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007348 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7349 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7350
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007351 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007352 * According to the spec the following bits should be
7353 * set in order to enable memory self-refresh and fbc:
7354 * The bit21 and bit22 of 0x42000
7355 * The bit21 and bit22 of 0x42004
7356 * The bit5 and bit7 of 0x42020
7357 * The bit14 of 0x70180
7358 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007359 *
7360 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007361 */
Jani Nikula5f461662020-11-30 13:15:58 +02007362 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7363 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007364 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
Jani Nikula5f461662020-11-30 13:15:58 +02007365 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7366 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007367 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Jani Nikula5f461662020-11-30 13:15:58 +02007368 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7369 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
Damien Lespiau231e54f2012-10-19 17:55:41 +01007370 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7371 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007372
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007373 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007374
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007375 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007376
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007377 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007378}
7379
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007380static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007381{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007382 /*
7383 * TODO: this bit should only be enabled when really needed, then
7384 * disabled when not needed anymore in order to save power.
7385 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007386 if (HAS_PCH_LPT_LP(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007387 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7388 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007389 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007390
7391 /* WADPOClockGatingDisable:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007392 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7393 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007394 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007395}
7396
Ville Syrjälä712bf362016-10-31 22:37:23 +02007397static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007398{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007399 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5f461662020-11-30 13:15:58 +02007400 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007401
7402 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007403 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
Imre Deak7d708ee2013-04-17 14:04:50 +03007404 }
7405}
7406
Imre Deak450174f2016-05-03 15:54:21 +03007407static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7408 int general_prio_credits,
7409 int high_prio_credits)
7410{
7411 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007412 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007413
7414 /* WaTempDisableDOPClkGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007415 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7416 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
Imre Deak450174f2016-05-03 15:54:21 +03007417
Jani Nikula5f461662020-11-30 13:15:58 +02007418 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Oscar Mateo930a7842017-10-17 13:25:45 -07007419 val &= ~L3_PRIO_CREDITS_MASK;
7420 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7421 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
Jani Nikula5f461662020-11-30 13:15:58 +02007422 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007423
7424 /*
7425 * Wait at least 100 clocks before re-enabling clock gating.
7426 * See the definition of L3SQCREG1 in BSpec.
7427 */
Jani Nikula5f461662020-11-30 13:15:58 +02007428 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
Imre Deak450174f2016-05-03 15:54:21 +03007429 udelay(1);
Jani Nikula5f461662020-11-30 13:15:58 +02007430 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
Imre Deak450174f2016-05-03 15:54:21 +03007431}
7432
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007433static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7434{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007435 /* Wa_1409120013:icl,ehl */
Jani Nikula5f461662020-11-30 13:15:58 +02007436 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
Ville Syrjälä885f1822020-07-08 16:12:20 +03007437 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7438
Matt Atwood6f4194c2020-01-13 23:11:28 -05007439 /*Wa_14010594013:icl, ehl */
7440 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
Lucas De Marchidbac4f32021-07-28 14:59:38 -07007441 0, ICL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007442}
7443
José Roberto de Souza35f08372021-01-13 05:37:59 -08007444static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
Michel Thierry5d869232019-08-23 01:20:34 -07007445{
José Roberto de Souzac4924052021-07-12 17:38:50 -07007446 /* Wa_1409120013:tgl,rkl,adl-s,dg1 */
Clint Taylor8c209f42021-06-08 10:47:21 -07007447 if (IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv) ||
7448 IS_ALDERLAKE_S(dev_priv) || IS_DG1(dev_priv))
7449 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
7450 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
Ville Syrjälä885f1822020-07-08 16:12:20 +03007451
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007452 /* Wa_1409825376:tgl (pre-prod)*/
Matt Roper46b0d702021-07-16 22:14:26 -07007453 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007454 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 -08007455 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007456
José Roberto de Souza41c70d22021-04-08 13:49:16 -07007457 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7458 if (DISPLAY_VER(dev_priv) == 12)
7459 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7460 CLKREQ_POLICY_MEM_UP_OVRD, 0);
Michel Thierry5d869232019-08-23 01:20:34 -07007461}
7462
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007463static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7464{
7465 gen12lp_init_clock_gating(dev_priv);
7466
7467 /* Wa_22011091694:adlp */
7468 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7469}
7470
Stuart Summersda9427502020-10-14 12:19:34 -07007471static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7472{
José Roberto de Souza35f08372021-01-13 05:37:59 -08007473 gen12lp_init_clock_gating(dev_priv);
7474
Stuart Summersda9427502020-10-14 12:19:34 -07007475 /* Wa_1409836686:dg1[a0] */
José Roberto de Souzac1f110e2021-10-19 17:23:53 -07007476 if (IS_DG1_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
Jani Nikula5f461662020-11-30 13:15:58 +02007477 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 -07007478 DPT_GATING_DIS);
7479}
7480
Stuart Summersd73dd1f2021-11-02 15:25:09 -07007481static void xehpsdv_init_clock_gating(struct drm_i915_private *dev_priv)
7482{
7483 /* Wa_22010146351:xehpsdv */
7484 if (IS_XEHPSDV_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
7485 intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGR_DIS);
7486}
7487
Matt Roper645cc0b2021-11-02 15:25:10 -07007488static void dg2_init_clock_gating(struct drm_i915_private *i915)
7489{
7490 /* Wa_22010954014:dg2_g10 */
7491 if (IS_DG2_G10(i915))
7492 intel_uncore_rmw(&i915->uncore, XEHP_CLOCK_GATE_DIS, 0,
7493 SGSI_SIDECLK_DIS);
7494
7495 /*
7496 * Wa_14010733611:dg2_g10
7497 * Wa_22010146351:dg2_g10
7498 */
7499 if (IS_DG2_GRAPHICS_STEP(i915, G10, STEP_A0, STEP_B0))
7500 intel_uncore_rmw(&i915->uncore, XEHP_CLOCK_GATE_DIS, 0,
7501 SGR_DIS | SGGI_DIS);
7502}
7503
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007504static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7505{
7506 if (!HAS_PCH_CNP(dev_priv))
7507 return;
7508
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007509 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Jani Nikula5f461662020-11-30 13:15:58 +02007510 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 -07007511 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007512}
7513
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007514static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7515{
7516 cnp_init_clock_gating(dev_priv);
7517 gen9_init_clock_gating(dev_priv);
7518
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007519 /* WAC6entrylatency:cfl */
Jani Nikula5f461662020-11-30 13:15:58 +02007520 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 +03007521 FBC_LLC_FULLY_OPEN);
7522
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007523 /*
7524 * WaFbcTurnOffFbcWatermark:cfl
7525 * Display WA #0562: cfl
7526 */
Jani Nikula5f461662020-11-30 13:15:58 +02007527 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 +03007528 DISP_FBC_WM_DIS);
7529
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007530 /*
7531 * WaFbcNukeOnHostModify:cfl
7532 * Display WA #0873: cfl
7533 */
Jani Nikula5f461662020-11-30 13:15:58 +02007534 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 -07007535 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7536}
7537
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007538static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007539{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007540 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007541
Ville Syrjälä4d6bde582020-07-16 22:04:26 +03007542 /* WAC6entrylatency:kbl */
Jani Nikula5f461662020-11-30 13:15:58 +02007543 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 +03007544 FBC_LLC_FULLY_OPEN);
7545
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007546 /* WaDisableSDEUnitClockGating:kbl */
José Roberto de Souzac1f110e2021-10-19 17:23:53 -07007547 if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007548 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007549 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007550
7551 /* WaDisableGamClockGating:kbl */
José Roberto de Souzac1f110e2021-10-19 17:23:53 -07007552 if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
Jani Nikula5f461662020-11-30 13:15:58 +02007553 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007554 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007555
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007556 /*
7557 * WaFbcTurnOffFbcWatermark:kbl
7558 * Display WA #0562: kbl
7559 */
Jani Nikula5f461662020-11-30 13:15:58 +02007560 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 +03007561 DISP_FBC_WM_DIS);
7562
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007563 /*
7564 * WaFbcNukeOnHostModify:kbl
7565 * Display WA #0873: kbl
7566 */
Jani Nikula5f461662020-11-30 13:15:58 +02007567 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 +03007568 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007569}
7570
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007571static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007572{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007573 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007574
Ville Syrjäläf1421192020-07-16 22:04:25 +03007575 /* WaDisableDopClockGating:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007576 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 +03007577 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7578
Mika Kuoppala44fff992016-06-07 17:19:09 +03007579 /* WAC6entrylatency:skl */
Jani Nikula5f461662020-11-30 13:15:58 +02007580 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 +03007581 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007582
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007583 /*
7584 * WaFbcTurnOffFbcWatermark:skl
7585 * Display WA #0562: skl
7586 */
Jani Nikula5f461662020-11-30 13:15:58 +02007587 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 +03007588 DISP_FBC_WM_DIS);
7589
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007590 /*
7591 * WaFbcNukeOnHostModify:skl
7592 * Display WA #0873: skl
7593 */
Jani Nikula5f461662020-11-30 13:15:58 +02007594 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 +03007595 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007596
Ville Syrjälä99bcf64e2020-07-08 16:12:23 +03007597 /*
7598 * WaFbcHighMemBwCorruptionAvoidance:skl
7599 * Display WA #0883: skl
7600 */
Jani Nikula5f461662020-11-30 13:15:58 +02007601 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 +03007602 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007603}
7604
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007605static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007606{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007607 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007608
Ville Syrjälä885f1822020-07-08 16:12:20 +03007609 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007610 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7611 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007612 HSW_FBCQ_DIS);
7613
Ben Widawskyab57fff2013-12-12 15:28:04 -08007614 /* WaSwitchSolVfFArbitrationPriority:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007615 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 -07007616
Ben Widawskyab57fff2013-12-12 15:28:04 -08007617 /* WaPsrDPAMaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007618 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7619 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007620
Damien Lespiau055e3932014-08-18 13:49:10 +01007621 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb7a70532021-02-20 12:33:03 +02007622 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007623 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7624 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007625 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007626 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007627
Ben Widawskyab57fff2013-12-12 15:28:04 -08007628 /* WaVSRefCountFullforceMissDisable:bdw */
7629 /* WaDSRefCountFullforceMissDisable:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007630 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7631 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ben Widawskyab57fff2013-12-12 15:28:04 -08007632 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007633
Jani Nikula5f461662020-11-30 13:15:58 +02007634 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007635 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007636
7637 /* WaDisableSDEUnitClockGating:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007638 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 +02007639 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007640
Imre Deak450174f2016-05-03 15:54:21 +03007641 /* WaProgramL3SqcReg1Default:bdw */
7642 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007643
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007644 /* WaKVMNotificationOnConfigChange:bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007645 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 +03007646 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7647
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007648 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007649
7650 /* WaDisableDopClockGating:bdw
7651 *
7652 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7653 * clock gating.
7654 */
Jani Nikula5f461662020-11-30 13:15:58 +02007655 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7656 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007657}
7658
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007659static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007660{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007661 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Jani Nikula5f461662020-11-30 13:15:58 +02007662 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7663 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007664 HSW_FBCQ_DIS);
7665
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007666 /* This is required by WaCatErrorRejectionIssue:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007667 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7668 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007669 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007670
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007671 /* WaSwitchSolVfFArbitrationPriority:hsw */
Jani Nikula5f461662020-11-30 13:15:58 +02007672 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 -07007673
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007674 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007675}
7676
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007677static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007678{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007679 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007680
Jani Nikula5f461662020-11-30 13:15:58 +02007681 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007682
Ville Syrjälä885f1822020-07-08 16:12:20 +03007683 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007684 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7685 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
Ville Syrjälä885f1822020-07-08 16:12:20 +03007686 ILK_FBCQ_DIS);
7687
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007688 /* WaDisableBackToBackFlipFix:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007689 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007690 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7691 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7692
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007693 if (IS_IVB_GT1(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007694 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007695 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007696 else {
7697 /* must write both registers */
Jani Nikula5f461662020-11-30 13:15:58 +02007698 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Ville Syrjälä412236c2014-01-22 21:32:44 +02007699 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jani Nikula5f461662020-11-30 13:15:58 +02007700 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007701 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007702 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007703
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007704 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007705 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007706 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007707 */
Jani Nikula5f461662020-11-30 13:15:58 +02007708 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007709 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007710
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007711 /* This is required by WaCatErrorRejectionIssue:ivb */
Jani Nikula5f461662020-11-30 13:15:58 +02007712 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7713 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007714 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7715
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007716 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007717
Jani Nikula5f461662020-11-30 13:15:58 +02007718 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
Ben Widawsky20848222012-05-04 18:58:59 -07007719 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7720 snpcr |= GEN6_MBC_SNPCR_MED;
Jani Nikula5f461662020-11-30 13:15:58 +02007721 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007722
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007723 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007724 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007725
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007726 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007727}
7728
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007729static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007730{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007731 /* WaDisableBackToBackFlipFix:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007732 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007733 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7734 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7735
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007736 /* WaDisableDopClockGating:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007737 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007738 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7739
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007740 /* This is required by WaCatErrorRejectionIssue:vlv */
Jani Nikula5f461662020-11-30 13:15:58 +02007741 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7742 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007743 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7744
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007745 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007746 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007747 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007748 */
Jani Nikula5f461662020-11-30 13:15:58 +02007749 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007750 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007751
Akash Goelc98f5062014-03-24 23:00:07 +05307752 /* WaDisableL3Bank2xClockGate:vlv
7753 * Disabling L3 clock gating- MMIO 940c[25] = 1
7754 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
Jani Nikula5f461662020-11-30 13:15:58 +02007755 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7756 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007757
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007758 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007759 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007760 * Disable clock gating on th GCFG unit to prevent a delay
7761 * in the reporting of vblank events.
7762 */
Jani Nikula5f461662020-11-30 13:15:58 +02007763 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007764}
7765
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007766static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007767{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007768 /* WaVSRefCountFullforceMissDisable:chv */
7769 /* WaDSRefCountFullforceMissDisable:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007770 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7771 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
Ville Syrjälä232ce332014-04-09 13:28:35 +03007772 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007773
7774 /* WaDisableSemaphoreAndSyncFlipWait:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007775 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007776 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007777
7778 /* WaDisableCSUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007779 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 +03007780 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007781
7782 /* WaDisableSDEUnitClockGating:chv */
Jani Nikula5f461662020-11-30 13:15:58 +02007783 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 +03007784 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007785
7786 /*
Imre Deak450174f2016-05-03 15:54:21 +03007787 * WaProgramL3SqcReg1Default:chv
7788 * See gfxspecs/Related Documents/Performance Guide/
7789 * LSQC Setting Recommendations.
7790 */
7791 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007792}
7793
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007794static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007795{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007796 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007797
Jani Nikula5f461662020-11-30 13:15:58 +02007798 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7799 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007800 GS_UNIT_CLOCK_GATE_DISABLE |
7801 CL_UNIT_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007802 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007803 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7804 OVRUNIT_CLOCK_GATE_DISABLE |
7805 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007806 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007807 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
Jani Nikula5f461662020-11-30 13:15:58 +02007808 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007809
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007810 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007811}
7812
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007813static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007814{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007815 struct intel_uncore *uncore = &dev_priv->uncore;
7816
7817 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7818 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7819 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7820 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7821 intel_uncore_write16(uncore, DEUC, 0);
7822 intel_uncore_write(uncore,
7823 MI_ARB_STATE,
7824 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007825}
7826
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007827static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007828{
Jani Nikula5f461662020-11-30 13:15:58 +02007829 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007830 I965_RCC_CLOCK_GATE_DISABLE |
7831 I965_RCPB_CLOCK_GATE_DISABLE |
7832 I965_ISC_CLOCK_GATE_DISABLE |
7833 I965_FBC_CLOCK_GATE_DISABLE);
Jani Nikula5f461662020-11-30 13:15:58 +02007834 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7835 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä20f94962013-06-07 10:47:02 +03007836 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007837}
7838
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007839static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007840{
Jani Nikula5f461662020-11-30 13:15:58 +02007841 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007842
7843 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7844 DSTATE_DOT_CLOCK_GATING;
Jani Nikula5f461662020-11-30 13:15:58 +02007845 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007846
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007847 if (IS_PINEVIEW(dev_priv))
Jani Nikula5f461662020-11-30 13:15:58 +02007848 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007849
7850 /* IIR "flip pending" means done if this bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007851 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007852
7853 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007854 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007855
7856 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
Jani Nikula5f461662020-11-30 13:15:58 +02007857 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 +03007858
Jani Nikula5f461662020-11-30 13:15:58 +02007859 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007860 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007861}
7862
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007863static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007864{
Jani Nikula5f461662020-11-30 13:15:58 +02007865 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007866
7867 /* interrupts should cause a wake up from C3 */
Jani Nikula5f461662020-11-30 13:15:58 +02007868 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007869 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007870
Jani Nikula5f461662020-11-30 13:15:58 +02007871 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007872 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007873
7874 /*
7875 * Have FBC ignore 3D activity since we use software
7876 * render tracking, and otherwise a pure 3D workload
7877 * (even if it just renders a single frame and then does
7878 * abosultely nothing) would not allow FBC to recompress
7879 * until a 2D blit occurs.
7880 */
Jani Nikula5f461662020-11-30 13:15:58 +02007881 intel_uncore_write(&dev_priv->uncore, SCPD0,
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007882 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007883}
7884
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007885static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007886{
Jani Nikula5f461662020-11-30 13:15:58 +02007887 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
Ville Syrjälä10383922014-08-15 01:21:54 +03007888 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7889 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007890}
7891
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007892void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007893{
Dave Airlieeba4b792021-09-29 01:58:07 +03007894 dev_priv->clock_gating_funcs->init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007895}
7896
Ville Syrjälä712bf362016-10-31 22:37:23 +02007897void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007898{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007899 if (HAS_PCH_LPT(dev_priv))
7900 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007901}
7902
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007903static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007904{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007905 drm_dbg_kms(&dev_priv->drm,
7906 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007907}
7908
Dave Airlieeba4b792021-09-29 01:58:07 +03007909#define CG_FUNCS(platform) \
7910static const struct drm_i915_clock_gating_funcs platform##_clock_gating_funcs = { \
7911 .init_clock_gating = platform##_init_clock_gating, \
7912}
7913
Matt Roper645cc0b2021-11-02 15:25:10 -07007914CG_FUNCS(dg2);
Stuart Summersd73dd1f2021-11-02 15:25:09 -07007915CG_FUNCS(xehpsdv);
Dave Airlieeba4b792021-09-29 01:58:07 +03007916CG_FUNCS(adlp);
7917CG_FUNCS(dg1);
7918CG_FUNCS(gen12lp);
7919CG_FUNCS(icl);
7920CG_FUNCS(cfl);
7921CG_FUNCS(skl);
7922CG_FUNCS(kbl);
7923CG_FUNCS(bxt);
7924CG_FUNCS(glk);
7925CG_FUNCS(bdw);
7926CG_FUNCS(chv);
7927CG_FUNCS(hsw);
7928CG_FUNCS(ivb);
7929CG_FUNCS(vlv);
7930CG_FUNCS(gen6);
7931CG_FUNCS(ilk);
7932CG_FUNCS(g4x);
7933CG_FUNCS(i965gm);
7934CG_FUNCS(i965g);
7935CG_FUNCS(gen3);
7936CG_FUNCS(i85x);
7937CG_FUNCS(i830);
7938CG_FUNCS(nop);
7939#undef CG_FUNCS
7940
Imre Deakbb400da2016-03-16 13:38:54 +02007941/**
7942 * intel_init_clock_gating_hooks - setup the clock gating hooks
7943 * @dev_priv: device private
7944 *
7945 * Setup the hooks that configure which clocks of a given platform can be
7946 * gated and also apply various GT and display specific workarounds for these
7947 * platforms. Note that some GT specific workarounds are applied separately
7948 * when GPU contexts or batchbuffers start their execution.
7949 */
7950void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7951{
Matt Roper645cc0b2021-11-02 15:25:10 -07007952 if (IS_DG2(dev_priv))
7953 dev_priv->clock_gating_funcs = &dg2_clock_gating_funcs;
7954 else if (IS_XEHPSDV(dev_priv))
Stuart Summersd73dd1f2021-11-02 15:25:09 -07007955 dev_priv->clock_gating_funcs = &xehpsdv_clock_gating_funcs;
7956 else if (IS_ALDERLAKE_P(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007957 dev_priv->clock_gating_funcs = &adlp_clock_gating_funcs;
José Roberto de Souzaa8a56da2021-05-14 08:37:09 -07007958 else if (IS_DG1(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007959 dev_priv->clock_gating_funcs = &dg1_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007960 else if (GRAPHICS_VER(dev_priv) == 12)
Dave Airlieeba4b792021-09-29 01:58:07 +03007961 dev_priv->clock_gating_funcs = &gen12lp_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007962 else if (GRAPHICS_VER(dev_priv) == 11)
Dave Airlieeba4b792021-09-29 01:58:07 +03007963 dev_priv->clock_gating_funcs = &icl_clock_gating_funcs;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007964 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007965 dev_priv->clock_gating_funcs = &cfl_clock_gating_funcs;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007966 else if (IS_SKYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007967 dev_priv->clock_gating_funcs = &skl_clock_gating_funcs;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007968 else if (IS_KABYLAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007969 dev_priv->clock_gating_funcs = &kbl_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007970 else if (IS_BROXTON(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007971 dev_priv->clock_gating_funcs = &bxt_clock_gating_funcs;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007972 else if (IS_GEMINILAKE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007973 dev_priv->clock_gating_funcs = &glk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007974 else if (IS_BROADWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007975 dev_priv->clock_gating_funcs = &bdw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007976 else if (IS_CHERRYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007977 dev_priv->clock_gating_funcs = &chv_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007978 else if (IS_HASWELL(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007979 dev_priv->clock_gating_funcs = &hsw_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007980 else if (IS_IVYBRIDGE(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007981 dev_priv->clock_gating_funcs = &ivb_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007982 else if (IS_VALLEYVIEW(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007983 dev_priv->clock_gating_funcs = &vlv_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007984 else if (GRAPHICS_VER(dev_priv) == 6)
Dave Airlieeba4b792021-09-29 01:58:07 +03007985 dev_priv->clock_gating_funcs = &gen6_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007986 else if (GRAPHICS_VER(dev_priv) == 5)
Dave Airlieeba4b792021-09-29 01:58:07 +03007987 dev_priv->clock_gating_funcs = &ilk_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007988 else if (IS_G4X(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007989 dev_priv->clock_gating_funcs = &g4x_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007990 else if (IS_I965GM(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007991 dev_priv->clock_gating_funcs = &i965gm_clock_gating_funcs;
Jani Nikulac0f86832016-12-07 12:13:04 +02007992 else if (IS_I965G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007993 dev_priv->clock_gating_funcs = &i965g_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007994 else if (GRAPHICS_VER(dev_priv) == 3)
Dave Airlieeba4b792021-09-29 01:58:07 +03007995 dev_priv->clock_gating_funcs = &gen3_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02007996 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
Dave Airlieeba4b792021-09-29 01:58:07 +03007997 dev_priv->clock_gating_funcs = &i85x_clock_gating_funcs;
Lucas De Marchi651e7d42021-06-05 21:50:49 -07007998 else if (GRAPHICS_VER(dev_priv) == 2)
Dave Airlieeba4b792021-09-29 01:58:07 +03007999 dev_priv->clock_gating_funcs = &i830_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02008000 else {
8001 MISSING_CASE(INTEL_DEVID(dev_priv));
Dave Airlieeba4b792021-09-29 01:58:07 +03008002 dev_priv->clock_gating_funcs = &nop_clock_gating_funcs;
Imre Deakbb400da2016-03-16 13:38:54 +02008003 }
8004}
8005
Dave Airliedde98a52021-09-29 01:58:08 +03008006static const struct drm_i915_wm_disp_funcs skl_wm_funcs = {
8007 .compute_global_watermarks = skl_compute_wm,
8008};
8009
8010static const struct drm_i915_wm_disp_funcs ilk_wm_funcs = {
8011 .compute_pipe_wm = ilk_compute_pipe_wm,
8012 .compute_intermediate_wm = ilk_compute_intermediate_wm,
8013 .initial_watermarks = ilk_initial_watermarks,
8014 .optimize_watermarks = ilk_optimize_watermarks,
8015};
8016
8017static const struct drm_i915_wm_disp_funcs vlv_wm_funcs = {
8018 .compute_pipe_wm = vlv_compute_pipe_wm,
8019 .compute_intermediate_wm = vlv_compute_intermediate_wm,
8020 .initial_watermarks = vlv_initial_watermarks,
8021 .optimize_watermarks = vlv_optimize_watermarks,
8022 .atomic_update_watermarks = vlv_atomic_update_fifo,
8023};
8024
8025static const struct drm_i915_wm_disp_funcs g4x_wm_funcs = {
8026 .compute_pipe_wm = g4x_compute_pipe_wm,
8027 .compute_intermediate_wm = g4x_compute_intermediate_wm,
8028 .initial_watermarks = g4x_initial_watermarks,
8029 .optimize_watermarks = g4x_optimize_watermarks,
8030};
8031
8032static const struct drm_i915_wm_disp_funcs pnv_wm_funcs = {
8033 .update_wm = pnv_update_wm,
8034};
8035
8036static const struct drm_i915_wm_disp_funcs i965_wm_funcs = {
8037 .update_wm = i965_update_wm,
8038};
8039
8040static const struct drm_i915_wm_disp_funcs i9xx_wm_funcs = {
8041 .update_wm = i9xx_update_wm,
8042};
8043
8044static const struct drm_i915_wm_disp_funcs i845_wm_funcs = {
8045 .update_wm = i845_update_wm,
8046};
8047
8048static const struct drm_i915_wm_disp_funcs nop_funcs = {
8049};
8050
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008051/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008052void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008053{
Daniel Vetterc921aba2012-04-26 23:28:17 +02008054 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008055 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08008056 pnv_get_mem_freq(dev_priv);
Lucas De Marchi651e7d42021-06-05 21:50:49 -07008057 else if (GRAPHICS_VER(dev_priv) == 5)
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08008058 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008059
James Ausmusb068a862019-10-09 10:23:14 -07008060 if (intel_has_sagv(dev_priv))
8061 skl_setup_sagv_block_time(dev_priv);
8062
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008063 /* For FIFO watermark updates */
Matt Roper7dadd282021-03-19 21:42:43 -07008064 if (DISPLAY_VER(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008065 skl_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008066 dev_priv->wm_disp = &skl_wm_funcs;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008067 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008068 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008069
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008070 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008071 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008072 (DISPLAY_VER(dev_priv) != 5 && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008073 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Dave Airliedde98a52021-09-29 01:58:08 +03008074 dev_priv->wm_disp = &ilk_wm_funcs;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008075 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008076 drm_dbg_kms(&dev_priv->drm,
8077 "Failed to read display plane latency. "
8078 "Disable CxSR\n");
Dave Airliedde98a52021-09-29 01:58:08 +03008079 dev_priv->wm_disp = &nop_funcs;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008080 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008081 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008082 vlv_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008083 dev_priv->wm_disp = &vlv_wm_funcs;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008084 } else if (IS_G4X(dev_priv)) {
8085 g4x_setup_wm_latency(dev_priv);
Dave Airliedde98a52021-09-29 01:58:08 +03008086 dev_priv->wm_disp = &g4x_wm_funcs;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008087 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00008088 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008089 dev_priv->is_ddr3,
8090 dev_priv->fsb_freq,
8091 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008092 drm_info(&dev_priv->drm,
8093 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008094 "(found ddr%s fsb freq %d, mem freq %d), "
8095 "disabling CxSR\n",
8096 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8097 dev_priv->fsb_freq, dev_priv->mem_freq);
8098 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008099 intel_set_memory_cxsr(dev_priv, false);
Dave Airliedde98a52021-09-29 01:58:08 +03008100 dev_priv->wm_disp = &nop_funcs;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008101 } else
Dave Airliedde98a52021-09-29 01:58:08 +03008102 dev_priv->wm_disp = &pnv_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008103 } else if (DISPLAY_VER(dev_priv) == 4) {
Dave Airliedde98a52021-09-29 01:58:08 +03008104 dev_priv->wm_disp = &i965_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008105 } else if (DISPLAY_VER(dev_priv) == 3) {
Dave Airliedde98a52021-09-29 01:58:08 +03008106 dev_priv->wm_disp = &i9xx_wm_funcs;
Lucas De Marchi93e7e612021-04-12 22:09:53 -07008107 } else if (DISPLAY_VER(dev_priv) == 2) {
Dave Airlie758b2fc2021-09-29 01:57:46 +03008108 if (INTEL_NUM_PIPES(dev_priv) == 1)
Dave Airliedde98a52021-09-29 01:58:08 +03008109 dev_priv->wm_disp = &i845_wm_funcs;
Dave Airlie758b2fc2021-09-29 01:57:46 +03008110 else
Dave Airliedde98a52021-09-29 01:58:08 +03008111 dev_priv->wm_disp = &i9xx_wm_funcs;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008112 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03008113 drm_err(&dev_priv->drm,
8114 "unexpected fall-through in %s\n", __func__);
Dave Airliedde98a52021-09-29 01:58:08 +03008115 dev_priv->wm_disp = &nop_funcs;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008116 }
8117}
8118
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008119void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008120{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01008121 dev_priv->runtime_pm.suspended = false;
8122 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008123}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02008124
8125static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
8126{
8127 struct intel_dbuf_state *dbuf_state;
8128
8129 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
8130 if (!dbuf_state)
8131 return NULL;
8132
8133 return &dbuf_state->base;
8134}
8135
8136static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
8137 struct intel_global_state *state)
8138{
8139 kfree(state);
8140}
8141
8142static const struct intel_global_state_funcs intel_dbuf_funcs = {
8143 .atomic_duplicate_state = intel_dbuf_duplicate_state,
8144 .atomic_destroy_state = intel_dbuf_destroy_state,
8145};
8146
8147struct intel_dbuf_state *
8148intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
8149{
8150 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8151 struct intel_global_state *dbuf_state;
8152
8153 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
8154 if (IS_ERR(dbuf_state))
8155 return ERR_CAST(dbuf_state);
8156
8157 return to_intel_dbuf_state(dbuf_state);
8158}
8159
8160int intel_dbuf_init(struct drm_i915_private *dev_priv)
8161{
8162 struct intel_dbuf_state *dbuf_state;
8163
8164 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
8165 if (!dbuf_state)
8166 return -ENOMEM;
8167
8168 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
8169 &dbuf_state->base, &intel_dbuf_funcs);
8170
8171 return 0;
8172}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008173
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008174/*
8175 * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
8176 * update the request state of all DBUS slices.
8177 */
8178static void update_mbus_pre_enable(struct intel_atomic_state *state)
8179{
8180 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8181 u32 mbus_ctl, dbuf_min_tracker_val;
8182 enum dbuf_slice slice;
8183 const struct intel_dbuf_state *dbuf_state =
8184 intel_atomic_get_new_dbuf_state(state);
8185
8186 if (!IS_ALDERLAKE_P(dev_priv))
8187 return;
8188
8189 /*
8190 * TODO: Implement vblank synchronized MBUS joining changes.
8191 * Must be properly coordinated with dbuf reprogramming.
8192 */
8193 if (dbuf_state->joined_mbus) {
8194 mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
8195 MBUS_JOIN_PIPE_SELECT_NONE;
8196 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
8197 } else {
8198 mbus_ctl = MBUS_HASHING_MODE_2x2 |
8199 MBUS_JOIN_PIPE_SELECT_NONE;
8200 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
8201 }
8202
8203 intel_de_rmw(dev_priv, MBUS_CTL,
8204 MBUS_HASHING_MODE_MASK | MBUS_JOIN |
8205 MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
8206
8207 for_each_dbuf_slice(dev_priv, slice)
8208 intel_de_rmw(dev_priv, DBUF_CTL_S(slice),
8209 DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
8210 dbuf_min_tracker_val);
8211}
8212
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008213void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
8214{
8215 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8216 const struct intel_dbuf_state *new_dbuf_state =
8217 intel_atomic_get_new_dbuf_state(state);
8218 const struct intel_dbuf_state *old_dbuf_state =
8219 intel_atomic_get_old_dbuf_state(state);
8220
8221 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008222 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8223 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008224 return;
8225
8226 WARN_ON(!new_dbuf_state->base.changed);
8227
Vandita Kulkarnif4dc0082021-05-18 17:06:17 -07008228 update_mbus_pre_enable(state);
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008229 gen9_dbuf_slices_update(dev_priv,
8230 old_dbuf_state->enabled_slices |
8231 new_dbuf_state->enabled_slices);
8232}
8233
8234void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
8235{
8236 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8237 const struct intel_dbuf_state *new_dbuf_state =
8238 intel_atomic_get_new_dbuf_state(state);
8239 const struct intel_dbuf_state *old_dbuf_state =
8240 intel_atomic_get_old_dbuf_state(state);
8241
8242 if (!new_dbuf_state ||
Stanislav Lisovskiy0d6695b2021-05-27 14:01:06 +03008243 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8244 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02008245 return;
8246
8247 WARN_ON(!new_dbuf_state->base.changed);
8248
8249 gen9_dbuf_slices_update(dev_priv,
8250 new_dbuf_state->enabled_slices);
8251}