blob: c341556935cd52073d1a7cfd494d2e5327edb960 [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"
Jani Nikula1d455f82019-08-06 14:39:33 +030036#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030037#include "display/intel_fbc.h"
38#include "display/intel_sprite.h"
39
Andi Shyti0dc3c562019-10-20 19:41:39 +010040#include "gt/intel_llc.h"
41
Eugeni Dodonov85208be2012-04-16 22:20:34 -030042#include "i915_drv.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030043#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030044#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030045#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010046#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020047#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030048
Ville Syrjälä46f16e62016-10-31 22:37:22 +020049static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030050{
Ville Syrjälä93564042017-08-24 22:10:51 +030051 if (HAS_LLC(dev_priv)) {
52 /*
53 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080054 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030055 *
56 * Must match Sampler, Pixel Back End, and Media. See
57 * WaCompressedResourceSamplerPbeMediaNewHashMode.
58 */
59 I915_WRITE(CHICKEN_PAR1_1,
60 I915_READ(CHICKEN_PAR1_1) |
61 SKL_DE_COMPRESSED_HASH_MODE);
62 }
63
Rodrigo Vivi82525c12017-06-08 08:50:00 -070064 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030065 I915_WRITE(CHICKEN_PAR1_1,
66 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
67
Rodrigo Vivi82525c12017-06-08 08:50:00 -070068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
Rodrigo Vivi82525c12017-06-08 08:50:00 -070072 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
73 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030074 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
Rodrigo Vivi82525c12017-06-08 08:50:00 -070078 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030079 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053081
82 if (IS_SKYLAKE(dev_priv)) {
83 /* WaDisableDopClockGating */
84 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
85 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
86 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +030087}
88
Ville Syrjälä46f16e62016-10-31 22:37:22 +020089static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020090{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020091 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020092
Nick Hoatha7546152015-06-29 14:07:32 +010093 /* WaDisableSDEUnitClockGating:bxt */
94 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
95 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
96
Imre Deak32608ca2015-03-11 11:10:27 +020097 /*
98 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020099 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200100 */
Imre Deak32608ca2015-03-11 11:10:27 +0200101 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200102 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200103
104 /*
105 * Wa: Backlight PWM may stop in the asserted state, causing backlight
106 * to stay fully on.
107 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200108 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
109 PWM1_GATING_DIS | PWM2_GATING_DIS);
Uma Shankar1d85a292018-08-07 21:15:35 +0530110
111 /*
112 * Lower the display internal timeout.
113 * This is needed to avoid any hard hangs when DSI port PLL
114 * is off and a MMIO access is attempted by any privilege
115 * application, using batch buffers or any other means.
116 */
117 I915_WRITE(RM_TIMEOUT, MMIO_TIMEOUT_US(950));
Imre Deaka82abe42015-03-27 14:00:04 +0200118}
119
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200120static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
121{
122 gen9_init_clock_gating(dev_priv);
123
124 /*
125 * WaDisablePWMClockGating:glk
126 * Backlight PWM may stop in the asserted state, causing backlight
127 * to stay fully on.
128 */
129 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
130 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200131
132 /* WaDDIIOTimeout:glk */
133 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
134 u32 val = I915_READ(CHICKEN_MISC_2);
135 val &= ~(GLK_CL0_PWR_DOWN |
136 GLK_CL1_PWR_DOWN |
137 GLK_CL2_PWR_DOWN);
138 I915_WRITE(CHICKEN_MISC_2, val);
139 }
140
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200141}
142
Lucas De Marchi1d218222019-12-24 00:40:04 -0800143static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200144{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200145 u32 tmp;
146
147 tmp = I915_READ(CLKCFG);
148
149 switch (tmp & CLKCFG_FSB_MASK) {
150 case CLKCFG_FSB_533:
151 dev_priv->fsb_freq = 533; /* 133*4 */
152 break;
153 case CLKCFG_FSB_800:
154 dev_priv->fsb_freq = 800; /* 200*4 */
155 break;
156 case CLKCFG_FSB_667:
157 dev_priv->fsb_freq = 667; /* 167*4 */
158 break;
159 case CLKCFG_FSB_400:
160 dev_priv->fsb_freq = 400; /* 100*4 */
161 break;
162 }
163
164 switch (tmp & CLKCFG_MEM_MASK) {
165 case CLKCFG_MEM_533:
166 dev_priv->mem_freq = 533;
167 break;
168 case CLKCFG_MEM_667:
169 dev_priv->mem_freq = 667;
170 break;
171 case CLKCFG_MEM_800:
172 dev_priv->mem_freq = 800;
173 break;
174 }
175
176 /* detect pineview DDR3 setting */
177 tmp = I915_READ(CSHRDDR3CTL);
178 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
179}
180
Lucas De Marchi9eae5e22019-12-24 00:40:09 -0800181static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200182{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200183 u16 ddrpll, csipll;
184
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100185 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
186 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200187
188 switch (ddrpll & 0xff) {
189 case 0xc:
190 dev_priv->mem_freq = 800;
191 break;
192 case 0x10:
193 dev_priv->mem_freq = 1066;
194 break;
195 case 0x14:
196 dev_priv->mem_freq = 1333;
197 break;
198 case 0x18:
199 dev_priv->mem_freq = 1600;
200 break;
201 default:
202 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
203 ddrpll & 0xff);
204 dev_priv->mem_freq = 0;
205 break;
206 }
207
Daniel Vetterc921aba2012-04-26 23:28:17 +0200208 switch (csipll & 0x3ff) {
209 case 0x00c:
210 dev_priv->fsb_freq = 3200;
211 break;
212 case 0x00e:
213 dev_priv->fsb_freq = 3733;
214 break;
215 case 0x010:
216 dev_priv->fsb_freq = 4266;
217 break;
218 case 0x012:
219 dev_priv->fsb_freq = 4800;
220 break;
221 case 0x014:
222 dev_priv->fsb_freq = 5333;
223 break;
224 case 0x016:
225 dev_priv->fsb_freq = 5866;
226 break;
227 case 0x018:
228 dev_priv->fsb_freq = 6400;
229 break;
230 default:
231 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
232 csipll & 0x3ff);
233 dev_priv->fsb_freq = 0;
234 break;
235 }
Daniel Vetterc921aba2012-04-26 23:28:17 +0200236}
237
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300238static const struct cxsr_latency cxsr_latency_table[] = {
239 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
240 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
241 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
242 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
243 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
244
245 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
246 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
247 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
248 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
249 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
250
251 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
252 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
253 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
254 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
255 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
256
257 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
258 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
259 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
260 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
261 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
262
263 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
264 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
265 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
266 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
267 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
268
269 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
270 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
271 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
272 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
273 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
274};
275
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100276static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
277 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300278 int fsb,
279 int mem)
280{
281 const struct cxsr_latency *latency;
282 int i;
283
284 if (fsb == 0 || mem == 0)
285 return NULL;
286
287 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
288 latency = &cxsr_latency_table[i];
289 if (is_desktop == latency->is_desktop &&
290 is_ddr3 == latency->is_ddr3 &&
291 fsb == latency->fsb_freq && mem == latency->mem_freq)
292 return latency;
293 }
294
295 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
296
297 return NULL;
298}
299
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200300static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
301{
302 u32 val;
303
Chris Wilson337fa6e2019-04-26 09:17:20 +0100304 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200305
306 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
307 if (enable)
308 val &= ~FORCE_DDR_HIGH_FREQ;
309 else
310 val |= FORCE_DDR_HIGH_FREQ;
311 val &= ~FORCE_DDR_LOW_FREQ;
312 val |= FORCE_DDR_FREQ_REQ_ACK;
313 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
314
315 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
316 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
317 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
318
Chris Wilson337fa6e2019-04-26 09:17:20 +0100319 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200320}
321
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200322static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
323{
324 u32 val;
325
Chris Wilson337fa6e2019-04-26 09:17:20 +0100326 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200327
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200328 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200329 if (enable)
330 val |= DSP_MAXFIFO_PM5_ENABLE;
331 else
332 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200333 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200334
Chris Wilson337fa6e2019-04-26 09:17:20 +0100335 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200336}
337
Ville Syrjäläf4998962015-03-10 17:02:21 +0200338#define FW_WM(value, plane) \
339 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
340
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200341static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300342{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200343 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300344 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300345
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100346 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200347 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300348 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300349 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200350 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200351 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300352 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300353 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200354 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200355 val = I915_READ(DSPFW3);
356 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
357 if (enable)
358 val |= PINEVIEW_SELF_REFRESH_EN;
359 else
360 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300361 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300362 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100363 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200364 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300365 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
366 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
367 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300368 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100369 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300370 /*
371 * FIXME can't find a bit like this for 915G, and
372 * and yet it does have the related watermark in
373 * FW_BLC_SELF. What's going on?
374 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200375 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300376 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
377 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
378 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300379 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300380 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200381 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300382 }
383
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200384 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
385
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200386 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
387 enableddisabled(enable),
388 enableddisabled(was_enabled));
389
390 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300391}
392
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300393/**
394 * intel_set_memory_cxsr - Configure CxSR state
395 * @dev_priv: i915 device
396 * @enable: Allow vs. disallow CxSR
397 *
398 * Allow or disallow the system to enter a special CxSR
399 * (C-state self refresh) state. What typically happens in CxSR mode
400 * is that several display FIFOs may get combined into a single larger
401 * FIFO for a particular plane (so called max FIFO mode) to allow the
402 * system to defer memory fetches longer, and the memory will enter
403 * self refresh.
404 *
405 * Note that enabling CxSR does not guarantee that the system enter
406 * this special mode, nor does it guarantee that the system stays
407 * in that mode once entered. So this just allows/disallows the system
408 * to autonomously utilize the CxSR mode. Other factors such as core
409 * C-states will affect when/if the system actually enters/exits the
410 * CxSR mode.
411 *
412 * Note that on VLV/CHV this actually only controls the max FIFO mode,
413 * and the system is free to enter/exit memory self refresh at any time
414 * even when the use of CxSR has been disallowed.
415 *
416 * While the system is actually in the CxSR/max FIFO mode, some plane
417 * control registers will not get latched on vblank. Thus in order to
418 * guarantee the system will respond to changes in the plane registers
419 * we must always disallow CxSR prior to making changes to those registers.
420 * Unfortunately the system will re-evaluate the CxSR conditions at
421 * frame start which happens after vblank start (which is when the plane
422 * registers would get latched), so we can't proceed with the plane update
423 * during the same frame where we disallowed CxSR.
424 *
425 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
426 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
427 * the hardware w.r.t. HPLL SR when writing to plane registers.
428 * Disallowing just CxSR is sufficient.
429 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200430bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200431{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200432 bool ret;
433
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200434 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200435 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300436 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
437 dev_priv->wm.vlv.cxsr = enable;
438 else if (IS_G4X(dev_priv))
439 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200440 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200441
442 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200443}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200444
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300445/*
446 * Latency for FIFO fetches is dependent on several factors:
447 * - memory configuration (speed, channels)
448 * - chipset
449 * - current MCH state
450 * It can be fairly high in some situations, so here we assume a fairly
451 * pessimal value. It's a tradeoff between extra memory fetches (if we
452 * set this value too high, the FIFO will fetch frequently to stay full)
453 * and power consumption (set it too low to save power and we might see
454 * FIFO underruns and display "flicker").
455 *
456 * A value of 5us seems to be a good balance; safe for very low end
457 * platforms but not overly aggressive on lower latency configs.
458 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100459static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300460
Ville Syrjäläb5004722015-03-05 21:19:47 +0200461#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
462 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
463
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200464static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200465{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +0100466 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200467 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200468 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200469 enum pipe pipe = crtc->pipe;
470 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200471
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200472 switch (pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200473 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200474 case PIPE_A:
475 dsparb = I915_READ(DSPARB);
476 dsparb2 = I915_READ(DSPARB2);
477 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
478 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
479 break;
480 case PIPE_B:
481 dsparb = I915_READ(DSPARB);
482 dsparb2 = I915_READ(DSPARB2);
483 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
484 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
485 break;
486 case PIPE_C:
487 dsparb2 = I915_READ(DSPARB2);
488 dsparb3 = I915_READ(DSPARB3);
489 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
490 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
491 break;
492 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200493 MISSING_CASE(pipe);
494 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200495 }
496
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200497 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
498 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
499 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
500 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200501}
502
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200503static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
504 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300505{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200506 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300507 int size;
508
509 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200510 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300511 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
512
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200513 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
514 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515
516 return size;
517}
518
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200519static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
520 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300521{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200522 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300523 int size;
524
525 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200526 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300527 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
528 size >>= 1; /* Convert to cachelines */
529
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200530 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
531 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300532
533 return size;
534}
535
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200536static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
537 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300538{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200539 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300540 int size;
541
542 size = dsparb & 0x7f;
543 size >>= 2; /* Convert to cachelines */
544
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200545 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
546 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300547
548 return size;
549}
550
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300551/* Pineview has different values for various configs */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800552static const struct intel_watermark_params pnv_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300553 .fifo_size = PINEVIEW_DISPLAY_FIFO,
554 .max_wm = PINEVIEW_MAX_WM,
555 .default_wm = PINEVIEW_DFT_WM,
556 .guard_size = PINEVIEW_GUARD_WM,
557 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300558};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800559
560static const struct intel_watermark_params pnv_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300561 .fifo_size = PINEVIEW_DISPLAY_FIFO,
562 .max_wm = PINEVIEW_MAX_WM,
563 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
564 .guard_size = PINEVIEW_GUARD_WM,
565 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300566};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800567
568static const struct intel_watermark_params pnv_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300569 .fifo_size = PINEVIEW_CURSOR_FIFO,
570 .max_wm = PINEVIEW_CURSOR_MAX_WM,
571 .default_wm = PINEVIEW_CURSOR_DFT_WM,
572 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
573 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300574};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800575
576static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300577 .fifo_size = PINEVIEW_CURSOR_FIFO,
578 .max_wm = PINEVIEW_CURSOR_MAX_WM,
579 .default_wm = PINEVIEW_CURSOR_DFT_WM,
580 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
581 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300582};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800583
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300584static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300585 .fifo_size = I965_CURSOR_FIFO,
586 .max_wm = I965_CURSOR_MAX_WM,
587 .default_wm = I965_CURSOR_DFT_WM,
588 .guard_size = 2,
589 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300590};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800591
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300592static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300593 .fifo_size = I945_FIFO_SIZE,
594 .max_wm = I915_MAX_WM,
595 .default_wm = 1,
596 .guard_size = 2,
597 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300598};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800599
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300601 .fifo_size = I915_FIFO_SIZE,
602 .max_wm = I915_MAX_WM,
603 .default_wm = 1,
604 .guard_size = 2,
605 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300606};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800607
Ville Syrjälä9d539102014-08-15 01:21:53 +0300608static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300609 .fifo_size = I855GM_FIFO_SIZE,
610 .max_wm = I915_MAX_WM,
611 .default_wm = 1,
612 .guard_size = 2,
613 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300614};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800615
Ville Syrjälä9d539102014-08-15 01:21:53 +0300616static const struct intel_watermark_params i830_bc_wm_info = {
617 .fifo_size = I855GM_FIFO_SIZE,
618 .max_wm = I915_MAX_WM/2,
619 .default_wm = 1,
620 .guard_size = 2,
621 .cacheline_size = I830_FIFO_LINE_SIZE,
622};
Lucas De Marchi1d218222019-12-24 00:40:04 -0800623
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200624static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300625 .fifo_size = I830_FIFO_SIZE,
626 .max_wm = I915_MAX_WM,
627 .default_wm = 1,
628 .guard_size = 2,
629 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300630};
631
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300632/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300633 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
634 * @pixel_rate: Pipe pixel rate in kHz
635 * @cpp: Plane bytes per pixel
636 * @latency: Memory wakeup latency in 0.1us units
637 *
638 * Compute the watermark using the method 1 or "small buffer"
639 * formula. The caller may additonally add extra cachelines
640 * to account for TLB misses and clock crossings.
641 *
642 * This method is concerned with the short term drain rate
643 * of the FIFO, ie. it does not account for blanking periods
644 * which would effectively reduce the average drain rate across
645 * a longer period. The name "small" refers to the fact the
646 * FIFO is relatively small compared to the amount of data
647 * fetched.
648 *
649 * The FIFO level vs. time graph might look something like:
650 *
651 * |\ |\
652 * | \ | \
653 * __---__---__ (- plane active, _ blanking)
654 * -> time
655 *
656 * or perhaps like this:
657 *
658 * |\|\ |\|\
659 * __----__----__ (- plane active, _ blanking)
660 * -> time
661 *
662 * Returns:
663 * The watermark in bytes
664 */
665static unsigned int intel_wm_method1(unsigned int pixel_rate,
666 unsigned int cpp,
667 unsigned int latency)
668{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200669 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300670
Ville Syrjäläd492a292019-04-08 18:27:01 +0300671 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300672 ret = DIV_ROUND_UP_ULL(ret, 10000);
673
674 return ret;
675}
676
677/**
678 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
679 * @pixel_rate: Pipe pixel rate in kHz
680 * @htotal: Pipe horizontal total
681 * @width: Plane width in pixels
682 * @cpp: Plane bytes per pixel
683 * @latency: Memory wakeup latency in 0.1us units
684 *
685 * Compute the watermark using the method 2 or "large buffer"
686 * formula. The caller may additonally add extra cachelines
687 * to account for TLB misses and clock crossings.
688 *
689 * This method is concerned with the long term drain rate
690 * of the FIFO, ie. it does account for blanking periods
691 * which effectively reduce the average drain rate across
692 * a longer period. The name "large" refers to the fact the
693 * FIFO is relatively large compared to the amount of data
694 * fetched.
695 *
696 * The FIFO level vs. time graph might look something like:
697 *
698 * |\___ |\___
699 * | \___ | \___
700 * | \ | \
701 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
702 * -> time
703 *
704 * Returns:
705 * The watermark in bytes
706 */
707static unsigned int intel_wm_method2(unsigned int pixel_rate,
708 unsigned int htotal,
709 unsigned int width,
710 unsigned int cpp,
711 unsigned int latency)
712{
713 unsigned int ret;
714
715 /*
716 * FIXME remove once all users are computing
717 * watermarks in the correct place.
718 */
719 if (WARN_ON_ONCE(htotal == 0))
720 htotal = 1;
721
722 ret = (latency * pixel_rate) / (htotal * 10000);
723 ret = (ret + 1) * width * cpp;
724
725 return ret;
726}
727
728/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300729 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300730 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300731 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000732 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200733 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300734 * @latency_ns: memory latency for the platform
735 *
736 * Calculate the watermark level (the level at which the display plane will
737 * start fetching from memory again). Each chip has a different display
738 * FIFO size and allocation, so the caller needs to figure that out and pass
739 * in the correct intel_watermark_params structure.
740 *
741 * As the pixel clock runs, the FIFO will be drained at a rate that depends
742 * on the pixel size. When it reaches the watermark level, it'll start
743 * fetching FIFO line sized based chunks from memory until the FIFO fills
744 * past the watermark point. If the FIFO drains completely, a FIFO underrun
745 * will occur, and a display engine hang could result.
746 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300747static unsigned int intel_calculate_wm(int pixel_rate,
748 const struct intel_watermark_params *wm,
749 int fifo_size, int cpp,
750 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300751{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300752 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300753
754 /*
755 * Note: we need to make sure we don't overflow for various clock &
756 * latency values.
757 * clocks go from a few thousand to several hundred thousand.
758 * latency is usually a few thousand
759 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300760 entries = intel_wm_method1(pixel_rate, cpp,
761 latency_ns / 100);
762 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
763 wm->guard_size;
764 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300765
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300766 wm_size = fifo_size - entries;
767 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300768
769 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300770 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771 wm_size = wm->max_wm;
772 if (wm_size <= 0)
773 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300774
775 /*
776 * Bspec seems to indicate that the value shouldn't be lower than
777 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
778 * Lets go for 8 which is the burst size since certain platforms
779 * already use a hardcoded 8 (which is what the spec says should be
780 * done).
781 */
782 if (wm_size <= 8)
783 wm_size = 8;
784
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300785 return wm_size;
786}
787
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300788static bool is_disabling(int old, int new, int threshold)
789{
790 return old >= threshold && new < threshold;
791}
792
793static bool is_enabling(int old, int new, int threshold)
794{
795 return old < threshold && new >= threshold;
796}
797
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300798static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
799{
800 return dev_priv->wm.max_level + 1;
801}
802
Ville Syrjälä24304d812017-03-14 17:10:49 +0200803static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
804 const struct intel_plane_state *plane_state)
805{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100806 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä24304d812017-03-14 17:10:49 +0200807
808 /* FIXME check the 'enable' instead */
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100809 if (!crtc_state->hw.active)
Ville Syrjälä24304d812017-03-14 17:10:49 +0200810 return false;
811
812 /*
813 * Treat cursor with fb as always visible since cursor updates
814 * can happen faster than the vrefresh rate, and the current
815 * watermark code doesn't handle that correctly. Cursor updates
816 * which set/clear the fb or change the cursor size are going
817 * to get throttled by intel_legacy_cursor_update() to work
818 * around this problem with the watermark code.
819 */
820 if (plane->id == PLANE_CURSOR)
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +0100821 return plane_state->hw.fb != NULL;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200822 else
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +0100823 return plane_state->uapi.visible;
Ville Syrjälä24304d812017-03-14 17:10:49 +0200824}
825
Ville Syrjälä04da7b92019-11-27 22:12:11 +0200826static bool intel_crtc_active(struct intel_crtc *crtc)
827{
828 /* Be paranoid as we can arrive here with only partial
829 * state retrieved from the hardware during setup.
830 *
831 * We can ditch the adjusted_mode.crtc_clock check as soon
832 * as Haswell has gained clock readout/fastboot support.
833 *
834 * We can ditch the crtc->primary->state->fb check as soon as we can
835 * properly reconstruct framebuffers.
836 *
837 * FIXME: The intel_crtc->active here should be switched to
838 * crtc->state->active once we have proper CRTC states wired up
839 * for atomic.
840 */
841 return crtc->active && crtc->base.primary->state->fb &&
842 crtc->config->hw.adjusted_mode.crtc_clock;
843}
844
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200845static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300846{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200847 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300848
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200849 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200850 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300851 if (enabled)
852 return NULL;
853 enabled = crtc;
854 }
855 }
856
857 return enabled;
858}
859
Lucas De Marchi1d218222019-12-24 00:40:04 -0800860static void pnv_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300861{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200862 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200863 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300864 const struct cxsr_latency *latency;
865 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300866 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300867
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000868 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100869 dev_priv->is_ddr3,
870 dev_priv->fsb_freq,
871 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300872 if (!latency) {
873 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300874 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300875 return;
876 }
877
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200878 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300879 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200880 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +0100881 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200882 const struct drm_framebuffer *fb =
883 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200884 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300885 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300886
887 /* Display SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800888 wm = intel_calculate_wm(clock, &pnv_display_wm,
889 pnv_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200890 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300891 reg = I915_READ(DSPFW1);
892 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200893 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300894 I915_WRITE(DSPFW1, reg);
895 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
896
897 /* cursor SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800898 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
899 pnv_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300900 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300901 reg = I915_READ(DSPFW3);
902 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200903 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300904 I915_WRITE(DSPFW3, reg);
905
906 /* Display HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800907 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
908 pnv_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200909 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300910 reg = I915_READ(DSPFW3);
911 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200912 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913 I915_WRITE(DSPFW3, reg);
914
915 /* cursor HPLL off SR */
Lucas De Marchi1d218222019-12-24 00:40:04 -0800916 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
917 pnv_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300918 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300919 reg = I915_READ(DSPFW3);
920 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200921 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300922 I915_WRITE(DSPFW3, reg);
923 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
924
Imre Deak5209b1f2014-07-01 12:36:17 +0300925 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300926 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300927 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300928 }
929}
930
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300931/*
932 * Documentation says:
933 * "If the line size is small, the TLB fetches can get in the way of the
934 * data fetches, causing some lag in the pixel data return which is not
935 * accounted for in the above formulas. The following adjustment only
936 * needs to be applied if eight whole lines fit in the buffer at once.
937 * The WM is adjusted upwards by the difference between the FIFO size
938 * and the size of 8 whole lines. This adjustment is always performed
939 * in the actual pixel depth regardless of whether FBC is enabled or not."
940 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000941static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300942{
943 int tlb_miss = fifo_size * 64 - width * cpp * 8;
944
945 return max(0, tlb_miss);
946}
947
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300948static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
949 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300950{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300951 enum pipe pipe;
952
953 for_each_pipe(dev_priv, pipe)
954 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
955
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300956 I915_WRITE(DSPFW1,
957 FW_WM(wm->sr.plane, SR) |
958 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
959 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
960 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
961 I915_WRITE(DSPFW2,
962 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
963 FW_WM(wm->sr.fbc, FBC_SR) |
964 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
965 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
966 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
967 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
968 I915_WRITE(DSPFW3,
969 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
970 FW_WM(wm->sr.cursor, CURSOR_SR) |
971 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
972 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300973
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300974 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300975}
976
Ville Syrjälä15665972015-03-10 16:16:28 +0200977#define FW_WM_VLV(value, plane) \
978 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
979
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200980static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200981 const struct vlv_wm_values *wm)
982{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200983 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200984
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200985 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200986 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
987
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200988 I915_WRITE(VLV_DDL(pipe),
989 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
990 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
991 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
992 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
993 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200994
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200995 /*
996 * Zero the (unused) WM1 watermarks, and also clear all the
997 * high order bits so that there are no out of bounds values
998 * present in the registers during the reprogramming.
999 */
1000 I915_WRITE(DSPHOWM, 0);
1001 I915_WRITE(DSPHOWM1, 0);
1002 I915_WRITE(DSPFW4, 0);
1003 I915_WRITE(DSPFW5, 0);
1004 I915_WRITE(DSPFW6, 0);
1005
Ville Syrjäläae801522015-03-05 21:19:49 +02001006 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +02001007 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001008 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1009 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1010 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001011 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001012 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1013 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1014 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001015 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001016 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001017
1018 if (IS_CHERRYVIEW(dev_priv)) {
1019 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001020 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1021 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001022 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001023 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1024 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001025 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001026 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1027 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001028 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001029 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001030 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1031 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1032 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1033 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1034 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1035 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1036 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1037 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1038 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001039 } else {
1040 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001041 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1042 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001043 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001044 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001045 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1046 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1047 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1048 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1049 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1050 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001051 }
1052
1053 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001054}
1055
Ville Syrjälä15665972015-03-10 16:16:28 +02001056#undef FW_WM_VLV
1057
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001058static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1059{
1060 /* all latencies in usec */
1061 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1062 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001063 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001064
Ville Syrjälä79d94302017-04-21 21:14:30 +03001065 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001066}
1067
1068static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1069{
1070 /*
1071 * DSPCNTR[13] supposedly controls whether the
1072 * primary plane can use the FIFO space otherwise
1073 * reserved for the sprite plane. It's not 100% clear
1074 * what the actual FIFO size is, but it looks like we
1075 * can happily set both primary and sprite watermarks
1076 * up to 127 cachelines. So that would seem to mean
1077 * that either DSPCNTR[13] doesn't do anything, or that
1078 * the total FIFO is >= 256 cachelines in size. Either
1079 * way, we don't seem to have to worry about this
1080 * repartitioning as the maximum watermark value the
1081 * register can hold for each plane is lower than the
1082 * minimum FIFO size.
1083 */
1084 switch (plane_id) {
1085 case PLANE_CURSOR:
1086 return 63;
1087 case PLANE_PRIMARY:
1088 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1089 case PLANE_SPRITE0:
1090 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1091 default:
1092 MISSING_CASE(plane_id);
1093 return 0;
1094 }
1095}
1096
1097static int g4x_fbc_fifo_size(int level)
1098{
1099 switch (level) {
1100 case G4X_WM_LEVEL_SR:
1101 return 7;
1102 case G4X_WM_LEVEL_HPLL:
1103 return 15;
1104 default:
1105 MISSING_CASE(level);
1106 return 0;
1107 }
1108}
1109
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001110static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1111 const struct intel_plane_state *plane_state,
1112 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001113{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001114 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001115 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1116 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01001117 &crtc_state->hw.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001118 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1119 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001120
1121 if (latency == 0)
1122 return USHRT_MAX;
1123
1124 if (!intel_wm_plane_visible(crtc_state, plane_state))
1125 return 0;
1126
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001127 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001128
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001129 /*
1130 * Not 100% sure which way ELK should go here as the
1131 * spec only says CL/CTG should assume 32bpp and BW
1132 * doesn't need to. But as these things followed the
1133 * mobile vs. desktop lines on gen3 as well, let's
1134 * assume ELK doesn't need this.
1135 *
1136 * The spec also fails to list such a restriction for
1137 * the HPLL watermark, which seems a little strange.
1138 * Let's use 32bpp for the HPLL watermark as well.
1139 */
1140 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1141 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001142 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001143
1144 clock = adjusted_mode->crtc_clock;
1145 htotal = adjusted_mode->crtc_htotal;
1146
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001147 width = drm_rect_width(&plane_state->uapi.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001148
1149 if (plane->id == PLANE_CURSOR) {
1150 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1151 } else if (plane->id == PLANE_PRIMARY &&
1152 level == G4X_WM_LEVEL_NORMAL) {
1153 wm = intel_wm_method1(clock, cpp, latency);
1154 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001155 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001156
1157 small = intel_wm_method1(clock, cpp, latency);
1158 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1159
1160 wm = min(small, large);
1161 }
1162
1163 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1164 width, cpp);
1165
1166 wm = DIV_ROUND_UP(wm, 64) + 2;
1167
Chris Wilson1a1f1282017-11-07 14:03:38 +00001168 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001169}
1170
1171static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1172 int level, enum plane_id plane_id, u16 value)
1173{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001174 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001175 bool dirty = false;
1176
1177 for (; level < intel_wm_num_levels(dev_priv); level++) {
1178 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1179
1180 dirty |= raw->plane[plane_id] != value;
1181 raw->plane[plane_id] = value;
1182 }
1183
1184 return dirty;
1185}
1186
1187static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1188 int level, u16 value)
1189{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001190 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001191 bool dirty = false;
1192
1193 /* NORMAL level doesn't have an FBC watermark */
1194 level = max(level, G4X_WM_LEVEL_SR);
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->fbc != value;
1200 raw->fbc = value;
1201 }
1202
1203 return dirty;
1204}
1205
Maarten Lankhorstec193642019-06-28 10:55:17 +02001206static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1207 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001208 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001209
1210static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1211 const struct intel_plane_state *plane_state)
1212{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001213 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001214 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1215 enum plane_id plane_id = plane->id;
1216 bool dirty = false;
1217 int level;
1218
1219 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1220 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1221 if (plane_id == PLANE_PRIMARY)
1222 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1223 goto out;
1224 }
1225
1226 for (level = 0; level < num_levels; level++) {
1227 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1228 int wm, max_wm;
1229
1230 wm = g4x_compute_wm(crtc_state, plane_state, level);
1231 max_wm = g4x_plane_fifo_size(plane_id, level);
1232
1233 if (wm > max_wm)
1234 break;
1235
1236 dirty |= raw->plane[plane_id] != wm;
1237 raw->plane[plane_id] = wm;
1238
1239 if (plane_id != PLANE_PRIMARY ||
1240 level == G4X_WM_LEVEL_NORMAL)
1241 continue;
1242
1243 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1244 raw->plane[plane_id]);
1245 max_wm = g4x_fbc_fifo_size(level);
1246
1247 /*
1248 * FBC wm is not mandatory as we
1249 * can always just disable its use.
1250 */
1251 if (wm > max_wm)
1252 wm = USHRT_MAX;
1253
1254 dirty |= raw->fbc != wm;
1255 raw->fbc = wm;
1256 }
1257
1258 /* mark watermarks as invalid */
1259 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1260
1261 if (plane_id == PLANE_PRIMARY)
1262 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1263
1264 out:
1265 if (dirty) {
1266 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1267 plane->base.name,
1268 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1269 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1270 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1271
1272 if (plane_id == PLANE_PRIMARY)
1273 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1274 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1275 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1276 }
1277
1278 return dirty;
1279}
1280
1281static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1282 enum plane_id plane_id, int level)
1283{
1284 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1285
1286 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1287}
1288
1289static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1290 int level)
1291{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001292 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001293
1294 if (level > dev_priv->wm.max_level)
1295 return false;
1296
1297 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1298 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1299 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1300}
1301
1302/* mark all levels starting from 'level' as invalid */
1303static void g4x_invalidate_wms(struct intel_crtc *crtc,
1304 struct g4x_wm_state *wm_state, int level)
1305{
1306 if (level <= G4X_WM_LEVEL_NORMAL) {
1307 enum plane_id plane_id;
1308
1309 for_each_plane_id_on_crtc(crtc, plane_id)
1310 wm_state->wm.plane[plane_id] = USHRT_MAX;
1311 }
1312
1313 if (level <= G4X_WM_LEVEL_SR) {
1314 wm_state->cxsr = false;
1315 wm_state->sr.cursor = USHRT_MAX;
1316 wm_state->sr.plane = USHRT_MAX;
1317 wm_state->sr.fbc = USHRT_MAX;
1318 }
1319
1320 if (level <= G4X_WM_LEVEL_HPLL) {
1321 wm_state->hpll_en = false;
1322 wm_state->hpll.cursor = USHRT_MAX;
1323 wm_state->hpll.plane = USHRT_MAX;
1324 wm_state->hpll.fbc = USHRT_MAX;
1325 }
1326}
1327
1328static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1329{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001330 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001331 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001332 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001333 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001334 int num_active_planes = hweight8(crtc_state->active_planes &
1335 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001336 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001337 const struct intel_plane_state *old_plane_state;
1338 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001339 struct intel_plane *plane;
1340 enum plane_id plane_id;
1341 int i, level;
1342 unsigned int dirty = 0;
1343
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001344 for_each_oldnew_intel_plane_in_state(state, plane,
1345 old_plane_state,
1346 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001347 if (new_plane_state->hw.crtc != &crtc->base &&
1348 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001349 continue;
1350
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001351 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001352 dirty |= BIT(plane->id);
1353 }
1354
1355 if (!dirty)
1356 return 0;
1357
1358 level = G4X_WM_LEVEL_NORMAL;
1359 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1360 goto out;
1361
1362 raw = &crtc_state->wm.g4x.raw[level];
1363 for_each_plane_id_on_crtc(crtc, plane_id)
1364 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1365
1366 level = G4X_WM_LEVEL_SR;
1367
1368 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1369 goto out;
1370
1371 raw = &crtc_state->wm.g4x.raw[level];
1372 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1373 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1374 wm_state->sr.fbc = raw->fbc;
1375
1376 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1377
1378 level = G4X_WM_LEVEL_HPLL;
1379
1380 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1381 goto out;
1382
1383 raw = &crtc_state->wm.g4x.raw[level];
1384 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1385 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1386 wm_state->hpll.fbc = raw->fbc;
1387
1388 wm_state->hpll_en = wm_state->cxsr;
1389
1390 level++;
1391
1392 out:
1393 if (level == G4X_WM_LEVEL_NORMAL)
1394 return -EINVAL;
1395
1396 /* invalidate the higher levels */
1397 g4x_invalidate_wms(crtc, wm_state, level);
1398
1399 /*
1400 * Determine if the FBC watermark(s) can be used. IF
1401 * this isn't the case we prefer to disable the FBC
1402 ( watermark(s) rather than disable the SR/HPLL
1403 * level(s) entirely.
1404 */
1405 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1406
1407 if (level >= G4X_WM_LEVEL_SR &&
1408 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1409 wm_state->fbc_en = false;
1410 else if (level >= G4X_WM_LEVEL_HPLL &&
1411 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1412 wm_state->fbc_en = false;
1413
1414 return 0;
1415}
1416
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001417static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001418{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001419 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001420 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1421 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1422 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001423 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001424 const struct intel_crtc_state *old_crtc_state =
1425 intel_atomic_get_old_crtc_state(intel_state, crtc);
1426 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001427 enum plane_id plane_id;
1428
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001429 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001430 *intermediate = *optimal;
1431
1432 intermediate->cxsr = false;
1433 intermediate->hpll_en = false;
1434 goto out;
1435 }
1436
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001437 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001438 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001439 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001440 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001441 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1442
1443 for_each_plane_id_on_crtc(crtc, plane_id) {
1444 intermediate->wm.plane[plane_id] =
1445 max(optimal->wm.plane[plane_id],
1446 active->wm.plane[plane_id]);
1447
1448 WARN_ON(intermediate->wm.plane[plane_id] >
1449 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1450 }
1451
1452 intermediate->sr.plane = max(optimal->sr.plane,
1453 active->sr.plane);
1454 intermediate->sr.cursor = max(optimal->sr.cursor,
1455 active->sr.cursor);
1456 intermediate->sr.fbc = max(optimal->sr.fbc,
1457 active->sr.fbc);
1458
1459 intermediate->hpll.plane = max(optimal->hpll.plane,
1460 active->hpll.plane);
1461 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1462 active->hpll.cursor);
1463 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1464 active->hpll.fbc);
1465
1466 WARN_ON((intermediate->sr.plane >
1467 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1468 intermediate->sr.cursor >
1469 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1470 intermediate->cxsr);
1471 WARN_ON((intermediate->sr.plane >
1472 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1473 intermediate->sr.cursor >
1474 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1475 intermediate->hpll_en);
1476
1477 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1478 intermediate->fbc_en && intermediate->cxsr);
1479 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1480 intermediate->fbc_en && intermediate->hpll_en);
1481
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001482out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001483 /*
1484 * If our intermediate WM are identical to the final WM, then we can
1485 * omit the post-vblank programming; only update if it's different.
1486 */
1487 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001488 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001489
1490 return 0;
1491}
1492
1493static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1494 struct g4x_wm_values *wm)
1495{
1496 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001497 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001498
1499 wm->cxsr = true;
1500 wm->hpll_en = true;
1501 wm->fbc_en = true;
1502
1503 for_each_intel_crtc(&dev_priv->drm, crtc) {
1504 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1505
1506 if (!crtc->active)
1507 continue;
1508
1509 if (!wm_state->cxsr)
1510 wm->cxsr = false;
1511 if (!wm_state->hpll_en)
1512 wm->hpll_en = false;
1513 if (!wm_state->fbc_en)
1514 wm->fbc_en = false;
1515
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001516 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001517 }
1518
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001519 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001520 wm->cxsr = false;
1521 wm->hpll_en = false;
1522 wm->fbc_en = false;
1523 }
1524
1525 for_each_intel_crtc(&dev_priv->drm, crtc) {
1526 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1527 enum pipe pipe = crtc->pipe;
1528
1529 wm->pipe[pipe] = wm_state->wm;
1530 if (crtc->active && wm->cxsr)
1531 wm->sr = wm_state->sr;
1532 if (crtc->active && wm->hpll_en)
1533 wm->hpll = wm_state->hpll;
1534 }
1535}
1536
1537static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1538{
1539 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1540 struct g4x_wm_values new_wm = {};
1541
1542 g4x_merge_wm(dev_priv, &new_wm);
1543
1544 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1545 return;
1546
1547 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1548 _intel_set_memory_cxsr(dev_priv, false);
1549
1550 g4x_write_wm_values(dev_priv, &new_wm);
1551
1552 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1553 _intel_set_memory_cxsr(dev_priv, true);
1554
1555 *old_wm = new_wm;
1556}
1557
1558static void g4x_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001559 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001560{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001561 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1562 const struct intel_crtc_state *crtc_state =
1563 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001564
1565 mutex_lock(&dev_priv->wm.wm_mutex);
1566 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1567 g4x_program_watermarks(dev_priv);
1568 mutex_unlock(&dev_priv->wm.wm_mutex);
1569}
1570
1571static void g4x_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001572 struct intel_crtc *crtc)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001573{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001574 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1575 const struct intel_crtc_state *crtc_state =
1576 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001577
1578 if (!crtc_state->wm.need_postvbl_update)
1579 return;
1580
1581 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001582 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001583 g4x_program_watermarks(dev_priv);
1584 mutex_unlock(&dev_priv->wm.wm_mutex);
1585}
1586
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001587/* latency must be in 0.1us units. */
1588static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001589 unsigned int htotal,
1590 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001591 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001592 unsigned int latency)
1593{
1594 unsigned int ret;
1595
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001596 ret = intel_wm_method2(pixel_rate, htotal,
1597 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001598 ret = DIV_ROUND_UP(ret, 64);
1599
1600 return ret;
1601}
1602
Ville Syrjäläbb726512016-10-31 22:37:24 +02001603static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001604{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001605 /* all latencies in usec */
1606 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1607
Ville Syrjälä58590c12015-09-08 21:05:12 +03001608 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1609
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001610 if (IS_CHERRYVIEW(dev_priv)) {
1611 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1612 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001613
1614 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001615 }
1616}
1617
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001618static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1619 const struct intel_plane_state *plane_state,
1620 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001621{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001622 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001623 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001624 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01001625 &crtc_state->hw.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001626 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001627
1628 if (dev_priv->wm.pri_latency[level] == 0)
1629 return USHRT_MAX;
1630
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001631 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001632 return 0;
1633
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001634 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001635 clock = adjusted_mode->crtc_clock;
1636 htotal = adjusted_mode->crtc_htotal;
1637 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001638
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001639 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001640 /*
1641 * FIXME the formula gives values that are
1642 * too big for the cursor FIFO, and hence we
1643 * would never be able to use cursors. For
1644 * now just hardcode the watermark.
1645 */
1646 wm = 63;
1647 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001648 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001649 dev_priv->wm.pri_latency[level] * 10);
1650 }
1651
Chris Wilson1a1f1282017-11-07 14:03:38 +00001652 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001653}
1654
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001655static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1656{
1657 return (active_planes & (BIT(PLANE_SPRITE0) |
1658 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1659}
1660
Ville Syrjälä5012e602017-03-02 19:14:56 +02001661static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001662{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001663 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001664 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001665 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001666 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001667 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001668 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001669 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001670 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001671 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001672 unsigned int total_rate;
1673 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001674
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001675 /*
1676 * When enabling sprite0 after sprite1 has already been enabled
1677 * we tend to get an underrun unless sprite0 already has some
1678 * FIFO space allcoated. Hence we always allocate at least one
1679 * cacheline for sprite0 whenever sprite1 is enabled.
1680 *
1681 * All other plane enable sequences appear immune to this problem.
1682 */
1683 if (vlv_need_sprite0_fifo_workaround(active_planes))
1684 sprite0_fifo_extra = 1;
1685
Ville Syrjälä5012e602017-03-02 19:14:56 +02001686 total_rate = raw->plane[PLANE_PRIMARY] +
1687 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001688 raw->plane[PLANE_SPRITE1] +
1689 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001690
Ville Syrjälä5012e602017-03-02 19:14:56 +02001691 if (total_rate > fifo_size)
1692 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001693
Ville Syrjälä5012e602017-03-02 19:14:56 +02001694 if (total_rate == 0)
1695 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001696
Ville Syrjälä5012e602017-03-02 19:14:56 +02001697 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001698 unsigned int rate;
1699
Ville Syrjälä5012e602017-03-02 19:14:56 +02001700 if ((active_planes & BIT(plane_id)) == 0) {
1701 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001702 continue;
1703 }
1704
Ville Syrjälä5012e602017-03-02 19:14:56 +02001705 rate = raw->plane[plane_id];
1706 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1707 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001708 }
1709
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001710 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1711 fifo_left -= sprite0_fifo_extra;
1712
Ville Syrjälä5012e602017-03-02 19:14:56 +02001713 fifo_state->plane[PLANE_CURSOR] = 63;
1714
1715 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001716
1717 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001718 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001719 int plane_extra;
1720
1721 if (fifo_left == 0)
1722 break;
1723
Ville Syrjälä5012e602017-03-02 19:14:56 +02001724 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001725 continue;
1726
1727 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001728 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001729 fifo_left -= plane_extra;
1730 }
1731
Ville Syrjälä5012e602017-03-02 19:14:56 +02001732 WARN_ON(active_planes != 0 && fifo_left != 0);
1733
1734 /* give it all to the first plane if none are active */
1735 if (active_planes == 0) {
1736 WARN_ON(fifo_left != fifo_size);
1737 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1738 }
1739
1740 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001741}
1742
Ville Syrjäläff32c542017-03-02 19:14:57 +02001743/* mark all levels starting from 'level' as invalid */
1744static void vlv_invalidate_wms(struct intel_crtc *crtc,
1745 struct vlv_wm_state *wm_state, int level)
1746{
1747 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1748
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001749 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001750 enum plane_id plane_id;
1751
1752 for_each_plane_id_on_crtc(crtc, plane_id)
1753 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1754
1755 wm_state->sr[level].cursor = USHRT_MAX;
1756 wm_state->sr[level].plane = USHRT_MAX;
1757 }
1758}
1759
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001760static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1761{
1762 if (wm > fifo_size)
1763 return USHRT_MAX;
1764 else
1765 return fifo_size - wm;
1766}
1767
Ville Syrjäläff32c542017-03-02 19:14:57 +02001768/*
1769 * Starting from 'level' set all higher
1770 * levels to 'value' in the "raw" watermarks.
1771 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001772static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001773 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001774{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001775 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001776 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001777 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001778
Ville Syrjäläff32c542017-03-02 19:14:57 +02001779 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001780 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001781
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001782 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001783 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001784 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001785
1786 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001787}
1788
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001789static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1790 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001791{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01001792 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001793 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001794 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001795 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001796 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001797
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001798 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001799 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1800 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001801 }
1802
1803 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001804 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001805 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1806 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1807
Ville Syrjäläff32c542017-03-02 19:14:57 +02001808 if (wm > max_wm)
1809 break;
1810
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001811 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001812 raw->plane[plane_id] = wm;
1813 }
1814
1815 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001816 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001817
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001818out:
1819 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001820 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001821 plane->base.name,
1822 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1823 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1824 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1825
1826 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001827}
1828
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001829static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1830 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001831{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001832 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001833 &crtc_state->wm.vlv.raw[level];
1834 const struct vlv_fifo_state *fifo_state =
1835 &crtc_state->wm.vlv.fifo_state;
1836
1837 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1838}
1839
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001840static 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 +02001841{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001842 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1843 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1844 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1845 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001846}
1847
1848static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001849{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001850 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001851 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001852 struct intel_atomic_state *state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001853 to_intel_atomic_state(crtc_state->uapi.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001854 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001855 const struct vlv_fifo_state *fifo_state =
1856 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001857 int num_active_planes = hweight8(crtc_state->active_planes &
1858 ~BIT(PLANE_CURSOR));
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01001859 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001860 const struct intel_plane_state *old_plane_state;
1861 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001862 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001863 enum plane_id plane_id;
1864 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001865 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001866
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001867 for_each_oldnew_intel_plane_in_state(state, plane,
1868 old_plane_state,
1869 new_plane_state, i) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01001870 if (new_plane_state->hw.crtc != &crtc->base &&
1871 old_plane_state->hw.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001872 continue;
1873
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001874 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001875 dirty |= BIT(plane->id);
1876 }
1877
1878 /*
1879 * DSPARB registers may have been reset due to the
1880 * power well being turned off. Make sure we restore
1881 * them to a consistent state even if no primary/sprite
1882 * planes are initially active.
1883 */
1884 if (needs_modeset)
1885 crtc_state->fifo_changed = true;
1886
1887 if (!dirty)
1888 return 0;
1889
1890 /* cursor changes don't warrant a FIFO recompute */
1891 if (dirty & ~BIT(PLANE_CURSOR)) {
1892 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001893 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001894 const struct vlv_fifo_state *old_fifo_state =
1895 &old_crtc_state->wm.vlv.fifo_state;
1896
1897 ret = vlv_compute_fifo(crtc_state);
1898 if (ret)
1899 return ret;
1900
1901 if (needs_modeset ||
1902 memcmp(old_fifo_state, fifo_state,
1903 sizeof(*fifo_state)) != 0)
1904 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001905 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001906
Ville Syrjäläff32c542017-03-02 19:14:57 +02001907 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001908 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001909 /*
1910 * Note that enabling cxsr with no primary/sprite planes
1911 * enabled can wedge the pipe. Hence we only allow cxsr
1912 * with exactly one enabled primary/sprite plane.
1913 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001914 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001915
Ville Syrjälä5012e602017-03-02 19:14:56 +02001916 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001917 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001918 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001919
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001920 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001921 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001922
Ville Syrjäläff32c542017-03-02 19:14:57 +02001923 for_each_plane_id_on_crtc(crtc, plane_id) {
1924 wm_state->wm[level].plane[plane_id] =
1925 vlv_invert_wm_value(raw->plane[plane_id],
1926 fifo_state->plane[plane_id]);
1927 }
1928
1929 wm_state->sr[level].plane =
1930 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001931 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001932 raw->plane[PLANE_SPRITE1]),
1933 sr_fifo_size);
1934
1935 wm_state->sr[level].cursor =
1936 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1937 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001938 }
1939
Ville Syrjäläff32c542017-03-02 19:14:57 +02001940 if (level == 0)
1941 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001942
Ville Syrjäläff32c542017-03-02 19:14:57 +02001943 /* limit to only levels we can actually handle */
1944 wm_state->num_levels = level;
1945
1946 /* invalidate the higher levels */
1947 vlv_invalidate_wms(crtc, wm_state, level);
1948
1949 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001950}
1951
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001952#define VLV_FIFO(plane, value) \
1953 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1954
Ville Syrjäläff32c542017-03-02 19:14:57 +02001955static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001956 struct intel_crtc *crtc)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001957{
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001958 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001959 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02001960 const struct intel_crtc_state *crtc_state =
1961 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001962 const struct vlv_fifo_state *fifo_state =
1963 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001964 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001965
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001966 if (!crtc_state->fifo_changed)
1967 return;
1968
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001969 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1970 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1971 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001972
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001973 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1974 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001975
Ville Syrjäläc137d662017-03-02 19:15:06 +02001976 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1977
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001978 /*
1979 * uncore.lock serves a double purpose here. It allows us to
1980 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1981 * it protects the DSPARB registers from getting clobbered by
1982 * parallel updates from multiple pipes.
1983 *
1984 * intel_pipe_update_start() has already disabled interrupts
1985 * for us, so a plain spin_lock() is sufficient here.
1986 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001987 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001988
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001989 switch (crtc->pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001990 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001991 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001992 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1993 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001994
1995 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1996 VLV_FIFO(SPRITEB, 0xff));
1997 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1998 VLV_FIFO(SPRITEB, sprite1_start));
1999
2000 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2001 VLV_FIFO(SPRITEB_HI, 0x1));
2002 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2003 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2004
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002005 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2006 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002007 break;
2008 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002009 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2010 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002011
2012 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2013 VLV_FIFO(SPRITED, 0xff));
2014 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2015 VLV_FIFO(SPRITED, sprite1_start));
2016
2017 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2018 VLV_FIFO(SPRITED_HI, 0xff));
2019 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2020 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2021
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002022 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2023 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002024 break;
2025 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002026 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2027 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002028
2029 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2030 VLV_FIFO(SPRITEF, 0xff));
2031 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2032 VLV_FIFO(SPRITEF, sprite1_start));
2033
2034 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2035 VLV_FIFO(SPRITEF_HI, 0xff));
2036 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2037 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2038
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002039 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2040 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002041 break;
2042 default:
2043 break;
2044 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002045
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002046 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002047
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002048 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002049}
2050
2051#undef VLV_FIFO
2052
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002053static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002054{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002055 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002056 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2057 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2058 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002059 to_intel_atomic_state(new_crtc_state->uapi.state);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002060 const struct intel_crtc_state *old_crtc_state =
2061 intel_atomic_get_old_crtc_state(intel_state, crtc);
2062 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002063 int level;
2064
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002065 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002066 *intermediate = *optimal;
2067
2068 intermediate->cxsr = false;
2069 goto out;
2070 }
2071
Ville Syrjälä4841da52017-03-02 19:14:59 +02002072 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002073 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002074 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002075
2076 for (level = 0; level < intermediate->num_levels; level++) {
2077 enum plane_id plane_id;
2078
2079 for_each_plane_id_on_crtc(crtc, plane_id) {
2080 intermediate->wm[level].plane[plane_id] =
2081 min(optimal->wm[level].plane[plane_id],
2082 active->wm[level].plane[plane_id]);
2083 }
2084
2085 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2086 active->sr[level].plane);
2087 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2088 active->sr[level].cursor);
2089 }
2090
2091 vlv_invalidate_wms(crtc, intermediate, level);
2092
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002093out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002094 /*
2095 * If our intermediate WM are identical to the final WM, then we can
2096 * omit the post-vblank programming; only update if it's different.
2097 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002098 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002099 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002100
2101 return 0;
2102}
2103
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002104static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002105 struct vlv_wm_values *wm)
2106{
2107 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002108 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002109
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002110 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002111 wm->cxsr = true;
2112
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002113 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002114 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002115
2116 if (!crtc->active)
2117 continue;
2118
2119 if (!wm_state->cxsr)
2120 wm->cxsr = false;
2121
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002122 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002123 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2124 }
2125
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002126 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002127 wm->cxsr = false;
2128
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002129 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002130 wm->level = VLV_WM_LEVEL_PM2;
2131
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002132 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002133 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002134 enum pipe pipe = crtc->pipe;
2135
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002136 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002137 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002138 wm->sr = wm_state->sr[wm->level];
2139
Ville Syrjälä1b313892016-11-28 19:37:08 +02002140 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2141 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2142 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2143 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002144 }
2145}
2146
Ville Syrjäläff32c542017-03-02 19:14:57 +02002147static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002148{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002149 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2150 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002151
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002152 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002153
Ville Syrjäläff32c542017-03-02 19:14:57 +02002154 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002155 return;
2156
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002157 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002158 chv_set_memory_dvfs(dev_priv, false);
2159
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002160 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002161 chv_set_memory_pm5(dev_priv, false);
2162
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002163 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002164 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002165
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002166 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002167
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002168 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002169 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002170
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002171 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002172 chv_set_memory_pm5(dev_priv, true);
2173
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002174 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002175 chv_set_memory_dvfs(dev_priv, true);
2176
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002177 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002178}
2179
Ville Syrjäläff32c542017-03-02 19:14:57 +02002180static void vlv_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002181 struct intel_crtc *crtc)
Ville Syrjäläff32c542017-03-02 19:14:57 +02002182{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002183 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2184 const struct intel_crtc_state *crtc_state =
2185 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläff32c542017-03-02 19:14:57 +02002186
2187 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002188 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2189 vlv_program_watermarks(dev_priv);
2190 mutex_unlock(&dev_priv->wm.wm_mutex);
2191}
2192
2193static void vlv_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002194 struct intel_crtc *crtc)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002195{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02002196 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2197 const struct intel_crtc_state *crtc_state =
2198 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002199
2200 if (!crtc_state->wm.need_postvbl_update)
2201 return;
2202
2203 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002204 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002205 vlv_program_watermarks(dev_priv);
2206 mutex_unlock(&dev_priv->wm.wm_mutex);
2207}
2208
Ville Syrjälä432081b2016-10-31 22:37:03 +02002209static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002210{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002211 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002212 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002213 int srwm = 1;
2214 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002215 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002216
2217 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002218 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002219 if (crtc) {
2220 /* self-refresh has much higher latency */
2221 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002222 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002223 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002224 const struct drm_framebuffer *fb =
2225 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002226 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002227 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002228 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002229 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002230 int entries;
2231
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002232 entries = intel_wm_method2(clock, htotal,
2233 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002234 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2235 srwm = I965_FIFO_SIZE - entries;
2236 if (srwm < 0)
2237 srwm = 1;
2238 srwm &= 0x1ff;
2239 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2240 entries, srwm);
2241
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002242 entries = intel_wm_method2(clock, htotal,
2243 crtc->base.cursor->state->crtc_w, 4,
2244 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002245 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002246 i965_cursor_wm_info.cacheline_size) +
2247 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002248
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002249 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002250 if (cursor_sr > i965_cursor_wm_info.max_wm)
2251 cursor_sr = i965_cursor_wm_info.max_wm;
2252
2253 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2254 "cursor %d\n", srwm, cursor_sr);
2255
Imre Deak98584252014-06-13 14:54:20 +03002256 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002257 } else {
Imre Deak98584252014-06-13 14:54:20 +03002258 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002259 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002260 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002261 }
2262
2263 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2264 srwm);
2265
2266 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002267 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2268 FW_WM(8, CURSORB) |
2269 FW_WM(8, PLANEB) |
2270 FW_WM(8, PLANEA));
2271 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2272 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002273 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002274 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002275
2276 if (cxsr_enabled)
2277 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002278}
2279
Ville Syrjäläf4998962015-03-10 17:02:21 +02002280#undef FW_WM
2281
Ville Syrjälä432081b2016-10-31 22:37:03 +02002282static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002283{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002284 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002285 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002286 u32 fwater_lo;
2287 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002288 int cwm, srwm = 1;
2289 int fifo_size;
2290 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002291 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002292
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002293 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002294 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002295 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002296 wm_info = &i915_wm_info;
2297 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002298 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002299
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002300 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2301 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002302 if (intel_crtc_active(crtc)) {
2303 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002304 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002305 const struct drm_framebuffer *fb =
2306 crtc->base.primary->state->fb;
2307 int cpp;
2308
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002309 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002310 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002311 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002312 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002313
Damien Lespiau241bfc32013-09-25 16:45:37 +01002314 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002315 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002316 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002317 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002318 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002319 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002320 if (planea_wm > (long)wm_info->max_wm)
2321 planea_wm = wm_info->max_wm;
2322 }
2323
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002324 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002325 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002326
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002327 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2328 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002329 if (intel_crtc_active(crtc)) {
2330 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002331 &crtc->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002332 const struct drm_framebuffer *fb =
2333 crtc->base.primary->state->fb;
2334 int cpp;
2335
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002336 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002337 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002338 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002339 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002340
Damien Lespiau241bfc32013-09-25 16:45:37 +01002341 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002342 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002343 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344 if (enabled == NULL)
2345 enabled = crtc;
2346 else
2347 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002348 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002349 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002350 if (planeb_wm > (long)wm_info->max_wm)
2351 planeb_wm = wm_info->max_wm;
2352 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002353
2354 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2355
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002356 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002357 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002358
Ville Syrjäläefc26112016-10-31 22:37:04 +02002359 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002360
2361 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002362 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002363 enabled = NULL;
2364 }
2365
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002366 /*
2367 * Overlay gets an aggressive default since video jitter is bad.
2368 */
2369 cwm = 2;
2370
2371 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002372 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002373
2374 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002375 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002376 /* self-refresh has much higher latency */
2377 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002378 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002379 &enabled->config->hw.adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002380 const struct drm_framebuffer *fb =
2381 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002382 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002383 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002384 int hdisplay = enabled->config->pipe_src_w;
2385 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002386 int entries;
2387
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002388 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002389 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002390 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002391 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002392
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002393 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2394 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002395 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2396 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2397 srwm = wm_info->fifo_size - entries;
2398 if (srwm < 0)
2399 srwm = 1;
2400
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002401 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002402 I915_WRITE(FW_BLC_SELF,
2403 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002404 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002405 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2406 }
2407
2408 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2409 planea_wm, planeb_wm, cwm, srwm);
2410
2411 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2412 fwater_hi = (cwm & 0x1f);
2413
2414 /* Set request length to 8 cachelines per fetch */
2415 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2416 fwater_hi = fwater_hi | (1 << 8);
2417
2418 I915_WRITE(FW_BLC, fwater_lo);
2419 I915_WRITE(FW_BLC2, fwater_hi);
2420
Imre Deak5209b1f2014-07-01 12:36:17 +03002421 if (enabled)
2422 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002423}
2424
Ville Syrjälä432081b2016-10-31 22:37:03 +02002425static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002426{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002427 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002428 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002429 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002430 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002431 int planea_wm;
2432
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002433 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002434 if (crtc == NULL)
2435 return;
2436
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002437 adjusted_mode = &crtc->config->hw.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002438 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002439 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002440 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002441 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002442 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2443 fwater_lo |= (3<<8) | planea_wm;
2444
2445 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2446
2447 I915_WRITE(FW_BLC, fwater_lo);
2448}
2449
Ville Syrjälä37126462013-08-01 16:18:55 +03002450/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002451static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2452 unsigned int cpp,
2453 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002454{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002455 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002456
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002457 ret = intel_wm_method1(pixel_rate, cpp, latency);
2458 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002459
2460 return ret;
2461}
2462
Ville Syrjälä37126462013-08-01 16:18:55 +03002463/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002464static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2465 unsigned int htotal,
2466 unsigned int width,
2467 unsigned int cpp,
2468 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002469{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002470 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002471
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002472 ret = intel_wm_method2(pixel_rate, htotal,
2473 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002474 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002475
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476 return ret;
2477}
2478
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002479static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002480{
Matt Roper15126882015-12-03 11:37:40 -08002481 /*
2482 * Neither of these should be possible since this function shouldn't be
2483 * called if the CRTC is off or the plane is invisible. But let's be
2484 * extra paranoid to avoid a potential divide-by-zero if we screw up
2485 * elsewhere in the driver.
2486 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002487 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002488 return 0;
2489 if (WARN_ON(!horiz_pixels))
2490 return 0;
2491
Ville Syrjäläac484962016-01-20 21:05:26 +02002492 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002493}
2494
Imre Deak820c1982013-12-17 14:46:36 +02002495struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002496 u16 pri;
2497 u16 spr;
2498 u16 cur;
2499 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002500};
2501
Ville Syrjälä37126462013-08-01 16:18:55 +03002502/*
2503 * For both WM_PIPE and WM_LP.
2504 * mem_value must be in 0.1us units.
2505 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002506static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2507 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002508 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002509{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002510 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002511 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002512
Ville Syrjälä03981c62018-11-14 19:34:40 +02002513 if (mem_value == 0)
2514 return U32_MAX;
2515
Maarten Lankhorstec193642019-06-28 10:55:17 +02002516 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002517 return 0;
2518
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002519 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002520
Maarten Lankhorstec193642019-06-28 10:55:17 +02002521 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002522
2523 if (!is_lp)
2524 return method1;
2525
Maarten Lankhorstec193642019-06-28 10:55:17 +02002526 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002527 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002528 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002529 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002530
2531 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002532}
2533
Ville Syrjälä37126462013-08-01 16:18:55 +03002534/*
2535 * For both WM_PIPE and WM_LP.
2536 * mem_value must be in 0.1us units.
2537 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002538static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2539 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002540 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002541{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002542 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002543 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002544
Ville Syrjälä03981c62018-11-14 19:34:40 +02002545 if (mem_value == 0)
2546 return U32_MAX;
2547
Maarten Lankhorstec193642019-06-28 10:55:17 +02002548 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002549 return 0;
2550
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002551 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002552
Maarten Lankhorstec193642019-06-28 10:55:17 +02002553 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2554 method2 = ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002555 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002556 drm_rect_width(&plane_state->uapi.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002557 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002558 return min(method1, method2);
2559}
2560
Ville Syrjälä37126462013-08-01 16:18:55 +03002561/*
2562 * For both WM_PIPE and WM_LP.
2563 * mem_value must be in 0.1us units.
2564 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002565static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2566 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002567 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002568{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002569 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002570
Ville Syrjälä03981c62018-11-14 19:34:40 +02002571 if (mem_value == 0)
2572 return U32_MAX;
2573
Maarten Lankhorstec193642019-06-28 10:55:17 +02002574 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002575 return 0;
2576
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002577 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002578
Maarten Lankhorstec193642019-06-28 10:55:17 +02002579 return ilk_wm_method2(crtc_state->pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002580 crtc_state->hw.adjusted_mode.crtc_htotal,
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002581 drm_rect_width(&plane_state->uapi.dst),
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002582 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002583}
2584
Paulo Zanonicca32e92013-05-31 11:45:06 -03002585/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002586static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2587 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002588 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002589{
Ville Syrjälä83054942016-11-18 21:53:00 +02002590 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002591
Maarten Lankhorstec193642019-06-28 10:55:17 +02002592 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002593 return 0;
2594
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01002595 cpp = plane_state->hw.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002596
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01002597 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2598 cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002599}
2600
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002601static unsigned int
2602ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002603{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002604 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002605 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002606 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002607 return 768;
2608 else
2609 return 512;
2610}
2611
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002612static unsigned int
2613ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2614 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002615{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002616 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002617 /* BDW primary/sprite plane watermarks */
2618 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002619 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002620 /* IVB/HSW primary/sprite plane watermarks */
2621 return level == 0 ? 127 : 1023;
2622 else if (!is_sprite)
2623 /* ILK/SNB primary plane watermarks */
2624 return level == 0 ? 127 : 511;
2625 else
2626 /* ILK/SNB sprite plane watermarks */
2627 return level == 0 ? 63 : 255;
2628}
2629
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002630static unsigned int
2631ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002632{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002633 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002634 return level == 0 ? 63 : 255;
2635 else
2636 return level == 0 ? 31 : 63;
2637}
2638
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002639static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002640{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002641 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002642 return 31;
2643 else
2644 return 15;
2645}
2646
Ville Syrjälä158ae642013-08-07 13:28:19 +03002647/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002648static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002649 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002650 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002651 enum intel_ddb_partitioning ddb_partitioning,
2652 bool is_sprite)
2653{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002654 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002655
2656 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002657 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002658 return 0;
2659
2660 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002661 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002662 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002663
2664 /*
2665 * For some reason the non self refresh
2666 * FIFO size is only half of the self
2667 * refresh FIFO size on ILK/SNB.
2668 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002669 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002670 fifo_size /= 2;
2671 }
2672
Ville Syrjälä240264f2013-08-07 13:29:12 +03002673 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002674 /* level 0 is always calculated with 1:1 split */
2675 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2676 if (is_sprite)
2677 fifo_size *= 5;
2678 fifo_size /= 6;
2679 } else {
2680 fifo_size /= 2;
2681 }
2682 }
2683
2684 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002685 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002686}
2687
2688/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002689static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002690 int level,
2691 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002692{
2693 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002694 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002695 return 64;
2696
2697 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002698 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002699}
2700
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002701static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002702 int level,
2703 const struct intel_wm_config *config,
2704 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002705 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002706{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002707 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2708 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2709 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2710 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002711}
2712
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002713static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002714 int level,
2715 struct ilk_wm_maximums *max)
2716{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002717 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2718 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2719 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2720 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002721}
2722
Ville Syrjäläd9395652013-10-09 19:18:10 +03002723static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002724 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002725 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002726{
2727 bool ret;
2728
2729 /* already determined to be invalid? */
2730 if (!result->enable)
2731 return false;
2732
2733 result->enable = result->pri_val <= max->pri &&
2734 result->spr_val <= max->spr &&
2735 result->cur_val <= max->cur;
2736
2737 ret = result->enable;
2738
2739 /*
2740 * HACK until we can pre-compute everything,
2741 * and thus fail gracefully if LP0 watermarks
2742 * are exceeded...
2743 */
2744 if (level == 0 && !result->enable) {
2745 if (result->pri_val > max->pri)
2746 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2747 level, result->pri_val, max->pri);
2748 if (result->spr_val > max->spr)
2749 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2750 level, result->spr_val, max->spr);
2751 if (result->cur_val > max->cur)
2752 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2753 level, result->cur_val, max->cur);
2754
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002755 result->pri_val = min_t(u32, result->pri_val, max->pri);
2756 result->spr_val = min_t(u32, result->spr_val, max->spr);
2757 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002758 result->enable = true;
2759 }
2760
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002761 return ret;
2762}
2763
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002764static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002765 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002766 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002767 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002768 const struct intel_plane_state *pristate,
2769 const struct intel_plane_state *sprstate,
2770 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002771 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002772{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002773 u16 pri_latency = dev_priv->wm.pri_latency[level];
2774 u16 spr_latency = dev_priv->wm.spr_latency[level];
2775 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002776
2777 /* WM1+ latency values stored in 0.5us units */
2778 if (level > 0) {
2779 pri_latency *= 5;
2780 spr_latency *= 5;
2781 cur_latency *= 5;
2782 }
2783
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002784 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002785 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002786 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002787 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002788 }
2789
2790 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002791 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002792
2793 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002794 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002795
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002796 result->enable = true;
2797}
2798
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002799static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02002800hsw_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002801{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002802 const struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01002803 to_intel_atomic_state(crtc_state->uapi.state);
Matt Roperee91a152015-12-03 11:37:39 -08002804 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002805 &crtc_state->hw.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002806 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002807
Maarten Lankhorst1326a922019-10-31 12:26:02 +01002808 if (!crtc_state->hw.active)
Matt Roperee91a152015-12-03 11:37:39 -08002809 return 0;
2810 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2811 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002812 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002813 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002814
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002815 /* The WM are computed with base on how long it takes to fill a single
2816 * row at the given clock rate, multiplied by 8.
2817 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002818 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2819 adjusted_mode->crtc_clock);
2820 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002821 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002822
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002823 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2824 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002825}
2826
Ville Syrjäläbb726512016-10-31 22:37:24 +02002827static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002828 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002829{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002830 struct intel_uncore *uncore = &dev_priv->uncore;
2831
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002832 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002833 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002834 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002835 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002836
2837 /* read the first set of memory latencies[0:3] */
2838 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002839 ret = sandybridge_pcode_read(dev_priv,
2840 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002841 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002842
2843 if (ret) {
2844 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2845 return;
2846 }
2847
2848 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2849 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2850 GEN9_MEM_LATENCY_LEVEL_MASK;
2851 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2852 GEN9_MEM_LATENCY_LEVEL_MASK;
2853 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2854 GEN9_MEM_LATENCY_LEVEL_MASK;
2855
2856 /* read the second set of memory latencies[4:7] */
2857 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002858 ret = sandybridge_pcode_read(dev_priv,
2859 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002860 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002861 if (ret) {
2862 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2863 return;
2864 }
2865
2866 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2867 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2868 GEN9_MEM_LATENCY_LEVEL_MASK;
2869 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2870 GEN9_MEM_LATENCY_LEVEL_MASK;
2871 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2872 GEN9_MEM_LATENCY_LEVEL_MASK;
2873
Vandana Kannan367294b2014-11-04 17:06:46 +00002874 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002875 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2876 * need to be disabled. We make sure to sanitize the values out
2877 * of the punit to satisfy this requirement.
2878 */
2879 for (level = 1; level <= max_level; level++) {
2880 if (wm[level] == 0) {
2881 for (i = level + 1; i <= max_level; i++)
2882 wm[i] = 0;
2883 break;
2884 }
2885 }
2886
2887 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002888 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002889 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002890 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002891 * to add 2us to the various latency levels we retrieve from the
2892 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002893 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002894 if (wm[0] == 0) {
2895 wm[0] += 2;
2896 for (level = 1; level <= max_level; level++) {
2897 if (wm[level] == 0)
2898 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002899 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002900 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002901 }
2902
Mahesh Kumar86b59282018-08-31 16:39:42 +05302903 /*
2904 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2905 * If we could not get dimm info enable this WA to prevent from
2906 * any underrun. If not able to get Dimm info assume 16GB dimm
2907 * to avoid any underrun.
2908 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002909 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302910 wm[0] += 1;
2911
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002912 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002913 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002914
2915 wm[0] = (sskpd >> 56) & 0xFF;
2916 if (wm[0] == 0)
2917 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002918 wm[1] = (sskpd >> 4) & 0xFF;
2919 wm[2] = (sskpd >> 12) & 0xFF;
2920 wm[3] = (sskpd >> 20) & 0x1FF;
2921 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002922 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002923 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002924
2925 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2926 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2927 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2928 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002929 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002930 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002931
2932 /* ILK primary LP0 latency is 700 ns */
2933 wm[0] = 7;
2934 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2935 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002936 } else {
2937 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002938 }
2939}
2940
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002941static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002942 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002943{
2944 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002945 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002946 wm[0] = 13;
2947}
2948
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002949static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002950 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002951{
2952 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002953 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002954 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002955}
2956
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002957int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002958{
2959 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002960 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002961 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002962 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002963 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002964 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002965 return 3;
2966 else
2967 return 2;
2968}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002969
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002970static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002971 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002972 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002973{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002974 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002975
2976 for (level = 0; level <= max_level; level++) {
2977 unsigned int latency = wm[level];
2978
2979 if (latency == 0) {
Chris Wilson86c1c872018-07-26 17:15:27 +01002980 DRM_DEBUG_KMS("%s WM%d latency not provided\n",
2981 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002982 continue;
2983 }
2984
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002985 /*
2986 * - latencies are in us on gen9.
2987 * - before then, WM1+ latency values are in 0.5us units
2988 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002989 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002990 latency *= 10;
2991 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002992 latency *= 5;
2993
2994 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2995 name, level, wm[level],
2996 latency / 10, latency % 10);
2997 }
2998}
2999
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003000static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003001 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003002{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003003 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003004
3005 if (wm[0] >= min)
3006 return false;
3007
3008 wm[0] = max(wm[0], min);
3009 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003010 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003011
3012 return true;
3013}
3014
Ville Syrjäläbb726512016-10-31 22:37:24 +02003015static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003016{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003017 bool changed;
3018
3019 /*
3020 * The BIOS provided WM memory latency values are often
3021 * inadequate for high resolution displays. Adjust them.
3022 */
3023 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3024 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3025 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3026
3027 if (!changed)
3028 return;
3029
3030 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003031 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3032 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3033 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003034}
3035
Ville Syrjälä03981c62018-11-14 19:34:40 +02003036static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3037{
3038 /*
3039 * On some SNB machines (Thinkpad X220 Tablet at least)
3040 * LP3 usage can cause vblank interrupts to be lost.
3041 * The DEIIR bit will go high but it looks like the CPU
3042 * never gets interrupted.
3043 *
3044 * It's not clear whether other interrupt source could
3045 * be affected or if this is somehow limited to vblank
3046 * interrupts only. To play it safe we disable LP3
3047 * watermarks entirely.
3048 */
3049 if (dev_priv->wm.pri_latency[3] == 0 &&
3050 dev_priv->wm.spr_latency[3] == 0 &&
3051 dev_priv->wm.cur_latency[3] == 0)
3052 return;
3053
3054 dev_priv->wm.pri_latency[3] = 0;
3055 dev_priv->wm.spr_latency[3] = 0;
3056 dev_priv->wm.cur_latency[3] = 0;
3057
3058 DRM_DEBUG_KMS("LP3 watermarks disabled due to potential for lost interrupts\n");
3059 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3060 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3061 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3062}
3063
Ville Syrjäläbb726512016-10-31 22:37:24 +02003064static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003065{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003066 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003067
3068 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3069 sizeof(dev_priv->wm.pri_latency));
3070 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3071 sizeof(dev_priv->wm.pri_latency));
3072
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003073 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003074 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003075
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003076 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3077 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3078 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003079
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003080 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003081 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003082 snb_wm_lp3_irq_quirk(dev_priv);
3083 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003084}
3085
Ville Syrjäläbb726512016-10-31 22:37:24 +02003086static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003087{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003088 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003089 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003090}
3091
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003092static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003093 struct intel_pipe_wm *pipe_wm)
3094{
3095 /* LP0 watermark maximums depend on this pipe alone */
3096 const struct intel_wm_config config = {
3097 .num_pipes_active = 1,
3098 .sprites_enabled = pipe_wm->sprites_enabled,
3099 .sprites_scaled = pipe_wm->sprites_scaled,
3100 };
3101 struct ilk_wm_maximums max;
3102
3103 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003104 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003105
3106 /* At least LP0 must be valid */
3107 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3108 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3109 return false;
3110 }
3111
3112 return true;
3113}
3114
Matt Roper261a27d2015-10-08 15:28:25 -07003115/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003116static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003117{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003118 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003120 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003121 struct intel_plane *plane;
3122 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003123 const struct intel_plane_state *pristate = NULL;
3124 const struct intel_plane_state *sprstate = NULL;
3125 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003126 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003127 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003128
Maarten Lankhorstec193642019-06-28 10:55:17 +02003129 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003130
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003131 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3132 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3133 pristate = plane_state;
3134 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3135 sprstate = plane_state;
3136 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3137 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003138 }
3139
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003140 pipe_wm->pipe_enabled = crtc_state->hw.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003141 if (sprstate) {
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01003142 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3143 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3144 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3145 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003146 }
3147
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003148 usable_level = max_level;
3149
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003150 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003151 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003152 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003153
3154 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003155 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003156 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003157
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003158 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorstec193642019-06-28 10:55:17 +02003159 ilk_compute_wm_level(dev_priv, intel_crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003160 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003161
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003162 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Maarten Lankhorstec193642019-06-28 10:55:17 +02003163 pipe_wm->linetime = hsw_compute_linetime_wm(crtc_state);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003164
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003165 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003166 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003167
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003168 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003169
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003170 for (level = 1; level <= usable_level; level++) {
3171 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003172
Maarten Lankhorstec193642019-06-28 10:55:17 +02003173 ilk_compute_wm_level(dev_priv, intel_crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003174 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003175
3176 /*
3177 * Disable any watermark level that exceeds the
3178 * register maximums since such watermarks are
3179 * always invalid.
3180 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003181 if (!ilk_validate_wm_level(level, &max, wm)) {
3182 memset(wm, 0, sizeof(*wm));
3183 break;
3184 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003185 }
3186
Matt Roper86c8bbb2015-09-24 15:53:16 -07003187 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003188}
3189
3190/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003191 * Build a set of 'intermediate' watermark values that satisfy both the old
3192 * state and the new state. These can be programmed to the hardware
3193 * immediately.
3194 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003195static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003196{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003197 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->uapi.crtc);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003198 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003199 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003200 struct intel_atomic_state *intel_state =
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003201 to_intel_atomic_state(newstate->uapi.state);
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003202 const struct intel_crtc_state *oldstate =
3203 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3204 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003205 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003206
3207 /*
3208 * Start with the final, target watermarks, then combine with the
3209 * currently active watermarks to get values that are safe both before
3210 * and after the vblank.
3211 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003212 *a = newstate->wm.ilk.optimal;
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003213 if (!newstate->hw.active || drm_atomic_crtc_needs_modeset(&newstate->uapi) ||
Ville Syrjäläf255c622018-11-08 17:10:13 +02003214 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003215 return 0;
3216
Matt Ropered4a6a72016-02-23 17:20:13 -08003217 a->pipe_enabled |= b->pipe_enabled;
3218 a->sprites_enabled |= b->sprites_enabled;
3219 a->sprites_scaled |= b->sprites_scaled;
3220
3221 for (level = 0; level <= max_level; level++) {
3222 struct intel_wm_level *a_wm = &a->wm[level];
3223 const struct intel_wm_level *b_wm = &b->wm[level];
3224
3225 a_wm->enable &= b_wm->enable;
3226 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3227 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3228 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3229 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3230 }
3231
3232 /*
3233 * We need to make sure that these merged watermark values are
3234 * actually a valid configuration themselves. If they're not,
3235 * there's no safe way to transition from the old state to
3236 * the new state, so we need to fail the atomic transaction.
3237 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003238 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003239 return -EINVAL;
3240
3241 /*
3242 * If our intermediate WM are identical to the final WM, then we can
3243 * omit the post-vblank programming; only update if it's different.
3244 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003245 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3246 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003247
3248 return 0;
3249}
3250
3251/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003252 * Merge the watermarks from all active pipes for a specific level.
3253 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003254static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003255 int level,
3256 struct intel_wm_level *ret_wm)
3257{
3258 const struct intel_crtc *intel_crtc;
3259
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003260 ret_wm->enable = true;
3261
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003262 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003263 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003264 const struct intel_wm_level *wm = &active->wm[level];
3265
3266 if (!active->pipe_enabled)
3267 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003268
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003269 /*
3270 * The watermark values may have been used in the past,
3271 * so we must maintain them in the registers for some
3272 * time even if the level is now disabled.
3273 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003274 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003275 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003276
3277 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3278 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3279 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3280 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3281 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003282}
3283
3284/*
3285 * Merge all low power watermarks for all active pipes.
3286 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003287static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003288 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003289 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003290 struct intel_pipe_wm *merged)
3291{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003292 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003293 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003294
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003295 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003296 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003297 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003298 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003299
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003300 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003301 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003302
3303 /* merge each WM1+ level */
3304 for (level = 1; level <= max_level; level++) {
3305 struct intel_wm_level *wm = &merged->wm[level];
3306
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003307 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003308
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003309 if (level > last_enabled_level)
3310 wm->enable = false;
3311 else if (!ilk_validate_wm_level(level, max, wm))
3312 /* make sure all following levels get disabled */
3313 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003314
3315 /*
3316 * The spec says it is preferred to disable
3317 * FBC WMs instead of disabling a WM level.
3318 */
3319 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003320 if (wm->enable)
3321 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003322 wm->fbc_val = 0;
3323 }
3324 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003325
3326 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3327 /*
3328 * FIXME this is racy. FBC might get enabled later.
3329 * What we should check here is whether FBC can be
3330 * enabled sometime later.
3331 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003332 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003333 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003334 for (level = 2; level <= max_level; level++) {
3335 struct intel_wm_level *wm = &merged->wm[level];
3336
3337 wm->enable = false;
3338 }
3339 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003340}
3341
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003342static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3343{
3344 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3345 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3346}
3347
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003348/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003349static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3350 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003351{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003352 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003353 return 2 * level;
3354 else
3355 return dev_priv->wm.pri_latency[level];
3356}
3357
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003358static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003359 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003360 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003361 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003362{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003363 struct intel_crtc *intel_crtc;
3364 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003365
Ville Syrjälä0362c782013-10-09 19:17:57 +03003366 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003367 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003368
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003369 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003370 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003371 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003372
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003373 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003374
Ville Syrjälä0362c782013-10-09 19:17:57 +03003375 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003376
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003377 /*
3378 * Maintain the watermark values even if the level is
3379 * disabled. Doing otherwise could cause underruns.
3380 */
3381 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003382 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003383 (r->pri_val << WM1_LP_SR_SHIFT) |
3384 r->cur_val;
3385
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003386 if (r->enable)
3387 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3388
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003389 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003390 results->wm_lp[wm_lp - 1] |=
3391 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3392 else
3393 results->wm_lp[wm_lp - 1] |=
3394 r->fbc_val << WM1_LP_FBC_SHIFT;
3395
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003396 /*
3397 * Always set WM1S_LP_EN when spr_val != 0, even if the
3398 * level is disabled. Doing otherwise could cause underruns.
3399 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003400 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003401 WARN_ON(wm_lp != 1);
3402 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3403 } else
3404 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003405 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003406
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003407 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003408 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003409 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003410 const struct intel_wm_level *r =
3411 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003412
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003413 if (WARN_ON(!r->enable))
3414 continue;
3415
Matt Ropered4a6a72016-02-23 17:20:13 -08003416 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003417
3418 results->wm_pipe[pipe] =
3419 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3420 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3421 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003422 }
3423}
3424
Paulo Zanoni861f3382013-05-31 10:19:21 -03003425/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3426 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003427static struct intel_pipe_wm *
3428ilk_find_best_result(struct drm_i915_private *dev_priv,
3429 struct intel_pipe_wm *r1,
3430 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003431{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003432 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003433 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003434
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003435 for (level = 1; level <= max_level; level++) {
3436 if (r1->wm[level].enable)
3437 level1 = level;
3438 if (r2->wm[level].enable)
3439 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003440 }
3441
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003442 if (level1 == level2) {
3443 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003444 return r2;
3445 else
3446 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003447 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003448 return r1;
3449 } else {
3450 return r2;
3451 }
3452}
3453
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003454/* dirty bits used to track which watermarks need changes */
3455#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3456#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3457#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3458#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3459#define WM_DIRTY_FBC (1 << 24)
3460#define WM_DIRTY_DDB (1 << 25)
3461
Damien Lespiau055e3932014-08-18 13:49:10 +01003462static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003463 const struct ilk_wm_values *old,
3464 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003465{
3466 unsigned int dirty = 0;
3467 enum pipe pipe;
3468 int wm_lp;
3469
Damien Lespiau055e3932014-08-18 13:49:10 +01003470 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003471 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3472 dirty |= WM_DIRTY_LINETIME(pipe);
3473 /* Must disable LP1+ watermarks too */
3474 dirty |= WM_DIRTY_LP_ALL;
3475 }
3476
3477 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3478 dirty |= WM_DIRTY_PIPE(pipe);
3479 /* Must disable LP1+ watermarks too */
3480 dirty |= WM_DIRTY_LP_ALL;
3481 }
3482 }
3483
3484 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3485 dirty |= WM_DIRTY_FBC;
3486 /* Must disable LP1+ watermarks too */
3487 dirty |= WM_DIRTY_LP_ALL;
3488 }
3489
3490 if (old->partitioning != new->partitioning) {
3491 dirty |= WM_DIRTY_DDB;
3492 /* Must disable LP1+ watermarks too */
3493 dirty |= WM_DIRTY_LP_ALL;
3494 }
3495
3496 /* LP1+ watermarks already deemed dirty, no need to continue */
3497 if (dirty & WM_DIRTY_LP_ALL)
3498 return dirty;
3499
3500 /* Find the lowest numbered LP1+ watermark in need of an update... */
3501 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3502 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3503 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3504 break;
3505 }
3506
3507 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3508 for (; wm_lp <= 3; wm_lp++)
3509 dirty |= WM_DIRTY_LP(wm_lp);
3510
3511 return dirty;
3512}
3513
Ville Syrjälä8553c182013-12-05 15:51:39 +02003514static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3515 unsigned int dirty)
3516{
Imre Deak820c1982013-12-17 14:46:36 +02003517 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003518 bool changed = false;
3519
3520 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3521 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3522 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3523 changed = true;
3524 }
3525 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3526 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3527 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3528 changed = true;
3529 }
3530 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3531 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3532 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3533 changed = true;
3534 }
3535
3536 /*
3537 * Don't touch WM1S_LP_EN here.
3538 * Doing so could cause underruns.
3539 */
3540
3541 return changed;
3542}
3543
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003544/*
3545 * The spec says we shouldn't write when we don't need, because every write
3546 * causes WMs to be re-evaluated, expending some power.
3547 */
Imre Deak820c1982013-12-17 14:46:36 +02003548static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3549 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003550{
Imre Deak820c1982013-12-17 14:46:36 +02003551 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003552 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003553 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003554
Damien Lespiau055e3932014-08-18 13:49:10 +01003555 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003556 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003557 return;
3558
Ville Syrjälä8553c182013-12-05 15:51:39 +02003559 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003560
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003561 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003562 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003563 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003564 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003565 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003566 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3567
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003568 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003569 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003570 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003571 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003572 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003573 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3574
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003575 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003576 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003577 val = I915_READ(WM_MISC);
3578 if (results->partitioning == INTEL_DDB_PART_1_2)
3579 val &= ~WM_MISC_DATA_PARTITION_5_6;
3580 else
3581 val |= WM_MISC_DATA_PARTITION_5_6;
3582 I915_WRITE(WM_MISC, val);
3583 } else {
3584 val = I915_READ(DISP_ARB_CTL2);
3585 if (results->partitioning == INTEL_DDB_PART_1_2)
3586 val &= ~DISP_DATA_PARTITION_5_6;
3587 else
3588 val |= DISP_DATA_PARTITION_5_6;
3589 I915_WRITE(DISP_ARB_CTL2, val);
3590 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003591 }
3592
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003593 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003594 val = I915_READ(DISP_ARB_CTL);
3595 if (results->enable_fbc_wm)
3596 val &= ~DISP_FBC_WM_DIS;
3597 else
3598 val |= DISP_FBC_WM_DIS;
3599 I915_WRITE(DISP_ARB_CTL, val);
3600 }
3601
Imre Deak954911e2013-12-17 14:46:34 +02003602 if (dirty & WM_DIRTY_LP(1) &&
3603 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3604 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3605
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003606 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003607 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3608 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3609 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3610 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3611 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003612
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003613 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003614 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003615 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003616 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003617 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003618 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003619
3620 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003621}
3622
Ville Syrjälä60aca572019-11-27 21:05:51 +02003623bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003624{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003625 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3626}
3627
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303628static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
3629{
3630 u8 enabled_slices;
3631
3632 /* Slice 1 will always be enabled */
3633 enabled_slices = 1;
3634
3635 /* Gen prior to GEN11 have only one DBuf slice */
3636 if (INTEL_GEN(dev_priv) < 11)
3637 return enabled_slices;
3638
Imre Deak209d7352019-03-07 12:32:35 +02003639 /*
3640 * FIXME: for now we'll only ever use 1 slice; pretend that we have
3641 * only that 1 slice enabled until we have a proper way for on-demand
3642 * toggling of the second slice.
3643 */
3644 if (0 && I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303645 enabled_slices++;
3646
3647 return enabled_slices;
3648}
3649
Matt Roper024c9042015-09-24 15:53:11 -07003650/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003651 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3652 * so assume we'll always need it in order to avoid underruns.
3653 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003654static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003655{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003656 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003657}
3658
Paulo Zanoni56feca92016-09-22 18:00:28 -03003659static bool
3660intel_has_sagv(struct drm_i915_private *dev_priv)
3661{
Lucas De Marchi8ffa4392019-09-04 14:34:18 -07003662 /* HACK! */
3663 if (IS_GEN(dev_priv, 12))
3664 return false;
3665
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003666 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3667 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003668}
3669
James Ausmusb068a862019-10-09 10:23:14 -07003670static void
3671skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3672{
James Ausmusda80f042019-10-09 10:23:15 -07003673 if (INTEL_GEN(dev_priv) >= 12) {
3674 u32 val = 0;
3675 int ret;
3676
3677 ret = sandybridge_pcode_read(dev_priv,
3678 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3679 &val, NULL);
3680 if (!ret) {
3681 dev_priv->sagv_block_time_us = val;
3682 return;
3683 }
3684
3685 DRM_DEBUG_DRIVER("Couldn't read SAGV block time!\n");
3686 } else if (IS_GEN(dev_priv, 11)) {
James Ausmusb068a862019-10-09 10:23:14 -07003687 dev_priv->sagv_block_time_us = 10;
3688 return;
3689 } else if (IS_GEN(dev_priv, 10)) {
3690 dev_priv->sagv_block_time_us = 20;
3691 return;
3692 } else if (IS_GEN(dev_priv, 9)) {
3693 dev_priv->sagv_block_time_us = 30;
3694 return;
3695 } else {
3696 MISSING_CASE(INTEL_GEN(dev_priv));
3697 }
3698
3699 /* Default to an unusable block time */
3700 dev_priv->sagv_block_time_us = -1;
3701}
3702
Lyude656d1b82016-08-17 15:55:54 -04003703/*
3704 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3705 * depending on power and performance requirements. The display engine access
3706 * to system memory is blocked during the adjustment time. Because of the
3707 * blocking time, having this enabled can cause full system hangs and/or pipe
3708 * underruns if we don't meet all of the following requirements:
3709 *
3710 * - <= 1 pipe enabled
3711 * - All planes can enable watermarks for latencies >= SAGV engine block time
3712 * - We're not using an interlaced display configuration
3713 */
3714int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003715intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003716{
3717 int ret;
3718
Paulo Zanoni56feca92016-09-22 18:00:28 -03003719 if (!intel_has_sagv(dev_priv))
3720 return 0;
3721
3722 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003723 return 0;
3724
Ville Syrjäläff61a972018-12-21 19:14:34 +02003725 DRM_DEBUG_KMS("Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003726 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3727 GEN9_SAGV_ENABLE);
3728
Ville Syrjäläff61a972018-12-21 19:14:34 +02003729 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003730
3731 /*
3732 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003733 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003734 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003735 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003736 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003737 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003738 return 0;
3739 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003740 DRM_ERROR("Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003741 return ret;
3742 }
3743
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003744 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003745 return 0;
3746}
3747
Lyude656d1b82016-08-17 15:55:54 -04003748int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003749intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003750{
Imre Deakb3b8e992016-12-05 18:27:38 +02003751 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003752
Paulo Zanoni56feca92016-09-22 18:00:28 -03003753 if (!intel_has_sagv(dev_priv))
3754 return 0;
3755
3756 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003757 return 0;
3758
Ville Syrjäläff61a972018-12-21 19:14:34 +02003759 DRM_DEBUG_KMS("Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003760 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003761 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3762 GEN9_SAGV_DISABLE,
3763 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3764 1);
Lyude656d1b82016-08-17 15:55:54 -04003765 /*
3766 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003767 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003768 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003769 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003770 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003771 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003772 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003773 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003774 DRM_ERROR("Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003775 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003776 }
3777
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003778 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003779 return 0;
3780}
3781
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003782bool intel_can_enable_sagv(struct intel_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003783{
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003784 struct drm_device *dev = state->base.dev;
Lyude656d1b82016-08-17 15:55:54 -04003785 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003786 struct intel_crtc *crtc;
3787 struct intel_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003788 struct intel_crtc_state *crtc_state;
Lyude656d1b82016-08-17 15:55:54 -04003789 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003790 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003791
Paulo Zanoni56feca92016-09-22 18:00:28 -03003792 if (!intel_has_sagv(dev_priv))
3793 return false;
3794
Lyude656d1b82016-08-17 15:55:54 -04003795 /*
Lyude656d1b82016-08-17 15:55:54 -04003796 * If there are no active CRTCs, no additional checks need be performed
3797 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003798 if (hweight8(state->active_pipes) == 0)
Lyude656d1b82016-08-17 15:55:54 -04003799 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003800
3801 /*
3802 * SKL+ workaround: bspec recommends we disable SAGV when we have
3803 * more then one pipe enabled
3804 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003805 if (hweight8(state->active_pipes) > 1)
Lyude656d1b82016-08-17 15:55:54 -04003806 return false;
3807
3808 /* Since we're now guaranteed to only have one active CRTC... */
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03003809 pipe = ffs(state->active_pipes) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003810 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003811 crtc_state = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003812
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003813 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003814 return false;
3815
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003816 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003817 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003818 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003819
Lyude656d1b82016-08-17 15:55:54 -04003820 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003821 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003822 continue;
3823
3824 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003825 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003826 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003827 { }
3828
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003829 latency = dev_priv->wm.skl_latency[level];
3830
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003831 if (skl_needs_memory_bw_wa(dev_priv) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003832 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003833 I915_FORMAT_MOD_X_TILED)
3834 latency += 15;
3835
Lyude656d1b82016-08-17 15:55:54 -04003836 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003837 * If any of the planes on this pipe don't enable wm levels that
3838 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003839 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003840 */
James Ausmusb068a862019-10-09 10:23:14 -07003841 if (latency < dev_priv->sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003842 return false;
3843 }
3844
3845 return true;
3846}
3847
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303848static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003849 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003850 const u64 total_data_rate,
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303851 const int num_active,
3852 struct skl_ddb_allocation *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303853{
3854 const struct drm_display_mode *adjusted_mode;
3855 u64 total_data_bw;
3856 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3857
3858 WARN_ON(ddb_size == 0);
3859
3860 if (INTEL_GEN(dev_priv) < 11)
3861 return ddb_size - 4; /* 4 blocks for bypass path allocation */
3862
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003863 adjusted_mode = &crtc_state->hw.adjusted_mode;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003864 total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303865
3866 /*
3867 * 12GB/s is maximum BW supported by single DBuf slice.
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003868 *
3869 * FIXME dbuf slice code is broken:
3870 * - must wait for planes to stop using the slice before powering it off
3871 * - plane straddling both slices is illegal in multi-pipe scenarios
3872 * - should validate we stay within the hw bandwidth limits
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303873 */
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003874 if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303875 ddb->enabled_slices = 2;
3876 } else {
3877 ddb->enabled_slices = 1;
3878 ddb_size /= 2;
3879 }
3880
3881 return ddb_size;
3882}
3883
Damien Lespiaub9cec072014-11-04 17:06:43 +00003884static void
Maarten Lankhorstb048a002018-10-18 13:51:30 +02003885skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003886 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003887 const u64 total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303888 struct skl_ddb_allocation *ddb,
Matt Roperc107acf2016-05-12 07:06:01 -07003889 struct skl_ddb_entry *alloc, /* out */
3890 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003891{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003892 struct drm_atomic_state *state = crtc_state->uapi.state;
Matt Roperc107acf2016-05-12 07:06:01 -07003893 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003894 struct drm_crtc *for_crtc = crtc_state->uapi.crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003895 const struct intel_crtc *crtc;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303896 u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
3897 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
3898 u16 ddb_size;
3899 u32 i;
Matt Roperc107acf2016-05-12 07:06:01 -07003900
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003901 if (WARN_ON(!state) || !crtc_state->hw.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003902 alloc->start = 0;
3903 alloc->end = 0;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003904 *num_active = hweight8(dev_priv->active_pipes);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003905 return;
3906 }
3907
Matt Ropera6d3460e2016-05-12 07:06:04 -07003908 if (intel_state->active_pipe_changes)
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003909 *num_active = hweight8(intel_state->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003910 else
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003911 *num_active = hweight8(dev_priv->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003912
Maarten Lankhorstec193642019-06-28 10:55:17 +02003913 ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303914 *num_active, ddb);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003915
Matt Roperc107acf2016-05-12 07:06:01 -07003916 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303917 * If the state doesn't change the active CRTC's or there is no
3918 * modeset request, then there's no need to recalculate;
3919 * the existing pipe allocation limits should remain unchanged.
3920 * Note that we're safe from racing commits since any racing commit
3921 * that changes the active CRTC list or do modeset would need to
3922 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003923 */
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303924 if (!intel_state->active_pipe_changes && !intel_state->modeset) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003925 /*
3926 * alloc may be cleared by clear_intel_crtc_state,
3927 * copy from old state to be sure
3928 */
3929 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003930 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003931 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003932
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303933 /*
3934 * Watermark/ddb requirement highly depends upon width of the
3935 * framebuffer, So instead of allocating DDB equally among pipes
3936 * distribute DDB based on resolution/width of the display.
3937 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003938 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3939 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003940 &crtc_state->hw.adjusted_mode;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003941 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303942 int hdisplay, vdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303943
Maarten Lankhorst1326a922019-10-31 12:26:02 +01003944 if (!crtc_state->hw.enable)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303945 continue;
3946
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303947 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
3948 total_width += hdisplay;
3949
3950 if (pipe < for_pipe)
3951 width_before_pipe += hdisplay;
3952 else if (pipe == for_pipe)
3953 pipe_width = hdisplay;
3954 }
3955
3956 alloc->start = ddb_size * width_before_pipe / total_width;
3957 alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003958}
3959
Ville Syrjälädf331de2019-03-19 18:03:11 +02003960static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
3961 int width, const struct drm_format_info *format,
3962 u64 modifier, unsigned int rotation,
3963 u32 plane_pixel_rate, struct skl_wm_params *wp,
3964 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003965static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02003966 int level,
3967 const struct skl_wm_params *wp,
3968 const struct skl_wm_level *result_prev,
3969 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003970
Ville Syrjälädf331de2019-03-19 18:03:11 +02003971static unsigned int
3972skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
3973 int num_active)
3974{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01003975 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003976 int level, max_level = ilk_wm_max_level(dev_priv);
3977 struct skl_wm_level wm = {};
3978 int ret, min_ddb_alloc = 0;
3979 struct skl_wm_params wp;
3980
3981 ret = skl_compute_wm_params(crtc_state, 256,
3982 drm_format_info(DRM_FORMAT_ARGB8888),
3983 DRM_FORMAT_MOD_LINEAR,
3984 DRM_MODE_ROTATE_0,
3985 crtc_state->pixel_rate, &wp, 0);
3986 WARN_ON(ret);
3987
3988 for (level = 0; level <= max_level; level++) {
Ville Syrjälä6086e472019-03-21 19:51:28 +02003989 skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003990 if (wm.min_ddb_alloc == U16_MAX)
3991 break;
3992
3993 min_ddb_alloc = wm.min_ddb_alloc;
3994 }
3995
3996 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003997}
3998
Mahesh Kumar37cde112018-04-26 19:55:17 +05303999static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4000 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00004001{
Mahesh Kumar37cde112018-04-26 19:55:17 +05304002
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02004003 entry->start = reg & DDB_ENTRY_MASK;
4004 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05304005
Damien Lespiau16160e32014-11-04 17:06:53 +00004006 if (entry->end)
4007 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00004008}
4009
Mahesh Kumarddf34312018-04-09 09:11:03 +05304010static void
4011skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4012 const enum pipe pipe,
4013 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004014 struct skl_ddb_entry *ddb_y,
4015 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05304016{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004017 u32 val, val2;
4018 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05304019
4020 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4021 if (plane_id == PLANE_CURSOR) {
4022 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004023 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304024 return;
4025 }
4026
4027 val = I915_READ(PLANE_CTL(pipe, plane_id));
4028
4029 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004030 if (val & PLANE_CTL_ENABLE)
4031 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4032 val & PLANE_CTL_ORDER_RGBX,
4033 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304034
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004035 if (INTEL_GEN(dev_priv) >= 11) {
4036 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
4037 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4038 } else {
4039 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07004040 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304041
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004042 if (fourcc &&
4043 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004044 swap(val, val2);
4045
4046 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4047 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304048 }
4049}
4050
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004051void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4052 struct skl_ddb_entry *ddb_y,
4053 struct skl_ddb_entry *ddb_uv)
4054{
4055 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4056 enum intel_display_power_domain power_domain;
4057 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004058 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004059 enum plane_id plane_id;
4060
4061 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004062 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4063 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004064 return;
4065
4066 for_each_plane_id_on_crtc(crtc, plane_id)
4067 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4068 plane_id,
4069 &ddb_y[plane_id],
4070 &ddb_uv[plane_id]);
4071
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004072 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004073}
4074
Damien Lespiau08db6652014-11-04 17:06:52 +00004075void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
4076 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00004077{
Mahesh Kumar74bd8002018-04-26 19:55:15 +05304078 ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
Damien Lespiaua269c582014-11-04 17:06:49 +00004079}
4080
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004081/*
4082 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4083 * The bspec defines downscale amount as:
4084 *
4085 * """
4086 * Horizontal down scale amount = maximum[1, Horizontal source size /
4087 * Horizontal destination size]
4088 * Vertical down scale amount = maximum[1, Vertical source size /
4089 * Vertical destination size]
4090 * Total down scale amount = Horizontal down scale amount *
4091 * Vertical down scale amount
4092 * """
4093 *
4094 * Return value is provided in 16.16 fixed point form to retain fractional part.
4095 * Caller should take care of dividing & rounding off the value.
4096 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304097static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004098skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4099 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004100{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004101 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304102 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4103 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004104
Maarten Lankhorstec193642019-06-28 10:55:17 +02004105 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304106 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004107
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004108 /*
4109 * Src coordinates are already rotated by 270 degrees for
4110 * the 90/270 degree plane rotation cases (to match the
4111 * GTT mapping), hence no need to account for rotation here.
4112 *
4113 * n.b., src is 16.16 fixed point, dst is whole integer.
4114 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004115 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4116 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4117 dst_w = drm_rect_width(&plane_state->uapi.dst);
4118 dst_h = drm_rect_height(&plane_state->uapi.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004119
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304120 fp_w_ratio = div_fixed16(src_w, dst_w);
4121 fp_h_ratio = div_fixed16(src_h, dst_h);
4122 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4123 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004124
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304125 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004126}
4127
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004128static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004129skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4130 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004131 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004132{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004133 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004134 const struct drm_framebuffer *fb = plane_state->hw.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004135 u32 data_rate;
4136 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304137 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004138 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004139
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004140 if (!plane_state->uapi.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004141 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004142
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004143 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004144 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004145
4146 if (color_plane == 1 &&
Imre Deak4941f352019-12-21 14:05:43 +02004147 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
Matt Ropera1de91e2016-05-12 07:05:57 -07004148 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004149
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004150 /*
4151 * Src coordinates are already rotated by 270 degrees for
4152 * the 90/270 degree plane rotation cases (to match the
4153 * GTT mapping), hence no need to account for rotation here.
4154 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004155 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4156 height = drm_rect_height(&plane_state->uapi.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004157
Mahesh Kumarb879d582018-04-09 09:11:01 +05304158 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004159 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304160 width /= 2;
4161 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004162 }
4163
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004164 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304165
Maarten Lankhorstec193642019-06-28 10:55:17 +02004166 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004167
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004168 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4169
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004170 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004171 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004172}
4173
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004174static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004175skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004176 u64 *plane_data_rate,
4177 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004178{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004179 struct drm_atomic_state *state = crtc_state->uapi.state;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004180 struct intel_plane *plane;
4181 const struct intel_plane_state *plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004182 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004183
4184 if (WARN_ON(!state))
4185 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004186
Matt Ropera1de91e2016-05-12 07:05:57 -07004187 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004188 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4189 enum plane_id plane_id = plane->id;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004190 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004191
Mahesh Kumarb879d582018-04-09 09:11:01 +05304192 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004193 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004194 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004195 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004196
Mahesh Kumarb879d582018-04-09 09:11:01 +05304197 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004198 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304199 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004200 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004201 }
4202
4203 return total_data_rate;
4204}
4205
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004206static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004207icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004208 u64 *plane_data_rate)
4209{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004210 struct intel_plane *plane;
4211 const struct intel_plane_state *plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004212 u64 total_data_rate = 0;
4213
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004214 if (WARN_ON(!crtc_state->uapi.state))
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004215 return 0;
4216
4217 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004218 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4219 enum plane_id plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004220 u64 rate;
4221
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004222 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004223 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004224 plane_data_rate[plane_id] = rate;
4225 total_data_rate += rate;
4226 } else {
4227 enum plane_id y_plane_id;
4228
4229 /*
4230 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004231 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004232 * and needs the master plane state which may be
4233 * NULL if we try get_new_plane_state(), so we
4234 * always calculate from the master.
4235 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004236 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004237 continue;
4238
4239 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004240 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004241 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004242 plane_data_rate[y_plane_id] = rate;
4243 total_data_rate += rate;
4244
Maarten Lankhorstec193642019-06-28 10:55:17 +02004245 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004246 plane_data_rate[plane_id] = rate;
4247 total_data_rate += rate;
4248 }
4249 }
4250
4251 return total_data_rate;
4252}
4253
Matt Roperc107acf2016-05-12 07:06:01 -07004254static int
Maarten Lankhorstec193642019-06-28 10:55:17 +02004255skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004256 struct skl_ddb_allocation *ddb /* out */)
4257{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004258 struct drm_atomic_state *state = crtc_state->uapi.state;
4259 struct drm_crtc *crtc = crtc_state->uapi.crtc;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004260 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004262 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004263 u16 alloc_size, start = 0;
4264 u16 total[I915_MAX_PLANES] = {};
4265 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004266 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004267 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004268 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004269 u64 plane_data_rate[I915_MAX_PLANES] = {};
4270 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004271 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004272 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004273
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004274 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004275 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4276 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004277
Matt Ropera6d3460e2016-05-12 07:06:04 -07004278 if (WARN_ON(!state))
4279 return 0;
4280
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004281 if (!crtc_state->hw.active) {
Lyudece0ba282016-09-15 10:46:35 -04004282 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004283 return 0;
4284 }
4285
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004286 if (INTEL_GEN(dev_priv) >= 11)
4287 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004288 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004289 plane_data_rate);
4290 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004291 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004292 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004293 plane_data_rate,
4294 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004295
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004296
Maarten Lankhorstec193642019-06-28 10:55:17 +02004297 skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004298 ddb, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004299 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304300 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004301 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004302
Matt Roperd8e87492018-12-11 09:31:07 -08004303 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004304 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004305 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004306 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004307 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004308 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004309
Matt Ropera1de91e2016-05-12 07:05:57 -07004310 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004311 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004312
Matt Roperd8e87492018-12-11 09:31:07 -08004313 /*
4314 * Find the highest watermark level for which we can satisfy the block
4315 * requirement of active planes.
4316 */
4317 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004318 blocks = 0;
Matt Roperd8e87492018-12-11 09:31:07 -08004319 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004320 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004321 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004322
4323 if (plane_id == PLANE_CURSOR) {
Vandita Kulkarni4ba48702019-12-16 13:36:19 +05304324 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
4325 WARN_ON(wm->wm[level].min_ddb_alloc != U16_MAX);
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004326 blocks = U32_MAX;
4327 break;
4328 }
4329 continue;
4330 }
4331
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004332 blocks += wm->wm[level].min_ddb_alloc;
4333 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004334 }
4335
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004336 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004337 alloc_size -= blocks;
4338 break;
4339 }
4340 }
4341
4342 if (level < 0) {
4343 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4344 DRM_DEBUG_KMS("minimum required %d/%d\n", blocks,
4345 alloc_size);
4346 return -EINVAL;
4347 }
4348
4349 /*
4350 * Grant each plane the blocks it requires at the highest achievable
4351 * watermark level, plus an extra share of the leftover blocks
4352 * proportional to its relative data rate.
4353 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004354 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004355 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004356 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004357 u64 rate;
4358 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004359
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004360 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004361 continue;
4362
Damien Lespiaub9cec072014-11-04 17:06:43 +00004363 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004364 * We've accounted for all active planes; remaining planes are
4365 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004366 */
Matt Roperd8e87492018-12-11 09:31:07 -08004367 if (total_data_rate == 0)
4368 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004369
Matt Roperd8e87492018-12-11 09:31:07 -08004370 rate = plane_data_rate[plane_id];
4371 extra = min_t(u16, alloc_size,
4372 DIV64_U64_ROUND_UP(alloc_size * rate,
4373 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004374 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004375 alloc_size -= extra;
4376 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004377
Matt Roperd8e87492018-12-11 09:31:07 -08004378 if (total_data_rate == 0)
4379 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004380
Matt Roperd8e87492018-12-11 09:31:07 -08004381 rate = uv_plane_data_rate[plane_id];
4382 extra = min_t(u16, alloc_size,
4383 DIV64_U64_ROUND_UP(alloc_size * rate,
4384 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004385 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004386 alloc_size -= extra;
4387 total_data_rate -= rate;
4388 }
4389 WARN_ON(alloc_size != 0 || total_data_rate != 0);
4390
4391 /* Set the actual DDB start/end points for each plane */
4392 start = alloc->start;
4393 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004394 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004395 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004396 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004397 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004398
4399 if (plane_id == PLANE_CURSOR)
4400 continue;
4401
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004402 /* Gen11+ uses a separate plane for UV watermarks */
Matt Roperd8e87492018-12-11 09:31:07 -08004403 WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004404
Matt Roperd8e87492018-12-11 09:31:07 -08004405 /* Leave disabled planes at (0,0) */
4406 if (total[plane_id]) {
4407 plane_alloc->start = start;
4408 start += total[plane_id];
4409 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004410 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004411
Matt Roperd8e87492018-12-11 09:31:07 -08004412 if (uv_total[plane_id]) {
4413 uv_plane_alloc->start = start;
4414 start += uv_total[plane_id];
4415 uv_plane_alloc->end = start;
4416 }
4417 }
4418
4419 /*
4420 * When we calculated watermark values we didn't know how high
4421 * of a level we'd actually be able to hit, so we just marked
4422 * all levels as "enabled." Go back now and disable the ones
4423 * that aren't actually possible.
4424 */
4425 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
4426 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004427 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004428 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004429
4430 /*
4431 * We only disable the watermarks for each plane if
4432 * they exceed the ddb allocation of said plane. This
4433 * is done so that we don't end up touching cursor
4434 * watermarks needlessly when some other plane reduces
4435 * our max possible watermark level.
4436 *
4437 * Bspec has this to say about the PLANE_WM enable bit:
4438 * "All the watermarks at this level for all enabled
4439 * planes must be enabled before the level will be used."
4440 * So this is actually safe to do.
4441 */
4442 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
4443 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
4444 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02004445
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004446 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004447 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004448 * Underruns with WM1+ disabled
4449 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07004450 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02004451 level == 1 && wm->wm[0].plane_en) {
4452 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004453 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
4454 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004455 }
Matt Roperd8e87492018-12-11 09:31:07 -08004456 }
4457 }
4458
4459 /*
4460 * Go back and disable the transition watermark if it turns out we
4461 * don't have enough DDB blocks for it.
4462 */
4463 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004464 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004465 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004466
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02004467 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08004468 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00004469 }
4470
Matt Roperc107acf2016-05-12 07:06:01 -07004471 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004472}
4473
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004474/*
4475 * 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 +02004476 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004477 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4478 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4479*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004480static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004481skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
4482 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004483{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004484 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304485 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004486
4487 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304488 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004489
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304490 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004491 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004492
4493 if (INTEL_GEN(dev_priv) >= 10)
4494 ret = add_fixed16_u32(ret, 1);
4495
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004496 return ret;
4497}
4498
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004499static uint_fixed_16_16_t
4500skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
4501 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004502{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004503 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304504 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004505
4506 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304507 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004508
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004509 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304510 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4511 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304512 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004513 return ret;
4514}
4515
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304516static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004517intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304518{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004519 u32 pixel_rate;
4520 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304521 uint_fixed_16_16_t linetime_us;
4522
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004523 if (!crtc_state->hw.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304524 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304525
Maarten Lankhorstec193642019-06-28 10:55:17 +02004526 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304527
4528 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304529 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304530
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004531 crtc_htotal = crtc_state->hw.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304532 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304533
4534 return linetime_us;
4535}
4536
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004537static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004538skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
4539 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004540{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004541 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304542 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004543
4544 /* Shouldn't reach here on disabled planes... */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004545 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004546 return 0;
4547
4548 /*
4549 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4550 * with additional adjustments for plane-specific scaling.
4551 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004552 adjusted_pixel_rate = crtc_state->pixel_rate;
4553 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004554
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304555 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4556 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004557}
4558
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304559static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004560skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4561 int width, const struct drm_format_info *format,
4562 u64 modifier, unsigned int rotation,
4563 u32 plane_pixel_rate, struct skl_wm_params *wp,
4564 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304565{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004566 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004567 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004568 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304569
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304570 /* only planar format has two planes */
Imre Deak4941f352019-12-21 14:05:43 +02004571 if (color_plane == 1 &&
4572 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304573 DRM_DEBUG_KMS("Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304574 return -EINVAL;
4575 }
4576
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004577 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
4578 modifier == I915_FORMAT_MOD_Yf_TILED ||
4579 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4580 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4581 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
4582 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4583 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Imre Deak4941f352019-12-21 14:05:43 +02004584 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304585
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004586 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02004587 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304588 wp->width /= 2;
4589
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004590 wp->cpp = format->cpp[color_plane];
4591 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304592
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004593 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004594 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004595 wp->dbuf_block_size = 256;
4596 else
4597 wp->dbuf_block_size = 512;
4598
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004599 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304600 switch (wp->cpp) {
4601 case 1:
4602 wp->y_min_scanlines = 16;
4603 break;
4604 case 2:
4605 wp->y_min_scanlines = 8;
4606 break;
4607 case 4:
4608 wp->y_min_scanlines = 4;
4609 break;
4610 default:
4611 MISSING_CASE(wp->cpp);
4612 return -EINVAL;
4613 }
4614 } else {
4615 wp->y_min_scanlines = 4;
4616 }
4617
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004618 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304619 wp->y_min_scanlines *= 2;
4620
4621 wp->plane_bytes_per_line = wp->width * wp->cpp;
4622 if (wp->y_tiled) {
4623 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004624 wp->y_min_scanlines,
4625 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304626
4627 if (INTEL_GEN(dev_priv) >= 10)
4628 interm_pbpl++;
4629
4630 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4631 wp->y_min_scanlines);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004632 } else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004633 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4634 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304635 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4636 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004637 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4638 wp->dbuf_block_size) + 1;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304639 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4640 }
4641
4642 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4643 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004644
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304645 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004646 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304647
4648 return 0;
4649}
4650
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004651static int
4652skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
4653 const struct intel_plane_state *plane_state,
4654 struct skl_wm_params *wp, int color_plane)
4655{
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004656 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004657 int width;
4658
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004659 /*
4660 * Src coordinates are already rotated by 270 degrees for
4661 * the 90/270 degree plane rotation cases (to match the
4662 * GTT mapping), hence no need to account for rotation here.
4663 */
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004664 width = drm_rect_width(&plane_state->uapi.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004665
4666 return skl_compute_wm_params(crtc_state, width,
4667 fb->format, fb->modifier,
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004668 plane_state->hw.rotation,
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004669 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
4670 wp, color_plane);
4671}
4672
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004673static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
4674{
4675 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4676 return true;
4677
4678 /* The number of lines are ignored for the level 0 watermark. */
4679 return level > 0;
4680}
4681
Maarten Lankhorstec193642019-06-28 10:55:17 +02004682static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08004683 int level,
4684 const struct skl_wm_params *wp,
4685 const struct skl_wm_level *result_prev,
4686 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004687{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004688 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004689 u32 latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304690 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304691 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004692 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02004693
Ville Syrjälä0aded172019-02-05 17:50:53 +02004694 if (latency == 0) {
4695 /* reject it */
4696 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02004697 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004698 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02004699
Ville Syrjälä25312ef2019-05-03 20:38:05 +03004700 /*
4701 * WaIncreaseLatencyIPCEnabled: kbl,cfl
4702 * Display WA #1141: kbl,cfl
4703 */
Ville Syrjälä5a7d2022019-05-03 20:38:06 +03004704 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004705 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304706 latency += 4;
4707
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004708 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004709 latency += 15;
4710
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304711 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004712 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304713 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004714 crtc_state->hw.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004715 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304716 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004717
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304718 if (wp->y_tiled) {
4719 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004720 } else {
Maarten Lankhorst1326a922019-10-31 12:26:02 +01004721 if ((wp->cpp * crtc_state->hw.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004722 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004723 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004724 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004725 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004726 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004727 !IS_GEMINILAKE(dev_priv))
4728 selected_result = min_fixed16(method1, method2);
4729 else
4730 selected_result = method2;
4731 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004732 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004733 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004734 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004735
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304736 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304737 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304738 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004739
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004740 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
4741 /* Display WA #1125: skl,bxt,kbl */
4742 if (level == 0 && wp->rc_surface)
4743 res_blocks +=
4744 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004745
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004746 /* Display WA #1126: skl,bxt,kbl */
4747 if (level >= 1 && level <= 7) {
4748 if (wp->y_tiled) {
4749 res_blocks +=
4750 fixed16_to_u32_round_up(wp->y_tile_minimum);
4751 res_lines += wp->y_min_scanlines;
4752 } else {
4753 res_blocks++;
4754 }
4755
4756 /*
4757 * Make sure result blocks for higher latency levels are
4758 * atleast as high as level below the current level.
4759 * Assumption in DDB algorithm optimization for special
4760 * cases. Also covers Display WA #1125 for RC.
4761 */
4762 if (result_prev->plane_res_b > res_blocks)
4763 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004764 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004765 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004766
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004767 if (INTEL_GEN(dev_priv) >= 11) {
4768 if (wp->y_tiled) {
4769 int extra_lines;
4770
4771 if (res_lines % wp->y_min_scanlines == 0)
4772 extra_lines = wp->y_min_scanlines;
4773 else
4774 extra_lines = wp->y_min_scanlines * 2 -
4775 res_lines % wp->y_min_scanlines;
4776
4777 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
4778 wp->plane_blocks_per_line);
4779 } else {
4780 min_ddb_alloc = res_blocks +
4781 DIV_ROUND_UP(res_blocks, 10);
4782 }
4783 }
4784
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004785 if (!skl_wm_has_lines(dev_priv, level))
4786 res_lines = 0;
4787
Ville Syrjälä0aded172019-02-05 17:50:53 +02004788 if (res_lines > 31) {
4789 /* reject it */
4790 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08004791 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004792 }
Matt Roperd8e87492018-12-11 09:31:07 -08004793
4794 /*
4795 * If res_lines is valid, assume we can use this watermark level
4796 * for now. We'll come back and disable it after we calculate the
4797 * DDB allocation if it turns out we don't actually have enough
4798 * blocks to satisfy it.
4799 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05304800 result->plane_res_b = res_blocks;
4801 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004802 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
4803 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05304804 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004805}
4806
Matt Roperd8e87492018-12-11 09:31:07 -08004807static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02004808skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304809 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004810 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004811{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004812 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304813 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004814 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04004815
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304816 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004817 struct skl_wm_level *result = &levels[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304818
Maarten Lankhorstec193642019-06-28 10:55:17 +02004819 skl_compute_plane_wm(crtc_state, level, wm_params,
Matt Roperd8e87492018-12-11 09:31:07 -08004820 result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004821
4822 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304823 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004824}
4825
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004826static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004827skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004828{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004829 struct drm_atomic_state *state = crtc_state->uapi.state;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304830 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304831 uint_fixed_16_16_t linetime_us;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004832 u32 linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004833
Maarten Lankhorstec193642019-06-28 10:55:17 +02004834 linetime_us = intel_get_linetime_us(crtc_state);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304835 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304836
Ville Syrjälä717671c2018-12-21 19:14:36 +02004837 /* Display WA #1135: BXT:ALL GLK:ALL */
4838 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304839 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304840
4841 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004842}
4843
Maarten Lankhorstec193642019-06-28 10:55:17 +02004844static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004845 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08004846 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004847{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01004848 struct drm_device *dev = crtc_state->uapi.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05304849 const struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004850 u16 trans_min, trans_y_tile_min;
4851 const u16 trans_amount = 10; /* This is configurable amount */
4852 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004853
Kumar, Maheshca476672017-08-17 19:15:24 +05304854 /* Transition WM are not recommended by HW team for GEN9 */
4855 if (INTEL_GEN(dev_priv) <= 9)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004856 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304857
4858 /* Transition WM don't make any sense if ipc is disabled */
4859 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004860 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304861
Paulo Zanoni91961a82018-10-04 16:15:56 -07004862 trans_min = 14;
4863 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05304864 trans_min = 4;
4865
4866 trans_offset_b = trans_min + trans_amount;
4867
Paulo Zanonicbacc792018-10-04 16:15:58 -07004868 /*
4869 * The spec asks for Selected Result Blocks for wm0 (the real value),
4870 * not Result Blocks (the integer value). Pay attention to the capital
4871 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
4872 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
4873 * and since we later will have to get the ceiling of the sum in the
4874 * transition watermarks calculation, we can just pretend Selected
4875 * Result Blocks is Result Blocks minus 1 and it should work for the
4876 * current platforms.
4877 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004878 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07004879
Kumar, Maheshca476672017-08-17 19:15:24 +05304880 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004881 trans_y_tile_min =
4882 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07004883 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05304884 trans_offset_b;
4885 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07004886 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05304887
4888 /* WA BUG:1938466 add one block for non y-tile planes */
4889 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4890 res_blocks += 1;
4891
4892 }
4893
Matt Roperd8e87492018-12-11 09:31:07 -08004894 /*
4895 * Just assume we can enable the transition watermark. After
4896 * computing the DDB we'll come back and disable it if that
4897 * assumption turns out to be false.
4898 */
4899 wm->trans_wm.plane_res_b = res_blocks + 1;
4900 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004901}
4902
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004903static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004904 const struct intel_plane_state *plane_state,
4905 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004906{
Ville Syrjälä83158472018-11-27 18:57:26 +02004907 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004908 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004909 int ret;
4910
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004911 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004912 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004913 if (ret)
4914 return ret;
4915
Ville Syrjälä67155a62019-03-12 22:58:37 +02004916 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Matt Roperd8e87492018-12-11 09:31:07 -08004917 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004918
4919 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004920}
4921
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004922static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004923 const struct intel_plane_state *plane_state,
4924 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004925{
Ville Syrjälä83158472018-11-27 18:57:26 +02004926 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
4927 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004928 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004929
Ville Syrjälä83158472018-11-27 18:57:26 +02004930 wm->is_planar = true;
4931
4932 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004933 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004934 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004935 if (ret)
4936 return ret;
4937
Ville Syrjälä67155a62019-03-12 22:58:37 +02004938 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004939
4940 return 0;
4941}
4942
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02004943static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004944 const struct intel_plane_state *plane_state)
4945{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004946 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004947 const struct drm_framebuffer *fb = plane_state->hw.fb;
Ville Syrjälä83158472018-11-27 18:57:26 +02004948 enum plane_id plane_id = plane->id;
4949 int ret;
4950
4951 if (!intel_wm_plane_visible(crtc_state, plane_state))
4952 return 0;
4953
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004954 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004955 plane_id, 0);
4956 if (ret)
4957 return ret;
4958
4959 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004960 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004961 plane_id);
4962 if (ret)
4963 return ret;
4964 }
4965
4966 return 0;
4967}
4968
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02004969static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004970 const struct intel_plane_state *plane_state)
4971{
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01004972 enum plane_id plane_id = to_intel_plane(plane_state->uapi.plane)->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02004973 int ret;
4974
4975 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004976 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02004977 return 0;
4978
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004979 if (plane_state->planar_linked_plane) {
Maarten Lankhorst7b3cb172019-10-31 12:26:07 +01004980 const struct drm_framebuffer *fb = plane_state->hw.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004981 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02004982
4983 WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
4984 WARN_ON(!fb->format->is_yuv ||
4985 fb->format->num_planes == 1);
4986
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004987 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004988 y_plane_id, 0);
4989 if (ret)
4990 return ret;
4991
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004992 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004993 plane_id, 1);
4994 if (ret)
4995 return ret;
4996 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004997 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004998 plane_id, 0);
4999 if (ret)
5000 return ret;
5001 }
5002
5003 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005004}
5005
Maarten Lankhorstec193642019-06-28 10:55:17 +02005006static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005007{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005008 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005009 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005010 struct intel_plane *plane;
5011 const struct intel_plane_state *plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005012 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005013
Lyudea62163e2016-10-04 14:28:20 -04005014 /*
5015 * We'll only calculate watermarks for planes that are actually
5016 * enabled, so make sure all other planes are set as disabled.
5017 */
5018 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5019
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005020 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state,
5021 crtc_state) {
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305022
Ville Syrjälä83158472018-11-27 18:57:26 +02005023 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005024 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005025 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005026 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305027 if (ret)
5028 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005029 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305030
Maarten Lankhorstec193642019-06-28 10:55:17 +02005031 pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005032
Matt Roper55994c22016-05-12 07:06:08 -07005033 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005034}
5035
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005036static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5037 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005038 const struct skl_ddb_entry *entry)
5039{
5040 if (entry->end)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005041 I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005042 else
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005043 I915_WRITE_FW(reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005044}
5045
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005046static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5047 i915_reg_t reg,
5048 const struct skl_wm_level *level)
5049{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005050 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005051
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005052 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005053 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005054 if (level->ignore_lines)
5055 val |= PLANE_WM_IGNORE_LINES;
5056 val |= level->plane_res_b;
5057 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005058
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005059 I915_WRITE_FW(reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005060}
5061
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005062void skl_write_plane_wm(struct intel_plane *plane,
5063 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005064{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005065 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005066 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005067 enum plane_id plane_id = plane->id;
5068 enum pipe pipe = plane->pipe;
5069 const struct skl_plane_wm *wm =
5070 &crtc_state->wm.skl.optimal.planes[plane_id];
5071 const struct skl_ddb_entry *ddb_y =
5072 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5073 const struct skl_ddb_entry *ddb_uv =
5074 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005075
5076 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005077 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005078 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005079 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005080 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005081 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005082
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005083 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005084 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005085 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5086 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305087 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005088
5089 if (wm->is_planar)
5090 swap(ddb_y, ddb_uv);
5091
5092 skl_ddb_entry_write(dev_priv,
5093 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5094 skl_ddb_entry_write(dev_priv,
5095 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005096}
5097
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005098void skl_write_cursor_wm(struct intel_plane *plane,
5099 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005100{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005101 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005102 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005103 enum plane_id plane_id = plane->id;
5104 enum pipe pipe = plane->pipe;
5105 const struct skl_plane_wm *wm =
5106 &crtc_state->wm.skl.optimal.planes[plane_id];
5107 const struct skl_ddb_entry *ddb =
5108 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005109
5110 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005111 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5112 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005113 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005114 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005115
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005116 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005117}
5118
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005119bool skl_wm_level_equals(const struct skl_wm_level *l1,
5120 const struct skl_wm_level *l2)
5121{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005122 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005123 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005124 l1->plane_res_l == l2->plane_res_l &&
5125 l1->plane_res_b == l2->plane_res_b;
5126}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005127
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005128static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5129 const struct skl_plane_wm *wm1,
5130 const struct skl_plane_wm *wm2)
5131{
5132 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005133
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005134 for (level = 0; level <= max_level; level++) {
5135 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
5136 !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
5137 return false;
5138 }
5139
5140 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005141}
5142
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005143static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
5144 const struct skl_pipe_wm *wm1,
5145 const struct skl_pipe_wm *wm2)
5146{
5147 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5148 enum plane_id plane_id;
5149
5150 for_each_plane_id_on_crtc(crtc, plane_id) {
5151 if (!skl_plane_wm_equals(dev_priv,
5152 &wm1->planes[plane_id],
5153 &wm2->planes[plane_id]))
5154 return false;
5155 }
5156
5157 return wm1->linetime == wm2->linetime;
5158}
5159
Lyude27082492016-08-24 07:48:10 +02005160static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5161 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005162{
Lyude27082492016-08-24 07:48:10 +02005163 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005164}
5165
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005166bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005167 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005168 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005169{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005170 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005171
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005172 for (i = 0; i < num_entries; i++) {
5173 if (i != ignore_idx &&
5174 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005175 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005176 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005177
Lyude27082492016-08-24 07:48:10 +02005178 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005179}
5180
Jani Nikulabb7791b2016-10-04 12:29:17 +03005181static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005182skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5183 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005184{
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005185 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
5186 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005187 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5188 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005189
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005190 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5191 struct intel_plane_state *plane_state;
5192 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005193
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005194 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5195 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5196 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5197 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005198 continue;
5199
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005200 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005201 if (IS_ERR(plane_state))
5202 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005203
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005204 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005205 }
5206
5207 return 0;
5208}
5209
5210static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005211skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005212{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005213 const struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5214 struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005215 struct intel_crtc_state *old_crtc_state;
5216 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305217 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305218 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005219
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005220 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
5221
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005222 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005223 new_crtc_state, i) {
5224 ret = skl_allocate_pipe_ddb(new_crtc_state, ddb);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005225 if (ret)
5226 return ret;
5227
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005228 ret = skl_ddb_add_affected_planes(old_crtc_state,
5229 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005230 if (ret)
5231 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005232 }
5233
5234 return 0;
5235}
5236
Ville Syrjäläab98e942019-02-08 22:05:27 +02005237static char enast(bool enable)
5238{
5239 return enable ? '*' : ' ';
5240}
5241
Matt Roper2722efb2016-08-17 15:55:55 -04005242static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005243skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005244{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005245 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5246 const struct intel_crtc_state *old_crtc_state;
5247 const struct intel_crtc_state *new_crtc_state;
5248 struct intel_plane *plane;
5249 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005250 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005251
Ville Syrjäläab98e942019-02-08 22:05:27 +02005252 if ((drm_debug & DRM_UT_KMS) == 0)
5253 return;
5254
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005255 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5256 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005257 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5258
5259 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5260 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5261
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005262 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5263 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005264 const struct skl_ddb_entry *old, *new;
5265
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005266 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5267 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005268
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005269 if (skl_ddb_entry_equal(old, new))
5270 continue;
5271
Ville Syrjäläab98e942019-02-08 22:05:27 +02005272 DRM_DEBUG_KMS("[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005273 plane->base.base.id, plane->base.name,
Ville Syrjäläab98e942019-02-08 22:05:27 +02005274 old->start, old->end, new->start, new->end,
5275 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
5276 }
5277
5278 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5279 enum plane_id plane_id = plane->id;
5280 const struct skl_plane_wm *old_wm, *new_wm;
5281
5282 old_wm = &old_pipe_wm->planes[plane_id];
5283 new_wm = &new_pipe_wm->planes[plane_id];
5284
5285 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5286 continue;
5287
5288 DRM_DEBUG_KMS("[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm"
5289 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm\n",
5290 plane->base.base.id, plane->base.name,
5291 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5292 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5293 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5294 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5295 enast(old_wm->trans_wm.plane_en),
5296 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5297 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5298 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5299 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
5300 enast(new_wm->trans_wm.plane_en));
5301
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005302 DRM_DEBUG_KMS("[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d"
5303 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d\n",
Ville Syrjäläab98e942019-02-08 22:05:27 +02005304 plane->base.base.id, plane->base.name,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005305 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5306 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5307 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5308 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5309 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5310 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5311 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5312 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5313 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
5314
5315 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5316 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5317 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5318 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5319 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5320 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5321 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5322 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
5323 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005324
5325 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5326 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5327 plane->base.base.id, plane->base.name,
5328 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5329 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5330 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5331 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5332 old_wm->trans_wm.plane_res_b,
5333 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5334 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5335 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5336 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
5337 new_wm->trans_wm.plane_res_b);
5338
5339 DRM_DEBUG_KMS("[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5340 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5341 plane->base.base.id, plane->base.name,
5342 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5343 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5344 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5345 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5346 old_wm->trans_wm.min_ddb_alloc,
5347 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5348 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5349 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5350 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
5351 new_wm->trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005352 }
5353 }
5354}
5355
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005356static int intel_add_all_pipes(struct intel_atomic_state *state)
5357{
5358 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5359 struct intel_crtc *crtc;
5360
5361 for_each_intel_crtc(&dev_priv->drm, crtc) {
5362 struct intel_crtc_state *crtc_state;
5363
5364 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5365 if (IS_ERR(crtc_state))
5366 return PTR_ERR(crtc_state);
5367 }
5368
5369 return 0;
5370}
5371
Matt Roper98d39492016-05-12 07:06:03 -07005372static int
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005373skl_ddb_add_affected_pipes(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005374{
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005375 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005376 int ret;
Matt Roper98d39492016-05-12 07:06:03 -07005377
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305378 /*
5379 * If this is our first atomic update following hardware readout,
5380 * we can't trust the DDB that the BIOS programmed for us. Let's
5381 * pretend that all pipes switched active status so that we'll
5382 * ensure a full DDB recompute.
5383 */
5384 if (dev_priv->wm.distrust_bios_wm) {
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005385 ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005386 state->base.acquire_ctx);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305387 if (ret)
5388 return ret;
5389
Ville Syrjälä8d9875b2019-10-11 23:09:45 +03005390 state->active_pipe_changes = INTEL_INFO(dev_priv)->pipe_mask;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305391
5392 /*
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005393 * We usually only initialize state->active_pipes if we
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305394 * we're doing a modeset; make sure this field is always
5395 * initialized during the sanitization process that happens
5396 * on the first commit too.
5397 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005398 if (!state->modeset)
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005399 state->active_pipes = dev_priv->active_pipes;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305400 }
5401
5402 /*
5403 * If the modeset changes which CRTC's are active, we need to
5404 * recompute the DDB allocation for *all* active pipes, even
5405 * those that weren't otherwise being modified in any way by this
5406 * atomic commit. Due to the shrinking of the per-pipe allocations
5407 * when new active CRTC's are added, it's possible for a pipe that
5408 * we were already using and aren't changing at all here to suddenly
5409 * become invalid if its DDB needs exceeds its new allocation.
5410 *
5411 * Note that if we wind up doing a full DDB recompute, we can't let
5412 * any other display updates race with this transaction, so we need
5413 * to grab the lock on *all* CRTC's.
5414 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005415 if (state->active_pipe_changes || state->modeset) {
Ville Syrjälä8d9875b2019-10-11 23:09:45 +03005416 state->wm_results.dirty_pipes = INTEL_INFO(dev_priv)->pipe_mask;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305417
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005418 ret = intel_add_all_pipes(state);
5419 if (ret)
5420 return ret;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305421 }
5422
5423 return 0;
5424}
5425
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005426/*
5427 * To make sure the cursor watermark registers are always consistent
5428 * with our computed state the following scenario needs special
5429 * treatment:
5430 *
5431 * 1. enable cursor
5432 * 2. move cursor entirely offscreen
5433 * 3. disable cursor
5434 *
5435 * Step 2. does call .disable_plane() but does not zero the watermarks
5436 * (since we consider an offscreen cursor still active for the purposes
5437 * of watermarks). Step 3. would not normally call .disable_plane()
5438 * because the actual plane visibility isn't changing, and we don't
5439 * deallocate the cursor ddb until the pipe gets disabled. So we must
5440 * force step 3. to call .disable_plane() to update the watermark
5441 * registers properly.
5442 *
5443 * Other planes do not suffer from this issues as their watermarks are
5444 * calculated based on the actual plane visibility. The only time this
5445 * can trigger for the other planes is during the initial readout as the
5446 * default value of the watermarks registers is not zero.
5447 */
5448static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
5449 struct intel_crtc *crtc)
5450{
5451 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5452 const struct intel_crtc_state *old_crtc_state =
5453 intel_atomic_get_old_crtc_state(state, crtc);
5454 struct intel_crtc_state *new_crtc_state =
5455 intel_atomic_get_new_crtc_state(state, crtc);
5456 struct intel_plane *plane;
5457
5458 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5459 struct intel_plane_state *plane_state;
5460 enum plane_id plane_id = plane->id;
5461
5462 /*
5463 * Force a full wm update for every plane on modeset.
5464 * Required because the reset value of the wm registers
5465 * is non-zero, whereas we want all disabled planes to
5466 * have zero watermarks. So if we turn off the relevant
5467 * power well the hardware state will go out of sync
5468 * with the software state.
5469 */
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005470 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005471 skl_plane_wm_equals(dev_priv,
5472 &old_crtc_state->wm.skl.optimal.planes[plane_id],
5473 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
5474 continue;
5475
5476 plane_state = intel_atomic_get_plane_state(state, plane);
5477 if (IS_ERR(plane_state))
5478 return PTR_ERR(plane_state);
5479
5480 new_crtc_state->update_planes |= BIT(plane_id);
5481 }
5482
5483 return 0;
5484}
5485
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305486static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005487skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305488{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005489 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005490 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005491 struct intel_crtc_state *old_crtc_state;
5492 struct skl_ddb_values *results = &state->wm_results;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305493 int ret, i;
5494
Matt Roper734fa012016-05-12 15:11:40 -07005495 /* Clear all dirty flags */
5496 results->dirty_pipes = 0;
5497
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005498 ret = skl_ddb_add_affected_pipes(state);
5499 if (ret)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305500 return ret;
5501
Matt Roper734fa012016-05-12 15:11:40 -07005502 /*
5503 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08005504 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Matt Roper734fa012016-05-12 15:11:40 -07005505 * weren't otherwise being modified (and set bits in dirty_pipes) if
5506 * pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07005507 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005508 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005509 new_crtc_state, i) {
5510 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005511 if (ret)
5512 return ret;
5513
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005514 ret = skl_wm_add_affected_planes(state, crtc);
Matt Roper734fa012016-05-12 15:11:40 -07005515 if (ret)
5516 return ret;
5517
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005518 if (!skl_pipe_wm_equals(crtc,
5519 &old_crtc_state->wm.skl.optimal,
5520 &new_crtc_state->wm.skl.optimal))
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005521 results->dirty_pipes |= BIT(crtc->pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005522 }
5523
Matt Roperd8e87492018-12-11 09:31:07 -08005524 ret = skl_compute_ddb(state);
5525 if (ret)
5526 return ret;
5527
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005528 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005529
Matt Roper98d39492016-05-12 07:06:03 -07005530 return 0;
5531}
5532
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005533static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005534 struct intel_crtc *crtc)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005535{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005536 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5537 const struct intel_crtc_state *crtc_state =
5538 intel_atomic_get_new_crtc_state(state, crtc);
5539 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005540 enum pipe pipe = crtc->pipe;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005541
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005542 if ((state->wm_results.dirty_pipes & BIT(crtc->pipe)) == 0)
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005543 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005544
5545 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
5546}
5547
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005548static void skl_initial_wm(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005549 struct intel_crtc *crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005550{
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005551 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005552 const struct intel_crtc_state *crtc_state =
5553 intel_atomic_get_new_crtc_state(state, crtc);
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305554 struct skl_ddb_values *results = &state->wm_results;
Bob Paauweadda50b2015-07-21 10:42:53 -07005555
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005556 if ((results->dirty_pipes & BIT(crtc->pipe)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005557 return;
5558
Matt Roper734fa012016-05-12 15:11:40 -07005559 mutex_lock(&dev_priv->wm.wm_mutex);
5560
Maarten Lankhorst2225f3c2019-10-31 12:26:03 +01005561 if (crtc_state->uapi.active_changed)
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005562 skl_atomic_update_crtc_wm(state, crtc);
Lyude27082492016-08-24 07:48:10 +02005563
Matt Roper734fa012016-05-12 15:11:40 -07005564 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005565}
5566
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005567static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02005568 struct intel_wm_config *config)
5569{
5570 struct intel_crtc *crtc;
5571
5572 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005573 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02005574 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5575
5576 if (!wm->pipe_enabled)
5577 continue;
5578
5579 config->sprites_enabled |= wm->sprites_enabled;
5580 config->sprites_scaled |= wm->sprites_scaled;
5581 config->num_pipes_active++;
5582 }
5583}
5584
Matt Ropered4a6a72016-02-23 17:20:13 -08005585static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005586{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005587 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005588 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005589 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005590 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005591 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005592
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005593 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02005594
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005595 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
5596 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005597
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005598 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005599 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005600 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005601 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
5602 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005603
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005604 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005605 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005606 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005607 }
5608
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005609 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005610 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005611
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005612 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005613
Imre Deak820c1982013-12-17 14:46:36 +02005614 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005615}
5616
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005617static void ilk_initial_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005618 struct intel_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005619{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005620 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5621 const struct intel_crtc_state *crtc_state =
5622 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005623
Matt Ropered4a6a72016-02-23 17:20:13 -08005624 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005625 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005626 ilk_program_watermarks(dev_priv);
5627 mutex_unlock(&dev_priv->wm.wm_mutex);
5628}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005629
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005630static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005631 struct intel_crtc *crtc)
Matt Ropered4a6a72016-02-23 17:20:13 -08005632{
Ville Syrjälä7a8fdb1f2019-11-18 18:44:26 +02005633 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5634 const struct intel_crtc_state *crtc_state =
5635 intel_atomic_get_new_crtc_state(state, crtc);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005636
5637 if (!crtc_state->wm.need_postvbl_update)
5638 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08005639
5640 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005641 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
5642 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08005643 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005644}
5645
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005646static inline void skl_wm_level_from_reg_val(u32 val,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005647 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005648{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005649 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005650 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005651 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5652 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5653 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005654}
5655
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005656void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005657 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005658{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005659 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5660 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005661 int level, max_level;
5662 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005663 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005664
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005665 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005666
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005667 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005668 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005669
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005670 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005671 if (plane_id != PLANE_CURSOR)
5672 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005673 else
5674 val = I915_READ(CUR_WM(pipe, level));
5675
5676 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5677 }
5678
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005679 if (plane_id != PLANE_CURSOR)
5680 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005681 else
5682 val = I915_READ(CUR_WM_TRANS(pipe));
5683
5684 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5685 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005686
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005687 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005688 return;
5689
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005690 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005691}
5692
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005693void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00005694{
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305695 struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005696 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005697 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005698 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00005699
Damien Lespiaua269c582014-11-04 17:06:49 +00005700 skl_ddb_get_hw_state(dev_priv, ddb);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005701 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02005702 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005703
Maarten Lankhorstec193642019-06-28 10:55:17 +02005704 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005705
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005706 if (crtc->active)
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005707 hw->dirty_pipes |= BIT(crtc->pipe);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005708 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005709
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005710 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07005711 /* Fully recompute DDB on first atomic commit */
5712 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07005713 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005714}
5715
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005716static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005717{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005718 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005719 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005720 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005721 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
5722 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005723 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005724 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005725 [PIPE_A] = WM0_PIPEA_ILK,
5726 [PIPE_B] = WM0_PIPEB_ILK,
5727 [PIPE_C] = WM0_PIPEC_IVB,
5728 };
5729
5730 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005731 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005732 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005733
Ville Syrjälä15606532016-05-13 17:55:17 +03005734 memset(active, 0, sizeof(*active));
5735
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005736 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005737
5738 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005739 u32 tmp = hw->wm_pipe[pipe];
5740
5741 /*
5742 * For active pipes LP0 watermark is marked as
5743 * enabled, and LP1+ watermaks as disabled since
5744 * we can't really reverse compute them in case
5745 * multiple pipes are active.
5746 */
5747 active->wm[0].enable = true;
5748 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5749 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5750 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5751 active->linetime = hw->wm_linetime[pipe];
5752 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005753 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005754
5755 /*
5756 * For inactive pipes, all watermark levels
5757 * should be marked as enabled but zeroed,
5758 * which is what we'd compute them to.
5759 */
5760 for (level = 0; level <= max_level; level++)
5761 active->wm[level].enable = true;
5762 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005763
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005764 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005765}
5766
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005767#define _FW_WM(value, plane) \
5768 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5769#define _FW_WM_VLV(value, plane) \
5770 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5771
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005772static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5773 struct g4x_wm_values *wm)
5774{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005775 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005776
5777 tmp = I915_READ(DSPFW1);
5778 wm->sr.plane = _FW_WM(tmp, SR);
5779 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5780 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5781 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5782
5783 tmp = I915_READ(DSPFW2);
5784 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5785 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5786 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5787 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5788 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5789 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5790
5791 tmp = I915_READ(DSPFW3);
5792 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5793 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5794 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5795 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5796}
5797
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005798static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5799 struct vlv_wm_values *wm)
5800{
5801 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005802 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005803
5804 for_each_pipe(dev_priv, pipe) {
5805 tmp = I915_READ(VLV_DDL(pipe));
5806
Ville Syrjälä1b313892016-11-28 19:37:08 +02005807 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005808 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005809 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005810 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005811 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005812 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005813 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005814 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5815 }
5816
5817 tmp = I915_READ(DSPFW1);
5818 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005819 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5820 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5821 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005822
5823 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005824 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5825 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5826 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005827
5828 tmp = I915_READ(DSPFW3);
5829 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5830
5831 if (IS_CHERRYVIEW(dev_priv)) {
5832 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005833 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5834 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005835
5836 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005837 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5838 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005839
5840 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005841 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5842 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005843
5844 tmp = I915_READ(DSPHOWM);
5845 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005846 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5847 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5848 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5849 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5850 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5851 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5852 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5853 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5854 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005855 } else {
5856 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005857 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5858 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005859
5860 tmp = I915_READ(DSPHOWM);
5861 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005862 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5863 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5864 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5865 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5866 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5867 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005868 }
5869}
5870
5871#undef _FW_WM
5872#undef _FW_WM_VLV
5873
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005874void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005875{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005876 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5877 struct intel_crtc *crtc;
5878
5879 g4x_read_wm_values(dev_priv, wm);
5880
5881 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5882
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005883 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005884 struct intel_crtc_state *crtc_state =
5885 to_intel_crtc_state(crtc->base.state);
5886 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5887 struct g4x_pipe_wm *raw;
5888 enum pipe pipe = crtc->pipe;
5889 enum plane_id plane_id;
5890 int level, max_level;
5891
5892 active->cxsr = wm->cxsr;
5893 active->hpll_en = wm->hpll_en;
5894 active->fbc_en = wm->fbc_en;
5895
5896 active->sr = wm->sr;
5897 active->hpll = wm->hpll;
5898
5899 for_each_plane_id_on_crtc(crtc, plane_id) {
5900 active->wm.plane[plane_id] =
5901 wm->pipe[pipe].plane[plane_id];
5902 }
5903
5904 if (wm->cxsr && wm->hpll_en)
5905 max_level = G4X_WM_LEVEL_HPLL;
5906 else if (wm->cxsr)
5907 max_level = G4X_WM_LEVEL_SR;
5908 else
5909 max_level = G4X_WM_LEVEL_NORMAL;
5910
5911 level = G4X_WM_LEVEL_NORMAL;
5912 raw = &crtc_state->wm.g4x.raw[level];
5913 for_each_plane_id_on_crtc(crtc, plane_id)
5914 raw->plane[plane_id] = active->wm.plane[plane_id];
5915
5916 if (++level > max_level)
5917 goto out;
5918
5919 raw = &crtc_state->wm.g4x.raw[level];
5920 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5921 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5922 raw->plane[PLANE_SPRITE0] = 0;
5923 raw->fbc = active->sr.fbc;
5924
5925 if (++level > max_level)
5926 goto out;
5927
5928 raw = &crtc_state->wm.g4x.raw[level];
5929 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5930 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5931 raw->plane[PLANE_SPRITE0] = 0;
5932 raw->fbc = active->hpll.fbc;
5933
5934 out:
5935 for_each_plane_id_on_crtc(crtc, plane_id)
5936 g4x_raw_plane_wm_set(crtc_state, level,
5937 plane_id, USHRT_MAX);
5938 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5939
5940 crtc_state->wm.g4x.optimal = *active;
5941 crtc_state->wm.g4x.intermediate = *active;
5942
5943 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5944 pipe_name(pipe),
5945 wm->pipe[pipe].plane[PLANE_PRIMARY],
5946 wm->pipe[pipe].plane[PLANE_CURSOR],
5947 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5948 }
5949
5950 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5951 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5952 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5953 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5954 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5955 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5956}
5957
5958void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5959{
5960 struct intel_plane *plane;
5961 struct intel_crtc *crtc;
5962
5963 mutex_lock(&dev_priv->wm.wm_mutex);
5964
5965 for_each_intel_plane(&dev_priv->drm, plane) {
5966 struct intel_crtc *crtc =
5967 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5968 struct intel_crtc_state *crtc_state =
5969 to_intel_crtc_state(crtc->base.state);
5970 struct intel_plane_state *plane_state =
5971 to_intel_plane_state(plane->base.state);
5972 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5973 enum plane_id plane_id = plane->id;
5974 int level;
5975
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01005976 if (plane_state->uapi.visible)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005977 continue;
5978
5979 for (level = 0; level < 3; level++) {
5980 struct g4x_pipe_wm *raw =
5981 &crtc_state->wm.g4x.raw[level];
5982
5983 raw->plane[plane_id] = 0;
5984 wm_state->wm.plane[plane_id] = 0;
5985 }
5986
5987 if (plane_id == PLANE_PRIMARY) {
5988 for (level = 0; level < 3; level++) {
5989 struct g4x_pipe_wm *raw =
5990 &crtc_state->wm.g4x.raw[level];
5991 raw->fbc = 0;
5992 }
5993
5994 wm_state->sr.fbc = 0;
5995 wm_state->hpll.fbc = 0;
5996 wm_state->fbc_en = false;
5997 }
5998 }
5999
6000 for_each_intel_crtc(&dev_priv->drm, crtc) {
6001 struct intel_crtc_state *crtc_state =
6002 to_intel_crtc_state(crtc->base.state);
6003
6004 crtc_state->wm.g4x.intermediate =
6005 crtc_state->wm.g4x.optimal;
6006 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6007 }
6008
6009 g4x_program_watermarks(dev_priv);
6010
6011 mutex_unlock(&dev_priv->wm.wm_mutex);
6012}
6013
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006014void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006015{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006016 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006017 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006018 u32 val;
6019
6020 vlv_read_wm_values(dev_priv, wm);
6021
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006022 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6023 wm->level = VLV_WM_LEVEL_PM2;
6024
6025 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006026 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006027
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006028 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006029 if (val & DSP_MAXFIFO_PM5_ENABLE)
6030 wm->level = VLV_WM_LEVEL_PM5;
6031
Ville Syrjälä58590c12015-09-08 21:05:12 +03006032 /*
6033 * If DDR DVFS is disabled in the BIOS, Punit
6034 * will never ack the request. So if that happens
6035 * assume we don't have to enable/disable DDR DVFS
6036 * dynamically. To test that just set the REQ_ACK
6037 * bit to poke the Punit, but don't change the
6038 * HIGH/LOW bits so that we don't actually change
6039 * the current state.
6040 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006041 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006042 val |= FORCE_DDR_FREQ_REQ_ACK;
6043 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6044
6045 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6046 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6047 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
6048 "assuming DDR DVFS is disabled\n");
6049 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6050 } else {
6051 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6052 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6053 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6054 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006055
Chris Wilson337fa6e2019-04-26 09:17:20 +01006056 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006057 }
6058
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006059 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006060 struct intel_crtc_state *crtc_state =
6061 to_intel_crtc_state(crtc->base.state);
6062 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6063 const struct vlv_fifo_state *fifo_state =
6064 &crtc_state->wm.vlv.fifo_state;
6065 enum pipe pipe = crtc->pipe;
6066 enum plane_id plane_id;
6067 int level;
6068
6069 vlv_get_fifo_size(crtc_state);
6070
6071 active->num_levels = wm->level + 1;
6072 active->cxsr = wm->cxsr;
6073
Ville Syrjäläff32c542017-03-02 19:14:57 +02006074 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006075 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006076 &crtc_state->wm.vlv.raw[level];
6077
6078 active->sr[level].plane = wm->sr.plane;
6079 active->sr[level].cursor = wm->sr.cursor;
6080
6081 for_each_plane_id_on_crtc(crtc, plane_id) {
6082 active->wm[level].plane[plane_id] =
6083 wm->pipe[pipe].plane[plane_id];
6084
6085 raw->plane[plane_id] =
6086 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6087 fifo_state->plane[plane_id]);
6088 }
6089 }
6090
6091 for_each_plane_id_on_crtc(crtc, plane_id)
6092 vlv_raw_plane_wm_set(crtc_state, level,
6093 plane_id, USHRT_MAX);
6094 vlv_invalidate_wms(crtc, active, level);
6095
6096 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006097 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006098
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006099 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
Ville Syrjälä1b313892016-11-28 19:37:08 +02006100 pipe_name(pipe),
6101 wm->pipe[pipe].plane[PLANE_PRIMARY],
6102 wm->pipe[pipe].plane[PLANE_CURSOR],
6103 wm->pipe[pipe].plane[PLANE_SPRITE0],
6104 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006105 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006106
6107 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6108 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6109}
6110
Ville Syrjälä602ae832017-03-02 19:15:02 +02006111void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6112{
6113 struct intel_plane *plane;
6114 struct intel_crtc *crtc;
6115
6116 mutex_lock(&dev_priv->wm.wm_mutex);
6117
6118 for_each_intel_plane(&dev_priv->drm, plane) {
6119 struct intel_crtc *crtc =
6120 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6121 struct intel_crtc_state *crtc_state =
6122 to_intel_crtc_state(crtc->base.state);
6123 struct intel_plane_state *plane_state =
6124 to_intel_plane_state(plane->base.state);
6125 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6126 const struct vlv_fifo_state *fifo_state =
6127 &crtc_state->wm.vlv.fifo_state;
6128 enum plane_id plane_id = plane->id;
6129 int level;
6130
Maarten Lankhorstf90a85e2019-10-31 12:26:08 +01006131 if (plane_state->uapi.visible)
Ville Syrjälä602ae832017-03-02 19:15:02 +02006132 continue;
6133
6134 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006135 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006136 &crtc_state->wm.vlv.raw[level];
6137
6138 raw->plane[plane_id] = 0;
6139
6140 wm_state->wm[level].plane[plane_id] =
6141 vlv_invert_wm_value(raw->plane[plane_id],
6142 fifo_state->plane[plane_id]);
6143 }
6144 }
6145
6146 for_each_intel_crtc(&dev_priv->drm, crtc) {
6147 struct intel_crtc_state *crtc_state =
6148 to_intel_crtc_state(crtc->base.state);
6149
6150 crtc_state->wm.vlv.intermediate =
6151 crtc_state->wm.vlv.optimal;
6152 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6153 }
6154
6155 vlv_program_watermarks(dev_priv);
6156
6157 mutex_unlock(&dev_priv->wm.wm_mutex);
6158}
6159
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006160/*
6161 * FIXME should probably kill this and improve
6162 * the real watermark readout/sanitation instead
6163 */
6164static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6165{
6166 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6167 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6168 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6169
6170 /*
6171 * Don't touch WM1S_LP_EN here.
6172 * Doing so could cause underruns.
6173 */
6174}
6175
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006176void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006177{
Imre Deak820c1982013-12-17 14:46:36 +02006178 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006179 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006180
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006181 ilk_init_lp_watermarks(dev_priv);
6182
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006183 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006184 ilk_pipe_wm_get_hw_state(crtc);
6185
6186 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6187 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6188 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6189
6190 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006191 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006192 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6193 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6194 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006195
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006196 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006197 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6198 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006199 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006200 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6201 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006202
6203 hw->enable_fbc_wm =
6204 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6205}
6206
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006207/**
6208 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006209 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006210 *
6211 * Calculate watermark values for the various WM regs based on current mode
6212 * and plane configuration.
6213 *
6214 * There are several cases to deal with here:
6215 * - normal (i.e. non-self-refresh)
6216 * - self-refresh (SR) mode
6217 * - lines are large relative to FIFO size (buffer can hold up to 2)
6218 * - lines are small relative to FIFO size (buffer can hold more than 2
6219 * lines), so need to account for TLB latency
6220 *
6221 * The normal calculation is:
6222 * watermark = dotclock * bytes per pixel * latency
6223 * where latency is platform & configuration dependent (we assume pessimal
6224 * values here).
6225 *
6226 * The SR calculation is:
6227 * watermark = (trunc(latency/line time)+1) * surface width *
6228 * bytes per pixel
6229 * where
6230 * line time = htotal / dotclock
6231 * surface width = hdisplay for normal plane and 64 for cursor
6232 * and latency is assumed to be high, as above.
6233 *
6234 * The final value programmed to the register should always be rounded up,
6235 * and include an extra 2 entries to account for clock crossings.
6236 *
6237 * We don't use the sprite, so we can ignore that. And on Crestline we have
6238 * to set the non-SR watermarks to 8.
6239 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006240void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006241{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006242 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006243
6244 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006245 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006246}
6247
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306248void intel_enable_ipc(struct drm_i915_private *dev_priv)
6249{
6250 u32 val;
6251
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006252 if (!HAS_IPC(dev_priv))
6253 return;
6254
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306255 val = I915_READ(DISP_ARB_CTL2);
6256
6257 if (dev_priv->ipc_enabled)
6258 val |= DISP_IPC_ENABLE;
6259 else
6260 val &= ~DISP_IPC_ENABLE;
6261
6262 I915_WRITE(DISP_ARB_CTL2, val);
6263}
6264
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006265static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6266{
6267 /* Display WA #0477 WaDisableIPC: skl */
6268 if (IS_SKYLAKE(dev_priv))
6269 return false;
6270
6271 /* Display WA #1141: SKL:all KBL:all CFL */
6272 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
6273 return dev_priv->dram_info.symmetric_memory;
6274
6275 return true;
6276}
6277
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306278void intel_init_ipc(struct drm_i915_private *dev_priv)
6279{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306280 if (!HAS_IPC(dev_priv))
6281 return;
6282
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006283 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006284
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306285 intel_enable_ipc(dev_priv);
6286}
6287
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006288static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006289{
Daniel Vetter3107bd42012-10-31 22:52:31 +01006290 /*
6291 * On Ibex Peak and Cougar Point, we need to disable clock
6292 * gating for the panel power sequencer or it will fail to
6293 * start up when no ports are active.
6294 */
6295 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6296}
6297
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006298static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006299{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006300 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006301
Damien Lespiau055e3932014-08-18 13:49:10 +01006302 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006303 I915_WRITE(DSPCNTR(pipe),
6304 I915_READ(DSPCNTR(pipe)) |
6305 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006306
6307 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6308 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006309 }
6310}
6311
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006312static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006313{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006314 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006315
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006316 /*
6317 * Required for FBC
6318 * WaFbcDisableDpfcClockGating:ilk
6319 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006320 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6321 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6322 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006323
6324 I915_WRITE(PCH_3DCGDIS0,
6325 MARIUNIT_CLOCK_GATE_DISABLE |
6326 SVSMUNIT_CLOCK_GATE_DISABLE);
6327 I915_WRITE(PCH_3DCGDIS1,
6328 VFMUNIT_CLOCK_GATE_DISABLE);
6329
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006330 /*
6331 * According to the spec the following bits should be set in
6332 * order to enable memory self-refresh
6333 * The bit 22/21 of 0x42004
6334 * The bit 5 of 0x42020
6335 * The bit 15 of 0x45000
6336 */
6337 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6338 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6339 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006340 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006341 I915_WRITE(DISP_ARB_CTL,
6342 (I915_READ(DISP_ARB_CTL) |
6343 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006344
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006345 /*
6346 * Based on the document from hardware guys the following bits
6347 * should be set unconditionally in order to enable FBC.
6348 * The bit 22 of 0x42000
6349 * The bit 22 of 0x42004
6350 * The bit 7,8,9 of 0x42020.
6351 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006352 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006353 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006354 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6355 I915_READ(ILK_DISPLAY_CHICKEN1) |
6356 ILK_FBCQ_DIS);
6357 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6358 I915_READ(ILK_DISPLAY_CHICKEN2) |
6359 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006360 }
6361
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006362 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6363
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006364 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6365 I915_READ(ILK_DISPLAY_CHICKEN2) |
6366 ILK_ELPIN_409_SELECT);
6367 I915_WRITE(_3D_CHICKEN2,
6368 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6369 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006370
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006371 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006372 I915_WRITE(CACHE_MODE_0,
6373 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006374
Akash Goel4e046322014-04-04 17:14:38 +05306375 /* WaDisable_RenderCache_OperationalFlush:ilk */
6376 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6377
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006378 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006379
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006380 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006381}
6382
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006383static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006384{
Ville Syrjäläd048a262019-08-21 20:30:31 +03006385 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006386 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006387
6388 /*
6389 * On Ibex Peak and Cougar Point, we need to disable clock
6390 * gating for the panel power sequencer or it will fail to
6391 * start up when no ports are active.
6392 */
Jesse Barnescd664072013-10-02 10:34:19 -07006393 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6394 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6395 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006396 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6397 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006398 /* The below fixes the weird display corruption, a few pixels shifted
6399 * downward, on (only) LVDS of some HP laptops with IVY.
6400 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006401 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006402 val = I915_READ(TRANS_CHICKEN2(pipe));
6403 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6404 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006405 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006406 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006407 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6408 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006409 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6410 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006411 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006412 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006413 I915_WRITE(TRANS_CHICKEN1(pipe),
6414 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6415 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006416}
6417
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006418static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006419{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006420 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006421
6422 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006423 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6424 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6425 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006426}
6427
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006428static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006429{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006430 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006431
Damien Lespiau231e54f2012-10-19 17:55:41 +01006432 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006433
6434 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6435 I915_READ(ILK_DISPLAY_CHICKEN2) |
6436 ILK_ELPIN_409_SELECT);
6437
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006438 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006439 I915_WRITE(_3D_CHICKEN,
6440 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6441
Akash Goel4e046322014-04-04 17:14:38 +05306442 /* WaDisable_RenderCache_OperationalFlush:snb */
6443 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6444
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006445 /*
6446 * BSpec recoomends 8x4 when MSAA is used,
6447 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006448 *
6449 * Note that PS/WM thread counts depend on the WIZ hashing
6450 * disable bit, which we don't touch here, but it's good
6451 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006452 */
6453 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006454 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006455
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006456 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006457 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006458
6459 I915_WRITE(GEN6_UCGCTL1,
6460 I915_READ(GEN6_UCGCTL1) |
6461 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6462 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6463
6464 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6465 * gating disable must be set. Failure to set it results in
6466 * flickering pixels due to Z write ordering failures after
6467 * some amount of runtime in the Mesa "fire" demo, and Unigine
6468 * Sanctuary and Tropics, and apparently anything else with
6469 * alpha test or pixel discard.
6470 *
6471 * According to the spec, bit 11 (RCCUNIT) must also be set,
6472 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006473 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006474 * WaDisableRCCUnitClockGating:snb
6475 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006476 */
6477 I915_WRITE(GEN6_UCGCTL2,
6478 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6479 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6480
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006481 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006482 I915_WRITE(_3D_CHICKEN3,
6483 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006484
6485 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006486 * Bspec says:
6487 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6488 * 3DSTATE_SF number of SF output attributes is more than 16."
6489 */
6490 I915_WRITE(_3D_CHICKEN3,
6491 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6492
6493 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006494 * According to the spec the following bits should be
6495 * set in order to enable memory self-refresh and fbc:
6496 * The bit21 and bit22 of 0x42000
6497 * The bit21 and bit22 of 0x42004
6498 * The bit5 and bit7 of 0x42020
6499 * The bit14 of 0x70180
6500 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006501 *
6502 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006503 */
6504 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6505 I915_READ(ILK_DISPLAY_CHICKEN1) |
6506 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6507 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6508 I915_READ(ILK_DISPLAY_CHICKEN2) |
6509 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006510 I915_WRITE(ILK_DSPCLK_GATE_D,
6511 I915_READ(ILK_DSPCLK_GATE_D) |
6512 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6513 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006514
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006515 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006516
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006517 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006518
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006519 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006520}
6521
6522static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6523{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006524 u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006525
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006526 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006527 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006528 *
6529 * This actually overrides the dispatch
6530 * mode for all thread types.
6531 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006532 reg &= ~GEN7_FF_SCHED_MASK;
6533 reg |= GEN7_FF_TS_SCHED_HW;
6534 reg |= GEN7_FF_VS_SCHED_HW;
6535 reg |= GEN7_FF_DS_SCHED_HW;
6536
6537 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6538}
6539
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006540static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006541{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006542 /*
6543 * TODO: this bit should only be enabled when really needed, then
6544 * disabled when not needed anymore in order to save power.
6545 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01006546 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006547 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6548 I915_READ(SOUTH_DSPCLK_GATE_D) |
6549 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006550
6551 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03006552 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6553 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006554 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006555}
6556
Ville Syrjälä712bf362016-10-31 22:37:23 +02006557static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03006558{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01006559 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006560 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03006561
6562 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6563 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6564 }
6565}
6566
Imre Deak450174f2016-05-03 15:54:21 +03006567static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
6568 int general_prio_credits,
6569 int high_prio_credits)
6570{
6571 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07006572 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03006573
6574 /* WaTempDisableDOPClkGating:bdw */
6575 misccpctl = I915_READ(GEN7_MISCCPCTL);
6576 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6577
Oscar Mateo930a7842017-10-17 13:25:45 -07006578 val = I915_READ(GEN8_L3SQCREG1);
6579 val &= ~L3_PRIO_CREDITS_MASK;
6580 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
6581 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
6582 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03006583
6584 /*
6585 * Wait at least 100 clocks before re-enabling clock gating.
6586 * See the definition of L3SQCREG1 in BSpec.
6587 */
6588 POSTING_READ(GEN8_L3SQCREG1);
6589 udelay(1);
6590 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6591}
6592
Oscar Mateod65dc3e2018-05-08 14:29:24 -07006593static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
6594{
6595 /* This is not an Wa. Enable to reduce Sampler power */
6596 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
6597 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07006598
6599 /* WaEnable32PlaneMode:icl */
6600 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
6601 _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
Matt Roperb9cf9da2019-12-23 17:20:25 -08006602
6603 /*
6604 * Wa_1408615072:icl,ehl (vsunit)
6605 * Wa_1407596294:icl,ehl (hsunit)
6606 */
6607 intel_uncore_rmw(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE,
6608 0, VSUNIT_CLKGATE_DIS | HSUNIT_CLKGATE_DIS);
6609
Oscar Mateod65dc3e2018-05-08 14:29:24 -07006610}
6611
Michel Thierry5d869232019-08-23 01:20:34 -07006612static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
6613{
6614 u32 vd_pg_enable = 0;
6615 unsigned int i;
6616
Matt Roper4ca15382019-12-23 17:20:26 -08006617 /* Wa_1408615072:tgl */
6618 intel_uncore_rmw(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE2,
6619 0, VSUNIT_CLKGATE_DIS_TGL);
6620
Michel Thierry5d869232019-08-23 01:20:34 -07006621 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
6622 for (i = 0; i < I915_MAX_VCS; i++) {
6623 if (HAS_ENGINE(dev_priv, _VCS(i)))
6624 vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
6625 VDN_MFX_POWERGATE_ENABLE(i);
6626 }
6627
6628 I915_WRITE(POWERGATE_ENABLE,
6629 I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
6630}
6631
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07006632static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
6633{
6634 if (!HAS_PCH_CNP(dev_priv))
6635 return;
6636
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08006637 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07006638 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
6639 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07006640}
6641
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006642static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07006643{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07006644 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07006645 cnp_init_clock_gating(dev_priv);
6646
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07006647 /* This is not an Wa. Enable for better image quality */
6648 I915_WRITE(_3D_CHICKEN3,
6649 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
6650
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07006651 /* WaEnableChickenDCPR:cnl */
6652 I915_WRITE(GEN8_CHICKEN_DCPR_1,
6653 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
6654
6655 /* WaFbcWakeMemOn:cnl */
6656 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
6657 DISP_FBC_MEMORY_WAKE);
6658
Chris Wilson34991bd2017-11-11 10:03:36 +00006659 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
6660 /* ReadHitWriteOnlyDisable:cnl */
6661 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07006662 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
6663 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00006664 val |= SARBUNIT_CLKGATE_DIS;
6665 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08006666
Rodrigo Vivia4713c52018-03-07 14:09:12 -08006667 /* Wa_2201832410:cnl */
6668 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
6669 val |= GWUNIT_CLKGATE_DIS;
6670 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
6671
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08006672 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08006673 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08006674 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
6675 val |= VFUNIT_CLKGATE_DIS;
6676 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07006677}
6678
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07006679static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
6680{
6681 cnp_init_clock_gating(dev_priv);
6682 gen9_init_clock_gating(dev_priv);
6683
6684 /* WaFbcNukeOnHostModify:cfl */
6685 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
6686 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
6687}
6688
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006689static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03006690{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006691 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03006692
6693 /* WaDisableSDEUnitClockGating:kbl */
6694 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
6695 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6696 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03006697
6698 /* WaDisableGamClockGating:kbl */
6699 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
6700 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6701 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03006702
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07006703 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03006704 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
6705 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03006706}
6707
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006708static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02006709{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006710 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03006711
6712 /* WAC6entrylatency:skl */
6713 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
6714 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03006715
6716 /* WaFbcNukeOnHostModify:skl */
6717 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
6718 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02006719}
6720
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006721static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006722{
Damien Lespiau07d27e22014-03-03 17:31:46 +00006723 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006724
Ben Widawskyab57fff2013-12-12 15:28:04 -08006725 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006726 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006727
Ben Widawskyab57fff2013-12-12 15:28:04 -08006728 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006729 I915_WRITE(CHICKEN_PAR1_1,
6730 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6731
Ben Widawskyab57fff2013-12-12 15:28:04 -08006732 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006733 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006734 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006735 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006736 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006737 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006738
Ben Widawskyab57fff2013-12-12 15:28:04 -08006739 /* WaVSRefCountFullforceMissDisable:bdw */
6740 /* WaDSRefCountFullforceMissDisable:bdw */
6741 I915_WRITE(GEN7_FF_THREAD_MODE,
6742 I915_READ(GEN7_FF_THREAD_MODE) &
6743 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006744
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006745 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6746 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006747
6748 /* WaDisableSDEUnitClockGating:bdw */
6749 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6750 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006751
Imre Deak450174f2016-05-03 15:54:21 +03006752 /* WaProgramL3SqcReg1Default:bdw */
6753 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006754
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03006755 /* WaKVMNotificationOnConfigChange:bdw */
6756 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
6757 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
6758
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006759 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00006760
6761 /* WaDisableDopClockGating:bdw
6762 *
6763 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
6764 * clock gating.
6765 */
6766 I915_WRITE(GEN6_UCGCTL1,
6767 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006768}
6769
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006770static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006771{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006772 /* L3 caching of data atomics doesn't work -- disable it. */
6773 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6774 I915_WRITE(HSW_ROW_CHICKEN3,
6775 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6776
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006777 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006778 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6779 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6780 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6781
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006782 /* WaVSRefCountFullforceMissDisable:hsw */
6783 I915_WRITE(GEN7_FF_THREAD_MODE,
6784 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006785
Akash Goel4e046322014-04-04 17:14:38 +05306786 /* WaDisable_RenderCache_OperationalFlush:hsw */
6787 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6788
Chia-I Wufe27c602014-01-28 13:29:33 +08006789 /* enable HiZ Raw Stall Optimization */
6790 I915_WRITE(CACHE_MODE_0_GEN7,
6791 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6792
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006793 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006794 I915_WRITE(CACHE_MODE_1,
6795 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006796
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006797 /*
6798 * BSpec recommends 8x4 when MSAA is used,
6799 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006800 *
6801 * Note that PS/WM thread counts depend on the WIZ hashing
6802 * disable bit, which we don't touch here, but it's good
6803 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006804 */
6805 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006806 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006807
Kenneth Graunke94411592014-12-31 16:23:00 -08006808 /* WaSampleCChickenBitEnable:hsw */
6809 I915_WRITE(HALF_SLICE_CHICKEN3,
6810 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6811
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006812 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006813 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6814
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006815 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006816}
6817
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006818static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006819{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02006820 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006821
Damien Lespiau231e54f2012-10-19 17:55:41 +01006822 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006823
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006824 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006825 I915_WRITE(_3D_CHICKEN3,
6826 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6827
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006828 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006829 I915_WRITE(IVB_CHICKEN3,
6830 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6831 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6832
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006833 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006834 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07006835 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6836 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006837
Akash Goel4e046322014-04-04 17:14:38 +05306838 /* WaDisable_RenderCache_OperationalFlush:ivb */
6839 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6840
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006841 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006842 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6843 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6844
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006845 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006846 I915_WRITE(GEN7_L3CNTLREG1,
6847 GEN7_WA_FOR_GEN7_L3_CONTROL);
6848 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006849 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006850 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07006851 I915_WRITE(GEN7_ROW_CHICKEN2,
6852 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006853 else {
6854 /* must write both registers */
6855 I915_WRITE(GEN7_ROW_CHICKEN2,
6856 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006857 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6858 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006859 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006860
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006861 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006862 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6863 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6864
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006865 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006866 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006867 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006868 */
6869 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006870 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006871
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006872 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006873 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6874 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6875 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6876
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006877 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006878
6879 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006880
Chris Wilson22721342014-03-04 09:41:43 +00006881 if (0) { /* causes HiZ corruption on ivb:gt1 */
6882 /* enable HiZ Raw Stall Optimization */
6883 I915_WRITE(CACHE_MODE_0_GEN7,
6884 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6885 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006886
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006887 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006888 I915_WRITE(CACHE_MODE_1,
6889 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006890
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006891 /*
6892 * BSpec recommends 8x4 when MSAA is used,
6893 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006894 *
6895 * Note that PS/WM thread counts depend on the WIZ hashing
6896 * disable bit, which we don't touch here, but it's good
6897 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006898 */
6899 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006900 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006901
Ben Widawsky20848222012-05-04 18:58:59 -07006902 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6903 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6904 snpcr |= GEN6_MBC_SNPCR_MED;
6905 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006906
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01006907 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006908 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006909
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006910 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006911}
6912
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006913static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006914{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006915 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006916 I915_WRITE(_3D_CHICKEN3,
6917 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6918
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006919 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006920 I915_WRITE(IVB_CHICKEN3,
6921 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6922 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6923
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006924 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006925 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006926 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006927 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6928 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006929
Akash Goel4e046322014-04-04 17:14:38 +05306930 /* WaDisable_RenderCache_OperationalFlush:vlv */
6931 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6932
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006933 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006934 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6935 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6936
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006937 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006938 I915_WRITE(GEN7_ROW_CHICKEN2,
6939 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6940
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006941 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006942 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6943 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6944 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6945
Ville Syrjälä46680e02014-01-22 21:33:01 +02006946 gen7_setup_fixed_func_scheduler(dev_priv);
6947
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006948 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006949 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006950 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006951 */
6952 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006953 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006954
Akash Goelc98f5062014-03-24 23:00:07 +05306955 /* WaDisableL3Bank2xClockGate:vlv
6956 * Disabling L3 clock gating- MMIO 940c[25] = 1
6957 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6958 I915_WRITE(GEN7_UCGCTL4,
6959 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006960
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006961 /*
6962 * BSpec says this must be set, even though
6963 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6964 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006965 I915_WRITE(CACHE_MODE_1,
6966 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006967
6968 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006969 * BSpec recommends 8x4 when MSAA is used,
6970 * however in practice 16x4 seems fastest.
6971 *
6972 * Note that PS/WM thread counts depend on the WIZ hashing
6973 * disable bit, which we don't touch here, but it's good
6974 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6975 */
6976 I915_WRITE(GEN7_GT_MODE,
6977 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6978
6979 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006980 * WaIncreaseL3CreditsForVLVB0:vlv
6981 * This is the hardware default actually.
6982 */
6983 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6984
6985 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006986 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006987 * Disable clock gating on th GCFG unit to prevent a delay
6988 * in the reporting of vblank events.
6989 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006990 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006991}
6992
Rodrigo Vivi91200c02017-08-28 22:20:26 -07006993static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006994{
Ville Syrjälä232ce332014-04-09 13:28:35 +03006995 /* WaVSRefCountFullforceMissDisable:chv */
6996 /* WaDSRefCountFullforceMissDisable:chv */
6997 I915_WRITE(GEN7_FF_THREAD_MODE,
6998 I915_READ(GEN7_FF_THREAD_MODE) &
6999 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007000
7001 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7002 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7003 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007004
7005 /* WaDisableCSUnitClockGating:chv */
7006 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7007 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007008
7009 /* WaDisableSDEUnitClockGating:chv */
7010 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7011 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007012
7013 /*
Imre Deak450174f2016-05-03 15:54:21 +03007014 * WaProgramL3SqcReg1Default:chv
7015 * See gfxspecs/Related Documents/Performance Guide/
7016 * LSQC Setting Recommendations.
7017 */
7018 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007019}
7020
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007021static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007022{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007023 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007024
7025 I915_WRITE(RENCLK_GATE_D1, 0);
7026 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7027 GS_UNIT_CLOCK_GATE_DISABLE |
7028 CL_UNIT_CLOCK_GATE_DISABLE);
7029 I915_WRITE(RAMCLK_GATE_D, 0);
7030 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7031 OVRUNIT_CLOCK_GATE_DISABLE |
7032 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007033 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007034 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7035 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007036
7037 /* WaDisableRenderCachePipelinedFlush */
7038 I915_WRITE(CACHE_MODE_0,
7039 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007040
Akash Goel4e046322014-04-04 17:14:38 +05307041 /* WaDisable_RenderCache_OperationalFlush:g4x */
7042 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7043
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007044 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007045}
7046
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007047static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007048{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007049 struct intel_uncore *uncore = &dev_priv->uncore;
7050
7051 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7052 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7053 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7054 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7055 intel_uncore_write16(uncore, DEUC, 0);
7056 intel_uncore_write(uncore,
7057 MI_ARB_STATE,
7058 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307059
7060 /* WaDisable_RenderCache_OperationalFlush:gen4 */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01007061 intel_uncore_write(uncore,
7062 CACHE_MODE_0,
7063 _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007064}
7065
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007066static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007067{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007068 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7069 I965_RCC_CLOCK_GATE_DISABLE |
7070 I965_RCPB_CLOCK_GATE_DISABLE |
7071 I965_ISC_CLOCK_GATE_DISABLE |
7072 I965_FBC_CLOCK_GATE_DISABLE);
7073 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007074 I915_WRITE(MI_ARB_STATE,
7075 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307076
7077 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7078 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007079}
7080
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007081static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007082{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007083 u32 dstate = I915_READ(D_STATE);
7084
7085 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7086 DSTATE_DOT_CLOCK_GATING;
7087 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007088
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007089 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007090 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007091
7092 /* IIR "flip pending" means done if this bit is set */
7093 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007094
7095 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007096 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007097
7098 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7099 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007100
7101 I915_WRITE(MI_ARB_STATE,
7102 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007103}
7104
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007105static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007106{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007107 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007108
7109 /* interrupts should cause a wake up from C3 */
7110 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7111 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007112
7113 I915_WRITE(MEM_MODE,
7114 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007115}
7116
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007117static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007118{
Ville Syrjälä10383922014-08-15 01:21:54 +03007119 I915_WRITE(MEM_MODE,
7120 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7121 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007122}
7123
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007124void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007125{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007126 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007127}
7128
Ville Syrjälä712bf362016-10-31 22:37:23 +02007129void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007130{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007131 if (HAS_PCH_LPT(dev_priv))
7132 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007133}
7134
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007135static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007136{
7137 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7138}
7139
7140/**
7141 * intel_init_clock_gating_hooks - setup the clock gating hooks
7142 * @dev_priv: device private
7143 *
7144 * Setup the hooks that configure which clocks of a given platform can be
7145 * gated and also apply various GT and display specific workarounds for these
7146 * platforms. Note that some GT specific workarounds are applied separately
7147 * when GPU contexts or batchbuffers start their execution.
7148 */
7149void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7150{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07007151 if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07007152 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07007153 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07007154 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07007155 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007156 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007157 else if (IS_COFFEELAKE(dev_priv))
7158 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07007159 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007160 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07007161 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007162 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007163 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007164 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007165 else if (IS_GEMINILAKE(dev_priv))
7166 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007167 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007168 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007169 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007170 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007171 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007172 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007173 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007174 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007175 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007176 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007177 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02007178 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007179 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007180 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007181 else if (IS_G4X(dev_priv))
7182 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007183 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007184 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007185 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007186 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007187 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02007188 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7189 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7190 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007191 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02007192 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7193 else {
7194 MISSING_CASE(INTEL_DEVID(dev_priv));
7195 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7196 }
7197}
7198
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007199/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007200void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007201{
Daniel Vetterc921aba2012-04-26 23:28:17 +02007202 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007203 if (IS_PINEVIEW(dev_priv))
Lucas De Marchi1d218222019-12-24 00:40:04 -08007204 pnv_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007205 else if (IS_GEN(dev_priv, 5))
Lucas De Marchi9eae5e22019-12-24 00:40:09 -08007206 ilk_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007207
James Ausmusb068a862019-10-09 10:23:14 -07007208 if (intel_has_sagv(dev_priv))
7209 skl_setup_sagv_block_time(dev_priv);
7210
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007211 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007212 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007213 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01007214 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01007215 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007216 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007217 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007218 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007219
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007220 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007221 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007222 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007223 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007224 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007225 dev_priv->display.compute_intermediate_wm =
7226 ilk_compute_intermediate_wm;
7227 dev_priv->display.initial_watermarks =
7228 ilk_initial_watermarks;
7229 dev_priv->display.optimize_watermarks =
7230 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007231 } else {
7232 DRM_DEBUG_KMS("Failed to read display plane latency. "
7233 "Disable CxSR\n");
7234 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007235 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007236 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007237 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007238 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007239 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007240 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007241 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03007242 } else if (IS_G4X(dev_priv)) {
7243 g4x_setup_wm_latency(dev_priv);
7244 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
7245 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
7246 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
7247 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007248 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00007249 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007250 dev_priv->is_ddr3,
7251 dev_priv->fsb_freq,
7252 dev_priv->mem_freq)) {
7253 DRM_INFO("failed to find known CxSR latency "
7254 "(found ddr%s fsb freq %d, mem freq %d), "
7255 "disabling CxSR\n",
7256 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7257 dev_priv->fsb_freq, dev_priv->mem_freq);
7258 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007259 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007260 dev_priv->display.update_wm = NULL;
7261 } else
Lucas De Marchi1d218222019-12-24 00:40:04 -08007262 dev_priv->display.update_wm = pnv_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007263 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007264 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007265 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007266 dev_priv->display.update_wm = i9xx_update_wm;
7267 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007268 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03007269 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007270 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007271 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007272 } else {
7273 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007274 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007275 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007276 } else {
7277 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007278 }
7279}
7280
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00007281void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01007282{
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01007283 dev_priv->runtime_pm.suspended = false;
7284 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007285}