blob: 88e7d6b3212e41148cd436d9cec78dad10fc4714 [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"
Stanislav Lisovskiycac91e62020-05-22 16:18:43 +030036#include "display/intel_bw.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030037#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030038#include "display/intel_fbc.h"
39#include "display/intel_sprite.h"
40
Andi Shyti0dc3c562019-10-20 19:41:39 +010041#include "gt/intel_llc.h"
42
Eugeni Dodonov85208be2012-04-16 22:20:34 -030043#include "i915_drv.h"
Jani Nikulaa10510a2020-02-27 19:00:47 +020044#include "i915_fixed.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030045#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030046#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030047#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010048#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020049#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030050
Jani Nikulaa10510a2020-02-27 19:00:47 +020051/* Stores plane specific WM parameters */
52struct skl_wm_params {
53 bool x_tiled, y_tiled;
54 bool rc_surface;
55 bool is_planar;
56 u32 width;
57 u8 cpp;
58 u32 plane_pixel_rate;
59 u32 y_min_scanlines;
60 u32 plane_bytes_per_line;
61 uint_fixed_16_16_t plane_blocks_per_line;
62 uint_fixed_16_16_t y_tile_minimum;
63 u32 linetime_us;
64 u32 dbuf_block_size;
65};
66
67/* used in computing the new watermarks state */
68struct intel_wm_config {
69 unsigned int num_pipes_active;
70 bool sprites_enabled;
71 bool sprites_scaled;
72};
73
Ville Syrjälä46f16e62016-10-31 22:37:22 +020074static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030075{
Ville Syrjälä93564042017-08-24 22:10:51 +030076 if (HAS_LLC(dev_priv)) {
77 /*
78 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080079 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030080 *
81 * Must match Sampler, Pixel Back End, and Media. See
82 * WaCompressedResourceSamplerPbeMediaNewHashMode.
83 */
84 I915_WRITE(CHICKEN_PAR1_1,
85 I915_READ(CHICKEN_PAR1_1) |
86 SKL_DE_COMPRESSED_HASH_MODE);
87 }
88
Rodrigo Vivi82525c12017-06-08 08:50:00 -070089 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030090 I915_WRITE(CHICKEN_PAR1_1,
91 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
92
Rodrigo Vivi82525c12017-06-08 08:50:00 -070093 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030094 I915_WRITE(GEN8_CHICKEN_DCPR_1,
95 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030096
Rodrigo Vivi82525c12017-06-08 08:50:00 -070097 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030098 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030099 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +0300100
Praveen Paneri32087d12017-08-03 23:02:10 +0530101 if (IS_SKYLAKE(dev_priv)) {
102 /* WaDisableDopClockGating */
103 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
104 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
105 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300106}
107
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200108static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200109{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200110 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200111
Nick Hoatha7546152015-06-29 14:07:32 +0100112 /* WaDisableSDEUnitClockGating:bxt */
113 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
114 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
115
Imre Deak32608ca2015-03-11 11:10:27 +0200116 /*
117 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200118 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200119 */
Imre Deak32608ca2015-03-11 11:10:27 +0200120 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200121 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200122
123 /*
124 * Wa: Backlight PWM may stop in the asserted state, causing backlight
125 * to stay fully on.
126 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200127 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
128 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530129
130 /*
131 * Lower the display internal timeout.
132 * This is needed to avoid any hard hangs when DSI port PLL
133 * is off and a MMIO access is attempted by any privilege
134 * application, using batch buffers or any other means.
135 */
136 I915_WRITE(RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Ville Syrjäläc4615b22020-07-08 16:12:21 +0300137
138 /* WaFbcTurnOffFbcWatermark:bxt */
139 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
140 DISP_FBC_WM_DIS);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +0300141
142 /* WaFbcHighMemBwCorruptionAvoidance:bxt */
143 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
144 ILK_DPFC_DISABLE_DUMMY0);
Imre Deaka82abe42015-03-27 14:00:04 +0200145}
146
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200147static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
148{
149 gen9_init_clock_gating(dev_priv);
150
151 /*
152 * WaDisablePWMClockGating:glk
153 * Backlight PWM may stop in the asserted state, causing backlight
154 * to stay fully on.
155 */
156 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
157 PWM1_GATING_DIS | PWM2_GATING_DIS);
158}
159
Lucas De Marchi1d218222019-12-24 00:40:04 -0800160static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200161{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200162 u32 tmp;
163
164 tmp = I915_READ(CLKCFG);
165
166 switch (tmp & CLKCFG_FSB_MASK) {
167 case CLKCFG_FSB_533:
168 dev_priv->fsb_freq = 533; /* 133*4 */
169 break;
170 case CLKCFG_FSB_800:
171 dev_priv->fsb_freq = 800; /* 200*4 */
172 break;
173 case CLKCFG_FSB_667:
174 dev_priv->fsb_freq = 667; /* 167*4 */
175 break;
176 case CLKCFG_FSB_400:
177 dev_priv->fsb_freq = 400; /* 100*4 */
178 break;
179 }
180
181 switch (tmp & CLKCFG_MEM_MASK) {
182 case CLKCFG_MEM_533:
183 dev_priv->mem_freq = 533;
184 break;
185 case CLKCFG_MEM_667:
186 dev_priv->mem_freq = 667;
187 break;
188 case CLKCFG_MEM_800:
189 dev_priv->mem_freq = 800;
190 break;
191 }
192
193 /* detect pineview DDR3 setting */
194 tmp = I915_READ(CSHRDDR3CTL);
195 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
196}
197
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800198static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200199{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200200 u16 ddrpll, csipll;
201
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100202 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
203 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200204
205 switch (ddrpll & 0xff) {
206 case 0xc:
207 dev_priv->mem_freq = 800;
208 break;
209 case 0x10:
210 dev_priv->mem_freq = 1066;
211 break;
212 case 0x14:
213 dev_priv->mem_freq = 1333;
214 break;
215 case 0x18:
216 dev_priv->mem_freq = 1600;
217 break;
218 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300219 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
220 ddrpll & 0xff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200221 dev_priv->mem_freq = 0;
222 break;
223 }
224
Daniel Vetterc921aba2012-04-26 23:28:17 +0200225 switch (csipll & 0x3ff) {
226 case 0x00c:
227 dev_priv->fsb_freq = 3200;
228 break;
229 case 0x00e:
230 dev_priv->fsb_freq = 3733;
231 break;
232 case 0x010:
233 dev_priv->fsb_freq = 4266;
234 break;
235 case 0x012:
236 dev_priv->fsb_freq = 4800;
237 break;
238 case 0x014:
239 dev_priv->fsb_freq = 5333;
240 break;
241 case 0x016:
242 dev_priv->fsb_freq = 5866;
243 break;
244 case 0x018:
245 dev_priv->fsb_freq = 6400;
246 break;
247 default:
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300248 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
249 csipll & 0x3ff);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200250 dev_priv->fsb_freq = 0;
251 break;
252 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200253}
254
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300255static const struct cxsr_latency cxsr_latency_table[] = {
256 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
257 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
258 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
259 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
260 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
261
262 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
263 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
264 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
265 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
266 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
267
268 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
269 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
270 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
271 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
272 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
273
274 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
275 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
276 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
277 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
278 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
279
280 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
281 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
282 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
283 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
284 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
285
286 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
287 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
288 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
289 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
290 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
291};
292
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100293static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
294 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300295 int fsb,
296 int mem)
297{
298 const struct cxsr_latency *latency;
299 int i;
300
301 if (fsb == 0 || mem == 0)
302 return NULL;
303
304 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
305 latency = &cxsr_latency_table[i];
306 if (is_desktop == latency->is_desktop &&
307 is_ddr3 == latency->is_ddr3 &&
308 fsb == latency->fsb_freq && mem == latency->mem_freq)
309 return latency;
310 }
311
312 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
313
314 return NULL;
315}
316
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200317static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
318{
319 u32 val;
320
Chris Wilson337fa6e2019-04-26 09:17:20 +0100321 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200322
323 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
324 if (enable)
325 val &= ~FORCE_DDR_HIGH_FREQ;
326 else
327 val |= FORCE_DDR_HIGH_FREQ;
328 val &= ~FORCE_DDR_LOW_FREQ;
329 val |= FORCE_DDR_FREQ_REQ_ACK;
330 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
331
332 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
333 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300334 drm_err(&dev_priv->drm,
335 "timed out waiting for Punit DDR DVFS request\n");
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200336
Chris Wilson337fa6e2019-04-26 09:17:20 +0100337 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200338}
339
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200340static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
341{
342 u32 val;
343
Chris Wilson337fa6e2019-04-26 09:17:20 +0100344 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200345
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200346 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200347 if (enable)
348 val |= DSP_MAXFIFO_PM5_ENABLE;
349 else
350 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200351 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200352
Chris Wilson337fa6e2019-04-26 09:17:20 +0100353 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200354}
355
Ville Syrjäläf4998962015-03-10 17:02:21 +0200356#define FW_WM(value, plane) \
357 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
358
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200359static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300360{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200361 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300362 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300363
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100364 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200365 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300366 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300367 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200368 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200369 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300370 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300371 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200372 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200373 val = I915_READ(DSPFW3);
374 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
375 if (enable)
376 val |= PINEVIEW_SELF_REFRESH_EN;
377 else
378 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300379 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300380 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100381 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200382 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300383 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
384 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
385 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300386 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100387 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300388 /*
389 * FIXME can't find a bit like this for 915G, and
390 * and yet it does have the related watermark in
391 * FW_BLC_SELF. What's going on?
392 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200393 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300394 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
395 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
396 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300397 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300398 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200399 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300400 }
401
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200402 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
403
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300404 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
405 enableddisabled(enable),
406 enableddisabled(was_enabled));
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200407
408 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300409}
410
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300411/**
412 * intel_set_memory_cxsr - Configure CxSR state
413 * @dev_priv: i915 device
414 * @enable: Allow vs. disallow CxSR
415 *
416 * Allow or disallow the system to enter a special CxSR
417 * (C-state self refresh) state. What typically happens in CxSR mode
418 * is that several display FIFOs may get combined into a single larger
419 * FIFO for a particular plane (so called max FIFO mode) to allow the
420 * system to defer memory fetches longer, and the memory will enter
421 * self refresh.
422 *
423 * Note that enabling CxSR does not guarantee that the system enter
424 * this special mode, nor does it guarantee that the system stays
425 * in that mode once entered. So this just allows/disallows the system
426 * to autonomously utilize the CxSR mode. Other factors such as core
427 * C-states will affect when/if the system actually enters/exits the
428 * CxSR mode.
429 *
430 * Note that on VLV/CHV this actually only controls the max FIFO mode,
431 * and the system is free to enter/exit memory self refresh at any time
432 * even when the use of CxSR has been disallowed.
433 *
434 * While the system is actually in the CxSR/max FIFO mode, some plane
435 * control registers will not get latched on vblank. Thus in order to
436 * guarantee the system will respond to changes in the plane registers
437 * we must always disallow CxSR prior to making changes to those registers.
438 * Unfortunately the system will re-evaluate the CxSR conditions at
439 * frame start which happens after vblank start (which is when the plane
440 * registers would get latched), so we can't proceed with the plane update
441 * during the same frame where we disallowed CxSR.
442 *
443 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
444 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
445 * the hardware w.r.t. HPLL SR when writing to plane registers.
446 * Disallowing just CxSR is sufficient.
447 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200448bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200449{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200450 bool ret;
451
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200452 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200453 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300454 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
455 dev_priv->wm.vlv.cxsr = enable;
456 else if (IS_G4X(dev_priv))
457 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200458 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200459
460 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200461}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200462
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300463/*
464 * Latency for FIFO fetches is dependent on several factors:
465 * - memory configuration (speed, channels)
466 * - chipset
467 * - current MCH state
468 * It can be fairly high in some situations, so here we assume a fairly
469 * pessimal value. It's a tradeoff between extra memory fetches (if we
470 * set this value too high, the FIFO will fetch frequently to stay full)
471 * and power consumption (set it too low to save power and we might see
472 * FIFO underruns and display "flicker").
473 *
474 * A value of 5us seems to be a good balance; safe for very low end
475 * platforms but not overly aggressive on lower latency configs.
476 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100477static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300478
Ville Syrjäläb5004722015-03-05 21:19:47 +0200479#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
480 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
481
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200482static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200483{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100484 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200485 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200486 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200487 enum pipe pipe = crtc->pipe;
488 int sprite0_start, sprite1_start;
Kees Cook2713eb42020-02-20 16:05:17 -0800489 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200490
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200491 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200492 case PIPE_A:
493 dsparb = I915_READ(DSPARB);
494 dsparb2 = I915_READ(DSPARB2);
495 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
496 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
497 break;
498 case PIPE_B:
499 dsparb = I915_READ(DSPARB);
500 dsparb2 = I915_READ(DSPARB2);
501 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
502 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
503 break;
504 case PIPE_C:
505 dsparb2 = I915_READ(DSPARB2);
506 dsparb3 = I915_READ(DSPARB3);
507 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
508 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
509 break;
510 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200511 MISSING_CASE(pipe);
512 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200513 }
514
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200515 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
516 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
517 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
518 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200519}
520
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200521static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
522 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300523{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200524 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300525 int size;
526
527 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200528 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300529 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
530
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300531 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
532 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533
534 return size;
535}
536
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200537static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
538 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300539{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200540 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300541 int size;
542
543 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200544 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
546 size >>= 1; /* Convert to cachelines */
547
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300548 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
549 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300550
551 return size;
552}
553
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200554static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
555 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300556{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200557 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300558 int size;
559
560 size = dsparb & 0x7f;
561 size >>= 2; /* Convert to cachelines */
562
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300563 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
564 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300565
566 return size;
567}
568
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300569/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800570static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300571 .fifo_size = PINEVIEW_DISPLAY_FIFO,
572 .max_wm = PINEVIEW_MAX_WM,
573 .default_wm = PINEVIEW_DFT_WM,
574 .guard_size = PINEVIEW_GUARD_WM,
575 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300576};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800577
578static const struct intel_watermark_params pnv_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300579 .fifo_size = PINEVIEW_DISPLAY_FIFO,
580 .max_wm = PINEVIEW_MAX_WM,
581 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
582 .guard_size = PINEVIEW_GUARD_WM,
583 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300584};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800585
586static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300587 .fifo_size = PINEVIEW_CURSOR_FIFO,
588 .max_wm = PINEVIEW_CURSOR_MAX_WM,
589 .default_wm = PINEVIEW_CURSOR_DFT_WM,
590 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
591 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300592};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800593
594static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300595 .fifo_size = PINEVIEW_CURSOR_FIFO,
596 .max_wm = PINEVIEW_CURSOR_MAX_WM,
597 .default_wm = PINEVIEW_CURSOR_DFT_WM,
598 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
599 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800601
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300602static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300603 .fifo_size = I965_CURSOR_FIFO,
604 .max_wm = I965_CURSOR_MAX_WM,
605 .default_wm = I965_CURSOR_DFT_WM,
606 .guard_size = 2,
607 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300608};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800609
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300610static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300611 .fifo_size = I945_FIFO_SIZE,
612 .max_wm = I915_MAX_WM,
613 .default_wm = 1,
614 .guard_size = 2,
615 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300616};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800617
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300618static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300619 .fifo_size = I915_FIFO_SIZE,
620 .max_wm = I915_MAX_WM,
621 .default_wm = 1,
622 .guard_size = 2,
623 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300624};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800625
Ville Syrjälä9d539102014-08-15 01:21:53 +0300626static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300627 .fifo_size = I855GM_FIFO_SIZE,
628 .max_wm = I915_MAX_WM,
629 .default_wm = 1,
630 .guard_size = 2,
631 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300632};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800633
Ville Syrjälä9d539102014-08-15 01:21:53 +0300634static const struct intel_watermark_params i830_bc_wm_info = {
635 .fifo_size = I855GM_FIFO_SIZE,
636 .max_wm = I915_MAX_WM/2,
637 .default_wm = 1,
638 .guard_size = 2,
639 .cacheline_size = I830_FIFO_LINE_SIZE,
640};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800641
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200642static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300643 .fifo_size = I830_FIFO_SIZE,
644 .max_wm = I915_MAX_WM,
645 .default_wm = 1,
646 .guard_size = 2,
647 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300648};
649
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300650/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300651 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
652 * @pixel_rate: Pipe pixel rate in kHz
653 * @cpp: Plane bytes per pixel
654 * @latency: Memory wakeup latency in 0.1us units
655 *
656 * Compute the watermark using the method 1 or "small buffer"
657 * formula. The caller may additonally add extra cachelines
658 * to account for TLB misses and clock crossings.
659 *
660 * This method is concerned with the short term drain rate
661 * of the FIFO, ie. it does not account for blanking periods
662 * which would effectively reduce the average drain rate across
663 * a longer period. The name "small" refers to the fact the
664 * FIFO is relatively small compared to the amount of data
665 * fetched.
666 *
667 * The FIFO level vs. time graph might look something like:
668 *
669 * |\ |\
670 * | \ | \
671 * __---__---__ (- plane active, _ blanking)
672 * -> time
673 *
674 * or perhaps like this:
675 *
676 * |\|\ |\|\
677 * __----__----__ (- plane active, _ blanking)
678 * -> time
679 *
680 * Returns:
681 * The watermark in bytes
682 */
683static unsigned int intel_wm_method1(unsigned int pixel_rate,
684 unsigned int cpp,
685 unsigned int latency)
686{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200687 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300688
Ville Syrjäläd492a292019-04-08 18:27:01 +0300689 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300690 ret = DIV_ROUND_UP_ULL(ret, 10000);
691
692 return ret;
693}
694
695/**
696 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
697 * @pixel_rate: Pipe pixel rate in kHz
698 * @htotal: Pipe horizontal total
699 * @width: Plane width in pixels
700 * @cpp: Plane bytes per pixel
701 * @latency: Memory wakeup latency in 0.1us units
702 *
703 * Compute the watermark using the method 2 or "large buffer"
704 * formula. The caller may additonally add extra cachelines
705 * to account for TLB misses and clock crossings.
706 *
707 * This method is concerned with the long term drain rate
708 * of the FIFO, ie. it does account for blanking periods
709 * which effectively reduce the average drain rate across
710 * a longer period. The name "large" refers to the fact the
711 * FIFO is relatively large compared to the amount of data
712 * fetched.
713 *
714 * The FIFO level vs. time graph might look something like:
715 *
716 * |\___ |\___
717 * | \___ | \___
718 * | \ | \
719 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
720 * -> time
721 *
722 * Returns:
723 * The watermark in bytes
724 */
725static unsigned int intel_wm_method2(unsigned int pixel_rate,
726 unsigned int htotal,
727 unsigned int width,
728 unsigned int cpp,
729 unsigned int latency)
730{
731 unsigned int ret;
732
733 /*
734 * FIXME remove once all users are computing
735 * watermarks in the correct place.
736 */
737 if (WARN_ON_ONCE(htotal == 0))
738 htotal = 1;
739
740 ret = (latency * pixel_rate) / (htotal * 10000);
741 ret = (ret + 1) * width * cpp;
742
743 return ret;
744}
745
746/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300747 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300748 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300749 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000750 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200751 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300752 * @latency_ns: memory latency for the platform
753 *
754 * Calculate the watermark level (the level at which the display plane will
755 * start fetching from memory again). Each chip has a different display
756 * FIFO size and allocation, so the caller needs to figure that out and pass
757 * in the correct intel_watermark_params structure.
758 *
759 * As the pixel clock runs, the FIFO will be drained at a rate that depends
760 * on the pixel size. When it reaches the watermark level, it'll start
761 * fetching FIFO line sized based chunks from memory until the FIFO fills
762 * past the watermark point. If the FIFO drains completely, a FIFO underrun
763 * will occur, and a display engine hang could result.
764 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300765static unsigned int intel_calculate_wm(int pixel_rate,
766 const struct intel_watermark_params *wm,
767 int fifo_size, int cpp,
768 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300769{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300770 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771
772 /*
773 * Note: we need to make sure we don't overflow for various clock &
774 * latency values.
775 * clocks go from a few thousand to several hundred thousand.
776 * latency is usually a few thousand
777 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300778 entries = intel_wm_method1(pixel_rate, cpp,
779 latency_ns / 100);
780 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
781 wm->guard_size;
782 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300783
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300784 wm_size = fifo_size - entries;
785 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300786
787 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300788 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300789 wm_size = wm->max_wm;
790 if (wm_size <= 0)
791 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300792
793 /*
794 * Bspec seems to indicate that the value shouldn't be lower than
795 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
796 * Lets go for 8 which is the burst size since certain platforms
797 * already use a hardcoded 8 (which is what the spec says should be
798 * done).
799 */
800 if (wm_size <= 8)
801 wm_size = 8;
802
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300803 return wm_size;
804}
805
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300806static bool is_disabling(int old, int new, int threshold)
807{
808 return old >= threshold && new < threshold;
809}
810
811static bool is_enabling(int old, int new, int threshold)
812{
813 return old < threshold && new >= threshold;
814}
815
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300816static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
817{
818 return dev_priv->wm.max_level + 1;
819}
820
Ville Syrjälä24304d812017-03-14 17:10:49 +0200821static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
822 const struct intel_plane_state *plane_state)
823{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100824 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200825
826 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100827 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200828 return false;
829
830 /*
831 * Treat cursor with fb as always visible since cursor updates
832 * can happen faster than the vrefresh rate, and the current
833 * watermark code doesn't handle that correctly. Cursor updates
834 * which set/clear the fb or change the cursor size are going
835 * to get throttled by intel_legacy_cursor_update() to work
836 * around this problem with the watermark code.
837 */
838 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100839 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200840 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100841 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200842}
843
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200844static bool intel_crtc_active(struct intel_crtc *crtc)
845{
846 /* Be paranoid as we can arrive here with only partial
847 * state retrieved from the hardware during setup.
848 *
849 * We can ditch the adjusted_mode.crtc_clock check as soon
850 * as Haswell has gained clock readout/fastboot support.
851 *
852 * We can ditch the crtc->primary->state->fb check as soon as we can
853 * properly reconstruct framebuffers.
854 *
855 * FIXME: The intel_crtc->active here should be switched to
856 * crtc->state->active once we have proper CRTC states wired up
857 * for atomic.
858 */
859 return crtc->active && crtc->base.primary->state->fb &&
860 crtc->config->hw.adjusted_mode.crtc_clock;
861}
862
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200863static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300864{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200865 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200867 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200868 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300869 if (enabled)
870 return NULL;
871 enabled = crtc;
872 }
873 }
874
875 return enabled;
876}
877
Lucas De Marchi1d218222019-12-24 00:40:04 -0800878static void pnv_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300879{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200880 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200881 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300882 const struct cxsr_latency *latency;
883 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300884 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000886 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100887 dev_priv->is_ddr3,
888 dev_priv->fsb_freq,
889 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300890 if (!latency) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300891 drm_dbg_kms(&dev_priv->drm,
892 "Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300893 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300894 return;
895 }
896
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200897 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300898 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200899 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100900 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200901 const struct drm_framebuffer *fb =
902 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200903 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300904 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300905
906 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800907 wm = intel_calculate_wm(clock, &pnv_display_wm,
908 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200909 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300910 reg = I915_READ(DSPFW1);
911 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200912 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913 I915_WRITE(DSPFW1, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300914 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300915
916 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800917 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
918 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300919 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300920 reg = I915_READ(DSPFW3);
921 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200922 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300923 I915_WRITE(DSPFW3, reg);
924
925 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800926 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
927 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200928 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300929 reg = I915_READ(DSPFW3);
930 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200931 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300932 I915_WRITE(DSPFW3, reg);
933
934 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800935 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
936 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300937 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938 reg = I915_READ(DSPFW3);
939 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200940 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300941 I915_WRITE(DSPFW3, reg);
Wambui Karugaf8d18d52020-01-07 18:13:30 +0300942 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300943
Imre Deak5209b1f2014-07-01 12:36:17 +0300944 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300945 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300946 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300947 }
948}
949
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300950/*
951 * Documentation says:
952 * "If the line size is small, the TLB fetches can get in the way of the
953 * data fetches, causing some lag in the pixel data return which is not
954 * accounted for in the above formulas. The following adjustment only
955 * needs to be applied if eight whole lines fit in the buffer at once.
956 * The WM is adjusted upwards by the difference between the FIFO size
957 * and the size of 8 whole lines. This adjustment is always performed
958 * in the actual pixel depth regardless of whether FBC is enabled or not."
959 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000960static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300961{
962 int tlb_miss = fifo_size * 64 - width * cpp * 8;
963
964 return max(0, tlb_miss);
965}
966
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300967static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
968 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300969{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300970 enum pipe pipe;
971
972 for_each_pipe(dev_priv, pipe)
973 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
974
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300975 I915_WRITE(DSPFW1,
976 FW_WM(wm->sr.plane, SR) |
977 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
978 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
979 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
980 I915_WRITE(DSPFW2,
981 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
982 FW_WM(wm->sr.fbc, FBC_SR) |
983 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
984 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
985 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
986 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
987 I915_WRITE(DSPFW3,
988 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
989 FW_WM(wm->sr.cursor, CURSOR_SR) |
990 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
991 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300992
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300993 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300994}
995
Ville Syrjälä15665972015-03-10 16:16:28 +0200996#define FW_WM_VLV(value, plane) \
997 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
998
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200999static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001000 const struct vlv_wm_values *wm)
1001{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001002 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001003
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001004 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +02001005 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1006
Ville Syrjälä50f4cae2016-11-28 19:37:15 +02001007 I915_WRITE(VLV_DDL(pipe),
1008 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1009 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1010 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1011 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1012 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001013
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +02001014 /*
1015 * Zero the (unused) WM1 watermarks, and also clear all the
1016 * high order bits so that there are no out of bounds values
1017 * present in the registers during the reprogramming.
1018 */
1019 I915_WRITE(DSPHOWM, 0);
1020 I915_WRITE(DSPHOWM1, 0);
1021 I915_WRITE(DSPFW4, 0);
1022 I915_WRITE(DSPFW5, 0);
1023 I915_WRITE(DSPFW6, 0);
1024
Ville Syrjäläae801522015-03-05 21:19:49 +02001025 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001026 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001027 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1028 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1029 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001030 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001031 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1032 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1033 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001034 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001035 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001036
1037 if (IS_CHERRYVIEW(dev_priv)) {
1038 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001039 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1040 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001041 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001042 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1043 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001044 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001045 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1046 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001047 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001048 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001049 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1050 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1051 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1052 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1053 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1054 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1055 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1056 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1057 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001058 } else {
1059 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001060 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1061 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001062 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001063 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001064 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1065 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1066 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1067 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1068 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1069 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001070 }
1071
1072 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001073}
1074
Ville Syrjälä15665972015-03-10 16:16:28 +02001075#undef FW_WM_VLV
1076
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001077static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1078{
1079 /* all latencies in usec */
1080 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1081 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001082 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001083
Ville Syrjälä79d94302017-04-21 21:14:30 +03001084 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001085}
1086
1087static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1088{
1089 /*
1090 * DSPCNTR[13] supposedly controls whether the
1091 * primary plane can use the FIFO space otherwise
1092 * reserved for the sprite plane. It's not 100% clear
1093 * what the actual FIFO size is, but it looks like we
1094 * can happily set both primary and sprite watermarks
1095 * up to 127 cachelines. So that would seem to mean
1096 * that either DSPCNTR[13] doesn't do anything, or that
1097 * the total FIFO is >= 256 cachelines in size. Either
1098 * way, we don't seem to have to worry about this
1099 * repartitioning as the maximum watermark value the
1100 * register can hold for each plane is lower than the
1101 * minimum FIFO size.
1102 */
1103 switch (plane_id) {
1104 case PLANE_CURSOR:
1105 return 63;
1106 case PLANE_PRIMARY:
1107 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1108 case PLANE_SPRITE0:
1109 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1110 default:
1111 MISSING_CASE(plane_id);
1112 return 0;
1113 }
1114}
1115
1116static int g4x_fbc_fifo_size(int level)
1117{
1118 switch (level) {
1119 case G4X_WM_LEVEL_SR:
1120 return 7;
1121 case G4X_WM_LEVEL_HPLL:
1122 return 15;
1123 default:
1124 MISSING_CASE(level);
1125 return 0;
1126 }
1127}
1128
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001129static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1130 const struct intel_plane_state *plane_state,
1131 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001132{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001133 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001134 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1135 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01001136 &crtc_state->hw.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001137 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1138 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001139
1140 if (latency == 0)
1141 return USHRT_MAX;
1142
1143 if (!intel_wm_plane_visible(crtc_state, plane_state))
1144 return 0;
1145
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001146 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001147
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001148 /*
1149 * Not 100% sure which way ELK should go here as the
1150 * spec only says CL/CTG should assume 32bpp and BW
1151 * doesn't need to. But as these things followed the
1152 * mobile vs. desktop lines on gen3 as well, let's
1153 * assume ELK doesn't need this.
1154 *
1155 * The spec also fails to list such a restriction for
1156 * the HPLL watermark, which seems a little strange.
1157 * Let's use 32bpp for the HPLL watermark as well.
1158 */
1159 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1160 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001161 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001162
1163 clock = adjusted_mode->crtc_clock;
1164 htotal = adjusted_mode->crtc_htotal;
1165
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001166 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001167
1168 if (plane->id == PLANE_CURSOR) {
1169 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1170 } else if (plane->id == PLANE_PRIMARY &&
1171 level == G4X_WM_LEVEL_NORMAL) {
1172 wm = intel_wm_method1(clock, cpp, latency);
1173 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001174 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001175
1176 small = intel_wm_method1(clock, cpp, latency);
1177 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1178
1179 wm = min(small, large);
1180 }
1181
1182 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1183 width, cpp);
1184
1185 wm = DIV_ROUND_UP(wm, 64) + 2;
1186
Chris Wilson1a1f1282017-11-07 14:03:38 +00001187 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001188}
1189
1190static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1191 int level, enum plane_id plane_id, u16 value)
1192{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001193 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001194 bool dirty = false;
1195
1196 for (; level < intel_wm_num_levels(dev_priv); level++) {
1197 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1198
1199 dirty |= raw->plane[plane_id] != value;
1200 raw->plane[plane_id] = value;
1201 }
1202
1203 return dirty;
1204}
1205
1206static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1207 int level, u16 value)
1208{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001209 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001210 bool dirty = false;
1211
1212 /* NORMAL level doesn't have an FBC watermark */
1213 level = max(level, G4X_WM_LEVEL_SR);
1214
1215 for (; level < intel_wm_num_levels(dev_priv); level++) {
1216 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1217
1218 dirty |= raw->fbc != value;
1219 raw->fbc = value;
1220 }
1221
1222 return dirty;
1223}
1224
Maarten Lankhorstec193642019-06-28 10:55:17 +02001225static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1226 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001227 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001228
1229static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1230 const struct intel_plane_state *plane_state)
1231{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001232 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001233 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001234 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1235 enum plane_id plane_id = plane->id;
1236 bool dirty = false;
1237 int level;
1238
1239 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1240 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1241 if (plane_id == PLANE_PRIMARY)
1242 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1243 goto out;
1244 }
1245
1246 for (level = 0; level < num_levels; level++) {
1247 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1248 int wm, max_wm;
1249
1250 wm = g4x_compute_wm(crtc_state, plane_state, level);
1251 max_wm = g4x_plane_fifo_size(plane_id, level);
1252
1253 if (wm > max_wm)
1254 break;
1255
1256 dirty |= raw->plane[plane_id] != wm;
1257 raw->plane[plane_id] = wm;
1258
1259 if (plane_id != PLANE_PRIMARY ||
1260 level == G4X_WM_LEVEL_NORMAL)
1261 continue;
1262
1263 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1264 raw->plane[plane_id]);
1265 max_wm = g4x_fbc_fifo_size(level);
1266
1267 /*
1268 * FBC wm is not mandatory as we
1269 * can always just disable its use.
1270 */
1271 if (wm > max_wm)
1272 wm = USHRT_MAX;
1273
1274 dirty |= raw->fbc != wm;
1275 raw->fbc = wm;
1276 }
1277
1278 /* mark watermarks as invalid */
1279 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1280
1281 if (plane_id == PLANE_PRIMARY)
1282 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1283
1284 out:
1285 if (dirty) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001286 drm_dbg_kms(&dev_priv->drm,
1287 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1288 plane->base.name,
1289 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1290 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1291 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001292
1293 if (plane_id == PLANE_PRIMARY)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001294 drm_dbg_kms(&dev_priv->drm,
1295 "FBC watermarks: SR=%d, HPLL=%d\n",
1296 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1297 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001298 }
1299
1300 return dirty;
1301}
1302
1303static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1304 enum plane_id plane_id, int level)
1305{
1306 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1307
1308 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1309}
1310
1311static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1312 int level)
1313{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001314 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001315
1316 if (level > dev_priv->wm.max_level)
1317 return false;
1318
1319 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1320 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1321 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1322}
1323
1324/* mark all levels starting from 'level' as invalid */
1325static void g4x_invalidate_wms(struct intel_crtc *crtc,
1326 struct g4x_wm_state *wm_state, int level)
1327{
1328 if (level <= G4X_WM_LEVEL_NORMAL) {
1329 enum plane_id plane_id;
1330
1331 for_each_plane_id_on_crtc(crtc, plane_id)
1332 wm_state->wm.plane[plane_id] = USHRT_MAX;
1333 }
1334
1335 if (level <= G4X_WM_LEVEL_SR) {
1336 wm_state->cxsr = false;
1337 wm_state->sr.cursor = USHRT_MAX;
1338 wm_state->sr.plane = USHRT_MAX;
1339 wm_state->sr.fbc = USHRT_MAX;
1340 }
1341
1342 if (level <= G4X_WM_LEVEL_HPLL) {
1343 wm_state->hpll_en = false;
1344 wm_state->hpll.cursor = USHRT_MAX;
1345 wm_state->hpll.plane = USHRT_MAX;
1346 wm_state->hpll.fbc = USHRT_MAX;
1347 }
1348}
1349
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001350static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1351 int level)
1352{
1353 if (level < G4X_WM_LEVEL_SR)
1354 return false;
1355
1356 if (level >= G4X_WM_LEVEL_SR &&
1357 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1358 return false;
1359
1360 if (level >= G4X_WM_LEVEL_HPLL &&
1361 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1362 return false;
1363
1364 return true;
1365}
1366
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001367static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1368{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001369 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001370 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001371 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001372 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001373 int num_active_planes = hweight8(crtc_state->active_planes &
1374 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001375 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001376 const struct intel_plane_state *old_plane_state;
1377 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001378 struct intel_plane *plane;
1379 enum plane_id plane_id;
1380 int i, level;
1381 unsigned int dirty = 0;
1382
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001383 for_each_oldnew_intel_plane_in_state(state, plane,
1384 old_plane_state,
1385 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001386 if (new_plane_state->hw.crtc != &crtc->base &&
1387 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001388 continue;
1389
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001390 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001391 dirty |= BIT(plane->id);
1392 }
1393
1394 if (!dirty)
1395 return 0;
1396
1397 level = G4X_WM_LEVEL_NORMAL;
1398 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1399 goto out;
1400
1401 raw = &crtc_state->wm.g4x.raw[level];
1402 for_each_plane_id_on_crtc(crtc, plane_id)
1403 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1404
1405 level = G4X_WM_LEVEL_SR;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001406 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1407 goto out;
1408
1409 raw = &crtc_state->wm.g4x.raw[level];
1410 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1411 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1412 wm_state->sr.fbc = raw->fbc;
1413
1414 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1415
1416 level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001417 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1418 goto out;
1419
1420 raw = &crtc_state->wm.g4x.raw[level];
1421 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1422 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1423 wm_state->hpll.fbc = raw->fbc;
1424
1425 wm_state->hpll_en = wm_state->cxsr;
1426
1427 level++;
1428
1429 out:
1430 if (level == G4X_WM_LEVEL_NORMAL)
1431 return -EINVAL;
1432
1433 /* invalidate the higher levels */
1434 g4x_invalidate_wms(crtc, wm_state, level);
1435
1436 /*
1437 * Determine if the FBC watermark(s) can be used. IF
1438 * this isn't the case we prefer to disable the FBC
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001439 * watermark(s) rather than disable the SR/HPLL
1440 * level(s) entirely. 'level-1' is the highest valid
1441 * level here.
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001442 */
Ville Syrjäläfd7a9d82020-04-29 13:10:33 +03001443 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001444
1445 return 0;
1446}
1447
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001448static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001449{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001450 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301451 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001452 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1453 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1454 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001455 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001456 const struct intel_crtc_state *old_crtc_state =
1457 intel_atomic_get_old_crtc_state(intel_state, crtc);
1458 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001459 enum plane_id plane_id;
1460
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001461 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001462 *intermediate = *optimal;
1463
1464 intermediate->cxsr = false;
1465 intermediate->hpll_en = false;
1466 goto out;
1467 }
1468
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001469 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001470 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001471 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001472 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001473 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1474
1475 for_each_plane_id_on_crtc(crtc, plane_id) {
1476 intermediate->wm.plane[plane_id] =
1477 max(optimal->wm.plane[plane_id],
1478 active->wm.plane[plane_id]);
1479
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301480 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1481 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001482 }
1483
1484 intermediate->sr.plane = max(optimal->sr.plane,
1485 active->sr.plane);
1486 intermediate->sr.cursor = max(optimal->sr.cursor,
1487 active->sr.cursor);
1488 intermediate->sr.fbc = max(optimal->sr.fbc,
1489 active->sr.fbc);
1490
1491 intermediate->hpll.plane = max(optimal->hpll.plane,
1492 active->hpll.plane);
1493 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1494 active->hpll.cursor);
1495 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1496 active->hpll.fbc);
1497
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301498 drm_WARN_ON(&dev_priv->drm,
1499 (intermediate->sr.plane >
1500 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1501 intermediate->sr.cursor >
1502 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1503 intermediate->cxsr);
1504 drm_WARN_ON(&dev_priv->drm,
1505 (intermediate->sr.plane >
1506 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1507 intermediate->sr.cursor >
1508 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1509 intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001510
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301511 drm_WARN_ON(&dev_priv->drm,
1512 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1513 intermediate->fbc_en && intermediate->cxsr);
1514 drm_WARN_ON(&dev_priv->drm,
1515 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1516 intermediate->fbc_en && intermediate->hpll_en);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001517
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001518out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001519 /*
1520 * If our intermediate WM are identical to the final WM, then we can
1521 * omit the post-vblank programming; only update if it's different.
1522 */
1523 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001524 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001525
1526 return 0;
1527}
1528
1529static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1530 struct g4x_wm_values *wm)
1531{
1532 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001533 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001534
1535 wm->cxsr = true;
1536 wm->hpll_en = true;
1537 wm->fbc_en = true;
1538
1539 for_each_intel_crtc(&dev_priv->drm, crtc) {
1540 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1541
1542 if (!crtc->active)
1543 continue;
1544
1545 if (!wm_state->cxsr)
1546 wm->cxsr = false;
1547 if (!wm_state->hpll_en)
1548 wm->hpll_en = false;
1549 if (!wm_state->fbc_en)
1550 wm->fbc_en = false;
1551
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001552 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001553 }
1554
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001555 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001556 wm->cxsr = false;
1557 wm->hpll_en = false;
1558 wm->fbc_en = false;
1559 }
1560
1561 for_each_intel_crtc(&dev_priv->drm, crtc) {
1562 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1563 enum pipe pipe = crtc->pipe;
1564
1565 wm->pipe[pipe] = wm_state->wm;
1566 if (crtc->active && wm->cxsr)
1567 wm->sr = wm_state->sr;
1568 if (crtc->active && wm->hpll_en)
1569 wm->hpll = wm_state->hpll;
1570 }
1571}
1572
1573static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1574{
1575 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1576 struct g4x_wm_values new_wm = {};
1577
1578 g4x_merge_wm(dev_priv, &new_wm);
1579
1580 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1581 return;
1582
1583 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1584 _intel_set_memory_cxsr(dev_priv, false);
1585
1586 g4x_write_wm_values(dev_priv, &new_wm);
1587
1588 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1589 _intel_set_memory_cxsr(dev_priv, true);
1590
1591 *old_wm = new_wm;
1592}
1593
1594static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001595 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001596{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001597 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1598 const struct intel_crtc_state *crtc_state =
1599 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001600
1601 mutex_lock(&dev_priv->wm.wm_mutex);
1602 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1603 g4x_program_watermarks(dev_priv);
1604 mutex_unlock(&dev_priv->wm.wm_mutex);
1605}
1606
1607static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001608 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001609{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1611 const struct intel_crtc_state *crtc_state =
1612 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001613
1614 if (!crtc_state->wm.need_postvbl_update)
1615 return;
1616
1617 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001618 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001619 g4x_program_watermarks(dev_priv);
1620 mutex_unlock(&dev_priv->wm.wm_mutex);
1621}
1622
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001623/* latency must be in 0.1us units. */
1624static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001625 unsigned int htotal,
1626 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001627 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001628 unsigned int latency)
1629{
1630 unsigned int ret;
1631
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001632 ret = intel_wm_method2(pixel_rate, htotal,
1633 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001634 ret = DIV_ROUND_UP(ret, 64);
1635
1636 return ret;
1637}
1638
Ville Syrjäläbb726512016-10-31 22:37:24 +02001639static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001640{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001641 /* all latencies in usec */
1642 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1643
Ville Syrjälä58590c12015-09-08 21:05:12 +03001644 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1645
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001646 if (IS_CHERRYVIEW(dev_priv)) {
1647 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1648 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001649
1650 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001651 }
1652}
1653
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001654static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1655 const struct intel_plane_state *plane_state,
1656 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001657{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001658 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001659 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001660 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01001661 &crtc_state->hw.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001662 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001663
1664 if (dev_priv->wm.pri_latency[level] == 0)
1665 return USHRT_MAX;
1666
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001667 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001668 return 0;
1669
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001670 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001671 clock = adjusted_mode->crtc_clock;
1672 htotal = adjusted_mode->crtc_htotal;
1673 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001674
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001675 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001676 /*
1677 * FIXME the formula gives values that are
1678 * too big for the cursor FIFO, and hence we
1679 * would never be able to use cursors. For
1680 * now just hardcode the watermark.
1681 */
1682 wm = 63;
1683 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001684 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001685 dev_priv->wm.pri_latency[level] * 10);
1686 }
1687
Chris Wilson1a1f1282017-11-07 14:03:38 +00001688 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001689}
1690
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001691static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1692{
1693 return (active_planes & (BIT(PLANE_SPRITE0) |
1694 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1695}
1696
Ville Syrjälä5012e602017-03-02 19:14:56 +02001697static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001698{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001699 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301700 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001701 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001702 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001703 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001704 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001705 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001706 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001707 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001708 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001709 unsigned int total_rate;
1710 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001711
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001712 /*
1713 * When enabling sprite0 after sprite1 has already been enabled
1714 * we tend to get an underrun unless sprite0 already has some
1715 * FIFO space allcoated. Hence we always allocate at least one
1716 * cacheline for sprite0 whenever sprite1 is enabled.
1717 *
1718 * All other plane enable sequences appear immune to this problem.
1719 */
1720 if (vlv_need_sprite0_fifo_workaround(active_planes))
1721 sprite0_fifo_extra = 1;
1722
Ville Syrjälä5012e602017-03-02 19:14:56 +02001723 total_rate = raw->plane[PLANE_PRIMARY] +
1724 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001725 raw->plane[PLANE_SPRITE1] +
1726 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001727
Ville Syrjälä5012e602017-03-02 19:14:56 +02001728 if (total_rate > fifo_size)
1729 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001730
Ville Syrjälä5012e602017-03-02 19:14:56 +02001731 if (total_rate == 0)
1732 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001733
Ville Syrjälä5012e602017-03-02 19:14:56 +02001734 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001735 unsigned int rate;
1736
Ville Syrjälä5012e602017-03-02 19:14:56 +02001737 if ((active_planes & BIT(plane_id)) == 0) {
1738 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001739 continue;
1740 }
1741
Ville Syrjälä5012e602017-03-02 19:14:56 +02001742 rate = raw->plane[plane_id];
1743 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1744 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001745 }
1746
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001747 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1748 fifo_left -= sprite0_fifo_extra;
1749
Ville Syrjälä5012e602017-03-02 19:14:56 +02001750 fifo_state->plane[PLANE_CURSOR] = 63;
1751
1752 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001753
1754 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001755 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001756 int plane_extra;
1757
1758 if (fifo_left == 0)
1759 break;
1760
Ville Syrjälä5012e602017-03-02 19:14:56 +02001761 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001762 continue;
1763
1764 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001765 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001766 fifo_left -= plane_extra;
1767 }
1768
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301769 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001770
1771 /* give it all to the first plane if none are active */
1772 if (active_planes == 0) {
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05301773 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001774 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1775 }
1776
1777 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001778}
1779
Ville Syrjäläff32c542017-03-02 19:14:57 +02001780/* mark all levels starting from 'level' as invalid */
1781static void vlv_invalidate_wms(struct intel_crtc *crtc,
1782 struct vlv_wm_state *wm_state, int level)
1783{
1784 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1785
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001786 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001787 enum plane_id plane_id;
1788
1789 for_each_plane_id_on_crtc(crtc, plane_id)
1790 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1791
1792 wm_state->sr[level].cursor = USHRT_MAX;
1793 wm_state->sr[level].plane = USHRT_MAX;
1794 }
1795}
1796
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001797static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1798{
1799 if (wm > fifo_size)
1800 return USHRT_MAX;
1801 else
1802 return fifo_size - wm;
1803}
1804
Ville Syrjäläff32c542017-03-02 19:14:57 +02001805/*
1806 * Starting from 'level' set all higher
1807 * levels to 'value' in the "raw" watermarks.
1808 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001809static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001810 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001811{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001812 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001813 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001814 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001815
Ville Syrjäläff32c542017-03-02 19:14:57 +02001816 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001817 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001818
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001819 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001820 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001821 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001822
1823 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001824}
1825
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001826static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1827 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001828{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001829 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001830 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001831 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001832 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001833 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001834 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001836 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001837 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1838 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001839 }
1840
1841 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001842 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001843 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1844 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1845
Ville Syrjäläff32c542017-03-02 19:14:57 +02001846 if (wm > max_wm)
1847 break;
1848
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001849 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001850 raw->plane[plane_id] = wm;
1851 }
1852
1853 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001854 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001855
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001856out:
1857 if (dirty)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03001858 drm_dbg_kms(&dev_priv->drm,
1859 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1860 plane->base.name,
1861 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1862 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1863 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001864
1865 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001866}
1867
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001868static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1869 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001870{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001871 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001872 &crtc_state->wm.vlv.raw[level];
1873 const struct vlv_fifo_state *fifo_state =
1874 &crtc_state->wm.vlv.fifo_state;
1875
1876 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1877}
1878
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001879static 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 +02001880{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001881 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1882 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1883 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1884 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001885}
1886
1887static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001888{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001890 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001891 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001892 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001893 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001894 const struct vlv_fifo_state *fifo_state =
1895 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001896 int num_active_planes = hweight8(crtc_state->active_planes &
1897 ~BIT(PLANE_CURSOR));
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001898 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001899 const struct intel_plane_state *old_plane_state;
1900 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001901 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001902 enum plane_id plane_id;
1903 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001904 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001905
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001906 for_each_oldnew_intel_plane_in_state(state, plane,
1907 old_plane_state,
1908 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001909 if (new_plane_state->hw.crtc != &crtc->base &&
1910 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001911 continue;
1912
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001913 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001914 dirty |= BIT(plane->id);
1915 }
1916
1917 /*
1918 * DSPARB registers may have been reset due to the
1919 * power well being turned off. Make sure we restore
1920 * them to a consistent state even if no primary/sprite
1921 * planes are initially active.
1922 */
1923 if (needs_modeset)
1924 crtc_state->fifo_changed = true;
1925
1926 if (!dirty)
1927 return 0;
1928
1929 /* cursor changes don't warrant a FIFO recompute */
1930 if (dirty & ~BIT(PLANE_CURSOR)) {
1931 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001932 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001933 const struct vlv_fifo_state *old_fifo_state =
1934 &old_crtc_state->wm.vlv.fifo_state;
1935
1936 ret = vlv_compute_fifo(crtc_state);
1937 if (ret)
1938 return ret;
1939
1940 if (needs_modeset ||
1941 memcmp(old_fifo_state, fifo_state,
1942 sizeof(*fifo_state)) != 0)
1943 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001944 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001945
Ville Syrjäläff32c542017-03-02 19:14:57 +02001946 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001947 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001948 /*
1949 * Note that enabling cxsr with no primary/sprite planes
1950 * enabled can wedge the pipe. Hence we only allow cxsr
1951 * with exactly one enabled primary/sprite plane.
1952 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001953 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001954
Ville Syrjälä5012e602017-03-02 19:14:56 +02001955 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001956 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001957 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001958
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001959 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001960 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001961
Ville Syrjäläff32c542017-03-02 19:14:57 +02001962 for_each_plane_id_on_crtc(crtc, plane_id) {
1963 wm_state->wm[level].plane[plane_id] =
1964 vlv_invert_wm_value(raw->plane[plane_id],
1965 fifo_state->plane[plane_id]);
1966 }
1967
1968 wm_state->sr[level].plane =
1969 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001970 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001971 raw->plane[PLANE_SPRITE1]),
1972 sr_fifo_size);
1973
1974 wm_state->sr[level].cursor =
1975 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1976 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001977 }
1978
Ville Syrjäläff32c542017-03-02 19:14:57 +02001979 if (level == 0)
1980 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001981
Ville Syrjäläff32c542017-03-02 19:14:57 +02001982 /* limit to only levels we can actually handle */
1983 wm_state->num_levels = level;
1984
1985 /* invalidate the higher levels */
1986 vlv_invalidate_wms(crtc, wm_state, level);
1987
1988 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001989}
1990
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001991#define VLV_FIFO(plane, value) \
1992 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1993
Ville Syrjäläff32c542017-03-02 19:14:57 +02001994static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001995 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001996{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001997 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001998 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001999 const struct intel_crtc_state *crtc_state =
2000 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02002001 const struct vlv_fifo_state *fifo_state =
2002 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002003 int sprite0_start, sprite1_start, fifo_size;
Kees Cook2713eb42020-02-20 16:05:17 -08002004 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002005
Ville Syrjälä236c48e2017-03-02 19:14:58 +02002006 if (!crtc_state->fifo_changed)
2007 return;
2008
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02002009 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2010 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2011 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002012
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05302013 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2014 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002015
Ville Syrjäläc137d662017-03-02 19:15:06 +02002016 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2017
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002018 /*
2019 * uncore.lock serves a double purpose here. It allows us to
2020 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2021 * it protects the DSPARB registers from getting clobbered by
2022 * parallel updates from multiple pipes.
2023 *
2024 * intel_pipe_update_start() has already disabled interrupts
2025 * for us, so a plain spin_lock() is sufficient here.
2026 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002027 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002028
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002029 switch (crtc->pipe) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002030 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002031 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2032 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002033
2034 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2035 VLV_FIFO(SPRITEB, 0xff));
2036 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2037 VLV_FIFO(SPRITEB, sprite1_start));
2038
2039 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2040 VLV_FIFO(SPRITEB_HI, 0x1));
2041 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2042 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2043
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002044 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2045 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002046 break;
2047 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002048 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2049 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002050
2051 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2052 VLV_FIFO(SPRITED, 0xff));
2053 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2054 VLV_FIFO(SPRITED, sprite1_start));
2055
2056 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2057 VLV_FIFO(SPRITED_HI, 0xff));
2058 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2059 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2060
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002061 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2062 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002063 break;
2064 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002065 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2066 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002067
2068 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2069 VLV_FIFO(SPRITEF, 0xff));
2070 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2071 VLV_FIFO(SPRITEF, sprite1_start));
2072
2073 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2074 VLV_FIFO(SPRITEF_HI, 0xff));
2075 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2076 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2077
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002078 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2079 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002080 break;
2081 default:
2082 break;
2083 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002084
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002085 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002086
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002087 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002088}
2089
2090#undef VLV_FIFO
2091
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002092static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002093{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002094 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002095 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2096 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2097 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002098 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002099 const struct intel_crtc_state *old_crtc_state =
2100 intel_atomic_get_old_crtc_state(intel_state, crtc);
2101 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002102 int level;
2103
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002104 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002105 *intermediate = *optimal;
2106
2107 intermediate->cxsr = false;
2108 goto out;
2109 }
2110
Ville Syrjälä4841da52017-03-02 19:14:59 +02002111 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002112 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002113 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002114
2115 for (level = 0; level < intermediate->num_levels; level++) {
2116 enum plane_id plane_id;
2117
2118 for_each_plane_id_on_crtc(crtc, plane_id) {
2119 intermediate->wm[level].plane[plane_id] =
2120 min(optimal->wm[level].plane[plane_id],
2121 active->wm[level].plane[plane_id]);
2122 }
2123
2124 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2125 active->sr[level].plane);
2126 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2127 active->sr[level].cursor);
2128 }
2129
2130 vlv_invalidate_wms(crtc, intermediate, level);
2131
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002132out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002133 /*
2134 * If our intermediate WM are identical to the final WM, then we can
2135 * omit the post-vblank programming; only update if it's different.
2136 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002137 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002138 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002139
2140 return 0;
2141}
2142
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002143static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002144 struct vlv_wm_values *wm)
2145{
2146 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002147 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002148
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002149 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002150 wm->cxsr = true;
2151
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002152 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002153 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002154
2155 if (!crtc->active)
2156 continue;
2157
2158 if (!wm_state->cxsr)
2159 wm->cxsr = false;
2160
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002161 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002162 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2163 }
2164
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002165 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002166 wm->cxsr = false;
2167
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002168 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002169 wm->level = VLV_WM_LEVEL_PM2;
2170
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002171 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002172 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002173 enum pipe pipe = crtc->pipe;
2174
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002175 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002176 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002177 wm->sr = wm_state->sr[wm->level];
2178
Ville Syrjälä1b313892016-11-28 19:37:08 +02002179 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2180 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2181 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2182 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002183 }
2184}
2185
Ville Syrjäläff32c542017-03-02 19:14:57 +02002186static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002187{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002188 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2189 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002190
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002191 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002192
Ville Syrjäläff32c542017-03-02 19:14:57 +02002193 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002194 return;
2195
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002196 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002197 chv_set_memory_dvfs(dev_priv, false);
2198
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002199 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002200 chv_set_memory_pm5(dev_priv, false);
2201
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002202 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002203 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002204
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002205 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002206
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002207 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002208 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002209
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002210 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002211 chv_set_memory_pm5(dev_priv, true);
2212
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002213 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002214 chv_set_memory_dvfs(dev_priv, true);
2215
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002216 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002217}
2218
Ville Syrjäläff32c542017-03-02 19:14:57 +02002219static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002220 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002221{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002222 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2223 const struct intel_crtc_state *crtc_state =
2224 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002225
2226 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002227 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2228 vlv_program_watermarks(dev_priv);
2229 mutex_unlock(&dev_priv->wm.wm_mutex);
2230}
2231
2232static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002233 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002234{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002235 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2236 const struct intel_crtc_state *crtc_state =
2237 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002238
2239 if (!crtc_state->wm.need_postvbl_update)
2240 return;
2241
2242 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002243 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002244 vlv_program_watermarks(dev_priv);
2245 mutex_unlock(&dev_priv->wm.wm_mutex);
2246}
2247
Ville Syrjälä432081b2016-10-31 22:37:03 +02002248static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002249{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002250 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002251 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002252 int srwm = 1;
2253 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002254 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002255
2256 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002257 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002258 if (crtc) {
2259 /* self-refresh has much higher latency */
2260 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002261 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002262 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002263 const struct drm_framebuffer *fb =
2264 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002265 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002266 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002267 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002268 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002269 int entries;
2270
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002271 entries = intel_wm_method2(clock, htotal,
2272 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002273 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2274 srwm = I965_FIFO_SIZE - entries;
2275 if (srwm < 0)
2276 srwm = 1;
2277 srwm &= 0x1ff;
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002278 drm_dbg_kms(&dev_priv->drm,
2279 "self-refresh entries: %d, wm: %d\n",
2280 entries, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002281
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002282 entries = intel_wm_method2(clock, htotal,
2283 crtc->base.cursor->state->crtc_w, 4,
2284 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002285 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002286 i965_cursor_wm_info.cacheline_size) +
2287 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002288
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002289 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002290 if (cursor_sr > i965_cursor_wm_info.max_wm)
2291 cursor_sr = i965_cursor_wm_info.max_wm;
2292
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002293 drm_dbg_kms(&dev_priv->drm,
2294 "self-refresh watermark: display plane %d "
2295 "cursor %d\n", srwm, cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002296
Imre Deak98584252014-06-13 14:54:20 +03002297 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002298 } else {
Imre Deak98584252014-06-13 14:54:20 +03002299 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002300 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002301 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002302 }
2303
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002304 drm_dbg_kms(&dev_priv->drm,
2305 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2306 srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002307
2308 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002309 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2310 FW_WM(8, CURSORB) |
2311 FW_WM(8, PLANEB) |
2312 FW_WM(8, PLANEA));
2313 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2314 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002315 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002316 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002317
2318 if (cxsr_enabled)
2319 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002320}
2321
Ville Syrjäläf4998962015-03-10 17:02:21 +02002322#undef FW_WM
2323
Ville Syrjälä432081b2016-10-31 22:37:03 +02002324static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002325{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002326 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002327 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002328 u32 fwater_lo;
2329 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002330 int cwm, srwm = 1;
2331 int fifo_size;
2332 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002333 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002334
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002335 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002336 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002337 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002338 wm_info = &i915_wm_info;
2339 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002340 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002342 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2343 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002344 if (intel_crtc_active(crtc)) {
2345 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002346 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002347 const struct drm_framebuffer *fb =
2348 crtc->base.primary->state->fb;
2349 int cpp;
2350
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002351 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002352 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002353 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002354 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002355
Damien Lespiau241bfc32013-09-25 16:45:37 +01002356 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002357 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002358 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002359 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002360 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002361 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002362 if (planea_wm > (long)wm_info->max_wm)
2363 planea_wm = wm_info->max_wm;
2364 }
2365
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002366 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002367 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002368
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002369 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2370 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002371 if (intel_crtc_active(crtc)) {
2372 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002373 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002374 const struct drm_framebuffer *fb =
2375 crtc->base.primary->state->fb;
2376 int cpp;
2377
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002378 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002379 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002380 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002381 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002382
Damien Lespiau241bfc32013-09-25 16:45:37 +01002383 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002384 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002385 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002386 if (enabled == NULL)
2387 enabled = crtc;
2388 else
2389 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002390 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002391 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002392 if (planeb_wm > (long)wm_info->max_wm)
2393 planeb_wm = wm_info->max_wm;
2394 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002395
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002396 drm_dbg_kms(&dev_priv->drm,
2397 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002398
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002399 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002400 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002401
Ville Syrjäläefc26112016-10-31 22:37:04 +02002402 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002403
2404 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002405 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002406 enabled = NULL;
2407 }
2408
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002409 /*
2410 * Overlay gets an aggressive default since video jitter is bad.
2411 */
2412 cwm = 2;
2413
2414 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002415 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002416
2417 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002418 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002419 /* self-refresh has much higher latency */
2420 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002421 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002422 &enabled->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002423 const struct drm_framebuffer *fb =
2424 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002425 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002426 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002427 int hdisplay = enabled->config->pipe_src_w;
2428 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002429 int entries;
2430
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002431 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002432 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002433 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002434 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002435
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002436 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2437 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002438 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002439 drm_dbg_kms(&dev_priv->drm,
2440 "self-refresh entries: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002441 srwm = wm_info->fifo_size - entries;
2442 if (srwm < 0)
2443 srwm = 1;
2444
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002445 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002446 I915_WRITE(FW_BLC_SELF,
2447 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002448 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002449 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2450 }
2451
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002452 drm_dbg_kms(&dev_priv->drm,
2453 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2454 planea_wm, planeb_wm, cwm, srwm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002455
2456 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2457 fwater_hi = (cwm & 0x1f);
2458
2459 /* Set request length to 8 cachelines per fetch */
2460 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2461 fwater_hi = fwater_hi | (1 << 8);
2462
2463 I915_WRITE(FW_BLC, fwater_lo);
2464 I915_WRITE(FW_BLC2, fwater_hi);
2465
Imre Deak5209b1f2014-07-01 12:36:17 +03002466 if (enabled)
2467 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002468}
2469
Ville Syrjälä432081b2016-10-31 22:37:03 +02002470static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002471{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002472 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002473 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002474 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002475 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002476 int planea_wm;
2477
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002478 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002479 if (crtc == NULL)
2480 return;
2481
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002482 adjusted_mode = &crtc->config->hw.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002483 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002484 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002485 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002486 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002487 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2488 fwater_lo |= (3<<8) | planea_wm;
2489
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002490 drm_dbg_kms(&dev_priv->drm,
2491 "Setting FIFO watermarks - A: %d\n", planea_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002492
2493 I915_WRITE(FW_BLC, fwater_lo);
2494}
2495
Ville Syrjälä37126462013-08-01 16:18:55 +03002496/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002497static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2498 unsigned int cpp,
2499 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002500{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002501 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002502
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002503 ret = intel_wm_method1(pixel_rate, cpp, latency);
2504 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002505
2506 return ret;
2507}
2508
Ville Syrjälä37126462013-08-01 16:18:55 +03002509/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002510static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2511 unsigned int htotal,
2512 unsigned int width,
2513 unsigned int cpp,
2514 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002515{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002516 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002517
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002518 ret = intel_wm_method2(pixel_rate, htotal,
2519 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002520 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002521
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002522 return ret;
2523}
2524
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002525static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002526{
Matt Roper15126882015-12-03 11:37:40 -08002527 /*
2528 * Neither of these should be possible since this function shouldn't be
2529 * called if the CRTC is off or the plane is invisible. But let's be
2530 * extra paranoid to avoid a potential divide-by-zero if we screw up
2531 * elsewhere in the driver.
2532 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002533 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002534 return 0;
2535 if (WARN_ON(!horiz_pixels))
2536 return 0;
2537
Ville Syrjäläac484962016-01-20 21:05:26 +02002538 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002539}
2540
Imre Deak820c1982013-12-17 14:46:36 +02002541struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002542 u16 pri;
2543 u16 spr;
2544 u16 cur;
2545 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002546};
2547
Ville Syrjälä37126462013-08-01 16:18:55 +03002548/*
2549 * For both WM_PIPE and WM_LP.
2550 * mem_value must be in 0.1us units.
2551 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002552static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2553 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002554 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002555{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002556 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002557 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002558
Ville Syrjälä03981c62018-11-14 19:34:40 +02002559 if (mem_value == 0)
2560 return U32_MAX;
2561
Maarten Lankhorstec193642019-06-28 10:55:17 +02002562 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002563 return 0;
2564
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002565 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002566
Maarten Lankhorstec193642019-06-28 10:55:17 +02002567 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002568
2569 if (!is_lp)
2570 return method1;
2571
Maarten Lankhorstec193642019-06-28 10:55:17 +02002572 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002573 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002574 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002575 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002576
2577 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002578}
2579
Ville Syrjälä37126462013-08-01 16:18:55 +03002580/*
2581 * For both WM_PIPE and WM_LP.
2582 * mem_value must be in 0.1us units.
2583 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002584static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2585 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002586 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002587{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002588 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002589 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002590
Ville Syrjälä03981c62018-11-14 19:34:40 +02002591 if (mem_value == 0)
2592 return U32_MAX;
2593
Maarten Lankhorstec193642019-06-28 10:55:17 +02002594 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002595 return 0;
2596
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002597 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002598
Maarten Lankhorstec193642019-06-28 10:55:17 +02002599 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2600 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002601 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002602 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002603 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002604 return min(method1, method2);
2605}
2606
Ville Syrjälä37126462013-08-01 16:18:55 +03002607/*
2608 * For both WM_PIPE and WM_LP.
2609 * mem_value must be in 0.1us units.
2610 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002611static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2612 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002613 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002614{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002615 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002616
Ville Syrjälä03981c62018-11-14 19:34:40 +02002617 if (mem_value == 0)
2618 return U32_MAX;
2619
Maarten Lankhorstec193642019-06-28 10:55:17 +02002620 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002621 return 0;
2622
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002623 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002624
Maarten Lankhorstec193642019-06-28 10:55:17 +02002625 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002626 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002627 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002628 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002629}
2630
Paulo Zanonicca32e92013-05-31 11:45:06 -03002631/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002632static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2633 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002634 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002635{
Ville Syrjälä83054942016-11-18 21:53:00 +02002636 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002637
Maarten Lankhorstec193642019-06-28 10:55:17 +02002638 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002639 return 0;
2640
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002641 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002642
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002643 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2644 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002645}
2646
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002647static unsigned int
2648ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002649{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002650 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002651 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002652 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002653 return 768;
2654 else
2655 return 512;
2656}
2657
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002658static unsigned int
2659ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2660 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002661{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002662 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002663 /* BDW primary/sprite plane watermarks */
2664 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002665 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002666 /* IVB/HSW primary/sprite plane watermarks */
2667 return level == 0 ? 127 : 1023;
2668 else if (!is_sprite)
2669 /* ILK/SNB primary plane watermarks */
2670 return level == 0 ? 127 : 511;
2671 else
2672 /* ILK/SNB sprite plane watermarks */
2673 return level == 0 ? 63 : 255;
2674}
2675
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002676static unsigned int
2677ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002678{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002679 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002680 return level == 0 ? 63 : 255;
2681 else
2682 return level == 0 ? 31 : 63;
2683}
2684
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002685static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002686{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002687 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002688 return 31;
2689 else
2690 return 15;
2691}
2692
Ville Syrjälä158ae642013-08-07 13:28:19 +03002693/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002694static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002695 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002696 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002697 enum intel_ddb_partitioning ddb_partitioning,
2698 bool is_sprite)
2699{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002700 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002701
2702 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002703 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002704 return 0;
2705
2706 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002707 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002708 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002709
2710 /*
2711 * For some reason the non self refresh
2712 * FIFO size is only half of the self
2713 * refresh FIFO size on ILK/SNB.
2714 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002715 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002716 fifo_size /= 2;
2717 }
2718
Ville Syrjälä240264f2013-08-07 13:29:12 +03002719 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002720 /* level 0 is always calculated with 1:1 split */
2721 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2722 if (is_sprite)
2723 fifo_size *= 5;
2724 fifo_size /= 6;
2725 } else {
2726 fifo_size /= 2;
2727 }
2728 }
2729
2730 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002731 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002732}
2733
2734/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002735static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002736 int level,
2737 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002738{
2739 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002740 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002741 return 64;
2742
2743 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002744 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002745}
2746
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002747static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002748 int level,
2749 const struct intel_wm_config *config,
2750 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002751 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002752{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002753 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2754 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2755 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2756 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002757}
2758
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002759static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002760 int level,
2761 struct ilk_wm_maximums *max)
2762{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002763 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2764 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2765 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2766 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002767}
2768
Ville Syrjäläd9395652013-10-09 19:18:10 +03002769static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002770 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002771 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002772{
2773 bool ret;
2774
2775 /* already determined to be invalid? */
2776 if (!result->enable)
2777 return false;
2778
2779 result->enable = result->pri_val <= max->pri &&
2780 result->spr_val <= max->spr &&
2781 result->cur_val <= max->cur;
2782
2783 ret = result->enable;
2784
2785 /*
2786 * HACK until we can pre-compute everything,
2787 * and thus fail gracefully if LP0 watermarks
2788 * are exceeded...
2789 */
2790 if (level == 0 && !result->enable) {
2791 if (result->pri_val > max->pri)
2792 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2793 level, result->pri_val, max->pri);
2794 if (result->spr_val > max->spr)
2795 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2796 level, result->spr_val, max->spr);
2797 if (result->cur_val > max->cur)
2798 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2799 level, result->cur_val, max->cur);
2800
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002801 result->pri_val = min_t(u32, result->pri_val, max->pri);
2802 result->spr_val = min_t(u32, result->spr_val, max->spr);
2803 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002804 result->enable = true;
2805 }
2806
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002807 return ret;
2808}
2809
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002810static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02002811 const struct intel_crtc *crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002812 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002813 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002814 const struct intel_plane_state *pristate,
2815 const struct intel_plane_state *sprstate,
2816 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002817 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002818{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002819 u16 pri_latency = dev_priv->wm.pri_latency[level];
2820 u16 spr_latency = dev_priv->wm.spr_latency[level];
2821 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002822
2823 /* WM1+ latency values stored in 0.5us units */
2824 if (level > 0) {
2825 pri_latency *= 5;
2826 spr_latency *= 5;
2827 cur_latency *= 5;
2828 }
2829
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002830 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002831 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002832 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002833 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002834 }
2835
2836 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002837 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002838
2839 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002840 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002841
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002842 result->enable = true;
2843}
2844
Ville Syrjäläbb726512016-10-31 22:37:24 +02002845static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002846 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002847{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002848 struct intel_uncore *uncore = &dev_priv->uncore;
2849
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002850 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002851 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002852 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002853 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002854
2855 /* read the first set of memory latencies[0:3] */
2856 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002857 ret = sandybridge_pcode_read(dev_priv,
2858 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002859 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002860
2861 if (ret) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03002862 drm_err(&dev_priv->drm,
2863 "SKL Mailbox read error = %d\n", ret);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002864 return;
2865 }
2866
2867 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2868 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2869 GEN9_MEM_LATENCY_LEVEL_MASK;
2870 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2871 GEN9_MEM_LATENCY_LEVEL_MASK;
2872 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2873 GEN9_MEM_LATENCY_LEVEL_MASK;
2874
2875 /* read the second set of memory latencies[4:7] */
2876 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002877 ret = sandybridge_pcode_read(dev_priv,
2878 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002879 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002880 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
2886 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2887 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2888 GEN9_MEM_LATENCY_LEVEL_MASK;
2889 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2890 GEN9_MEM_LATENCY_LEVEL_MASK;
2891 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2892 GEN9_MEM_LATENCY_LEVEL_MASK;
2893
Vandana Kannan367294b2014-11-04 17:06:46 +00002894 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002895 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2896 * need to be disabled. We make sure to sanitize the values out
2897 * of the punit to satisfy this requirement.
2898 */
2899 for (level = 1; level <= max_level; level++) {
2900 if (wm[level] == 0) {
2901 for (i = level + 1; i <= max_level; i++)
2902 wm[i] = 0;
2903 break;
2904 }
2905 }
2906
2907 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002908 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002909 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002910 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002911 * to add 2us to the various latency levels we retrieve from the
2912 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002913 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002914 if (wm[0] == 0) {
2915 wm[0] += 2;
2916 for (level = 1; level <= max_level; level++) {
2917 if (wm[level] == 0)
2918 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002919 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002920 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002921 }
2922
Mahesh Kumar86b59282018-08-31 16:39:42 +05302923 /*
2924 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2925 * If we could not get dimm info enable this WA to prevent from
2926 * any underrun. If not able to get Dimm info assume 16GB dimm
2927 * to avoid any underrun.
2928 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002929 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302930 wm[0] += 1;
2931
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002932 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002933 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002934
2935 wm[0] = (sskpd >> 56) & 0xFF;
2936 if (wm[0] == 0)
2937 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002938 wm[1] = (sskpd >> 4) & 0xFF;
2939 wm[2] = (sskpd >> 12) & 0xFF;
2940 wm[3] = (sskpd >> 20) & 0x1FF;
2941 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002942 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002943 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002944
2945 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2946 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2947 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2948 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002949 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002950 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002951
2952 /* ILK primary LP0 latency is 700 ns */
2953 wm[0] = 7;
2954 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2955 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002956 } else {
2957 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002958 }
2959}
2960
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002961static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002962 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002963{
2964 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002965 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002966 wm[0] = 13;
2967}
2968
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002969static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002970 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002971{
2972 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002973 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002974 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002975}
2976
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002977int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002978{
2979 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002980 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002981 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002982 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002983 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002984 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002985 return 3;
2986 else
2987 return 2;
2988}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002989
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002990static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002991 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002992 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002993{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002994 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002995
2996 for (level = 0; level <= max_level; level++) {
2997 unsigned int latency = wm[level];
2998
2999 if (latency == 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003000 drm_dbg_kms(&dev_priv->drm,
3001 "%s WM%d latency not provided\n",
3002 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003003 continue;
3004 }
3005
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003006 /*
3007 * - latencies are in us on gen9.
3008 * - before then, WM1+ latency values are in 0.5us units
3009 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07003010 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003011 latency *= 10;
3012 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003013 latency *= 5;
3014
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003015 drm_dbg_kms(&dev_priv->drm,
3016 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3017 wm[level], latency / 10, latency % 10);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003018 }
3019}
3020
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003021static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003022 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003023{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003024 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003025
3026 if (wm[0] >= min)
3027 return false;
3028
3029 wm[0] = max(wm[0], min);
3030 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003031 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003032
3033 return true;
3034}
3035
Ville Syrjäläbb726512016-10-31 22:37:24 +02003036static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003037{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003038 bool changed;
3039
3040 /*
3041 * The BIOS provided WM memory latency values are often
3042 * inadequate for high resolution displays. Adjust them.
3043 */
3044 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3045 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3046 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3047
3048 if (!changed)
3049 return;
3050
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003051 drm_dbg_kms(&dev_priv->drm,
3052 "WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003053 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3054 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3055 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003056}
3057
Ville Syrjälä03981c62018-11-14 19:34:40 +02003058static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3059{
3060 /*
3061 * On some SNB machines (Thinkpad X220 Tablet at least)
3062 * LP3 usage can cause vblank interrupts to be lost.
3063 * The DEIIR bit will go high but it looks like the CPU
3064 * never gets interrupted.
3065 *
3066 * It's not clear whether other interrupt source could
3067 * be affected or if this is somehow limited to vblank
3068 * interrupts only. To play it safe we disable LP3
3069 * watermarks entirely.
3070 */
3071 if (dev_priv->wm.pri_latency[3] == 0 &&
3072 dev_priv->wm.spr_latency[3] == 0 &&
3073 dev_priv->wm.cur_latency[3] == 0)
3074 return;
3075
3076 dev_priv->wm.pri_latency[3] = 0;
3077 dev_priv->wm.spr_latency[3] = 0;
3078 dev_priv->wm.cur_latency[3] = 0;
3079
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003080 drm_dbg_kms(&dev_priv->drm,
3081 "LP3 watermarks disabled due to potential for lost interrupts\n");
Ville Syrjälä03981c62018-11-14 19:34:40 +02003082 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3083 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3084 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3085}
3086
Ville Syrjäläbb726512016-10-31 22:37:24 +02003087static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003088{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003089 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003090
3091 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3092 sizeof(dev_priv->wm.pri_latency));
3093 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3094 sizeof(dev_priv->wm.pri_latency));
3095
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003096 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003097 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003098
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003099 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3100 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3101 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003102
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003103 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003104 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003105 snb_wm_lp3_irq_quirk(dev_priv);
3106 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003107}
3108
Ville Syrjäläbb726512016-10-31 22:37:24 +02003109static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003110{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003111 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003112 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003113}
3114
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003115static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003116 struct intel_pipe_wm *pipe_wm)
3117{
3118 /* LP0 watermark maximums depend on this pipe alone */
3119 const struct intel_wm_config config = {
3120 .num_pipes_active = 1,
3121 .sprites_enabled = pipe_wm->sprites_enabled,
3122 .sprites_scaled = pipe_wm->sprites_scaled,
3123 };
3124 struct ilk_wm_maximums max;
3125
3126 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003127 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003128
3129 /* At least LP0 must be valid */
3130 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003131 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
Matt Ropered4a6a72016-02-23 17:20:13 -08003132 return false;
3133 }
3134
3135 return true;
3136}
3137
Matt Roper261a27d2015-10-08 15:28:25 -07003138/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003139static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003140{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003141 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003142 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003143 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003144 struct intel_plane *plane;
3145 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003146 const struct intel_plane_state *pristate = NULL;
3147 const struct intel_plane_state *sprstate = NULL;
3148 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003149 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003150 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003151
Maarten Lankhorstec193642019-06-28 10:55:17 +02003152 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003153
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003154 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3155 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3156 pristate = plane_state;
3157 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3158 sprstate = plane_state;
3159 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3160 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003161 }
3162
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003163 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003164 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003165 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3166 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3167 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3168 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003169 }
3170
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003171 usable_level = max_level;
3172
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003173 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003174 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003175 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003176
3177 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003178 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003179 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003180
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003181 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003182 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003183 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003184
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003185 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003186 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003187
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003188 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003189
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003190 for (level = 1; level <= usable_level; level++) {
3191 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003192
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02003193 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003194 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003195
3196 /*
3197 * Disable any watermark level that exceeds the
3198 * register maximums since such watermarks are
3199 * always invalid.
3200 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003201 if (!ilk_validate_wm_level(level, &max, wm)) {
3202 memset(wm, 0, sizeof(*wm));
3203 break;
3204 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003205 }
3206
Matt Roper86c8bbb2015-09-24 15:53:16 -07003207 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003208}
3209
3210/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003211 * Build a set of 'intermediate' watermark values that satisfy both the old
3212 * state and the new state. These can be programmed to the hardware
3213 * immediately.
3214 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003215static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003216{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003217 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->uapi.crtc);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003218 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003219 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003220 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003221 to_intel_atomic_state(newstate->uapi.state);
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003222 const struct intel_crtc_state *oldstate =
3223 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3224 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003225 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003226
3227 /*
3228 * Start with the final, target watermarks, then combine with the
3229 * currently active watermarks to get values that are safe both before
3230 * and after the vblank.
3231 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003232 *a = newstate->wm.ilk.optimal;
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003233 if (!newstate->hw.active || drm_atomic_crtc_needs_modeset(&newstate->uapi) ||
Ville Syrjäläf255c622018-11-08 17:10:13 +02003234 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003235 return 0;
3236
Matt Ropered4a6a72016-02-23 17:20:13 -08003237 a->pipe_enabled |= b->pipe_enabled;
3238 a->sprites_enabled |= b->sprites_enabled;
3239 a->sprites_scaled |= b->sprites_scaled;
3240
3241 for (level = 0; level <= max_level; level++) {
3242 struct intel_wm_level *a_wm = &a->wm[level];
3243 const struct intel_wm_level *b_wm = &b->wm[level];
3244
3245 a_wm->enable &= b_wm->enable;
3246 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3247 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3248 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3249 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3250 }
3251
3252 /*
3253 * We need to make sure that these merged watermark values are
3254 * actually a valid configuration themselves. If they're not,
3255 * there's no safe way to transition from the old state to
3256 * the new state, so we need to fail the atomic transaction.
3257 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003258 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003259 return -EINVAL;
3260
3261 /*
3262 * If our intermediate WM are identical to the final WM, then we can
3263 * omit the post-vblank programming; only update if it's different.
3264 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003265 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3266 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003267
3268 return 0;
3269}
3270
3271/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003272 * Merge the watermarks from all active pipes for a specific level.
3273 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003274static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003275 int level,
3276 struct intel_wm_level *ret_wm)
3277{
3278 const struct intel_crtc *intel_crtc;
3279
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003280 ret_wm->enable = true;
3281
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003282 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003283 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003284 const struct intel_wm_level *wm = &active->wm[level];
3285
3286 if (!active->pipe_enabled)
3287 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003288
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003289 /*
3290 * The watermark values may have been used in the past,
3291 * so we must maintain them in the registers for some
3292 * time even if the level is now disabled.
3293 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003294 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003295 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003296
3297 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3298 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3299 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3300 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3301 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003302}
3303
3304/*
3305 * Merge all low power watermarks for all active pipes.
3306 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003307static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003308 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003309 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003310 struct intel_pipe_wm *merged)
3311{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003312 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003313 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003314
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003315 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003316 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003317 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003318 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003319
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003320 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003321 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003322
3323 /* merge each WM1+ level */
3324 for (level = 1; level <= max_level; level++) {
3325 struct intel_wm_level *wm = &merged->wm[level];
3326
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003327 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003328
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003329 if (level > last_enabled_level)
3330 wm->enable = false;
3331 else if (!ilk_validate_wm_level(level, max, wm))
3332 /* make sure all following levels get disabled */
3333 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003334
3335 /*
3336 * The spec says it is preferred to disable
3337 * FBC WMs instead of disabling a WM level.
3338 */
3339 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003340 if (wm->enable)
3341 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003342 wm->fbc_val = 0;
3343 }
3344 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003345
3346 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3347 /*
3348 * FIXME this is racy. FBC might get enabled later.
3349 * What we should check here is whether FBC can be
3350 * enabled sometime later.
3351 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003352 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003353 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003354 for (level = 2; level <= max_level; level++) {
3355 struct intel_wm_level *wm = &merged->wm[level];
3356
3357 wm->enable = false;
3358 }
3359 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003360}
3361
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003362static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3363{
3364 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3365 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3366}
3367
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003368/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003369static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3370 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003371{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003372 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003373 return 2 * level;
3374 else
3375 return dev_priv->wm.pri_latency[level];
3376}
3377
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003378static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003379 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003380 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003381 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003382{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003383 struct intel_crtc *intel_crtc;
3384 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003385
Ville Syrjälä0362c782013-10-09 19:17:57 +03003386 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003387 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003388
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003389 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003390 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003391 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003392
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003393 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003394
Ville Syrjälä0362c782013-10-09 19:17:57 +03003395 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003396
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003397 /*
3398 * Maintain the watermark values even if the level is
3399 * disabled. Doing otherwise could cause underruns.
3400 */
3401 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003402 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003403 (r->pri_val << WM1_LP_SR_SHIFT) |
3404 r->cur_val;
3405
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003406 if (r->enable)
3407 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3408
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003409 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003410 results->wm_lp[wm_lp - 1] |=
3411 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3412 else
3413 results->wm_lp[wm_lp - 1] |=
3414 r->fbc_val << WM1_LP_FBC_SHIFT;
3415
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003416 /*
3417 * Always set WM1S_LP_EN when spr_val != 0, even if the
3418 * level is disabled. Doing otherwise could cause underruns.
3419 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003420 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303421 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003422 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3423 } else
3424 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003425 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003426
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003427 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003428 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003429 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä0560b0c2020-01-20 19:47:11 +02003430 const struct intel_pipe_wm *pipe_wm = &intel_crtc->wm.active.ilk;
3431 const struct intel_wm_level *r = &pipe_wm->wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003432
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05303433 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003434 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003435
3436 results->wm_pipe[pipe] =
3437 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3438 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3439 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003440 }
3441}
3442
Paulo Zanoni861f3382013-05-31 10:19:21 -03003443/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3444 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003445static struct intel_pipe_wm *
3446ilk_find_best_result(struct drm_i915_private *dev_priv,
3447 struct intel_pipe_wm *r1,
3448 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003449{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003450 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003451 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003452
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003453 for (level = 1; level <= max_level; level++) {
3454 if (r1->wm[level].enable)
3455 level1 = level;
3456 if (r2->wm[level].enable)
3457 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003458 }
3459
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003460 if (level1 == level2) {
3461 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003462 return r2;
3463 else
3464 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003465 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003466 return r1;
3467 } else {
3468 return r2;
3469 }
3470}
3471
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003472/* dirty bits used to track which watermarks need changes */
3473#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003474#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3475#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3476#define WM_DIRTY_FBC (1 << 24)
3477#define WM_DIRTY_DDB (1 << 25)
3478
Damien Lespiau055e3932014-08-18 13:49:10 +01003479static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003480 const struct ilk_wm_values *old,
3481 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003482{
3483 unsigned int dirty = 0;
3484 enum pipe pipe;
3485 int wm_lp;
3486
Damien Lespiau055e3932014-08-18 13:49:10 +01003487 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003488 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3489 dirty |= WM_DIRTY_PIPE(pipe);
3490 /* Must disable LP1+ watermarks too */
3491 dirty |= WM_DIRTY_LP_ALL;
3492 }
3493 }
3494
3495 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3496 dirty |= WM_DIRTY_FBC;
3497 /* Must disable LP1+ watermarks too */
3498 dirty |= WM_DIRTY_LP_ALL;
3499 }
3500
3501 if (old->partitioning != new->partitioning) {
3502 dirty |= WM_DIRTY_DDB;
3503 /* Must disable LP1+ watermarks too */
3504 dirty |= WM_DIRTY_LP_ALL;
3505 }
3506
3507 /* LP1+ watermarks already deemed dirty, no need to continue */
3508 if (dirty & WM_DIRTY_LP_ALL)
3509 return dirty;
3510
3511 /* Find the lowest numbered LP1+ watermark in need of an update... */
3512 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3513 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3514 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3515 break;
3516 }
3517
3518 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3519 for (; wm_lp <= 3; wm_lp++)
3520 dirty |= WM_DIRTY_LP(wm_lp);
3521
3522 return dirty;
3523}
3524
Ville Syrjälä8553c182013-12-05 15:51:39 +02003525static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3526 unsigned int dirty)
3527{
Imre Deak820c1982013-12-17 14:46:36 +02003528 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003529 bool changed = false;
3530
3531 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3532 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3533 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3534 changed = true;
3535 }
3536 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3537 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3538 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3539 changed = true;
3540 }
3541 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3542 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3543 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3544 changed = true;
3545 }
3546
3547 /*
3548 * Don't touch WM1S_LP_EN here.
3549 * Doing so could cause underruns.
3550 */
3551
3552 return changed;
3553}
3554
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003555/*
3556 * The spec says we shouldn't write when we don't need, because every write
3557 * causes WMs to be re-evaluated, expending some power.
3558 */
Imre Deak820c1982013-12-17 14:46:36 +02003559static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3560 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003561{
Imre Deak820c1982013-12-17 14:46:36 +02003562 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003563 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003564 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003565
Damien Lespiau055e3932014-08-18 13:49:10 +01003566 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003567 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003568 return;
3569
Ville Syrjälä8553c182013-12-05 15:51:39 +02003570 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003571
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003572 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003573 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003574 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003575 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003576 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003577 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3578
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003579 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003580 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003581 val = I915_READ(WM_MISC);
3582 if (results->partitioning == INTEL_DDB_PART_1_2)
3583 val &= ~WM_MISC_DATA_PARTITION_5_6;
3584 else
3585 val |= WM_MISC_DATA_PARTITION_5_6;
3586 I915_WRITE(WM_MISC, val);
3587 } else {
3588 val = I915_READ(DISP_ARB_CTL2);
3589 if (results->partitioning == INTEL_DDB_PART_1_2)
3590 val &= ~DISP_DATA_PARTITION_5_6;
3591 else
3592 val |= DISP_DATA_PARTITION_5_6;
3593 I915_WRITE(DISP_ARB_CTL2, val);
3594 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003595 }
3596
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003597 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003598 val = I915_READ(DISP_ARB_CTL);
3599 if (results->enable_fbc_wm)
3600 val &= ~DISP_FBC_WM_DIS;
3601 else
3602 val |= DISP_FBC_WM_DIS;
3603 I915_WRITE(DISP_ARB_CTL, val);
3604 }
3605
Imre Deak954911e2013-12-17 14:46:34 +02003606 if (dirty & WM_DIRTY_LP(1) &&
3607 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3608 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3609
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003610 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003611 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3612 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3613 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3614 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3615 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003616
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003617 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003618 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003619 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003620 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003621 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003622 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003623
3624 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003625}
3626
Ville Syrjälä60aca572019-11-27 21:05:51 +02003627bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003628{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003629 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3630}
3631
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003632u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303633{
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003634 int i;
3635 int max_slices = INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
3636 u8 enabled_slices_mask = 0;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303637
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003638 for (i = 0; i < max_slices; i++) {
3639 if (I915_READ(DBUF_CTL_S(i)) & DBUF_POWER_STATE)
3640 enabled_slices_mask |= BIT(i);
3641 }
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303642
Stanislav Lisovskiy0f0f9ae2020-02-03 01:06:29 +02003643 return enabled_slices_mask;
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303644}
3645
Matt Roper024c9042015-09-24 15:53:11 -07003646/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003647 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3648 * so assume we'll always need it in order to avoid underruns.
3649 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003650static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003651{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003652 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003653}
3654
Paulo Zanoni56feca92016-09-22 18:00:28 -03003655static bool
3656intel_has_sagv(struct drm_i915_private *dev_priv)
3657{
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003658 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3659 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003660}
3661
James Ausmusb068a862019-10-09 10:23:14 -07003662static void
3663skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3664{
James Ausmusda80f042019-10-09 10:23:15 -07003665 if (INTEL_GEN(dev_priv) >= 12) {
3666 u32 val = 0;
3667 int ret;
3668
3669 ret = sandybridge_pcode_read(dev_priv,
3670 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3671 &val, NULL);
3672 if (!ret) {
3673 dev_priv->sagv_block_time_us = val;
3674 return;
3675 }
3676
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003677 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
James Ausmusda80f042019-10-09 10:23:15 -07003678 } else if (IS_GEN(dev_priv, 11)) {
James Ausmusb068a862019-10-09 10:23:14 -07003679 dev_priv->sagv_block_time_us = 10;
3680 return;
3681 } else if (IS_GEN(dev_priv, 10)) {
3682 dev_priv->sagv_block_time_us = 20;
3683 return;
3684 } else if (IS_GEN(dev_priv, 9)) {
3685 dev_priv->sagv_block_time_us = 30;
3686 return;
3687 } else {
3688 MISSING_CASE(INTEL_GEN(dev_priv));
3689 }
3690
3691 /* Default to an unusable block time */
3692 dev_priv->sagv_block_time_us = -1;
3693}
3694
Lyude656d1b82016-08-17 15:55:54 -04003695/*
3696 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3697 * depending on power and performance requirements. The display engine access
3698 * to system memory is blocked during the adjustment time. Because of the
3699 * blocking time, having this enabled can cause full system hangs and/or pipe
3700 * underruns if we don't meet all of the following requirements:
3701 *
3702 * - <= 1 pipe enabled
3703 * - All planes can enable watermarks for latencies >= SAGV engine block time
3704 * - We're not using an interlaced display configuration
3705 */
3706int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003707intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003708{
3709 int ret;
3710
Paulo Zanoni56feca92016-09-22 18:00:28 -03003711 if (!intel_has_sagv(dev_priv))
3712 return 0;
3713
3714 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003715 return 0;
3716
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003717 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003718 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3719 GEN9_SAGV_ENABLE);
3720
Ville Syrjäläff61a972018-12-21 19:14:34 +02003721 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003722
3723 /*
3724 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003725 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003726 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003727 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003728 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003729 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003730 return 0;
3731 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003732 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003733 return ret;
3734 }
3735
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003736 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003737 return 0;
3738}
3739
Lyude656d1b82016-08-17 15:55:54 -04003740int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003741intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003742{
Imre Deakb3b8e992016-12-05 18:27:38 +02003743 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003744
Paulo Zanoni56feca92016-09-22 18:00:28 -03003745 if (!intel_has_sagv(dev_priv))
3746 return 0;
3747
3748 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003749 return 0;
3750
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003751 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003752 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003753 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3754 GEN9_SAGV_DISABLE,
3755 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3756 1);
Lyude656d1b82016-08-17 15:55:54 -04003757 /*
3758 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003759 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003760 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003761 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003762 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003763 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003764 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003765 } else if (ret < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03003766 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003767 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003768 }
3769
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003770 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003771 return 0;
3772}
3773
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003774void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3775{
3776 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003777 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003778 const struct intel_bw_state *old_bw_state;
3779 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003780
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003781 /*
3782 * Just return if we can't control SAGV or don't have it.
3783 * This is different from situation when we have SAGV but just can't
3784 * afford it due to DBuf limitation - in case if SAGV is completely
3785 * disabled in a BIOS, we are not even allowed to send a PCode request,
3786 * as it will throw an error. So have to check it here.
3787 */
3788 if (!intel_has_sagv(dev_priv))
3789 return;
3790
3791 new_bw_state = intel_atomic_get_new_bw_state(state);
3792 if (!new_bw_state)
3793 return;
3794
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003795 if (INTEL_GEN(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003796 intel_disable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003797 return;
3798 }
3799
3800 old_bw_state = intel_atomic_get_old_bw_state(state);
3801 /*
3802 * Nothing to mask
3803 */
3804 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3805 return;
3806
3807 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3808
3809 /*
3810 * If new mask is zero - means there is nothing to mask,
3811 * we can only unmask, which should be done in unmask.
3812 */
3813 if (!new_mask)
3814 return;
3815
3816 /*
3817 * Restrict required qgv points before updating the configuration.
3818 * According to BSpec we can't mask and unmask qgv points at the same
3819 * time. Also masking should be done before updating the configuration
3820 * and unmasking afterwards.
3821 */
3822 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003823}
3824
3825void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3826{
3827 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003828 const struct intel_bw_state *new_bw_state;
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003829 const struct intel_bw_state *old_bw_state;
3830 u32 new_mask = 0;
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003831
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003832 /*
3833 * Just return if we can't control SAGV or don't have it.
3834 * This is different from situation when we have SAGV but just can't
3835 * afford it due to DBuf limitation - in case if SAGV is completely
3836 * disabled in a BIOS, we are not even allowed to send a PCode request,
3837 * as it will throw an error. So have to check it here.
3838 */
3839 if (!intel_has_sagv(dev_priv))
3840 return;
3841
3842 new_bw_state = intel_atomic_get_new_bw_state(state);
3843 if (!new_bw_state)
3844 return;
3845
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003846 if (INTEL_GEN(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003847 intel_enable_sagv(dev_priv);
Stanislav Lisovskiy20f505f2020-05-14 10:48:52 +03003848 return;
3849 }
3850
3851 old_bw_state = intel_atomic_get_old_bw_state(state);
3852 /*
3853 * Nothing to unmask
3854 */
3855 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3856 return;
3857
3858 new_mask = new_bw_state->qgv_points_mask;
3859
3860 /*
3861 * Allow required qgv points after updating the configuration.
3862 * According to BSpec we can't mask and unmask qgv points at the same
3863 * time. Also masking should be done before updating the configuration
3864 * and unmasking afterwards.
3865 */
3866 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
Stanislav Lisovskiy680e1af2020-04-15 17:39:04 +03003867}
3868
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003869static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
Lyude656d1b82016-08-17 15:55:54 -04003870{
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003871 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003872 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003873 struct intel_plane *plane;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003874 const struct intel_plane_state *plane_state;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003875 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003876
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003877 if (!intel_has_sagv(dev_priv))
3878 return false;
3879
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003880 if (!crtc_state->hw.active)
Lyude656d1b82016-08-17 15:55:54 -04003881 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003882
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003883 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003884 return false;
3885
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003886 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003887 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003888 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003889
Lyude656d1b82016-08-17 15:55:54 -04003890 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003891 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003892 continue;
3893
3894 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003895 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003896 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003897 { }
3898
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003899 latency = dev_priv->wm.skl_latency[level];
3900
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003901 if (skl_needs_memory_bw_wa(dev_priv) &&
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003902 plane_state->uapi.fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003903 I915_FORMAT_MOD_X_TILED)
3904 latency += 15;
3905
Lyude656d1b82016-08-17 15:55:54 -04003906 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003907 * If any of the planes on this pipe don't enable wm levels that
3908 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003909 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003910 */
James Ausmusb068a862019-10-09 10:23:14 -07003911 if (latency < dev_priv->sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003912 return false;
3913 }
3914
3915 return true;
3916}
3917
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003918static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3919{
3920 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3921 enum plane_id plane_id;
3922
3923 if (!crtc_state->hw.active)
3924 return true;
3925
3926 for_each_plane_id_on_crtc(crtc, plane_id) {
3927 const struct skl_ddb_entry *plane_alloc =
3928 &crtc_state->wm.skl.plane_ddb_y[plane_id];
3929 const struct skl_plane_wm *wm =
3930 &crtc_state->wm.skl.optimal.planes[plane_id];
3931
3932 if (skl_ddb_entry_size(plane_alloc) < wm->sagv_wm0.min_ddb_alloc)
3933 return false;
3934 }
3935
3936 return true;
3937}
3938
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003939static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3940{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003941 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3942 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3943
3944 if (INTEL_GEN(dev_priv) >= 12)
3945 return tgl_crtc_can_enable_sagv(crtc_state);
3946 else
3947 return skl_crtc_can_enable_sagv(crtc_state);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003948}
3949
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003950bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3951 const struct intel_bw_state *bw_state)
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003952{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003953 if (INTEL_GEN(dev_priv) < 11 &&
3954 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003955 return false;
3956
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003957 return bw_state->pipe_sagv_reject == 0;
3958}
3959
3960static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3961{
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03003962 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003963 int ret;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003964 struct intel_crtc *crtc;
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003965 struct intel_crtc_state *new_crtc_state;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003966 struct intel_bw_state *new_bw_state = NULL;
3967 const struct intel_bw_state *old_bw_state = NULL;
3968 int i;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003969
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003970 for_each_new_intel_crtc_in_state(state, crtc,
3971 new_crtc_state, i) {
3972 new_bw_state = intel_atomic_get_bw_state(state);
3973 if (IS_ERR(new_bw_state))
3974 return PTR_ERR(new_bw_state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003975
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003976 old_bw_state = intel_atomic_get_old_bw_state(state);
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003977
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003978 if (intel_crtc_can_enable_sagv(new_crtc_state))
3979 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
3980 else
3981 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
3982 }
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003983
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03003984 if (!new_bw_state)
3985 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03003986
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003987 new_bw_state->active_pipes =
3988 intel_calc_active_pipes(state, old_bw_state->active_pipes);
Stanislav Lisovskiy1d0a6c82020-05-13 12:38:12 +03003989
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03003990 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
3991 ret = intel_atomic_lock_global_state(&new_bw_state->base);
3992 if (ret)
3993 return ret;
3994 }
3995
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03003996 for_each_new_intel_crtc_in_state(state, crtc,
3997 new_crtc_state, i) {
3998 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
3999
4000 /*
4001 * We store use_sagv_wm in the crtc state rather than relying on
4002 * that bw state since we have no convenient way to get at the
4003 * latter from the plane commit hooks (especially in the legacy
4004 * cursor case)
4005 */
4006 pipe_wm->use_sagv_wm = INTEL_GEN(dev_priv) >= 12 &&
4007 intel_can_enable_sagv(dev_priv, new_bw_state);
4008 }
4009
Stanislav Lisovskiyd8d5afe2020-05-13 12:38:13 +03004010 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4011 intel_can_enable_sagv(dev_priv, old_bw_state)) {
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03004012 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4013 if (ret)
4014 return ret;
4015 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4016 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4017 if (ret)
4018 return ret;
4019 }
4020
4021 return 0;
Stanislav Lisovskiya389c492020-04-15 17:57:40 +03004022}
4023
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004024/*
4025 * Calculate initial DBuf slice offset, based on slice size
4026 * and mask(i.e if slice size is 1024 and second slice is enabled
4027 * offset would be 1024)
4028 */
4029static unsigned int
4030icl_get_first_dbuf_slice_offset(u32 dbuf_slice_mask,
4031 u32 slice_size,
4032 u32 ddb_size)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304033{
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004034 unsigned int offset = 0;
4035
4036 if (!dbuf_slice_mask)
4037 return 0;
4038
4039 offset = (ffs(dbuf_slice_mask) - 1) * slice_size;
4040
4041 WARN_ON(offset >= ddb_size);
4042 return offset;
4043}
4044
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004045u16 intel_get_ddb_size(struct drm_i915_private *dev_priv)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004046{
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304047 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304048 drm_WARN_ON(&dev_priv->drm, ddb_size == 0);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304049
4050 if (INTEL_GEN(dev_priv) < 11)
4051 return ddb_size - 4; /* 4 blocks for bypass path allocation */
4052
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05304053 return ddb_size;
4054}
4055
Stanislav Lisovskiycd191542020-05-20 18:00:58 +03004056u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4057 const struct skl_ddb_entry *entry)
4058{
4059 u32 slice_mask = 0;
4060 u16 ddb_size = intel_get_ddb_size(dev_priv);
4061 u16 num_supported_slices = INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
4062 u16 slice_size = ddb_size / num_supported_slices;
4063 u16 start_slice;
4064 u16 end_slice;
4065
4066 if (!skl_ddb_entry_size(entry))
4067 return 0;
4068
4069 start_slice = entry->start / slice_size;
4070 end_slice = (entry->end - 1) / slice_size;
4071
4072 /*
4073 * Per plane DDB entry can in a really worst case be on multiple slices
4074 * but single entry is anyway contigious.
4075 */
4076 while (start_slice <= end_slice) {
4077 slice_mask |= BIT(start_slice);
4078 start_slice++;
4079 }
4080
4081 return slice_mask;
4082}
4083
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004084static u8 skl_compute_dbuf_slices(const struct intel_crtc_state *crtc_state,
Ville Syrjälä05e81552020-02-25 19:11:09 +02004085 u8 active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004086
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004087static int
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004088skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004089 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004090 const u64 total_data_rate,
Matt Roperc107acf2016-05-12 07:06:01 -07004091 struct skl_ddb_entry *alloc, /* out */
4092 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004093{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004094 struct drm_atomic_state *state = crtc_state->uapi.state;
Matt Roperc107acf2016-05-12 07:06:01 -07004095 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004096 struct drm_crtc *for_crtc = crtc_state->uapi.crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004097 const struct intel_crtc *crtc;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004098 u32 pipe_width = 0, total_width_in_range = 0, width_before_pipe_in_range = 0;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304099 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004100 struct intel_dbuf_state *new_dbuf_state =
4101 intel_atomic_get_new_dbuf_state(intel_state);
4102 const struct intel_dbuf_state *old_dbuf_state =
4103 intel_atomic_get_old_dbuf_state(intel_state);
4104 u8 active_pipes = new_dbuf_state->active_pipes;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304105 u16 ddb_size;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004106 u32 ddb_range_size;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304107 u32 i;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004108 u32 dbuf_slice_mask;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004109 u32 offset;
4110 u32 slice_size;
4111 u32 total_slice_mask;
4112 u32 start, end;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004113 int ret;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004114
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004115 *num_active = hweight8(active_pipes);
4116
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004117 if (!crtc_state->hw.active) {
4118 alloc->start = 0;
4119 alloc->end = 0;
4120 return 0;
4121 }
4122
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004123 ddb_size = intel_get_ddb_size(dev_priv);
4124
4125 slice_size = ddb_size / INTEL_INFO(dev_priv)->num_supported_dbuf_slices;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004126
Matt Roperc107acf2016-05-12 07:06:01 -07004127 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304128 * If the state doesn't change the active CRTC's or there is no
4129 * modeset request, then there's no need to recalculate;
4130 * the existing pipe allocation limits should remain unchanged.
4131 * Note that we're safe from racing commits since any racing commit
4132 * that changes the active CRTC list or do modeset would need to
4133 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07004134 */
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004135 if (old_dbuf_state->active_pipes == new_dbuf_state->active_pipes &&
4136 !dev_priv->wm.distrust_bios_wm) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01004137 /*
4138 * alloc may be cleared by clear_intel_crtc_state,
4139 * copy from old state to be sure
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004140 *
4141 * FIXME get rid of this mess
Maarten Lankhorst512b5522016-11-08 13:55:34 +01004142 */
4143 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004144 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004145 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07004146
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304147 /*
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004148 * Get allowed DBuf slices for correspondent pipe and platform.
4149 */
4150 dbuf_slice_mask = skl_compute_dbuf_slices(crtc_state, active_pipes);
4151
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004152 /*
4153 * Figure out at which DBuf slice we start, i.e if we start at Dbuf S2
4154 * and slice size is 1024, the offset would be 1024
4155 */
4156 offset = icl_get_first_dbuf_slice_offset(dbuf_slice_mask,
4157 slice_size, ddb_size);
4158
4159 /*
4160 * Figure out total size of allowed DBuf slices, which is basically
4161 * a number of allowed slices for that pipe multiplied by slice size.
4162 * Inside of this
4163 * range ddb entries are still allocated in proportion to display width.
4164 */
4165 ddb_range_size = hweight8(dbuf_slice_mask) * slice_size;
4166
4167 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304168 * Watermark/ddb requirement highly depends upon width of the
4169 * framebuffer, So instead of allocating DDB equally among pipes
4170 * distribute DDB based on resolution/width of the display.
4171 */
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004172 total_slice_mask = dbuf_slice_mask;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004173 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
4174 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004175 &crtc_state->hw.adjusted_mode;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004176 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304177 int hdisplay, vdisplay;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004178 u32 pipe_dbuf_slice_mask;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304179
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004180 if (!crtc_state->hw.active)
4181 continue;
4182
4183 pipe_dbuf_slice_mask = skl_compute_dbuf_slices(crtc_state,
4184 active_pipes);
4185
4186 /*
4187 * According to BSpec pipe can share one dbuf slice with another
4188 * pipes or pipe can use multiple dbufs, in both cases we
4189 * account for other pipes only if they have exactly same mask.
4190 * However we need to account how many slices we should enable
4191 * in total.
4192 */
4193 total_slice_mask |= pipe_dbuf_slice_mask;
4194
4195 /*
4196 * Do not account pipes using other slice sets
4197 * luckily as of current BSpec slice sets do not partially
4198 * intersect(pipes share either same one slice or same slice set
4199 * i.e no partial intersection), so it is enough to check for
4200 * equality for now.
4201 */
4202 if (dbuf_slice_mask != pipe_dbuf_slice_mask)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304203 continue;
4204
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304205 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004206
4207 total_width_in_range += hdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304208
4209 if (pipe < for_pipe)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004210 width_before_pipe_in_range += hdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05304211 else if (pipe == for_pipe)
4212 pipe_width = hdisplay;
4213 }
4214
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004215 /*
4216 * FIXME: For now we always enable slice S1 as per
4217 * the Bspec display initialization sequence.
4218 */
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004219 new_dbuf_state->enabled_slices = total_slice_mask | BIT(DBUF_S1);
4220
4221 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices) {
4222 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
4223 if (ret)
4224 return ret;
4225 }
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004226
4227 start = ddb_range_size * width_before_pipe_in_range / total_width_in_range;
4228 end = ddb_range_size *
4229 (width_before_pipe_in_range + pipe_width) / total_width_in_range;
4230
4231 alloc->start = offset + start;
4232 alloc->end = offset + end;
4233
Ville Syrjälä70b1a262020-02-25 19:11:16 +02004234 drm_dbg_kms(&dev_priv->drm,
4235 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x\n",
4236 for_crtc->base.id, for_crtc->name,
4237 dbuf_slice_mask, alloc->start, alloc->end, active_pipes);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004238
4239 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004240}
4241
Ville Syrjälädf331de2019-03-19 18:03:11 +02004242static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4243 int width, const struct drm_format_info *format,
4244 u64 modifier, unsigned int rotation,
4245 u32 plane_pixel_rate, struct skl_wm_params *wp,
4246 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004247static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004248 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004249 unsigned int latency,
Ville Syrjälädf331de2019-03-19 18:03:11 +02004250 const struct skl_wm_params *wp,
4251 const struct skl_wm_level *result_prev,
4252 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004253
Ville Syrjälädf331de2019-03-19 18:03:11 +02004254static unsigned int
4255skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4256 int num_active)
4257{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004258 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004259 int level, max_level = ilk_wm_max_level(dev_priv);
4260 struct skl_wm_level wm = {};
4261 int ret, min_ddb_alloc = 0;
4262 struct skl_wm_params wp;
4263
4264 ret = skl_compute_wm_params(crtc_state, 256,
4265 drm_format_info(DRM_FORMAT_ARGB8888),
4266 DRM_FORMAT_MOD_LINEAR,
4267 DRM_MODE_ROTATE_0,
4268 crtc_state->pixel_rate, &wp, 0);
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304269 drm_WARN_ON(&dev_priv->drm, ret);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004270
4271 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03004272 unsigned int latency = dev_priv->wm.skl_latency[level];
4273
4274 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02004275 if (wm.min_ddb_alloc == U16_MAX)
4276 break;
4277
4278 min_ddb_alloc = wm.min_ddb_alloc;
4279 }
4280
4281 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004282}
4283
Mahesh Kumar37cde112018-04-26 19:55:17 +05304284static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4285 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004286{
Mahesh Kumar37cde112018-04-26 19:55:17 +05304287
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004288 entry->start = reg & DDB_ENTRY_MASK;
4289 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304290
Damien Lespiau16160e32014-11-04 17:06:53 +00004291 if (entry->end)
4292 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004293}
4294
Mahesh Kumarddf34312018-04-09 09:11:03 +05304295static void
4296skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4297 const enum pipe pipe,
4298 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004299 struct skl_ddb_entry *ddb_y,
4300 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304301{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004302 u32 val, val2;
4303 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304304
4305 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4306 if (plane_id == PLANE_CURSOR) {
4307 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004308 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304309 return;
4310 }
4311
4312 val = I915_READ(PLANE_CTL(pipe, plane_id));
4313
4314 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004315 if (val & PLANE_CTL_ENABLE)
4316 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4317 val & PLANE_CTL_ORDER_RGBX,
4318 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304319
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004320 if (INTEL_GEN(dev_priv) >= 11) {
4321 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
4322 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4323 } else {
4324 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07004325 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304326
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004327 if (fourcc &&
4328 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004329 swap(val, val2);
4330
4331 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4332 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304333 }
4334}
4335
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004336void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4337 struct skl_ddb_entry *ddb_y,
4338 struct skl_ddb_entry *ddb_uv)
4339{
4340 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4341 enum intel_display_power_domain power_domain;
4342 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004343 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004344 enum plane_id plane_id;
4345
4346 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004347 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4348 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004349 return;
4350
4351 for_each_plane_id_on_crtc(crtc, plane_id)
4352 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4353 plane_id,
4354 &ddb_y[plane_id],
4355 &ddb_uv[plane_id]);
4356
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004357 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004358}
4359
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004360/*
4361 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4362 * The bspec defines downscale amount as:
4363 *
4364 * """
4365 * Horizontal down scale amount = maximum[1, Horizontal source size /
4366 * Horizontal destination size]
4367 * Vertical down scale amount = maximum[1, Vertical source size /
4368 * Vertical destination size]
4369 * Total down scale amount = Horizontal down scale amount *
4370 * Vertical down scale amount
4371 * """
4372 *
4373 * Return value is provided in 16.16 fixed point form to retain fractional part.
4374 * Caller should take care of dividing & rounding off the value.
4375 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304376static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004377skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4378 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004379{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304380 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004381 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304382 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4383 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004384
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05304385 if (drm_WARN_ON(&dev_priv->drm,
4386 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304387 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004388
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004389 /*
4390 * Src coordinates are already rotated by 270 degrees for
4391 * the 90/270 degree plane rotation cases (to match the
4392 * GTT mapping), hence no need to account for rotation here.
4393 *
4394 * n.b., src is 16.16 fixed point, dst is whole integer.
4395 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004396 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4397 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4398 dst_w = drm_rect_width(&plane_state->uapi.dst);
4399 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004400
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304401 fp_w_ratio = div_fixed16(src_w, dst_w);
4402 fp_h_ratio = div_fixed16(src_h, dst_h);
4403 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4404 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004405
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304406 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004407}
4408
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004409struct dbuf_slice_conf_entry {
4410 u8 active_pipes;
4411 u8 dbuf_mask[I915_MAX_PIPES];
4412};
4413
4414/*
4415 * Table taken from Bspec 12716
4416 * Pipes do have some preferred DBuf slice affinity,
4417 * plus there are some hardcoded requirements on how
4418 * those should be distributed for multipipe scenarios.
4419 * For more DBuf slices algorithm can get even more messy
4420 * and less readable, so decided to use a table almost
4421 * as is from BSpec itself - that way it is at least easier
4422 * to compare, change and check.
4423 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004424static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004425/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4426{
4427 {
4428 .active_pipes = BIT(PIPE_A),
4429 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004430 [PIPE_A] = BIT(DBUF_S1),
4431 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004432 },
4433 {
4434 .active_pipes = BIT(PIPE_B),
4435 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004436 [PIPE_B] = BIT(DBUF_S1),
4437 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004438 },
4439 {
4440 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4441 .dbuf_mask = {
4442 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004443 [PIPE_B] = BIT(DBUF_S2),
4444 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004445 },
4446 {
4447 .active_pipes = BIT(PIPE_C),
4448 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004449 [PIPE_C] = BIT(DBUF_S2),
4450 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004451 },
4452 {
4453 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4454 .dbuf_mask = {
4455 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004456 [PIPE_C] = BIT(DBUF_S2),
4457 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004458 },
4459 {
4460 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4461 .dbuf_mask = {
4462 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004463 [PIPE_C] = BIT(DBUF_S2),
4464 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004465 },
4466 {
4467 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4468 .dbuf_mask = {
4469 [PIPE_A] = BIT(DBUF_S1),
4470 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004471 [PIPE_C] = BIT(DBUF_S2),
4472 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004473 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004474 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004475};
4476
4477/*
4478 * Table taken from Bspec 49255
4479 * Pipes do have some preferred DBuf slice affinity,
4480 * plus there are some hardcoded requirements on how
4481 * those should be distributed for multipipe scenarios.
4482 * For more DBuf slices algorithm can get even more messy
4483 * and less readable, so decided to use a table almost
4484 * as is from BSpec itself - that way it is at least easier
4485 * to compare, change and check.
4486 */
Jani Nikulaf8226d02020-02-19 17:45:42 +02004487static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004488/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4489{
4490 {
4491 .active_pipes = BIT(PIPE_A),
4492 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004493 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4494 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004495 },
4496 {
4497 .active_pipes = BIT(PIPE_B),
4498 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004499 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4500 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004501 },
4502 {
4503 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4504 .dbuf_mask = {
4505 [PIPE_A] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004506 [PIPE_B] = BIT(DBUF_S1),
4507 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004508 },
4509 {
4510 .active_pipes = BIT(PIPE_C),
4511 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004512 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4513 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004514 },
4515 {
4516 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4517 .dbuf_mask = {
4518 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004519 [PIPE_C] = BIT(DBUF_S2),
4520 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004521 },
4522 {
4523 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4524 .dbuf_mask = {
4525 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004526 [PIPE_C] = BIT(DBUF_S2),
4527 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004528 },
4529 {
4530 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4531 .dbuf_mask = {
4532 [PIPE_A] = BIT(DBUF_S1),
4533 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004534 [PIPE_C] = BIT(DBUF_S2),
4535 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004536 },
4537 {
4538 .active_pipes = BIT(PIPE_D),
4539 .dbuf_mask = {
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004540 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4541 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004542 },
4543 {
4544 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4545 .dbuf_mask = {
4546 [PIPE_A] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004547 [PIPE_D] = BIT(DBUF_S2),
4548 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004549 },
4550 {
4551 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4552 .dbuf_mask = {
4553 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004554 [PIPE_D] = BIT(DBUF_S2),
4555 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004556 },
4557 {
4558 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4559 .dbuf_mask = {
4560 [PIPE_A] = BIT(DBUF_S1),
4561 [PIPE_B] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004562 [PIPE_D] = BIT(DBUF_S2),
4563 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004564 },
4565 {
4566 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4567 .dbuf_mask = {
4568 [PIPE_C] = BIT(DBUF_S1),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004569 [PIPE_D] = BIT(DBUF_S2),
4570 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004571 },
4572 {
4573 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4574 .dbuf_mask = {
4575 [PIPE_A] = BIT(DBUF_S1),
4576 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004577 [PIPE_D] = BIT(DBUF_S2),
4578 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004579 },
4580 {
4581 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4582 .dbuf_mask = {
4583 [PIPE_B] = BIT(DBUF_S1),
4584 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004585 [PIPE_D] = BIT(DBUF_S2),
4586 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004587 },
4588 {
4589 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4590 .dbuf_mask = {
4591 [PIPE_A] = BIT(DBUF_S1),
4592 [PIPE_B] = BIT(DBUF_S1),
4593 [PIPE_C] = BIT(DBUF_S2),
Ville Syrjälä06812bd2020-02-25 19:11:08 +02004594 [PIPE_D] = BIT(DBUF_S2),
4595 },
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004596 },
Ville Syrjälä05e81552020-02-25 19:11:09 +02004597 {}
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004598};
4599
Ville Syrjälä05e81552020-02-25 19:11:09 +02004600static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4601 const struct dbuf_slice_conf_entry *dbuf_slices)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004602{
4603 int i;
4604
Ville Syrjälä05e81552020-02-25 19:11:09 +02004605 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004606 if (dbuf_slices[i].active_pipes == active_pipes)
4607 return dbuf_slices[i].dbuf_mask[pipe];
4608 }
4609 return 0;
4610}
4611
4612/*
4613 * This function finds an entry with same enabled pipe configuration and
4614 * returns correspondent DBuf slice mask as stated in BSpec for particular
4615 * platform.
4616 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004617static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004618{
4619 /*
4620 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4621 * required calculating "pipe ratio" in order to determine
4622 * if one or two slices can be used for single pipe configurations
4623 * as additional constraint to the existing table.
4624 * However based on recent info, it should be not "pipe ratio"
4625 * but rather ratio between pixel_rate and cdclk with additional
4626 * constants, so for now we are using only table until this is
4627 * clarified. Also this is the reason why crtc_state param is
4628 * still here - we will need it once those additional constraints
4629 * pop up.
4630 */
Ville Syrjälä05e81552020-02-25 19:11:09 +02004631 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004632}
4633
Ville Syrjälä05e81552020-02-25 19:11:09 +02004634static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004635{
Ville Syrjälä05e81552020-02-25 19:11:09 +02004636 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004637}
4638
4639static u8 skl_compute_dbuf_slices(const struct intel_crtc_state *crtc_state,
Ville Syrjälä05e81552020-02-25 19:11:09 +02004640 u8 active_pipes)
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004641{
4642 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4643 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4644 enum pipe pipe = crtc->pipe;
4645
4646 if (IS_GEN(dev_priv, 12))
Ville Syrjälä05e81552020-02-25 19:11:09 +02004647 return tgl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004648 else if (IS_GEN(dev_priv, 11))
Ville Syrjälä05e81552020-02-25 19:11:09 +02004649 return icl_compute_dbuf_slices(pipe, active_pipes);
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004650 /*
4651 * For anything else just return one slice yet.
4652 * Should be extended for other platforms.
4653 */
Ville Syrjälä2f9078c2020-02-25 19:11:10 +02004654 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
Stanislav Lisovskiyff2cd862020-02-03 01:06:30 +02004655}
4656
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004657static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004658skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4659 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004660 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004661{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004662 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004663 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004664 u32 data_rate;
4665 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304666 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004667 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004668
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004669 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004670 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004671
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004672 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004673 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004674
4675 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004676 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004677 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004678
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004679 /*
4680 * Src coordinates are already rotated by 270 degrees for
4681 * the 90/270 degree plane rotation cases (to match the
4682 * GTT mapping), hence no need to account for rotation here.
4683 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004684 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4685 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004686
Mahesh Kumarb879d582018-04-09 09:11:01 +05304687 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004688 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304689 width /= 2;
4690 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004691 }
4692
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004693 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304694
Maarten Lankhorstec193642019-06-28 10:55:17 +02004695 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004696
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004697 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4698
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004699 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004700 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004701}
4702
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004703static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004704skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004705 u64 *plane_data_rate,
4706 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004707{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004708 struct intel_plane *plane;
4709 const struct intel_plane_state *plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004710 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004711
Matt Ropera1de91e2016-05-12 07:05:57 -07004712 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004713 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4714 enum plane_id plane_id = plane->id;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004715 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004716
Mahesh Kumarb879d582018-04-09 09:11:01 +05304717 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004718 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004719 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004720 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004721
Mahesh Kumarb879d582018-04-09 09:11:01 +05304722 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004723 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304724 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004725 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004726 }
4727
4728 return total_data_rate;
4729}
4730
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004731static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004732icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004733 u64 *plane_data_rate)
4734{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004735 struct intel_plane *plane;
4736 const struct intel_plane_state *plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004737 u64 total_data_rate = 0;
4738
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004739 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004740 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4741 enum plane_id plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004742 u64 rate;
4743
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004744 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004745 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004746 plane_data_rate[plane_id] = rate;
4747 total_data_rate += rate;
4748 } else {
4749 enum plane_id y_plane_id;
4750
4751 /*
4752 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004753 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004754 * and needs the master plane state which may be
4755 * NULL if we try get_new_plane_state(), so we
4756 * always calculate from the master.
4757 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004758 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004759 continue;
4760
4761 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004762 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004763 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004764 plane_data_rate[y_plane_id] = rate;
4765 total_data_rate += rate;
4766
Maarten Lankhorstec193642019-06-28 10:55:17 +02004767 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004768 plane_data_rate[plane_id] = rate;
4769 total_data_rate += rate;
4770 }
4771 }
4772
4773 return total_data_rate;
4774}
4775
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004776static const struct skl_wm_level *
4777skl_plane_wm_level(const struct intel_crtc_state *crtc_state,
4778 enum plane_id plane_id,
4779 int level)
4780{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03004781 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
4782 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4783
4784 if (level == 0 && pipe_wm->use_sagv_wm)
4785 return &wm->sagv_wm0;
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03004786
4787 return &wm->wm[level];
4788}
4789
Matt Roperc107acf2016-05-12 07:06:01 -07004790static int
Stanislav Lisovskiy072fcc32020-02-03 01:06:25 +02004791skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004792{
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004793 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4794 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004795 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004796 u16 alloc_size, start = 0;
4797 u16 total[I915_MAX_PLANES] = {};
4798 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004799 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004800 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004801 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004802 u64 plane_data_rate[I915_MAX_PLANES] = {};
4803 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004804 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004805 int level;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004806 int ret;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004807
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004808 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004809 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4810 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004811
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004812 if (!crtc_state->hw.active) {
Ville Syrjäläb6a13a32020-05-18 15:13:54 +03004813 struct intel_atomic_state *state =
4814 to_intel_atomic_state(crtc_state->uapi.state);
4815 struct intel_dbuf_state *new_dbuf_state =
4816 intel_atomic_get_new_dbuf_state(state);
4817 const struct intel_dbuf_state *old_dbuf_state =
4818 intel_atomic_get_old_dbuf_state(state);
4819
4820 /*
4821 * FIXME hack to make sure we compute this sensibly when
4822 * turning off all the pipes. Otherwise we leave it at
4823 * whatever we had previously, and then runtime PM will
4824 * mess it up by turning off all but S1. Remove this
4825 * once the dbuf state computation flow becomes sane.
4826 */
4827 if (new_dbuf_state->active_pipes == 0) {
4828 new_dbuf_state->enabled_slices = BIT(DBUF_S1);
4829
4830 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices) {
4831 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
4832 if (ret)
4833 return ret;
4834 }
4835 }
4836
Lyudece0ba282016-09-15 10:46:35 -04004837 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004838 return 0;
4839 }
4840
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004841 if (INTEL_GEN(dev_priv) >= 11)
4842 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004843 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004844 plane_data_rate);
4845 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004846 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004847 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004848 plane_data_rate,
4849 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004850
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02004851 ret = skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state,
4852 total_data_rate,
4853 alloc, &num_active);
4854 if (ret)
4855 return ret;
4856
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004857 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304858 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004859 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004860
Matt Roperd8e87492018-12-11 09:31:07 -08004861 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004862 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004863 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004864 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004865 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004866 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004867
Matt Ropera1de91e2016-05-12 07:05:57 -07004868 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004869 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004870
Matt Roperd8e87492018-12-11 09:31:07 -08004871 /*
4872 * Find the highest watermark level for which we can satisfy the block
4873 * requirement of active planes.
4874 */
4875 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004876 blocks = 0;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004877 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004878 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004879 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004880
4881 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05304882 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304883 drm_WARN_ON(&dev_priv->drm,
4884 wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004885 blocks = U32_MAX;
4886 break;
4887 }
4888 continue;
4889 }
4890
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004891 blocks += wm->wm[level].min_ddb_alloc;
4892 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004893 }
4894
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004895 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004896 alloc_size -= blocks;
4897 break;
4898 }
4899 }
4900
4901 if (level < 0) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03004902 drm_dbg_kms(&dev_priv->drm,
4903 "Requested display configuration exceeds system DDB limitations");
4904 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
4905 blocks, alloc_size);
Matt Roperd8e87492018-12-11 09:31:07 -08004906 return -EINVAL;
4907 }
4908
4909 /*
4910 * Grant each plane the blocks it requires at the highest achievable
4911 * watermark level, plus an extra share of the leftover blocks
4912 * proportional to its relative data rate.
4913 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004914 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004915 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004916 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004917 u64 rate;
4918 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004919
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004920 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004921 continue;
4922
Damien Lespiaub9cec072014-11-04 17:06:43 +00004923 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004924 * We've accounted for all active planes; remaining planes are
4925 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004926 */
Matt Roperd8e87492018-12-11 09:31:07 -08004927 if (total_data_rate == 0)
4928 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004929
Matt Roperd8e87492018-12-11 09:31:07 -08004930 rate = plane_data_rate[plane_id];
4931 extra = min_t(u16, alloc_size,
4932 DIV64_U64_ROUND_UP(alloc_size * rate,
4933 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004934 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004935 alloc_size -= extra;
4936 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004937
Matt Roperd8e87492018-12-11 09:31:07 -08004938 if (total_data_rate == 0)
4939 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004940
Matt Roperd8e87492018-12-11 09:31:07 -08004941 rate = uv_plane_data_rate[plane_id];
4942 extra = min_t(u16, alloc_size,
4943 DIV64_U64_ROUND_UP(alloc_size * rate,
4944 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004945 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004946 alloc_size -= extra;
4947 total_data_rate -= rate;
4948 }
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304949 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
Matt Roperd8e87492018-12-11 09:31:07 -08004950
4951 /* Set the actual DDB start/end points for each plane */
4952 start = alloc->start;
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004953 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004954 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004955 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004956 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004957 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004958
4959 if (plane_id == PLANE_CURSOR)
4960 continue;
4961
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004962 /* Gen11+ uses a separate plane for UV watermarks */
Pankaj Bharadiya48a1b8d2020-01-15 09:14:53 +05304963 drm_WARN_ON(&dev_priv->drm,
4964 INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004965
Matt Roperd8e87492018-12-11 09:31:07 -08004966 /* Leave disabled planes at (0,0) */
4967 if (total[plane_id]) {
4968 plane_alloc->start = start;
4969 start += total[plane_id];
4970 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004971 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004972
Matt Roperd8e87492018-12-11 09:31:07 -08004973 if (uv_total[plane_id]) {
4974 uv_plane_alloc->start = start;
4975 start += uv_total[plane_id];
4976 uv_plane_alloc->end = start;
4977 }
4978 }
4979
4980 /*
4981 * When we calculated watermark values we didn't know how high
4982 * of a level we'd actually be able to hit, so we just marked
4983 * all levels as "enabled." Go back now and disable the ones
4984 * that aren't actually possible.
4985 */
4986 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02004987 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004988 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004989 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004990
4991 /*
4992 * We only disable the watermarks for each plane if
4993 * they exceed the ddb allocation of said plane. This
4994 * is done so that we don't end up touching cursor
4995 * watermarks needlessly when some other plane reduces
4996 * our max possible watermark level.
4997 *
4998 * Bspec has this to say about the PLANE_WM enable bit:
4999 * "All the watermarks at this level for all enabled
5000 * planes must be enabled before the level will be used."
5001 * So this is actually safe to do.
5002 */
5003 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
5004 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
5005 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02005006
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005007 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07005008 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005009 * Underruns with WM1+ disabled
5010 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07005011 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02005012 level == 1 && wm->wm[0].plane_en) {
5013 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02005014 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
5015 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02005016 }
Matt Roperd8e87492018-12-11 09:31:07 -08005017 }
5018 }
5019
5020 /*
5021 * Go back and disable the transition watermark if it turns out we
5022 * don't have enough DDB blocks for it.
5023 */
Ville Syrjälä2a67054b2020-02-25 19:11:06 +02005024 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005025 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02005026 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02005027
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02005028 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08005029 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00005030 }
5031
Matt Roperc107acf2016-05-12 07:06:01 -07005032 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00005033}
5034
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005035/*
5036 * 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 +02005037 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005038 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5039 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5040*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005041static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005042skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5043 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005044{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005045 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305046 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005047
5048 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305049 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005050
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305051 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005052 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005053
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005054 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07005055 ret = add_fixed16_u32(ret, 1);
5056
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005057 return ret;
5058}
5059
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005060static uint_fixed_16_16_t
5061skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5062 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005063{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005064 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305065 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005066
5067 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305068 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005069
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005070 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305071 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5072 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305073 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005074 return ret;
5075}
5076
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305077static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02005078intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305079{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305080 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005081 u32 pixel_rate;
5082 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305083 uint_fixed_16_16_t linetime_us;
5084
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005085 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305086 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305087
Maarten Lankhorstec193642019-06-28 10:55:17 +02005088 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305089
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305090 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305091 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305092
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005093 crtc_htotal = crtc_state->hw.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305094 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05305095
5096 return linetime_us;
5097}
5098
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005099static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02005100skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
5101 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005102{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305103 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005104 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05305105 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005106
5107 /* Shouldn't reach here on disabled planes... */
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305108 if (drm_WARN_ON(&dev_priv->drm,
5109 !intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005110 return 0;
5111
5112 /*
5113 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
5114 * with additional adjustments for plane-specific scaling.
5115 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02005116 adjusted_pixel_rate = crtc_state->pixel_rate;
5117 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005118
Kumar, Mahesh7084b502017-05-17 17:28:23 +05305119 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
5120 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07005121}
5122
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305123static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005124skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5125 int width, const struct drm_format_info *format,
5126 u64 modifier, unsigned int rotation,
5127 u32 plane_pixel_rate, struct skl_wm_params *wp,
5128 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305129{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005130 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005131 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005132 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305133
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05305134 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02005135 if (color_plane == 1 &&
5136 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005137 drm_dbg_kms(&dev_priv->drm,
5138 "Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305139 return -EINVAL;
5140 }
5141
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005142 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5143 modifier == I915_FORMAT_MOD_Yf_TILED ||
5144 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5145 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5146 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5147 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5148 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02005149 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305150
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005151 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02005152 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305153 wp->width /= 2;
5154
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005155 wp->cpp = format->cpp[color_plane];
5156 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305157
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005158 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005159 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005160 wp->dbuf_block_size = 256;
5161 else
5162 wp->dbuf_block_size = 512;
5163
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005164 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305165 switch (wp->cpp) {
5166 case 1:
5167 wp->y_min_scanlines = 16;
5168 break;
5169 case 2:
5170 wp->y_min_scanlines = 8;
5171 break;
5172 case 4:
5173 wp->y_min_scanlines = 4;
5174 break;
5175 default:
5176 MISSING_CASE(wp->cpp);
5177 return -EINVAL;
5178 }
5179 } else {
5180 wp->y_min_scanlines = 4;
5181 }
5182
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005183 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305184 wp->y_min_scanlines *= 2;
5185
5186 wp->plane_bytes_per_line = wp->width * wp->cpp;
5187 if (wp->y_tiled) {
5188 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005189 wp->y_min_scanlines,
5190 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305191
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005192 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305193 interm_pbpl++;
5194
5195 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5196 wp->y_min_scanlines);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305197 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005198 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
Ville Syrjälä260a6c1b2020-04-30 15:58:21 +03005199 wp->dbuf_block_size);
5200
5201 if (!wp->x_tiled ||
5202 INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
5203 interm_pbpl++;
5204
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305205 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5206 }
5207
5208 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5209 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005210
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305211 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005212 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305213
5214 return 0;
5215}
5216
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005217static int
5218skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5219 const struct intel_plane_state *plane_state,
5220 struct skl_wm_params *wp, int color_plane)
5221{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005222 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005223 int width;
5224
Maarten Lankhorst3a612762019-10-04 13:34:54 +02005225 /*
5226 * Src coordinates are already rotated by 270 degrees for
5227 * the 90/270 degree plane rotation cases (to match the
5228 * GTT mapping), hence no need to account for rotation here.
5229 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005230 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005231
5232 return skl_compute_wm_params(crtc_state, width,
5233 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005234 plane_state->hw.rotation,
Ville Syrjäläc92558a2019-03-12 22:58:38 +02005235 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
5236 wp, color_plane);
5237}
5238
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005239static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5240{
5241 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
5242 return true;
5243
5244 /* The number of lines are ignored for the level 0 watermark. */
5245 return level > 0;
5246}
5247
Maarten Lankhorstec193642019-06-28 10:55:17 +02005248static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08005249 int level,
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005250 unsigned int latency,
Matt Roperd8e87492018-12-11 09:31:07 -08005251 const struct skl_wm_params *wp,
5252 const struct skl_wm_level *result_prev,
5253 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005254{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005255 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305256 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05305257 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005258 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02005259
Ville Syrjälä0aded172019-02-05 17:50:53 +02005260 if (latency == 0) {
5261 /* reject it */
5262 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02005263 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005264 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02005265
Ville Syrjälä25312ef2019-05-03 20:38:05 +03005266 /*
5267 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5268 * Display WA #1141: kbl,cfl
5269 */
Chris Wilson5f4ae272020-06-02 15:05:40 +01005270 if ((IS_KABYLAKE(dev_priv) ||
5271 IS_COFFEELAKE(dev_priv) ||
5272 IS_COMETLAKE(dev_priv)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07005273 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05305274 latency += 4;
5275
Ville Syrjälä60e983f2018-12-21 19:14:33 +02005276 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03005277 latency += 15;
5278
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305279 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005280 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305281 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005282 crtc_state->hw.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03005283 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305284 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005285
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305286 if (wp->y_tiled) {
5287 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005288 } else {
Maarten Lankhorst1326a922019-10-31 12:26:02 +01005289 if ((wp->cpp * crtc_state->hw.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02005290 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005291 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03005292 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005293 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08005294 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07005295 !IS_GEMINILAKE(dev_priv))
5296 selected_result = min_fixed16(method1, method2);
5297 else
5298 selected_result = method2;
5299 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005300 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07005301 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005302 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005303
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05305304 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05305305 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305306 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00005307
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005308 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
5309 /* Display WA #1125: skl,bxt,kbl */
5310 if (level == 0 && wp->rc_surface)
5311 res_blocks +=
5312 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07005313
Paulo Zanonia5b79d32018-11-13 17:24:32 -08005314 /* Display WA #1126: skl,bxt,kbl */
5315 if (level >= 1 && level <= 7) {
5316 if (wp->y_tiled) {
5317 res_blocks +=
5318 fixed16_to_u32_round_up(wp->y_tile_minimum);
5319 res_lines += wp->y_min_scanlines;
5320 } else {
5321 res_blocks++;
5322 }
5323
5324 /*
5325 * Make sure result blocks for higher latency levels are
5326 * atleast as high as level below the current level.
5327 * Assumption in DDB algorithm optimization for special
5328 * cases. Also covers Display WA #1125 for RC.
5329 */
5330 if (result_prev->plane_res_b > res_blocks)
5331 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03005332 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00005333 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00005334
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005335 if (INTEL_GEN(dev_priv) >= 11) {
5336 if (wp->y_tiled) {
5337 int extra_lines;
5338
5339 if (res_lines % wp->y_min_scanlines == 0)
5340 extra_lines = wp->y_min_scanlines;
5341 else
5342 extra_lines = wp->y_min_scanlines * 2 -
5343 res_lines % wp->y_min_scanlines;
5344
5345 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
5346 wp->plane_blocks_per_line);
5347 } else {
5348 min_ddb_alloc = res_blocks +
5349 DIV_ROUND_UP(res_blocks, 10);
5350 }
5351 }
5352
Ville Syrjäläb52c2732018-12-21 19:14:28 +02005353 if (!skl_wm_has_lines(dev_priv, level))
5354 res_lines = 0;
5355
Ville Syrjälä0aded172019-02-05 17:50:53 +02005356 if (res_lines > 31) {
5357 /* reject it */
5358 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08005359 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02005360 }
Matt Roperd8e87492018-12-11 09:31:07 -08005361
5362 /*
5363 * If res_lines is valid, assume we can use this watermark level
5364 * for now. We'll come back and disable it after we calculate the
5365 * DDB allocation if it turns out we don't actually have enough
5366 * blocks to satisfy it.
5367 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05305368 result->plane_res_b = res_blocks;
5369 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005370 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
5371 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05305372 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005373}
5374
Matt Roperd8e87492018-12-11 09:31:07 -08005375static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02005376skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305377 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005378 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005379{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005380 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305381 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005382 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04005383
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305384 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005385 struct skl_wm_level *result = &levels[level];
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005386 unsigned int latency = dev_priv->wm.skl_latency[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305387
Stanislav Lisovskiy7b994752020-04-09 18:47:18 +03005388 skl_compute_plane_wm(crtc_state, level, latency,
5389 wm_params, result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005390
5391 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05305392 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005393}
5394
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005395static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5396 const struct skl_wm_params *wm_params,
5397 struct skl_plane_wm *plane_wm)
5398{
5399 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5400 struct skl_wm_level *sagv_wm = &plane_wm->sagv_wm0;
5401 struct skl_wm_level *levels = plane_wm->wm;
5402 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5403
5404 skl_compute_plane_wm(crtc_state, 0, latency,
5405 wm_params, &levels[0],
5406 sagv_wm);
5407}
5408
Maarten Lankhorstec193642019-06-28 10:55:17 +02005409static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02005410 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08005411 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00005412{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005413 struct drm_device *dev = crtc_state->uapi.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05305414 const struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc834d032020-02-28 22:35:52 +02005415 u16 trans_min, trans_amount, trans_y_tile_min;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005416 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00005417
Kumar, Maheshca476672017-08-17 19:15:24 +05305418 /* Transition WM don't make any sense if ipc is disabled */
5419 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02005420 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05305421
Ville Syrjäläa7f1e8e2020-02-28 22:35:51 +02005422 /*
5423 * WaDisableTWM:skl,kbl,cfl,bxt
5424 * Transition WM are not recommended by HW team for GEN9
5425 */
5426 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
5427 return;
5428
Paulo Zanoni91961a82018-10-04 16:15:56 -07005429 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05305430 trans_min = 4;
Ville Syrjäläc834d032020-02-28 22:35:52 +02005431 else
5432 trans_min = 14;
5433
5434 /* Display WA #1140: glk,cnl */
5435 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
5436 trans_amount = 0;
5437 else
5438 trans_amount = 10; /* This is configurable amount */
Kumar, Maheshca476672017-08-17 19:15:24 +05305439
5440 trans_offset_b = trans_min + trans_amount;
5441
Paulo Zanonicbacc792018-10-04 16:15:58 -07005442 /*
5443 * The spec asks for Selected Result Blocks for wm0 (the real value),
5444 * not Result Blocks (the integer value). Pay attention to the capital
5445 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
5446 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5447 * and since we later will have to get the ceiling of the sum in the
5448 * transition watermarks calculation, we can just pretend Selected
5449 * Result Blocks is Result Blocks minus 1 and it should work for the
5450 * current platforms.
5451 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02005452 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07005453
Kumar, Maheshca476672017-08-17 19:15:24 +05305454 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005455 trans_y_tile_min =
5456 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07005457 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05305458 trans_offset_b;
5459 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07005460 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05305461 }
5462
Matt Roperd8e87492018-12-11 09:31:07 -08005463 /*
5464 * Just assume we can enable the transition watermark. After
5465 * computing the DDB we'll come back and disable it if that
5466 * assumption turns out to be false.
5467 */
5468 wm->trans_wm.plane_res_b = res_blocks + 1;
5469 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00005470}
5471
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005472static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005473 const struct intel_plane_state *plane_state,
5474 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005475{
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005476 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5477 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä83158472018-11-27 18:57:26 +02005478 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005479 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005480 int ret;
5481
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005482 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005483 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005484 if (ret)
5485 return ret;
5486
Ville Syrjälä67155a62019-03-12 22:58:37 +02005487 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005488
5489 if (INTEL_GEN(dev_priv) >= 12)
5490 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5491
Matt Roperd8e87492018-12-11 09:31:07 -08005492 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005493
5494 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005495}
5496
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005497static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005498 const struct intel_plane_state *plane_state,
5499 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005500{
Ville Syrjälä83158472018-11-27 18:57:26 +02005501 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
5502 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005503 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005504
Ville Syrjälä83158472018-11-27 18:57:26 +02005505 wm->is_planar = true;
5506
5507 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02005508 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005509 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005510 if (ret)
5511 return ret;
5512
Ville Syrjälä67155a62019-03-12 22:58:37 +02005513 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02005514
5515 return 0;
5516}
5517
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005518static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005519 const struct intel_plane_state *plane_state)
5520{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005521 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005522 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02005523 enum plane_id plane_id = plane->id;
5524 int ret;
5525
5526 if (!intel_wm_plane_visible(crtc_state, plane_state))
5527 return 0;
5528
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005529 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005530 plane_id, 0);
5531 if (ret)
5532 return ret;
5533
5534 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005535 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005536 plane_id);
5537 if (ret)
5538 return ret;
5539 }
5540
5541 return 0;
5542}
5543
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005544static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005545 const struct intel_plane_state *plane_state)
5546{
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305547 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005548 enum plane_id plane_id = to_intel_plane(plane_state->uapi.plane)->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005549 int ret;
5550
5551 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005552 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005553 return 0;
5554
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005555 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01005556 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005557 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005558
Pankaj Bharadiya19edeb382020-05-04 23:45:59 +05305559 drm_WARN_ON(&dev_priv->drm,
5560 !intel_wm_plane_visible(crtc_state, plane_state));
5561 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5562 fb->format->num_planes == 1);
Ville Syrjälä83158472018-11-27 18:57:26 +02005563
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005564 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005565 y_plane_id, 0);
5566 if (ret)
5567 return ret;
5568
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005569 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005570 plane_id, 1);
5571 if (ret)
5572 return ret;
5573 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005574 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005575 plane_id, 0);
5576 if (ret)
5577 return ret;
5578 }
5579
5580 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005581}
5582
Maarten Lankhorstec193642019-06-28 10:55:17 +02005583static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005584{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005585 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005586 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005587 struct intel_plane *plane;
5588 const struct intel_plane_state *plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005589 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005590
Lyudea62163e2016-10-04 14:28:20 -04005591 /*
5592 * We'll only calculate watermarks for planes that are actually
5593 * enabled, so make sure all other planes are set as disabled.
5594 */
5595 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5596
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005597 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state,
5598 crtc_state) {
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305599
Ville Syrjälä83158472018-11-27 18:57:26 +02005600 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005601 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005602 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005603 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305604 if (ret)
5605 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005606 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305607
Matt Roper55994c22016-05-12 07:06:08 -07005608 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005609}
5610
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005611static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5612 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005613 const struct skl_ddb_entry *entry)
5614{
5615 if (entry->end)
Jani Nikula9b6320a2020-01-23 16:00:04 +02005616 intel_de_write_fw(dev_priv, reg,
5617 (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005618 else
Jani Nikula9b6320a2020-01-23 16:00:04 +02005619 intel_de_write_fw(dev_priv, reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005620}
5621
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005622static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5623 i915_reg_t reg,
5624 const struct skl_wm_level *level)
5625{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005626 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005627
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005628 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005629 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005630 if (level->ignore_lines)
5631 val |= PLANE_WM_IGNORE_LINES;
5632 val |= level->plane_res_b;
5633 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005634
Jani Nikula9b6320a2020-01-23 16:00:04 +02005635 intel_de_write_fw(dev_priv, reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005636}
5637
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005638void skl_write_plane_wm(struct intel_plane *plane,
5639 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005640{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005641 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005642 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005643 enum plane_id plane_id = plane->id;
5644 enum pipe pipe = plane->pipe;
5645 const struct skl_plane_wm *wm =
5646 &crtc_state->wm.skl.optimal.planes[plane_id];
5647 const struct skl_ddb_entry *ddb_y =
5648 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5649 const struct skl_ddb_entry *ddb_uv =
5650 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005651
5652 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005653 const struct skl_wm_level *wm_level;
5654
5655 wm_level = skl_plane_wm_level(crtc_state, plane_id, level);
5656
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005657 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005658 wm_level);
Lyude62e0fb82016-08-22 12:50:08 -04005659 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005660 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005661 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005662
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005663 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005664 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005665 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5666 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305667 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005668
5669 if (wm->is_planar)
5670 swap(ddb_y, ddb_uv);
5671
5672 skl_ddb_entry_write(dev_priv,
5673 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5674 skl_ddb_entry_write(dev_priv,
5675 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005676}
5677
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005678void skl_write_cursor_wm(struct intel_plane *plane,
5679 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005680{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005681 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005682 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005683 enum plane_id plane_id = plane->id;
5684 enum pipe pipe = plane->pipe;
5685 const struct skl_plane_wm *wm =
5686 &crtc_state->wm.skl.optimal.planes[plane_id];
5687 const struct skl_ddb_entry *ddb =
5688 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005689
5690 for (level = 0; level <= max_level; level++) {
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005691 const struct skl_wm_level *wm_level;
5692
5693 wm_level = skl_plane_wm_level(crtc_state, plane_id, level);
5694
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005695 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
Stanislav Lisovskiyd9162342020-05-13 12:38:11 +03005696 wm_level);
Lyude62e0fb82016-08-22 12:50:08 -04005697 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005698 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005699
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005700 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005701}
5702
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005703bool skl_wm_level_equals(const struct skl_wm_level *l1,
5704 const struct skl_wm_level *l2)
5705{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005706 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005707 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005708 l1->plane_res_l == l2->plane_res_l &&
5709 l1->plane_res_b == l2->plane_res_b;
5710}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005711
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005712static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5713 const struct skl_plane_wm *wm1,
5714 const struct skl_plane_wm *wm2)
5715{
5716 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005717
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005718 for (level = 0; level <= max_level; level++) {
Ville Syrjäläe7f54e62020-02-28 22:35:49 +02005719 /*
5720 * We don't check uv_wm as the hardware doesn't actually
5721 * use it. It only gets used for calculating the required
5722 * ddb allocation.
5723 */
5724 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005725 return false;
5726 }
5727
5728 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005729}
5730
Jani Nikula81b55ef2020-04-20 17:04:38 +03005731static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5732 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005733{
Lyude27082492016-08-24 07:48:10 +02005734 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005735}
5736
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005737bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005738 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005739 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005740{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005741 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005742
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005743 for (i = 0; i < num_entries; i++) {
5744 if (i != ignore_idx &&
5745 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005746 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005747 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005748
Lyude27082492016-08-24 07:48:10 +02005749 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005750}
5751
Jani Nikulabb7791b2016-10-04 12:29:17 +03005752static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005753skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5754 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005755{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005756 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
5757 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005758 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5759 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005760
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005761 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5762 struct intel_plane_state *plane_state;
5763 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005764
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005765 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5766 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5767 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5768 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005769 continue;
5770
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005771 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005772 if (IS_ERR(plane_state))
5773 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005774
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005775 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005776 }
5777
5778 return 0;
5779}
5780
5781static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005782skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005783{
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005784 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5785 const struct intel_dbuf_state *old_dbuf_state;
5786 const struct intel_dbuf_state *new_dbuf_state;
5787 const struct intel_crtc_state *old_crtc_state;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005788 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305789 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305790 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005791
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005792 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005793 new_crtc_state, i) {
Stanislav Lisovskiy072fcc32020-02-03 01:06:25 +02005794 ret = skl_allocate_pipe_ddb(new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005795 if (ret)
5796 return ret;
5797
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005798 ret = skl_ddb_add_affected_planes(old_crtc_state,
5799 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005800 if (ret)
5801 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005802 }
5803
Ville Syrjälä70b1a262020-02-25 19:11:16 +02005804 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
5805 new_dbuf_state = intel_atomic_get_new_dbuf_state(state);
5806
5807 if (new_dbuf_state &&
5808 new_dbuf_state->enabled_slices != old_dbuf_state->enabled_slices)
5809 drm_dbg_kms(&dev_priv->drm,
5810 "Enabled dbuf slices 0x%x -> 0x%x (out of %d dbuf slices)\n",
5811 old_dbuf_state->enabled_slices,
5812 new_dbuf_state->enabled_slices,
5813 INTEL_INFO(dev_priv)->num_supported_dbuf_slices);
5814
Matt Roper98d39492016-05-12 07:06:03 -07005815 return 0;
5816}
5817
Ville Syrjäläab98e942019-02-08 22:05:27 +02005818static char enast(bool enable)
5819{
5820 return enable ? '*' : ' ';
5821}
5822
Matt Roper2722efb2016-08-17 15:55:55 -04005823static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005824skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005825{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005826 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5827 const struct intel_crtc_state *old_crtc_state;
5828 const struct intel_crtc_state *new_crtc_state;
5829 struct intel_plane *plane;
5830 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005831 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005832
Jani Nikulabdbf43d2019-10-28 12:38:15 +02005833 if (!drm_debug_enabled(DRM_UT_KMS))
Ville Syrjäläab98e942019-02-08 22:05:27 +02005834 return;
5835
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005836 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5837 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005838 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5839
5840 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5841 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5842
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005843 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5844 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005845 const struct skl_ddb_entry *old, *new;
5846
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005847 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5848 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005849
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005850 if (skl_ddb_entry_equal(old, new))
5851 continue;
5852
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005853 drm_dbg_kms(&dev_priv->drm,
5854 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
5855 plane->base.base.id, plane->base.name,
5856 old->start, old->end, new->start, new->end,
5857 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005858 }
5859
5860 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5861 enum plane_id plane_id = plane->id;
5862 const struct skl_plane_wm *old_wm, *new_wm;
5863
5864 old_wm = &old_pipe_wm->planes[plane_id];
5865 new_wm = &new_pipe_wm->planes[plane_id];
5866
5867 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5868 continue;
5869
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005870 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005871 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm"
5872 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005873 plane->base.base.id, plane->base.name,
5874 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5875 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5876 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5877 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5878 enast(old_wm->trans_wm.plane_en),
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005879 enast(old_wm->sagv_wm0.plane_en),
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005880 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5881 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5882 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5883 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005884 enast(new_wm->trans_wm.plane_en),
5885 enast(new_wm->sagv_wm0.plane_en));
Ville Syrjäläab98e942019-02-08 22:05:27 +02005886
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005887 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005888 "[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d"
5889 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005890 plane->base.base.id, plane->base.name,
5891 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5892 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5893 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5894 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5895 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5896 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5897 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5898 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5899 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005900 enast(old_wm->sagv_wm0.ignore_lines), old_wm->sagv_wm0.plane_res_l,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005901
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005902 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5903 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5904 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5905 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5906 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5907 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5908 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5909 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005910 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l,
5911 enast(new_wm->sagv_wm0.ignore_lines), new_wm->sagv_wm0.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005912
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005913 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005914 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5915 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005916 plane->base.base.id, plane->base.name,
5917 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5918 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5919 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5920 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5921 old_wm->trans_wm.plane_res_b,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005922 old_wm->sagv_wm0.plane_res_b,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005923 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5924 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5925 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5926 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005927 new_wm->trans_wm.plane_res_b,
5928 new_wm->sagv_wm0.plane_res_b);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005929
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005930 drm_dbg_kms(&dev_priv->drm,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005931 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5932 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005933 plane->base.base.id, plane->base.name,
5934 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5935 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5936 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5937 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5938 old_wm->trans_wm.min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005939 old_wm->sagv_wm0.min_ddb_alloc,
Wambui Karugaf8d18d52020-01-07 18:13:30 +03005940 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5941 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5942 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5943 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03005944 new_wm->trans_wm.min_ddb_alloc,
5945 new_wm->sagv_wm0.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005946 }
5947 }
5948}
5949
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005950static int intel_add_affected_pipes(struct intel_atomic_state *state,
5951 u8 pipe_mask)
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005952{
5953 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5954 struct intel_crtc *crtc;
5955
5956 for_each_intel_crtc(&dev_priv->drm, crtc) {
5957 struct intel_crtc_state *crtc_state;
5958
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005959 if ((pipe_mask & BIT(crtc->pipe)) == 0)
5960 continue;
5961
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005962 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5963 if (IS_ERR(crtc_state))
5964 return PTR_ERR(crtc_state);
5965 }
5966
5967 return 0;
5968}
5969
Matt Roper98d39492016-05-12 07:06:03 -07005970static int
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005971skl_ddb_add_affected_pipes(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005972{
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005973 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005974 struct intel_crtc_state *crtc_state;
5975 struct intel_crtc *crtc;
5976 int i, ret;
Matt Roper98d39492016-05-12 07:06:03 -07005977
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305978 if (dev_priv->wm.distrust_bios_wm) {
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005979 /*
5980 * skl_ddb_get_pipe_allocation_limits() currently requires
5981 * all active pipes to be included in the state so that
5982 * it can redistribute the dbuf among them, and it really
5983 * wants to recompute things when distrust_bios_wm is set
5984 * so we add all the pipes to the state.
5985 */
5986 ret = intel_add_affected_pipes(state, ~0);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305987 if (ret)
5988 return ret;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305989 }
5990
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005991 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5992 struct intel_dbuf_state *new_dbuf_state;
5993 const struct intel_dbuf_state *old_dbuf_state;
5994
5995 new_dbuf_state = intel_atomic_get_dbuf_state(state);
5996 if (IS_ERR(new_dbuf_state))
Chris Wilsoncba597a2020-05-16 20:09:40 +01005997 return PTR_ERR(new_dbuf_state);
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02005998
5999 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6000
6001 new_dbuf_state->active_pipes =
6002 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6003
6004 if (old_dbuf_state->active_pipes == new_dbuf_state->active_pipes)
6005 break;
6006
6007 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03006008 if (ret)
6009 return ret;
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02006010
6011 /*
6012 * skl_ddb_get_pipe_allocation_limits() currently requires
6013 * all active pipes to be included in the state so that
6014 * it can redistribute the dbuf among them.
6015 */
6016 ret = intel_add_affected_pipes(state,
6017 new_dbuf_state->active_pipes);
6018 if (ret)
6019 return ret;
6020
6021 break;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306022 }
6023
6024 return 0;
6025}
6026
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006027/*
6028 * To make sure the cursor watermark registers are always consistent
6029 * with our computed state the following scenario needs special
6030 * treatment:
6031 *
6032 * 1. enable cursor
6033 * 2. move cursor entirely offscreen
6034 * 3. disable cursor
6035 *
6036 * Step 2. does call .disable_plane() but does not zero the watermarks
6037 * (since we consider an offscreen cursor still active for the purposes
6038 * of watermarks). Step 3. would not normally call .disable_plane()
6039 * because the actual plane visibility isn't changing, and we don't
6040 * deallocate the cursor ddb until the pipe gets disabled. So we must
6041 * force step 3. to call .disable_plane() to update the watermark
6042 * registers properly.
6043 *
6044 * Other planes do not suffer from this issues as their watermarks are
6045 * calculated based on the actual plane visibility. The only time this
6046 * can trigger for the other planes is during the initial readout as the
6047 * default value of the watermarks registers is not zero.
6048 */
6049static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6050 struct intel_crtc *crtc)
6051{
6052 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6053 const struct intel_crtc_state *old_crtc_state =
6054 intel_atomic_get_old_crtc_state(state, crtc);
6055 struct intel_crtc_state *new_crtc_state =
6056 intel_atomic_get_new_crtc_state(state, crtc);
6057 struct intel_plane *plane;
6058
6059 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6060 struct intel_plane_state *plane_state;
6061 enum plane_id plane_id = plane->id;
6062
6063 /*
6064 * Force a full wm update for every plane on modeset.
6065 * Required because the reset value of the wm registers
6066 * is non-zero, whereas we want all disabled planes to
6067 * have zero watermarks. So if we turn off the relevant
6068 * power well the hardware state will go out of sync
6069 * with the software state.
6070 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01006071 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006072 skl_plane_wm_equals(dev_priv,
6073 &old_crtc_state->wm.skl.optimal.planes[plane_id],
6074 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
6075 continue;
6076
6077 plane_state = intel_atomic_get_plane_state(state, plane);
6078 if (IS_ERR(plane_state))
6079 return PTR_ERR(plane_state);
6080
6081 new_crtc_state->update_planes |= BIT(plane_id);
6082 }
6083
6084 return 0;
6085}
6086
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306087static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006088skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306089{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006090 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006091 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006092 struct intel_crtc_state *old_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306093 int ret, i;
6094
Ville Syrjäläd7a14582019-10-11 23:09:42 +03006095 ret = skl_ddb_add_affected_pipes(state);
6096 if (ret)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05306097 return ret;
6098
Matt Roper734fa012016-05-12 15:11:40 -07006099 /*
6100 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08006101 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Ville Syrjäläf119a5e2020-01-20 19:47:13 +02006102 * weren't otherwise being modified if pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07006103 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006104 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02006105 new_crtc_state, i) {
6106 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02006107 if (ret)
6108 return ret;
Matt Roper734fa012016-05-12 15:11:40 -07006109 }
6110
Matt Roperd8e87492018-12-11 09:31:07 -08006111 ret = skl_compute_ddb(state);
6112 if (ret)
6113 return ret;
6114
Stanislav Lisovskiyecab0f32020-04-30 22:56:34 +03006115 ret = intel_compute_sagv_mask(state);
6116 if (ret)
6117 return ret;
Stanislav Lisovskiy97288892020-04-30 22:17:57 +03006118
Ville Syrjälä23baedd2020-02-28 22:35:50 +02006119 /*
6120 * skl_compute_ddb() will have adjusted the final watermarks
6121 * based on how much ddb is available. Now we can actually
6122 * check if the final watermarks changed.
6123 */
6124 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6125 new_crtc_state, i) {
6126 ret = skl_wm_add_affected_planes(state, crtc);
6127 if (ret)
6128 return ret;
6129 }
6130
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006131 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04006132
Matt Roper98d39492016-05-12 07:06:03 -07006133 return 0;
6134}
6135
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006136static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02006137 struct intel_wm_config *config)
6138{
6139 struct intel_crtc *crtc;
6140
6141 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006142 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02006143 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6144
6145 if (!wm->pipe_enabled)
6146 continue;
6147
6148 config->sprites_enabled |= wm->sprites_enabled;
6149 config->sprites_scaled |= wm->sprites_scaled;
6150 config->num_pipes_active++;
6151 }
6152}
6153
Matt Ropered4a6a72016-02-23 17:20:13 -08006154static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03006155{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006156 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02006157 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02006158 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02006159 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006160 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07006161
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006162 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02006163
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006164 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6165 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03006166
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006167 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006168 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02006169 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006170 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6171 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03006172
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006173 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03006174 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006175 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006176 }
6177
Ville Syrjälä198a1e92013-10-09 19:17:58 +03006178 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03006179 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03006180
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006181 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03006182
Imre Deak820c1982013-12-17 14:46:36 +02006183 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03006184}
6185
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006186static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006187 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006188{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006189 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6190 const struct intel_crtc_state *crtc_state =
6191 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006192
Matt Ropered4a6a72016-02-23 17:20:13 -08006193 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006194 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08006195 ilk_program_watermarks(dev_priv);
6196 mutex_unlock(&dev_priv->wm.wm_mutex);
6197}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006198
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01006199static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006200 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08006201{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02006202 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6203 const struct intel_crtc_state *crtc_state =
6204 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006205
6206 if (!crtc_state->wm.need_postvbl_update)
6207 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08006208
6209 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03006210 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6211 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08006212 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07006213}
6214
Jani Nikula81b55ef2020-04-20 17:04:38 +03006215static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00006216{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006217 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02006218 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006219 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
6220 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
6221 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00006222}
6223
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006224void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006225 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00006226{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006227 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6228 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006229 int level, max_level;
6230 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006231 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00006232
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006233 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00006234
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006235 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006236 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00006237
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006238 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006239 if (plane_id != PLANE_CURSOR)
6240 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006241 else
6242 val = I915_READ(CUR_WM(pipe, level));
6243
6244 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6245 }
6246
Stanislav Lisovskiy7241c572020-05-14 10:48:51 +03006247 if (INTEL_GEN(dev_priv) >= 12)
6248 wm->sagv_wm0 = wm->wm[0];
6249
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02006250 if (plane_id != PLANE_CURSOR)
6251 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02006252 else
6253 val = I915_READ(CUR_WM_TRANS(pipe));
6254
6255 skl_wm_level_from_reg_val(val, &wm->trans_wm);
6256 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006257
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006258 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00006259 return;
Pradeep Bhat30789992014-11-04 17:06:45 +00006260}
6261
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006262void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00006263{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006264 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006265 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00006266
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006267 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02006268 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006269
Maarten Lankhorstec193642019-06-28 10:55:17 +02006270 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04006271 }
Matt Ropera1de91e2016-05-12 07:05:57 -07006272
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03006273 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07006274 /* Fully recompute DDB on first atomic commit */
6275 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07006276 }
Pradeep Bhat30789992014-11-04 17:06:45 +00006277}
6278
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006279static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006280{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006281 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006282 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02006283 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02006284 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6285 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006286 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02006287 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006288 [PIPE_A] = WM0_PIPEA_ILK,
6289 [PIPE_B] = WM0_PIPEB_ILK,
6290 [PIPE_C] = WM0_PIPEC_IVB,
6291 };
6292
6293 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006294
Ville Syrjälä15606532016-05-13 17:55:17 +03006295 memset(active, 0, sizeof(*active));
6296
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006297 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02006298
6299 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006300 u32 tmp = hw->wm_pipe[pipe];
6301
6302 /*
6303 * For active pipes LP0 watermark is marked as
6304 * enabled, and LP1+ watermaks as disabled since
6305 * we can't really reverse compute them in case
6306 * multiple pipes are active.
6307 */
6308 active->wm[0].enable = true;
6309 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6310 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6311 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006312 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006313 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006314
6315 /*
6316 * For inactive pipes, all watermark levels
6317 * should be marked as enabled but zeroed,
6318 * which is what we'd compute them to.
6319 */
6320 for (level = 0; level <= max_level; level++)
6321 active->wm[level].enable = true;
6322 }
Matt Roper4e0963c2015-09-24 15:53:15 -07006323
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006324 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006325}
6326
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006327#define _FW_WM(value, plane) \
6328 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6329#define _FW_WM_VLV(value, plane) \
6330 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6331
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006332static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6333 struct g4x_wm_values *wm)
6334{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006335 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006336
6337 tmp = I915_READ(DSPFW1);
6338 wm->sr.plane = _FW_WM(tmp, SR);
6339 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6340 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6341 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6342
6343 tmp = I915_READ(DSPFW2);
6344 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6345 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6346 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6347 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6348 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6349 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6350
6351 tmp = I915_READ(DSPFW3);
6352 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6353 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6354 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6355 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6356}
6357
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006358static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6359 struct vlv_wm_values *wm)
6360{
6361 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006362 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006363
6364 for_each_pipe(dev_priv, pipe) {
6365 tmp = I915_READ(VLV_DDL(pipe));
6366
Ville Syrjälä1b313892016-11-28 19:37:08 +02006367 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006368 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006369 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006370 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006371 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006372 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006373 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006374 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6375 }
6376
6377 tmp = I915_READ(DSPFW1);
6378 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006379 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6380 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6381 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006382
6383 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006384 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6385 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6386 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006387
6388 tmp = I915_READ(DSPFW3);
6389 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6390
6391 if (IS_CHERRYVIEW(dev_priv)) {
6392 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006393 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6394 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006395
6396 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006397 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6398 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006399
6400 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006401 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6402 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006403
6404 tmp = I915_READ(DSPHOWM);
6405 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006406 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6407 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6408 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6409 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6410 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6411 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6412 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6413 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6414 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006415 } else {
6416 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02006417 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6418 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006419
6420 tmp = I915_READ(DSPHOWM);
6421 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02006422 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6423 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6424 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6425 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6426 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6427 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006428 }
6429}
6430
6431#undef _FW_WM
6432#undef _FW_WM_VLV
6433
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006434void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006435{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006436 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6437 struct intel_crtc *crtc;
6438
6439 g4x_read_wm_values(dev_priv, wm);
6440
6441 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
6442
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006443 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006444 struct intel_crtc_state *crtc_state =
6445 to_intel_crtc_state(crtc->base.state);
6446 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6447 struct g4x_pipe_wm *raw;
6448 enum pipe pipe = crtc->pipe;
6449 enum plane_id plane_id;
6450 int level, max_level;
6451
6452 active->cxsr = wm->cxsr;
6453 active->hpll_en = wm->hpll_en;
6454 active->fbc_en = wm->fbc_en;
6455
6456 active->sr = wm->sr;
6457 active->hpll = wm->hpll;
6458
6459 for_each_plane_id_on_crtc(crtc, plane_id) {
6460 active->wm.plane[plane_id] =
6461 wm->pipe[pipe].plane[plane_id];
6462 }
6463
6464 if (wm->cxsr && wm->hpll_en)
6465 max_level = G4X_WM_LEVEL_HPLL;
6466 else if (wm->cxsr)
6467 max_level = G4X_WM_LEVEL_SR;
6468 else
6469 max_level = G4X_WM_LEVEL_NORMAL;
6470
6471 level = G4X_WM_LEVEL_NORMAL;
6472 raw = &crtc_state->wm.g4x.raw[level];
6473 for_each_plane_id_on_crtc(crtc, plane_id)
6474 raw->plane[plane_id] = active->wm.plane[plane_id];
6475
6476 if (++level > max_level)
6477 goto out;
6478
6479 raw = &crtc_state->wm.g4x.raw[level];
6480 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6481 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6482 raw->plane[PLANE_SPRITE0] = 0;
6483 raw->fbc = active->sr.fbc;
6484
6485 if (++level > max_level)
6486 goto out;
6487
6488 raw = &crtc_state->wm.g4x.raw[level];
6489 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6490 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6491 raw->plane[PLANE_SPRITE0] = 0;
6492 raw->fbc = active->hpll.fbc;
6493
6494 out:
6495 for_each_plane_id_on_crtc(crtc, plane_id)
6496 g4x_raw_plane_wm_set(crtc_state, level,
6497 plane_id, USHRT_MAX);
6498 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6499
6500 crtc_state->wm.g4x.optimal = *active;
6501 crtc_state->wm.g4x.intermediate = *active;
6502
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006503 drm_dbg_kms(&dev_priv->drm,
6504 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6505 pipe_name(pipe),
6506 wm->pipe[pipe].plane[PLANE_PRIMARY],
6507 wm->pipe[pipe].plane[PLANE_CURSOR],
6508 wm->pipe[pipe].plane[PLANE_SPRITE0]);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006509 }
6510
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006511 drm_dbg_kms(&dev_priv->drm,
6512 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6513 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6514 drm_dbg_kms(&dev_priv->drm,
6515 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6516 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6517 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6518 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006519}
6520
6521void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6522{
6523 struct intel_plane *plane;
6524 struct intel_crtc *crtc;
6525
6526 mutex_lock(&dev_priv->wm.wm_mutex);
6527
6528 for_each_intel_plane(&dev_priv->drm, plane) {
6529 struct intel_crtc *crtc =
6530 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6531 struct intel_crtc_state *crtc_state =
6532 to_intel_crtc_state(crtc->base.state);
6533 struct intel_plane_state *plane_state =
6534 to_intel_plane_state(plane->base.state);
6535 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6536 enum plane_id plane_id = plane->id;
6537 int level;
6538
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006539 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006540 continue;
6541
6542 for (level = 0; level < 3; level++) {
6543 struct g4x_pipe_wm *raw =
6544 &crtc_state->wm.g4x.raw[level];
6545
6546 raw->plane[plane_id] = 0;
6547 wm_state->wm.plane[plane_id] = 0;
6548 }
6549
6550 if (plane_id == PLANE_PRIMARY) {
6551 for (level = 0; level < 3; level++) {
6552 struct g4x_pipe_wm *raw =
6553 &crtc_state->wm.g4x.raw[level];
6554 raw->fbc = 0;
6555 }
6556
6557 wm_state->sr.fbc = 0;
6558 wm_state->hpll.fbc = 0;
6559 wm_state->fbc_en = false;
6560 }
6561 }
6562
6563 for_each_intel_crtc(&dev_priv->drm, crtc) {
6564 struct intel_crtc_state *crtc_state =
6565 to_intel_crtc_state(crtc->base.state);
6566
6567 crtc_state->wm.g4x.intermediate =
6568 crtc_state->wm.g4x.optimal;
6569 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6570 }
6571
6572 g4x_program_watermarks(dev_priv);
6573
6574 mutex_unlock(&dev_priv->wm.wm_mutex);
6575}
6576
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006577void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006578{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006579 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006580 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006581 u32 val;
6582
6583 vlv_read_wm_values(dev_priv, wm);
6584
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006585 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6586 wm->level = VLV_WM_LEVEL_PM2;
6587
6588 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006589 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006590
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006591 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006592 if (val & DSP_MAXFIFO_PM5_ENABLE)
6593 wm->level = VLV_WM_LEVEL_PM5;
6594
Ville Syrjälä58590c12015-09-08 21:05:12 +03006595 /*
6596 * If DDR DVFS is disabled in the BIOS, Punit
6597 * will never ack the request. So if that happens
6598 * assume we don't have to enable/disable DDR DVFS
6599 * dynamically. To test that just set the REQ_ACK
6600 * bit to poke the Punit, but don't change the
6601 * HIGH/LOW bits so that we don't actually change
6602 * the current state.
6603 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006604 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006605 val |= FORCE_DDR_FREQ_REQ_ACK;
6606 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6607
6608 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6609 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006610 drm_dbg_kms(&dev_priv->drm,
6611 "Punit not acking DDR DVFS request, "
6612 "assuming DDR DVFS is disabled\n");
Ville Syrjälä58590c12015-09-08 21:05:12 +03006613 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6614 } else {
6615 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6616 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6617 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6618 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006619
Chris Wilson337fa6e2019-04-26 09:17:20 +01006620 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006621 }
6622
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006623 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006624 struct intel_crtc_state *crtc_state =
6625 to_intel_crtc_state(crtc->base.state);
6626 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6627 const struct vlv_fifo_state *fifo_state =
6628 &crtc_state->wm.vlv.fifo_state;
6629 enum pipe pipe = crtc->pipe;
6630 enum plane_id plane_id;
6631 int level;
6632
6633 vlv_get_fifo_size(crtc_state);
6634
6635 active->num_levels = wm->level + 1;
6636 active->cxsr = wm->cxsr;
6637
Ville Syrjäläff32c542017-03-02 19:14:57 +02006638 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006639 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006640 &crtc_state->wm.vlv.raw[level];
6641
6642 active->sr[level].plane = wm->sr.plane;
6643 active->sr[level].cursor = wm->sr.cursor;
6644
6645 for_each_plane_id_on_crtc(crtc, plane_id) {
6646 active->wm[level].plane[plane_id] =
6647 wm->pipe[pipe].plane[plane_id];
6648
6649 raw->plane[plane_id] =
6650 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6651 fifo_state->plane[plane_id]);
6652 }
6653 }
6654
6655 for_each_plane_id_on_crtc(crtc, plane_id)
6656 vlv_raw_plane_wm_set(crtc_state, level,
6657 plane_id, USHRT_MAX);
6658 vlv_invalidate_wms(crtc, active, level);
6659
6660 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006661 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006662
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006663 drm_dbg_kms(&dev_priv->drm,
6664 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
6665 pipe_name(pipe),
6666 wm->pipe[pipe].plane[PLANE_PRIMARY],
6667 wm->pipe[pipe].plane[PLANE_CURSOR],
6668 wm->pipe[pipe].plane[PLANE_SPRITE0],
6669 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006670 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006671
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006672 drm_dbg_kms(&dev_priv->drm,
6673 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6674 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006675}
6676
Ville Syrjälä602ae832017-03-02 19:15:02 +02006677void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6678{
6679 struct intel_plane *plane;
6680 struct intel_crtc *crtc;
6681
6682 mutex_lock(&dev_priv->wm.wm_mutex);
6683
6684 for_each_intel_plane(&dev_priv->drm, plane) {
6685 struct intel_crtc *crtc =
6686 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6687 struct intel_crtc_state *crtc_state =
6688 to_intel_crtc_state(crtc->base.state);
6689 struct intel_plane_state *plane_state =
6690 to_intel_plane_state(plane->base.state);
6691 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6692 const struct vlv_fifo_state *fifo_state =
6693 &crtc_state->wm.vlv.fifo_state;
6694 enum plane_id plane_id = plane->id;
6695 int level;
6696
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006697 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02006698 continue;
6699
6700 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006701 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006702 &crtc_state->wm.vlv.raw[level];
6703
6704 raw->plane[plane_id] = 0;
6705
6706 wm_state->wm[level].plane[plane_id] =
6707 vlv_invert_wm_value(raw->plane[plane_id],
6708 fifo_state->plane[plane_id]);
6709 }
6710 }
6711
6712 for_each_intel_crtc(&dev_priv->drm, crtc) {
6713 struct intel_crtc_state *crtc_state =
6714 to_intel_crtc_state(crtc->base.state);
6715
6716 crtc_state->wm.vlv.intermediate =
6717 crtc_state->wm.vlv.optimal;
6718 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6719 }
6720
6721 vlv_program_watermarks(dev_priv);
6722
6723 mutex_unlock(&dev_priv->wm.wm_mutex);
6724}
6725
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006726/*
6727 * FIXME should probably kill this and improve
6728 * the real watermark readout/sanitation instead
6729 */
6730static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6731{
6732 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6733 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6734 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6735
6736 /*
6737 * Don't touch WM1S_LP_EN here.
6738 * Doing so could cause underruns.
6739 */
6740}
6741
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006742void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006743{
Imre Deak820c1982013-12-17 14:46:36 +02006744 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006745 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006746
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006747 ilk_init_lp_watermarks(dev_priv);
6748
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006749 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006750 ilk_pipe_wm_get_hw_state(crtc);
6751
6752 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6753 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6754 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6755
6756 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006757 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006758 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6759 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6760 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006761
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006762 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006763 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6764 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006765 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006766 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6767 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006768
6769 hw->enable_fbc_wm =
6770 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6771}
6772
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006773/**
6774 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006775 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006776 *
6777 * Calculate watermark values for the various WM regs based on current mode
6778 * and plane configuration.
6779 *
6780 * There are several cases to deal with here:
6781 * - normal (i.e. non-self-refresh)
6782 * - self-refresh (SR) mode
6783 * - lines are large relative to FIFO size (buffer can hold up to 2)
6784 * - lines are small relative to FIFO size (buffer can hold more than 2
6785 * lines), so need to account for TLB latency
6786 *
6787 * The normal calculation is:
6788 * watermark = dotclock * bytes per pixel * latency
6789 * where latency is platform & configuration dependent (we assume pessimal
6790 * values here).
6791 *
6792 * The SR calculation is:
6793 * watermark = (trunc(latency/line time)+1) * surface width *
6794 * bytes per pixel
6795 * where
6796 * line time = htotal / dotclock
6797 * surface width = hdisplay for normal plane and 64 for cursor
6798 * and latency is assumed to be high, as above.
6799 *
6800 * The final value programmed to the register should always be rounded up,
6801 * and include an extra 2 entries to account for clock crossings.
6802 *
6803 * We don't use the sprite, so we can ignore that. And on Crestline we have
6804 * to set the non-SR watermarks to 8.
6805 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006806void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006807{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006808 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006809
6810 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006811 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006812}
6813
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306814void intel_enable_ipc(struct drm_i915_private *dev_priv)
6815{
6816 u32 val;
6817
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006818 if (!HAS_IPC(dev_priv))
6819 return;
6820
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306821 val = I915_READ(DISP_ARB_CTL2);
6822
6823 if (dev_priv->ipc_enabled)
6824 val |= DISP_IPC_ENABLE;
6825 else
6826 val &= ~DISP_IPC_ENABLE;
6827
6828 I915_WRITE(DISP_ARB_CTL2, val);
6829}
6830
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006831static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6832{
6833 /* Display WA #0477 WaDisableIPC: skl */
6834 if (IS_SKYLAKE(dev_priv))
6835 return false;
6836
6837 /* Display WA #1141: SKL:all KBL:all CFL */
Chris Wilson5f4ae272020-06-02 15:05:40 +01006838 if (IS_KABYLAKE(dev_priv) ||
6839 IS_COFFEELAKE(dev_priv) ||
6840 IS_COMETLAKE(dev_priv))
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006841 return dev_priv->dram_info.symmetric_memory;
6842
6843 return true;
6844}
6845
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306846void intel_init_ipc(struct drm_i915_private *dev_priv)
6847{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306848 if (!HAS_IPC(dev_priv))
6849 return;
6850
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006851 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006852
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306853 intel_enable_ipc(dev_priv);
6854}
6855
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006856static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006857{
Daniel Vetter3107bd42012-10-31 22:52:31 +01006858 /*
6859 * On Ibex Peak and Cougar Point, we need to disable clock
6860 * gating for the panel power sequencer or it will fail to
6861 * start up when no ports are active.
6862 */
6863 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6864}
6865
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006866static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006867{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006868 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006869
Damien Lespiau055e3932014-08-18 13:49:10 +01006870 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006871 I915_WRITE(DSPCNTR(pipe),
6872 I915_READ(DSPCNTR(pipe)) |
6873 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006874
6875 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6876 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006877 }
6878}
6879
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006880static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006881{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006882 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006883
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006884 /*
6885 * Required for FBC
6886 * WaFbcDisableDpfcClockGating:ilk
6887 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006888 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6889 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6890 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006891
6892 I915_WRITE(PCH_3DCGDIS0,
6893 MARIUNIT_CLOCK_GATE_DISABLE |
6894 SVSMUNIT_CLOCK_GATE_DISABLE);
6895 I915_WRITE(PCH_3DCGDIS1,
6896 VFMUNIT_CLOCK_GATE_DISABLE);
6897
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006898 /*
6899 * According to the spec the following bits should be set in
6900 * order to enable memory self-refresh
6901 * The bit 22/21 of 0x42004
6902 * The bit 5 of 0x42020
6903 * The bit 15 of 0x45000
6904 */
6905 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6906 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6907 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006908 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006909 I915_WRITE(DISP_ARB_CTL,
6910 (I915_READ(DISP_ARB_CTL) |
6911 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006912
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006913 /*
6914 * Based on the document from hardware guys the following bits
6915 * should be set unconditionally in order to enable FBC.
6916 * The bit 22 of 0x42000
6917 * The bit 22 of 0x42004
6918 * The bit 7,8,9 of 0x42020.
6919 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006920 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006921 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006922 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6923 I915_READ(ILK_DISPLAY_CHICKEN1) |
6924 ILK_FBCQ_DIS);
6925 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6926 I915_READ(ILK_DISPLAY_CHICKEN2) |
6927 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006928 }
6929
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006930 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6931
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006932 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6933 I915_READ(ILK_DISPLAY_CHICKEN2) |
6934 ILK_ELPIN_409_SELECT);
Akash Goel4e046322014-04-04 17:14:38 +05306935
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006936 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006937
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006938 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006939}
6940
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006941static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006942{
Ville Syrjäläd048a262019-08-21 20:30:31 +03006943 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006944 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006945
6946 /*
6947 * On Ibex Peak and Cougar Point, we need to disable clock
6948 * gating for the panel power sequencer or it will fail to
6949 * start up when no ports are active.
6950 */
Jesse Barnescd664072013-10-02 10:34:19 -07006951 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6952 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6953 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006954 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6955 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006956 /* The below fixes the weird display corruption, a few pixels shifted
6957 * downward, on (only) LVDS of some HP laptops with IVY.
6958 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006959 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006960 val = I915_READ(TRANS_CHICKEN2(pipe));
6961 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6962 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006963 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006964 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006965 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6966 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006967 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6968 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006969 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006970 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006971 I915_WRITE(TRANS_CHICKEN1(pipe),
6972 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6973 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006974}
6975
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006976static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006977{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006978 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006979
6980 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006981 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
Wambui Karugaf8d18d52020-01-07 18:13:30 +03006982 drm_dbg_kms(&dev_priv->drm,
6983 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6984 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006985}
6986
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006987static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006988{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006989 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006990
Damien Lespiau231e54f2012-10-19 17:55:41 +01006991 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006992
6993 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6994 I915_READ(ILK_DISPLAY_CHICKEN2) |
6995 ILK_ELPIN_409_SELECT);
6996
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006997 I915_WRITE(GEN6_UCGCTL1,
6998 I915_READ(GEN6_UCGCTL1) |
6999 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7000 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7001
7002 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7003 * gating disable must be set. Failure to set it results in
7004 * flickering pixels due to Z write ordering failures after
7005 * some amount of runtime in the Mesa "fire" demo, and Unigine
7006 * Sanctuary and Tropics, and apparently anything else with
7007 * alpha test or pixel discard.
7008 *
7009 * According to the spec, bit 11 (RCCUNIT) must also be set,
7010 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007011 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007012 * WaDisableRCCUnitClockGating:snb
7013 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007014 */
7015 I915_WRITE(GEN6_UCGCTL2,
7016 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7017 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7018
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007019 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007020 * According to the spec the following bits should be
7021 * set in order to enable memory self-refresh and fbc:
7022 * The bit21 and bit22 of 0x42000
7023 * The bit21 and bit22 of 0x42004
7024 * The bit5 and bit7 of 0x42020
7025 * The bit14 of 0x70180
7026 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007027 *
7028 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007029 */
7030 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7031 I915_READ(ILK_DISPLAY_CHICKEN1) |
7032 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7033 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7034 I915_READ(ILK_DISPLAY_CHICKEN2) |
7035 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007036 I915_WRITE(ILK_DSPCLK_GATE_D,
7037 I915_READ(ILK_DSPCLK_GATE_D) |
7038 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7039 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007040
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007041 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007042
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007043 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007044
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007045 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007046}
7047
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007048static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007049{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007050 /*
7051 * TODO: this bit should only be enabled when really needed, then
7052 * disabled when not needed anymore in order to save power.
7053 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007054 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007055 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7056 I915_READ(SOUTH_DSPCLK_GATE_D) |
7057 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007058
7059 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007060 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7061 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007062 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007063}
7064
Ville Syrjälä712bf362016-10-31 22:37:23 +02007065static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007066{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007067 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007068 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03007069
7070 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7071 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7072 }
7073}
7074
Imre Deak450174f2016-05-03 15:54:21 +03007075static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7076 int general_prio_credits,
7077 int high_prio_credits)
7078{
7079 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07007080 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03007081
7082 /* WaTempDisableDOPClkGating:bdw */
7083 misccpctl = I915_READ(GEN7_MISCCPCTL);
7084 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7085
Oscar Mateo930a7842017-10-17 13:25:45 -07007086 val = I915_READ(GEN8_L3SQCREG1);
7087 val &= ~L3_PRIO_CREDITS_MASK;
7088 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7089 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
7090 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03007091
7092 /*
7093 * Wait at least 100 clocks before re-enabling clock gating.
7094 * See the definition of L3SQCREG1 in BSpec.
7095 */
7096 POSTING_READ(GEN8_L3SQCREG1);
7097 udelay(1);
7098 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7099}
7100
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007101static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7102{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007103 /* Wa_1409120013:icl,ehl */
7104 I915_WRITE(ILK_DPFC_CHICKEN,
7105 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7106
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007107 /* This is not an Wa. Enable to reduce Sampler power */
7108 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
7109 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07007110
Matt Atwood6f4194c2020-01-13 23:11:28 -05007111 /*Wa_14010594013:icl, ehl */
7112 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7113 0, CNL_DELAY_PMRSP);
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007114}
7115
Michel Thierry5d869232019-08-23 01:20:34 -07007116static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
7117{
7118 u32 vd_pg_enable = 0;
7119 unsigned int i;
7120
Ville Syrjälä885f1822020-07-08 16:12:20 +03007121 /* Wa_1409120013:tgl */
7122 I915_WRITE(ILK_DPFC_CHICKEN,
7123 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7124
Michel Thierry5d869232019-08-23 01:20:34 -07007125 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
7126 for (i = 0; i < I915_MAX_VCS; i++) {
Daniele Ceraolo Spurio242613a2020-07-07 17:39:45 -07007127 if (HAS_ENGINE(&dev_priv->gt, _VCS(i)))
Michel Thierry5d869232019-08-23 01:20:34 -07007128 vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
7129 VDN_MFX_POWERGATE_ENABLE(i);
7130 }
7131
7132 I915_WRITE(POWERGATE_ENABLE,
7133 I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
Radhakrishna Sripadaf78d5da2020-01-09 14:37:27 -08007134
7135 /* Wa_1409825376:tgl (pre-prod)*/
7136 if (IS_TGL_REVID(dev_priv, TGL_REVID_A0, TGL_REVID_A0))
7137 I915_WRITE(GEN9_CLKGATE_DIS_3, I915_READ(GEN9_CLKGATE_DIS_3) |
7138 TGL_VRH_GATING_DIS);
Matt Atwoodf9d77422020-04-15 15:35:35 -04007139
7140 /* Wa_14011059788:tgl */
7141 intel_uncore_rmw(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7142 0, DFR_DISABLE);
Michel Thierry5d869232019-08-23 01:20:34 -07007143}
7144
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007145static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7146{
7147 if (!HAS_PCH_CNP(dev_priv))
7148 return;
7149
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08007150 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07007151 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
7152 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007153}
7154
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007155static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007156{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07007157 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007158 cnp_init_clock_gating(dev_priv);
7159
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07007160 /* This is not an Wa. Enable for better image quality */
7161 I915_WRITE(_3D_CHICKEN3,
7162 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
7163
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007164 /* WaEnableChickenDCPR:cnl */
7165 I915_WRITE(GEN8_CHICKEN_DCPR_1,
7166 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
7167
7168 /* WaFbcWakeMemOn:cnl */
7169 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7170 DISP_FBC_MEMORY_WAKE);
7171
Chris Wilson34991bd2017-11-11 10:03:36 +00007172 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
7173 /* ReadHitWriteOnlyDisable:cnl */
7174 val |= RCCUNIT_CLKGATE_DIS;
Chris Wilson34991bd2017-11-11 10:03:36 +00007175 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007176
Rodrigo Vivia4713c52018-03-07 14:09:12 -08007177 /* Wa_2201832410:cnl */
7178 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
7179 val |= GWUNIT_CLKGATE_DIS;
7180 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
7181
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007182 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08007183 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08007184 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
7185 val |= VFUNIT_CLKGATE_DIS;
7186 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007187}
7188
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007189static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7190{
7191 cnp_init_clock_gating(dev_priv);
7192 gen9_init_clock_gating(dev_priv);
7193
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007194 /* WaFbcTurnOffFbcWatermark:cfl */
7195 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7196 DISP_FBC_WM_DIS);
7197
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007198 /* WaFbcNukeOnHostModify:cfl */
7199 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7200 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7201}
7202
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007203static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007204{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007205 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007206
7207 /* WaDisableSDEUnitClockGating:kbl */
7208 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7209 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7210 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007211
7212 /* WaDisableGamClockGating:kbl */
7213 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7214 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7215 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007216
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007217 /* WaFbcTurnOffFbcWatermark:kbl */
7218 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7219 DISP_FBC_WM_DIS);
7220
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007221 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007222 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7223 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007224}
7225
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007226static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007227{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007228 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007229
7230 /* WAC6entrylatency:skl */
7231 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7232 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007233
Ville Syrjäläc4615b22020-07-08 16:12:21 +03007234 /* WaFbcTurnOffFbcWatermark:skl */
7235 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
7236 DISP_FBC_WM_DIS);
7237
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007238 /* WaFbcNukeOnHostModify:skl */
7239 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7240 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Ville Syrjäläcd7a8812020-07-08 16:12:22 +03007241
7242 /* WaFbcHighMemBwCorruptionAvoidance:skl */
7243 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7244 ILK_DPFC_DISABLE_DUMMY0);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007245}
7246
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007247static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007248{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007249 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007250
Ville Syrjälä885f1822020-07-08 16:12:20 +03007251 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7252 I915_WRITE(CHICKEN_PIPESL_1(PIPE_A),
7253 I915_READ(CHICKEN_PIPESL_1(PIPE_A)) |
7254 HSW_FBCQ_DIS);
7255
Ben Widawskyab57fff2013-12-12 15:28:04 -08007256 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007257 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007258
Ben Widawskyab57fff2013-12-12 15:28:04 -08007259 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007260 I915_WRITE(CHICKEN_PAR1_1,
7261 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7262
Ben Widawskyab57fff2013-12-12 15:28:04 -08007263 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007264 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007265 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007266 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007267 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007268 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007269
Ben Widawskyab57fff2013-12-12 15:28:04 -08007270 /* WaVSRefCountFullforceMissDisable:bdw */
7271 /* WaDSRefCountFullforceMissDisable:bdw */
7272 I915_WRITE(GEN7_FF_THREAD_MODE,
7273 I915_READ(GEN7_FF_THREAD_MODE) &
7274 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007275
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007276 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7277 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007278
7279 /* WaDisableSDEUnitClockGating:bdw */
7280 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7281 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007282
Imre Deak450174f2016-05-03 15:54:21 +03007283 /* WaProgramL3SqcReg1Default:bdw */
7284 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007285
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007286 /* WaKVMNotificationOnConfigChange:bdw */
7287 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7288 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7289
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007290 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007291
7292 /* WaDisableDopClockGating:bdw
7293 *
7294 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7295 * clock gating.
7296 */
7297 I915_WRITE(GEN6_UCGCTL1,
7298 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007299}
7300
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007301static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007302{
Ville Syrjälä885f1822020-07-08 16:12:20 +03007303 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7304 I915_WRITE(CHICKEN_PIPESL_1(PIPE_A),
7305 I915_READ(CHICKEN_PIPESL_1(PIPE_A)) |
7306 HSW_FBCQ_DIS);
7307
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007308 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007309 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
Chris Wilsonf93ec5f2020-06-11 10:30:15 +01007310 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7311 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
Kenneth Graunke94411592014-12-31 16:23:00 -08007312
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007313 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007314 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7315
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007316 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007317}
7318
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007319static void ivb_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 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007322
Damien Lespiau231e54f2012-10-19 17:55:41 +01007323 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007324
Ville Syrjälä885f1822020-07-08 16:12:20 +03007325 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
7326 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7327 I915_READ(ILK_DISPLAY_CHICKEN1) |
7328 ILK_FBCQ_DIS);
7329
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007330 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007331 I915_WRITE(IVB_CHICKEN3,
7332 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7333 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7334
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007335 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007336 I915_WRITE(GEN7_ROW_CHICKEN2,
7337 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007338 else {
7339 /* must write both registers */
7340 I915_WRITE(GEN7_ROW_CHICKEN2,
7341 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007342 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7343 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007344 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007345
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007346 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007347 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007348 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007349 */
7350 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007351 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007352
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007353 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007354 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7355 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7356 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7357
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007358 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007359
Ben Widawsky20848222012-05-04 18:58:59 -07007360 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7361 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7362 snpcr |= GEN6_MBC_SNPCR_MED;
7363 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007364
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007365 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007366 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007367
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007368 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007369}
7370
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007371static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007372{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007373 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007374 I915_WRITE(IVB_CHICKEN3,
7375 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7376 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7377
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007378 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007379 I915_WRITE(GEN7_ROW_CHICKEN2,
7380 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7381
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007382 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007383 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7384 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7385 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7386
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007387 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007388 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007389 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007390 */
7391 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007392 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007393
Akash Goelc98f5062014-03-24 23:00:07 +05307394 /* WaDisableL3Bank2xClockGate:vlv
7395 * Disabling L3 clock gating- MMIO 940c[25] = 1
7396 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7397 I915_WRITE(GEN7_UCGCTL4,
7398 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007399
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007400 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007401 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007402 * Disable clock gating on th GCFG unit to prevent a delay
7403 * in the reporting of vblank events.
7404 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007405 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007406}
7407
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007408static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007409{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007410 /* WaVSRefCountFullforceMissDisable:chv */
7411 /* WaDSRefCountFullforceMissDisable:chv */
7412 I915_WRITE(GEN7_FF_THREAD_MODE,
7413 I915_READ(GEN7_FF_THREAD_MODE) &
7414 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007415
7416 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7417 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7418 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007419
7420 /* WaDisableCSUnitClockGating:chv */
7421 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7422 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007423
7424 /* WaDisableSDEUnitClockGating:chv */
7425 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7426 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007427
7428 /*
Imre Deak450174f2016-05-03 15:54:21 +03007429 * WaProgramL3SqcReg1Default:chv
7430 * See gfxspecs/Related Documents/Performance Guide/
7431 * LSQC Setting Recommendations.
7432 */
7433 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007434}
7435
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007436static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007437{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007438 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007439
7440 I915_WRITE(RENCLK_GATE_D1, 0);
7441 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7442 GS_UNIT_CLOCK_GATE_DISABLE |
7443 CL_UNIT_CLOCK_GATE_DISABLE);
7444 I915_WRITE(RAMCLK_GATE_D, 0);
7445 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7446 OVRUNIT_CLOCK_GATE_DISABLE |
7447 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007448 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007449 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7450 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007451
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007452 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007453}
7454
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007455static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007456{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007457 struct intel_uncore *uncore = &dev_priv->uncore;
7458
7459 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7460 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7461 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7462 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7463 intel_uncore_write16(uncore, DEUC, 0);
7464 intel_uncore_write(uncore,
7465 MI_ARB_STATE,
7466 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007467}
7468
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007469static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007470{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007471 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7472 I965_RCC_CLOCK_GATE_DISABLE |
7473 I965_RCPB_CLOCK_GATE_DISABLE |
7474 I965_ISC_CLOCK_GATE_DISABLE |
7475 I965_FBC_CLOCK_GATE_DISABLE);
7476 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007477 I915_WRITE(MI_ARB_STATE,
7478 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007479}
7480
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007481static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007482{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007483 u32 dstate = I915_READ(D_STATE);
7484
7485 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7486 DSTATE_DOT_CLOCK_GATING;
7487 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007488
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007489 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007490 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007491
7492 /* IIR "flip pending" means done if this bit is set */
7493 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007494
7495 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007496 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007497
7498 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7499 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007500
7501 I915_WRITE(MI_ARB_STATE,
7502 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007503}
7504
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007505static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007506{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007507 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007508
7509 /* interrupts should cause a wake up from C3 */
7510 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7511 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007512
7513 I915_WRITE(MEM_MODE,
7514 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Ville Syrjälä5cecf502020-07-02 18:37:23 +03007515
7516 /*
7517 * Have FBC ignore 3D activity since we use software
7518 * render tracking, and otherwise a pure 3D workload
7519 * (even if it just renders a single frame and then does
7520 * abosultely nothing) would not allow FBC to recompress
7521 * until a 2D blit occurs.
7522 */
7523 I915_WRITE(SCPD0,
7524 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007525}
7526
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007527static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007528{
Ville Syrjälä10383922014-08-15 01:21:54 +03007529 I915_WRITE(MEM_MODE,
7530 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7531 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007532}
7533
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007534void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007535{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007536 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007537}
7538
Ville Syrjälä712bf362016-10-31 22:37:23 +02007539void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007540{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007541 if (HAS_PCH_LPT(dev_priv))
7542 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007543}
7544
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007545static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007546{
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007547 drm_dbg_kms(&dev_priv->drm,
7548 "No clock gating settings or workarounds applied.\n");
Imre Deakbb400da2016-03-16 13:38:54 +02007549}
7550
7551/**
7552 * intel_init_clock_gating_hooks - setup the clock gating hooks
7553 * @dev_priv: device private
7554 *
7555 * Setup the hooks that configure which clocks of a given platform can be
7556 * gated and also apply various GT and display specific workarounds for these
7557 * platforms. Note that some GT specific workarounds are applied separately
7558 * when GPU contexts or batchbuffers start their execution.
7559 */
7560void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7561{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07007562 if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07007563 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07007564 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007565 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07007566 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007567 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Chris Wilson5f4ae272020-06-02 15:05:40 +01007568 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007569 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007570 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007571 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007572 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007573 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007574 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007575 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007576 else if (IS_GEMINILAKE(dev_priv))
7577 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007578 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007579 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007580 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007581 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007582 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007583 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007584 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007585 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007586 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007587 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007588 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02007589 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007590 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007591 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007592 else if (IS_G4X(dev_priv))
7593 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007594 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007595 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007596 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007597 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007598 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02007599 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7600 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7601 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007602 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02007603 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7604 else {
7605 MISSING_CASE(INTEL_DEVID(dev_priv));
7606 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7607 }
7608}
7609
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007610/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007611void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007612{
Daniel Vetterc921aba2012-04-26 23:28:17 +02007613 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007614 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08007615 pnv_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007616 else if (IS_GEN(dev_priv, 5))
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08007617 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007618
James Ausmusb068a862019-10-09 10:23:14 -07007619 if (intel_has_sagv(dev_priv))
7620 skl_setup_sagv_block_time(dev_priv);
7621
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007622 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007623 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007624 skl_setup_wm_latency(dev_priv);
Matt Roper98d39492016-05-12 07:06:03 -07007625 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007626 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007627 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007628
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007629 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007630 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007631 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007632 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007633 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007634 dev_priv->display.compute_intermediate_wm =
7635 ilk_compute_intermediate_wm;
7636 dev_priv->display.initial_watermarks =
7637 ilk_initial_watermarks;
7638 dev_priv->display.optimize_watermarks =
7639 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007640 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007641 drm_dbg_kms(&dev_priv->drm,
7642 "Failed to read display plane latency. "
7643 "Disable CxSR\n");
Ville Syrjäläbd602542014-01-07 16:14:10 +02007644 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007645 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007646 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007647 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007648 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007649 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007650 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007651 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03007652 } else if (IS_G4X(dev_priv)) {
7653 g4x_setup_wm_latency(dev_priv);
7654 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
7655 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
7656 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
7657 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007658 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00007659 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007660 dev_priv->is_ddr3,
7661 dev_priv->fsb_freq,
7662 dev_priv->mem_freq)) {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007663 drm_info(&dev_priv->drm,
7664 "failed to find known CxSR latency "
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007665 "(found ddr%s fsb freq %d, mem freq %d), "
7666 "disabling CxSR\n",
7667 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7668 dev_priv->fsb_freq, dev_priv->mem_freq);
7669 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007670 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007671 dev_priv->display.update_wm = NULL;
7672 } else
Lucas De Marchi1d218222019-12-24 00:40:04 -08007673 dev_priv->display.update_wm = pnv_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007674 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007675 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007676 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007677 dev_priv->display.update_wm = i9xx_update_wm;
7678 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007679 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03007680 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007681 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007682 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007683 } else {
7684 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007685 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007686 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007687 } else {
Wambui Karugaf8d18d52020-01-07 18:13:30 +03007688 drm_err(&dev_priv->drm,
7689 "unexpected fall-through in %s\n", __func__);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007690 }
7691}
7692
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00007693void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01007694{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01007695 dev_priv->runtime_pm.suspended = false;
7696 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007697}
Ville Syrjälä3cf43cd2020-02-25 19:11:13 +02007698
7699static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
7700{
7701 struct intel_dbuf_state *dbuf_state;
7702
7703 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
7704 if (!dbuf_state)
7705 return NULL;
7706
7707 return &dbuf_state->base;
7708}
7709
7710static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
7711 struct intel_global_state *state)
7712{
7713 kfree(state);
7714}
7715
7716static const struct intel_global_state_funcs intel_dbuf_funcs = {
7717 .atomic_duplicate_state = intel_dbuf_duplicate_state,
7718 .atomic_destroy_state = intel_dbuf_destroy_state,
7719};
7720
7721struct intel_dbuf_state *
7722intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
7723{
7724 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7725 struct intel_global_state *dbuf_state;
7726
7727 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
7728 if (IS_ERR(dbuf_state))
7729 return ERR_CAST(dbuf_state);
7730
7731 return to_intel_dbuf_state(dbuf_state);
7732}
7733
7734int intel_dbuf_init(struct drm_i915_private *dev_priv)
7735{
7736 struct intel_dbuf_state *dbuf_state;
7737
7738 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
7739 if (!dbuf_state)
7740 return -ENOMEM;
7741
7742 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
7743 &dbuf_state->base, &intel_dbuf_funcs);
7744
7745 return 0;
7746}
Ville Syrjäläc7c0e7e2020-02-25 19:11:15 +02007747
7748void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
7749{
7750 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7751 const struct intel_dbuf_state *new_dbuf_state =
7752 intel_atomic_get_new_dbuf_state(state);
7753 const struct intel_dbuf_state *old_dbuf_state =
7754 intel_atomic_get_old_dbuf_state(state);
7755
7756 if (!new_dbuf_state ||
7757 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7758 return;
7759
7760 WARN_ON(!new_dbuf_state->base.changed);
7761
7762 gen9_dbuf_slices_update(dev_priv,
7763 old_dbuf_state->enabled_slices |
7764 new_dbuf_state->enabled_slices);
7765}
7766
7767void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
7768{
7769 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7770 const struct intel_dbuf_state *new_dbuf_state =
7771 intel_atomic_get_new_dbuf_state(state);
7772 const struct intel_dbuf_state *old_dbuf_state =
7773 intel_atomic_get_old_dbuf_state(state);
7774
7775 if (!new_dbuf_state ||
7776 new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
7777 return;
7778
7779 WARN_ON(!new_dbuf_state->base.changed);
7780
7781 gen9_dbuf_slices_update(dev_priv,
7782 new_dbuf_state->enabled_slices);
7783}